Skip to content

Commit

Permalink
Extracted layer size update as a separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
ice0 committed Aug 23, 2021
1 parent 5c930dc commit fc742a4
Showing 1 changed file with 44 additions and 85 deletions.
129 changes: 44 additions & 85 deletions synfig-studio/src/synfigapp/canvasinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,45 @@ CanvasInterface::jump_to_prev_keyframe()
//catch(...) { synfig::warning("Unable to find prev keyframe"); }
}

static void update_layer_size(CanvasInterface& canvas_interface, Layer::Handle& layer, bool resize_image) {
int w, h;

w = layer->get_param("_width").get(int());
h = layer->get_param("_height").get(int());
const RendDesc& rend_desc = canvas_interface.get_canvas()->rend_desc();
if (w && h) {
Vector x, size = rend_desc.get_br()-rend_desc.get_tl();

// vector from top left of canvas to bottom right
if (resize_image) {
if(abs(size[0])<abs(size[1])) {// if canvas is tall and thin
x[0]=size[0]; // use full width
x[1]=size[0]/w*h; // and scale for height
if ((size[0]<0) ^ (size[1]<0)) x[1] = -x[1];
} else { // else canvas is short and fat (or maybe square)
x[1]=size[1]; // use full height
x[0]=size[1]/h*w; // and scale for width
if ((size[0]<0) ^ (size[1]<0)) x[0] = -x[0];
}
} else {
x[0] = w*rend_desc.get_pw();
x[1] = h*rend_desc.get_ph();
if((size[0]<0)) x[0]=-x[0];
if((size[1]<0)) x[1]=-x[1];
}

if(!layer->set_param("tl",ValueBase(-x/2)))
throw int();
if(!layer->set_param("br",ValueBase(x/2)))
throw int();
} else {
if(!layer->set_param("tl",ValueBase(rend_desc.get_tl())))
throw int();
if(!layer->set_param("br",ValueBase(rend_desc.get_br())))
throw int();
}
}

bool
CanvasInterface::import(
const synfig::String &filename,
Expand Down Expand Up @@ -857,61 +896,15 @@ CanvasInterface::import(
try
{
Layer::Handle layer(add_layer_to("Import",get_canvas()));
int w,h;
if(!layer)
throw int();
if(!layer->set_param("filename",ValueBase(short_filename)))
throw int();
w=layer->get_param("_width").get(int());
h=layer->get_param("_height").get(int());
update_layer_size(*this, layer, resize_image);
layer->monitor(filename);
if(w&&h)
{
const RendDesc& rend_desc = get_canvas()->rend_desc();
Vector x, size = rend_desc.get_br()-rend_desc.get_tl();

// vector from top left of canvas to bottom right
if (resize_image)
{
if(abs(size[0])<abs(size[1])) // if canvas is tall and thin
{
x[0]=size[0]; // use full width
x[1]=size[0]/w*h; // and scale for height
if((size[0]<0) ^ (size[1]<0))
x[1]=-x[1];
}
else // else canvas is short and fat (or maybe square)
{
x[1]=size[1]; // use full height
x[0]=size[1]/h*w; // and scale for width
if((size[0]<0) ^ (size[1]<0))
x[0]=-x[0];
}
}
else
{
x[0] = w*rend_desc.get_pw();
x[1] = h*rend_desc.get_ph();
if((size[0]<0)) x[0]=-x[0];
if((size[1]<0)) x[1]=-x[1];
}

if(!layer->set_param("tl",ValueBase(-x/2)))
throw int();
if(!layer->set_param("br",ValueBase(x/2)))
throw int();
}
else
{
if(!layer->set_param("tl",ValueBase(get_canvas()->rend_desc().get_tl())))
throw int();
if(!layer->set_param("br",ValueBase(get_canvas()->rend_desc().get_br())))
throw int();
}

layer->set_description(etl::basename(filename));
signal_layer_new_description()(layer,etl::basename(filename));

String desc = etl::basename(filename);
layer->set_description(desc);
signal_layer_new_description()(layer, desc);
//get_instance()->set_selected_layer(get_canvas(), layer);
//get_instance()->set_selected_layer(layer, get_canvas());

Expand Down Expand Up @@ -1011,7 +1004,6 @@ CanvasInterface::import_sequence(

try {
layer = add_layer_to("Import",get_canvas());
int w, h;
if (!layer)
throw int();
if (!layer->set_param("filename", ValueBase(short_filename)))
Expand All @@ -1038,41 +1030,8 @@ CanvasInterface::import_sequence(
first_time = false;
}
}

w = layer->get_param("_width").get(int());
h = layer->get_param("_height").get(int());
update_layer_size(*this, layer, resize_image);
layer->monitor(filename);
if (w && h) {
const RendDesc& rend_desc = get_canvas()->rend_desc();
Vector x, size = rend_desc.get_br()-rend_desc.get_tl();
// vector from top left of canvas to bottom right
if (resize_image) {
if(abs(size[0])<abs(size[1])) {// if canvas is tall and thin
x[0]=size[0]; // use full width
x[1]=size[0]/w*h; // and scale for height
if ((size[0]<0) ^ (size[1]<0)) x[1] = -x[1];
} else { // else canvas is short and fat (or maybe square)
x[1]=size[1]; // use full height
x[0]=size[1]/h*w; // and scale for width
if ((size[0]<0) ^ (size[1]<0)) x[0] = -x[0];
}
} else {
x[0] = w*rend_desc.get_pw();
x[1] = h*rend_desc.get_ph();
if((size[0]<0)) x[0]=-x[0];
if((size[1]<0)) x[1]=-x[1];
}
if(!layer->set_param("tl",ValueBase(-x/2)))
throw int();
if(!layer->set_param("br",ValueBase(x/2)))
throw int();
} else {
if(!layer->set_param("tl",ValueBase(get_canvas()->rend_desc().get_tl())))
throw int();
if(!layer->set_param("br",ValueBase(get_canvas()->rend_desc().get_br())))
throw int();
}

String desc = etl::basename(filename);
layer->set_description(desc);
signal_layer_new_description()(layer, desc);
Expand Down

0 comments on commit fc742a4

Please sign in to comment.