1 /* === S Y N F I G ========================================================= */
2 /*! \file canvasinterface.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
36 #include <synfig/valuenode_scale.h>
37 #include <synfig/valuenode_timedswap.h>
38 #include <synfig/valuenode_composite.h>
39 #include <synfig/valuenode_subtract.h>
40 #include <synfig/valuenode_linear.h>
41 #include <synfig/valuenode_reference.h>
42 #include <synfig/valuenode_twotone.h>
43 #include <synfig/valuenode_stripes.h>
45 #include <synfig/waypoint.h>
46 #include <synfig/loadcanvas.h>
47 #include <synfig/importer.h>
48 #include <synfig/guidset.h>
50 #include "canvasinterface.h"
53 #include "actions/layeradd.h"
54 #include "actions/valuedescconvert.h"
55 #include "actions/valuenodeadd.h"
56 #include "actions/editmodeset.h"
57 #include "action_system.h"
61 #include <synfig/gradient.h>
65 /* === U S I N G =========================================================== */
69 using namespace synfig;
70 using namespace synfigapp;
72 /* === M A C R O S ========================================================= */
74 /* === G L O B A L S ======================================================= */
76 /* === P R O C E D U R E S ================================================= */
78 /* === M E T H O D S ======================================================= */
80 CanvasInterface::CanvasInterface(loose_handle<Instance> instance,handle<Canvas> canvas):
83 cur_time_(canvas->rend_desc().get_frame_start()),
84 mode_(MODE_NORMAL|MODE_ANIMATE_PAST|MODE_ANIMATE_FUTURE)
86 set_selection_manager(get_instance()->get_selection_manager());
87 set_ui_interface(get_instance()->get_ui_interface());
90 CanvasInterface::~CanvasInterface()
92 synfig::info("synfigapp::CanvasInterface::~CanvasInterface(): Deleted");
96 CanvasInterface::set_time(synfig::Time x)
98 if(get_canvas()->rend_desc().get_frame_rate())
100 float fps(get_canvas()->rend_desc().get_frame_rate());
101 Time r(x.round(fps));
102 //synfig::info("CanvasInterface::set_time(): %s rounded to %s\n",x.get_string(fps).c_str(),r.get_string(fps).c_str());
105 if(cur_time_.is_equal(x))
107 get_canvas()->set_time(cur_time_=x);
109 // update the time in all the child canvases
110 Canvas::Children children = get_canvas()->get_root()->children();
111 handle<CanvasInterface> interface;
112 for (Canvas::Children::iterator iter = children.begin(); iter != children.end(); iter++)
113 if ((interface = get_instance()->find_canvas_interface(*iter)) != this)
114 interface->set_time(interface->get_canvas()->get_time());
116 signal_time_changed()();
117 signal_dirty_preview()();
121 CanvasInterface::get_time()const
127 CanvasInterface::refresh_current_values()
129 get_canvas()->set_time(cur_time_);
130 signal_time_changed()();
131 signal_dirty_preview()();
134 etl::handle<CanvasInterface>
135 CanvasInterface::create(loose_handle<Instance> instance,handle<Canvas> canvas)
137 etl::handle<CanvasInterface> intrfc;
138 intrfc=new CanvasInterface(instance,canvas);
139 instance->canvas_interface_list().push_front(intrfc);
144 CanvasInterface::set_mode(Mode x)
146 Action::Handle action(Action::EditModeSet::create());
150 action->set_param("canvas",get_canvas());
151 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
152 action->set_param("edit_mode",x);
154 if(!action->is_ready())
156 get_ui_interface()->error(_("Action Not Ready, unable to change mode"));
161 if(!get_instance()->perform_action(action))
162 get_ui_interface()->error(_("Unable to change mode"));
165 // signal_mode_changed_(x);
168 CanvasInterface::Mode
169 CanvasInterface::get_mode()const
177 CanvasInterface::add_layer_to(String name, Canvas::Handle canvas, int depth)
179 synfigapp::Action::PassiveGrouper group(get_instance().get(),_("Add Layer To"));
181 Layer::Handle layer(Layer::create(name));
188 if(canvas!=get_canvas() && !canvas->is_inline())
190 synfig::error("Bad canvas passed to \"add_layer_to\"");
194 layer->set_canvas(canvas);
196 // Apply some defaults
197 if(layer->set_param("fg",synfigapp::Main::get_foreground_color()))
198 layer->set_param("bg",synfigapp::Main::get_background_color());
200 layer->set_param("color",synfigapp::Main::get_foreground_color());
202 layer->set_param("width",synfigapp::Main::get_bline_width().units(get_canvas()->rend_desc()));
203 layer->set_param("gradient",synfigapp::Main::get_gradient());
205 layer->set_param("amount",synfigapp::Main::get_opacity());
206 layer->set_param("blend_method",synfigapp::Main::get_blend_method());
209 // Grab the layer's list of parameters
210 Layer::ParamList paramlist=layer->get_param_list();
211 Layer::ParamList::iterator iter;
212 for(iter=paramlist.begin();iter!=paramlist.end();++iter)
214 ValueNode::Handle value_node;
216 if(iter->second.get_type()==ValueBase::TYPE_LIST)
217 value_node=LinkableValueNode::create("dynamic_list",iter->second);
218 else if(LinkableValueNode::check_type("composite",iter->second.get_type()) &&
219 (iter->second.get_type()!=ValueBase::TYPE_COLOR && iter->second.get_type()!=ValueBase::TYPE_VECTOR)
221 value_node=LinkableValueNode::create("composite",iter->second);
224 layer->connect_dynamic_param(iter->first,value_node);
228 // Action to add the layer
229 Action::Handle action(Action::LayerAdd::create());
235 action->set_param("canvas",canvas);
236 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
237 action->set_param("new",layer);
239 if(!action->is_ready())
241 get_ui_interface()->error(_("Action Not Ready"));
245 if(!get_instance()->perform_action(action))
247 get_ui_interface()->error(_("Action Failed."));
251 synfig::info("DEPTH=%d",depth);
252 // Action to move the layer (if necessary)
255 Action::Handle action(Action::create("layer_move"));
261 action->set_param("canvas",canvas);
262 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
263 action->set_param("layer",layer);
264 action->set_param("new_index",depth);
266 if(!action->is_ready())
268 get_ui_interface()->error(_("Move Action Not Ready"));
272 if(!get_instance()->perform_action(action))
274 get_ui_interface()->error(_("Move Action Failed."));
285 CanvasInterface::convert(ValueDesc value_desc, String type)
287 Action::Handle action(Action::ValueDescConvert::create());
293 action->set_param("canvas",get_canvas());
294 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
295 action->set_param("value_desc",value_desc);
296 action->set_param("type",type);
298 if(!action->is_ready())
300 get_ui_interface()->error(_("Action Not Ready"));
304 if(get_instance()->perform_action(action))
307 get_ui_interface()->error(_("Action Failed."));
312 CanvasInterface::add_value_node(synfig::ValueNode::Handle value_node, synfig::String name)
316 get_ui_interface()->error(_("Empty name!"));
320 Action::Handle action(Action::ValueNodeAdd::create());
326 action->set_param("canvas",get_canvas());
327 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
328 action->set_param("new",value_node);
329 action->set_param("name",name);
331 if(!action->is_ready())
333 get_ui_interface()->error(_("Action Not Ready"));
337 if(get_instance()->perform_action(action))
340 get_ui_interface()->error(_("Action Failed."));
345 CanvasInterface::generate_param_list(const ValueDesc &value_desc)
347 synfigapp::Action::ParamList param_list;
348 param_list.add("time",get_time());
349 param_list.add("canvas_interface",etl::handle<CanvasInterface>(this));
350 param_list.add("canvas",get_canvas());
352 param_list.add("value_desc",value_desc);
354 if(value_desc.parent_is_value_node())
355 param_list.add("parent_value_node",value_desc.get_parent_value_node());
357 if(value_desc.is_value_node())
358 param_list.add("value_node",value_desc.get_value_node());
360 if(value_desc.is_const())
361 param_list.add("value",value_desc.get_value());
363 if(value_desc.parent_is_layer_param())
365 param_list.add("parent_layer",value_desc.get_layer());
366 param_list.add("parent_layer_param",value_desc.get_param_name());
370 synfigapp::SelectionManager::ChildrenList children_list;
371 children_list=get_selection_manager()->get_selected_children();
372 if(!value_desc.parent_is_canvas() && children_list.size()==1)
374 param_list.add("dest",value_desc);
375 param_list.add("src",children_list.front().get_value_node());
382 CanvasInterface::generate_param_list(const std::list<synfigapp::ValueDesc> &value_desc_list)
384 synfigapp::Action::ParamList param_list;
385 param_list.add("time",get_time());
386 param_list.add("canvas_interface",etl::handle<CanvasInterface>(this));
387 param_list.add("canvas",get_canvas());
389 std::list<synfigapp::ValueDesc>::const_iterator iter;
390 for(iter=value_desc_list.begin();iter!=value_desc_list.end();++iter)
392 param_list.add("value_desc",*iter);
393 if(iter->is_value_node())
395 param_list.add("value_node",iter->get_value_node());
404 CanvasInterface::set_rend_desc(const synfig::RendDesc &rend_desc)
406 Action::Handle action(Action::create("canvas_rend_desc_set"));
412 action->set_param("canvas",get_canvas());
413 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
414 action->set_param("rend_desc",rend_desc);
416 if(!get_instance()->perform_action(action))
417 get_ui_interface()->error(_("Action Failed."));
421 CanvasInterface::set_name(const String &x)
423 //! \todo This needs to be converted into an action
424 get_canvas()->set_name(x);
425 signal_id_changed_();
430 CanvasInterface::set_description(const String &x)
432 //! \todo This needs to be converted into an action
433 get_canvas()->set_description(x);
438 CanvasInterface::set_id(const String &x)
440 //! \todo This needs to be converted into an action
441 get_canvas()->set_id(x);
442 signal_id_changed_();
448 CanvasInterface::jump_to_next_keyframe()
450 synfig::info("Current time: %s",get_time().get_string().c_str());
453 synfig::Keyframe keyframe(*get_canvas()->keyframe_list().find_next(get_time()));
454 synfig::info("Jumping to keyframe \"%s\" at %s",keyframe.get_description().c_str(),keyframe.get_time().get_string().c_str());
455 set_time(keyframe.get_time());
457 catch(...) { synfig::warning("Unable to find next keyframe"); }
461 CanvasInterface::jump_to_prev_keyframe()
463 synfig::info("Current time: %s",get_time().get_string().c_str());
466 synfig::Keyframe keyframe(*get_canvas()->keyframe_list().find_prev(get_time()));
467 synfig::info("Jumping to keyframe \"%s\" at %s",keyframe.get_description().c_str(),keyframe.get_time().get_string().c_str());
468 set_time(keyframe.get_time());
470 catch(...) { synfig::warning("Unable to find prev keyframe"); }
474 CanvasInterface::import(const synfig::String &filename, bool /*copy*/)
476 Action::PassiveGrouper group(get_instance().get(),_("Import Image"));
478 synfig::info("Attempting to import "+filename);
480 if (filename_extension(filename) == "")
482 get_ui_interface()->error("Filename must have an extension!");
486 String ext(filename_extension(filename));
487 if (ext.size()) ext = ext.substr(1); // skip initial '.'
488 std::transform(ext.begin(),ext.end(),ext.begin(),&::tolower);
490 // If this is a SIF file, then we need to do things slightly differently
491 if(ext=="sif" || ext=="sifz")try
494 Canvas::Handle outside_canvas(synfig::open_canvas(filename));
496 throw String(_("Unable to open this composition"));
498 Layer::Handle layer(add_layer_to("PasteCanvas",get_canvas()));
500 throw String(_("Unable to create \"Paste Canvas\" layer"));
501 if(!layer->set_param("canvas",ValueBase(outside_canvas)))
504 //layer->set_description(basename(filename));
505 signal_layer_new_description()(layer,filename);
510 get_ui_interface()->error(x+" -- "+filename);
515 get_ui_interface()->error(_("Uncaught exception when attempting\nto open this composition -- ")+filename);
519 if(!Importer::book().count(ext))
521 get_ui_interface()->error(_("I don't know how to open images of this type -- ")+ext);
527 Layer::Handle layer(add_layer_to("Import",get_canvas()));
531 if(!layer->set_param("filename",ValueBase(filename)))
533 w=layer->get_param("_width").get(int());
534 h=layer->get_param("_height").get(int());
537 Vector size=ValueBase(get_canvas()->rend_desc().get_br()-get_canvas()->rend_desc().get_tl());
543 if(size[0]<0 ^ size[1]<0)
550 if(size[0]<0 ^ size[1]<0)
553 if(!layer->set_param("tl",ValueBase(-x/2)))
555 if(!layer->set_param("br",ValueBase(x/2)))
560 if(!layer->set_param("tl",ValueBase(get_canvas()->rend_desc().get_tl())))
562 if(!layer->set_param("br",ValueBase(get_canvas()->rend_desc().get_br())))
566 layer->set_description(basename(filename));
567 signal_layer_new_description()(layer,filename);
573 get_ui_interface()->error("Unable to import "+filename);
581 CanvasInterface::waypoint_duplicate(synfigapp::ValueDesc value_desc,synfig::Waypoint waypoint)
583 Action::Handle action(Action::create("waypoint_set_smart"));
589 waypoint.make_unique();
590 waypoint.set_time(get_time());
592 ValueNode::Handle value_node(value_desc.get_value_node());
594 action->set_param("canvas",get_canvas());
595 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
596 action->set_param("waypoint",waypoint);
597 action->set_param("time",get_time());
598 action->set_param("value_node",value_node);
600 if(!get_instance()->perform_action(action))
601 get_ui_interface()->error(_("Action Failed."));
605 CanvasInterface::waypoint_remove(synfigapp::ValueDesc value_desc,synfig::Waypoint waypoint)
607 Action::Handle action(Action::create("waypoint_remove"));
613 ValueNode::Handle value_node(value_desc.get_value_node());
615 action->set_param("canvas",get_canvas());
616 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
617 action->set_param("waypoint",waypoint);
618 action->set_param("value_node",value_node);
620 if(!get_instance()->perform_action(action))
621 get_ui_interface()->error(_("Action Failed."));
626 CanvasInterface::auto_export(ValueNode::Handle /*value_node*/)
629 // Check to see if we are already exported.
630 if(value_node->is_exported())
633 Action::Handle action(Action::create("value_node_add"));
639 String name(strprintf(_("Unnamed%08d"),synfig::UniqueID().get_uid()));
641 action->set_param("canvas",get_canvas());
642 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
643 action->set_param("new",value_node);
644 action->set_param("name",name);
646 if(!get_instance()->perform_action(action))
647 get_ui_interface()->error(_("Action Failed."));
652 CanvasInterface::auto_export(const ValueDesc& /*value_desc*/)
654 // THIS FUNCTION IS DEPRECATED, AND IS NOW A STUB.
656 // Check to see if we are already exported.
657 if(value_desc.is_exported())
660 Action::Handle action(Action::create("value_desc_export"));
666 String name(strprintf(_("Unnamed%08d"),synfig::UniqueID().get_uid()));
668 action->set_param("canvas",get_canvas());
669 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
670 action->set_param("value_desc",value_desc);
671 action->set_param("name",name);
673 if(!get_instance()->perform_action(action))
674 get_ui_interface()->error(_("Action Failed."));
679 CanvasInterface::change_value(synfigapp::ValueDesc value_desc,synfig::ValueBase new_value)
681 // If this isn't really a change, then don't bother
682 if(new_value==value_desc.get_value(get_time()))
685 // If this change needs to take place elsewhere, then so be it.
686 if(value_desc.get_canvas() && value_desc.get_canvas()->get_root()!=get_canvas()->get_root())do
688 etl::handle<Instance> instance;
689 instance=find_instance(value_desc.get_canvas()->get_root());
692 return instance->find_canvas_interface(value_desc.get_canvas())->change_value(value_desc,new_value);
695 get_ui_interface()->error(_("The value you are trying to edit is in a composition\nwhich doesn't seem to be open. Open that composition and you\nshould be able to edit this value as normal."));
701 { synfig::warning("Can't get canvas from value desc...?"); }
704 synfigapp::Action::Handle action(synfigapp::Action::create("value_desc_set"));
710 action->set_param("canvas",get_canvas());
711 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
712 action->set_param("time",get_time());
713 action->set_param("value_desc",value_desc);
714 action->set_param("new_value",new_value);
716 return get_instance()->perform_action(action);
720 CanvasInterface::set_meta_data(const synfig::String& key,const synfig::String& data)
722 get_canvas()->set_meta_data(key,data);
726 // this function goes with find_important_value_descs()
728 _process_value_desc(const synfigapp::ValueDesc& value_desc,std::vector<synfigapp::ValueDesc>& out, synfig::GUIDSet& guid_set)
732 if(value_desc.get_value_type()==ValueBase::TYPE_CANVAS)
734 Canvas::Handle canvas;
735 canvas=value_desc.get_value().get(canvas);
736 if(!canvas || !canvas->is_inline())
738 ret+=CanvasInterface::find_important_value_descs(canvas,out,guid_set);
741 if(value_desc.is_value_node())
743 ValueNode::Handle value_node(value_desc.get_value_node());
745 if(guid_set.count(value_node->get_guid()))
747 guid_set.insert(value_node->get_guid());
749 if(LinkableValueNode::Handle::cast_dynamic(value_node))
751 if(ValueNode_DynamicList::Handle::cast_dynamic(value_node))
753 out.push_back(value_desc);
756 // Process the linkable ValueNode's children
757 LinkableValueNode::Handle value_node_copy(LinkableValueNode::Handle::cast_dynamic(value_node));
759 for(i=0;i<value_node_copy->link_count();i++)
761 ValueNode::Handle link(value_node_copy->get_link(i));
762 if(!link->is_exported())
763 ret+=_process_value_desc(ValueDesc(value_node_copy,i),out,guid_set);
766 else if(ValueNode_Animated::Handle::cast_dynamic(value_node))
768 out.push_back(value_desc);
777 CanvasInterface::find_important_value_descs(synfig::Canvas::Handle canvas,std::vector<synfigapp::ValueDesc>& out,synfig::GUIDSet& guid_set)
780 if(!canvas->is_inline())
782 ValueNodeList::const_iterator iter;
785 iter=canvas->value_node_list().begin();
786 iter!=canvas->value_node_list().end();
788 ret+=_process_value_desc(ValueDesc(canvas,(*iter)->get_id()),out,guid_set);
791 Canvas::const_iterator iter;
793 for(iter=canvas->begin();iter!=canvas->end();++iter)
795 Layer::Handle layer(*iter);
797 Layer::DynamicParamList::const_iterator iter;
799 iter=layer->dynamic_param_list().begin();
800 iter!=layer->dynamic_param_list().end();
803 if(!iter->second->is_exported())
804 ret+=_process_value_desc(ValueDesc(layer,iter->first),out,guid_set);
806 ValueBase value(layer->get_param("canvas"));
808 ret+=_process_value_desc(ValueDesc(layer,"canvas"),out,guid_set);
815 CanvasInterface::find_important_value_descs(std::vector<synfigapp::ValueDesc>& out)
818 return find_important_value_descs(get_canvas(),out,tmp);
822 CanvasInterface::seek_frame(int frames)
826 float fps(get_canvas()->rend_desc().get_frame_rate());
827 Time newtime(get_time()+(float)frames/fps);
828 newtime=newtime.round(fps);
830 if(newtime<=get_canvas()->rend_desc().get_time_start())
831 newtime=get_canvas()->rend_desc().get_time_start();
832 if(newtime>=get_canvas()->rend_desc().get_time_end())
833 newtime=get_canvas()->rend_desc().get_time_end();
838 CanvasInterface::seek_time(synfig::Time time)
843 float fps(get_canvas()->rend_desc().get_frame_rate());
845 if(time>=synfig::Time::end())
847 set_time(get_canvas()->rend_desc().get_time_end());
850 if(time<=synfig::Time::begin())
852 set_time(get_canvas()->rend_desc().get_time_start());
856 Time newtime(get_time()+time);
857 newtime=newtime.round(fps);
859 if(newtime<=get_canvas()->rend_desc().get_time_start())
860 newtime=get_canvas()->rend_desc().get_time_start();
861 if(newtime>=get_canvas()->rend_desc().get_time_end())
862 newtime=get_canvas()->rend_desc().get_time_end();