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 for (Canvas::Children::iterator iter = children.begin(); iter != children.end(); iter++)
112 if (handle<CanvasInterface> interface = get_instance()->find_canvas_interface(*iter))
113 interface->set_time(interface->get_canvas()->get_time());
115 signal_time_changed()();
116 signal_dirty_preview()();
120 CanvasInterface::get_time()const
126 CanvasInterface::refresh_current_values()
128 get_canvas()->set_time(cur_time_);
129 signal_time_changed()();
130 signal_dirty_preview()();
133 etl::handle<CanvasInterface>
134 CanvasInterface::create(loose_handle<Instance> instance,handle<Canvas> canvas)
136 etl::handle<CanvasInterface> intrfc;
137 intrfc=new CanvasInterface(instance,canvas);
138 instance->canvas_interface_list().push_front(intrfc);
143 CanvasInterface::set_mode(Mode x)
145 Action::Handle action(Action::EditModeSet::create());
149 action->set_param("canvas",get_canvas());
150 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
151 action->set_param("edit_mode",x);
153 if(!action->is_ready())
155 get_ui_interface()->error(_("Action Not Ready, unable to change mode"));
160 if(!get_instance()->perform_action(action))
161 get_ui_interface()->error(_("Unable to change mode"));
164 // signal_mode_changed_(x);
167 CanvasInterface::Mode
168 CanvasInterface::get_mode()const
176 CanvasInterface::add_layer_to(String name, Canvas::Handle canvas, int depth)
178 synfigapp::Action::PassiveGrouper group(get_instance().get(),_("Add Layer To"));
180 Layer::Handle layer(Layer::create(name));
187 if(canvas!=get_canvas() && !canvas->is_inline())
189 synfig::error("Bad canvas passed to \"add_layer_to\"");
193 layer->set_canvas(canvas);
195 // Apply some defaults
196 if(layer->set_param("fg",synfigapp::Main::get_foreground_color()))
197 layer->set_param("bg",synfigapp::Main::get_background_color());
199 layer->set_param("color",synfigapp::Main::get_foreground_color());
201 layer->set_param("width",synfigapp::Main::get_bline_width().units(get_canvas()->rend_desc()));
202 layer->set_param("gradient",synfigapp::Main::get_gradient());
204 layer->set_param("amount",synfigapp::Main::get_opacity());
205 layer->set_param("blend_method",synfigapp::Main::get_blend_method());
208 // Grab the layer's list pf parameters
209 Layer::ParamList paramlist=layer->get_param_list();
210 Layer::ParamList::iterator iter;
211 for(iter=paramlist.begin();iter!=paramlist.end();++iter)
213 ValueNode::Handle value_node;
215 if(iter->second.get_type()==ValueBase::TYPE_LIST)
216 value_node=LinkableValueNode::create("dynamic_list",iter->second);
217 else if(LinkableValueNode::check_type("composite",iter->second.get_type()) &&
218 (iter->second.get_type()!=ValueBase::TYPE_COLOR && iter->second.get_type()!=ValueBase::TYPE_VECTOR)
220 value_node=LinkableValueNode::create("composite",iter->second);
223 layer->connect_dynamic_param(iter->first,value_node);
227 // Action to add the layer
228 Action::Handle action(Action::LayerAdd::create());
234 action->set_param("canvas",canvas);
235 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
236 action->set_param("new",layer);
238 if(!action->is_ready())
240 get_ui_interface()->error(_("Action Not Ready"));
244 if(!get_instance()->perform_action(action))
246 get_ui_interface()->error(_("Action Failed."));
250 synfig::info("DEPTH=%d",depth);
251 // Action to move the layer (if necessary)
254 Action::Handle action(Action::create("layer_move"));
260 action->set_param("canvas",canvas);
261 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
262 action->set_param("layer",layer);
263 action->set_param("new_index",depth);
265 if(!action->is_ready())
267 get_ui_interface()->error(_("Move Action Not Ready"));
271 if(!get_instance()->perform_action(action))
273 get_ui_interface()->error(_("Move Action Failed."));
284 CanvasInterface::convert(ValueDesc value_desc, String type)
286 Action::Handle action(Action::ValueDescConvert::create());
292 action->set_param("canvas",get_canvas());
293 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
294 action->set_param("value_desc",value_desc);
295 action->set_param("type",type);
297 if(!action->is_ready())
299 get_ui_interface()->error(_("Action Not Ready"));
303 if(get_instance()->perform_action(action))
306 get_ui_interface()->error(_("Action Failed."));
311 CanvasInterface::add_value_node(synfig::ValueNode::Handle value_node, synfig::String name)
315 get_ui_interface()->error(_("Empty name!"));
319 Action::Handle action(Action::ValueNodeAdd::create());
325 action->set_param("canvas",get_canvas());
326 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
327 action->set_param("new",value_node);
328 action->set_param("name",name);
330 if(!action->is_ready())
332 get_ui_interface()->error(_("Action Not Ready"));
336 if(get_instance()->perform_action(action))
339 get_ui_interface()->error(_("Action Failed."));
344 CanvasInterface::generate_param_list(const ValueDesc &value_desc)
346 synfigapp::Action::ParamList param_list;
347 param_list.add("time",get_time());
348 param_list.add("canvas_interface",etl::handle<CanvasInterface>(this));
349 param_list.add("canvas",get_canvas());
351 param_list.add("value_desc",value_desc);
353 if(value_desc.parent_is_value_node())
354 param_list.add("parent_value_node",value_desc.get_parent_value_node());
356 if(value_desc.is_value_node())
357 param_list.add("value_node",value_desc.get_value_node());
359 if(value_desc.is_const())
360 param_list.add("value",value_desc.get_value());
362 if(value_desc.parent_is_layer_param())
364 param_list.add("parent_layer",value_desc.get_layer());
365 param_list.add("parent_layer_param",value_desc.get_param_name());
369 synfigapp::SelectionManager::ChildrenList children_list;
370 children_list=get_selection_manager()->get_selected_children();
371 if(!value_desc.parent_is_canvas() && children_list.size()==1)
373 param_list.add("dest",value_desc);
374 param_list.add("src",children_list.front().get_value_node());
381 CanvasInterface::generate_param_list(const std::list<synfigapp::ValueDesc> &value_desc_list)
383 synfigapp::Action::ParamList param_list;
384 param_list.add("time",get_time());
385 param_list.add("canvas_interface",etl::handle<CanvasInterface>(this));
386 param_list.add("canvas",get_canvas());
388 std::list<synfigapp::ValueDesc>::const_iterator iter;
389 for(iter=value_desc_list.begin();iter!=value_desc_list.end();++iter)
391 param_list.add("value_desc",*iter);
392 if(iter->is_value_node())
394 param_list.add("value_node",iter->get_value_node());
403 CanvasInterface::set_rend_desc(const synfig::RendDesc &rend_desc)
405 Action::Handle action(Action::create("canvas_rend_desc_set"));
411 action->set_param("canvas",get_canvas());
412 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
413 action->set_param("rend_desc",rend_desc);
415 if(!get_instance()->perform_action(action))
416 get_ui_interface()->error(_("Action Failed."));
420 CanvasInterface::set_name(const String &x)
422 //! \todo This needs to be converted into an action
423 get_canvas()->set_name(x);
424 signal_id_changed_();
429 CanvasInterface::set_description(const String &x)
431 //! \todo This needs to be converted into an action
432 get_canvas()->set_description(x);
437 CanvasInterface::set_id(const String &x)
439 //! \todo This needs to be converted into an action
440 get_canvas()->set_id(x);
441 signal_id_changed_();
447 CanvasInterface::jump_to_next_keyframe()
449 synfig::info("Current time: %s",get_time().get_string().c_str());
452 synfig::Keyframe keyframe(*get_canvas()->keyframe_list().find_next(get_time()));
453 synfig::info("Jumping to keyframe \"%s\" at %s",keyframe.get_description().c_str(),keyframe.get_time().get_string().c_str());
454 set_time(keyframe.get_time());
456 catch(...) { synfig::warning("Unable to find next keyframe"); }
460 CanvasInterface::jump_to_prev_keyframe()
462 synfig::info("Current time: %s",get_time().get_string().c_str());
465 synfig::Keyframe keyframe(*get_canvas()->keyframe_list().find_prev(get_time()));
466 synfig::info("Jumping to keyframe \"%s\" at %s",keyframe.get_description().c_str(),keyframe.get_time().get_string().c_str());
467 set_time(keyframe.get_time());
469 catch(...) { synfig::warning("Unable to find prev keyframe"); }
473 CanvasInterface::import(const synfig::String &filename, bool /*copy*/)
475 Action::PassiveGrouper group(get_instance().get(),_("Import Image"));
477 synfig::info("Attempting to import "+filename);
480 if(find(filename.begin(),filename.end(),'.')==filename.end())
482 get_ui_interface()->error("Filename must have an extension!");
486 String ext(String(filename.begin()+filename.find_last_of('.')+1,filename.end()));
487 std::transform(ext.begin(),ext.end(),ext.begin(),&::tolower);
489 // If this is a SIF file, then we need to do things slightly differently
490 if(ext=="sif" || ext=="sifz")try
493 Canvas::Handle outside_canvas(synfig::open_canvas(filename));
495 throw String(_("Unable to open this composition"));
497 Layer::Handle layer(add_layer_to("PasteCanvas",get_canvas()));
499 throw String(_("Unable to create \"Paste Canvas\" layer"));
500 if(!layer->set_param("canvas",ValueBase(outside_canvas)))
503 //layer->set_description(basename(filename));
504 signal_layer_new_description()(layer,filename);
509 get_ui_interface()->error(x+" -- "+filename);
514 get_ui_interface()->error(_("Uncaught exception when attempting\nto open this composition -- ")+filename);
518 if(!Importer::book().count(ext))
520 get_ui_interface()->error(_("I don't know how to open images of this type -- ")+ext);
526 Layer::Handle layer(add_layer_to("Import",get_canvas()));
530 if(!layer->set_param("filename",ValueBase(filename)))
532 w=layer->get_param("_width").get(int());
533 h=layer->get_param("_height").get(int());
536 Vector size=ValueBase(get_canvas()->rend_desc().get_br()-get_canvas()->rend_desc().get_tl());
542 if(size[0]<0 ^ size[1]<0)
549 if(size[0]<0 ^ size[1]<0)
552 if(!layer->set_param("tl",ValueBase(-x/2)))
554 if(!layer->set_param("br",ValueBase(x/2)))
559 if(!layer->set_param("tl",ValueBase(get_canvas()->rend_desc().get_tl())))
561 if(!layer->set_param("br",ValueBase(get_canvas()->rend_desc().get_br())))
565 layer->set_description(basename(filename));
566 signal_layer_new_description()(layer,filename);
572 get_ui_interface()->error("Unable to import "+filename);
580 CanvasInterface::waypoint_duplicate(synfigapp::ValueDesc value_desc,synfig::Waypoint waypoint)
582 Action::Handle action(Action::create("waypoint_set_smart"));
588 waypoint.make_unique();
589 waypoint.set_time(get_time());
591 ValueNode::Handle value_node(value_desc.get_value_node());
593 action->set_param("canvas",get_canvas());
594 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
595 action->set_param("waypoint",waypoint);
596 action->set_param("time",get_time());
597 action->set_param("value_node",value_node);
599 if(!get_instance()->perform_action(action))
600 get_ui_interface()->error(_("Action Failed."));
604 CanvasInterface::waypoint_remove(synfigapp::ValueDesc value_desc,synfig::Waypoint waypoint)
606 Action::Handle action(Action::create("waypoint_remove"));
612 ValueNode::Handle value_node(value_desc.get_value_node());
614 action->set_param("canvas",get_canvas());
615 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
616 action->set_param("waypoint",waypoint);
617 action->set_param("value_node",value_node);
619 if(!get_instance()->perform_action(action))
620 get_ui_interface()->error(_("Action Failed."));
625 CanvasInterface::auto_export(ValueNode::Handle /*value_node*/)
628 // Check to see if we are already exported.
629 if(value_node->is_exported())
632 Action::Handle action(Action::create("value_node_add"));
638 String name(strprintf(_("Unnamed%08d"),synfig::UniqueID().get_uid()));
640 action->set_param("canvas",get_canvas());
641 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
642 action->set_param("new",value_node);
643 action->set_param("name",name);
645 if(!get_instance()->perform_action(action))
646 get_ui_interface()->error(_("Action Failed."));
651 CanvasInterface::auto_export(const ValueDesc& /*value_desc*/)
653 // THIS FUNCTION IS DEPRECATED, AND IS NOW A STUB.
655 // Check to see if we are already exported.
656 if(value_desc.is_exported())
659 Action::Handle action(Action::create("value_desc_export"));
665 String name(strprintf(_("Unnamed%08d"),synfig::UniqueID().get_uid()));
667 action->set_param("canvas",get_canvas());
668 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
669 action->set_param("value_desc",value_desc);
670 action->set_param("name",name);
672 if(!get_instance()->perform_action(action))
673 get_ui_interface()->error(_("Action Failed."));
678 CanvasInterface::change_value(synfigapp::ValueDesc value_desc,synfig::ValueBase new_value)
680 // If this isn't really a change, then don't bother
681 if(new_value==value_desc.get_value(get_time()))
684 // If this change needs to take place elsewhere, then so be it.
685 if(value_desc.get_canvas() && value_desc.get_canvas()->get_root()!=get_canvas()->get_root())do
687 etl::handle<Instance> instance;
688 instance=find_instance(value_desc.get_canvas()->get_root());
691 return instance->find_canvas_interface(value_desc.get_canvas())->change_value(value_desc,new_value);
694 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."));
700 { synfig::warning("Can't get canvas from value desc...?"); }
703 synfigapp::Action::Handle action(synfigapp::Action::create("value_desc_set"));
709 action->set_param("canvas",get_canvas());
710 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
711 action->set_param("time",get_time());
712 action->set_param("value_desc",value_desc);
713 action->set_param("new_value",new_value);
715 return get_instance()->perform_action(action);
719 CanvasInterface::set_meta_data(const synfig::String& key,const synfig::String& data)
721 get_canvas()->set_meta_data(key,data);
725 // this function goes with find_important_value_descs()
727 _process_value_desc(const synfigapp::ValueDesc& value_desc,std::vector<synfigapp::ValueDesc>& out, synfig::GUIDSet& guid_set)
731 if(value_desc.get_value_type()==ValueBase::TYPE_CANVAS)
733 Canvas::Handle canvas;
734 canvas=value_desc.get_value().get(canvas);
735 if(!canvas || !canvas->is_inline())
737 ret+=CanvasInterface::find_important_value_descs(canvas,out,guid_set);
740 if(value_desc.is_value_node())
742 ValueNode::Handle value_node(value_desc.get_value_node());
744 if(guid_set.count(value_node->get_guid()))
746 guid_set.insert(value_node->get_guid());
748 if(LinkableValueNode::Handle::cast_dynamic(value_node))
750 if(ValueNode_DynamicList::Handle::cast_dynamic(value_node))
752 out.push_back(value_desc);
755 // Process the linkable ValueNode's children
756 LinkableValueNode::Handle value_node_copy(LinkableValueNode::Handle::cast_dynamic(value_node));
758 for(i=0;i<value_node_copy->link_count();i++)
760 ValueNode::Handle link(value_node_copy->get_link(i));
761 if(!link->is_exported())
762 ret+=_process_value_desc(ValueDesc(value_node_copy,i),out,guid_set);
765 else if(ValueNode_Animated::Handle::cast_dynamic(value_node))
767 out.push_back(value_desc);
776 CanvasInterface::find_important_value_descs(synfig::Canvas::Handle canvas,std::vector<synfigapp::ValueDesc>& out,synfig::GUIDSet& guid_set)
779 if(!canvas->is_inline())
781 ValueNodeList::const_iterator iter;
784 iter=canvas->value_node_list().begin();
785 iter!=canvas->value_node_list().end();
787 ret+=_process_value_desc(ValueDesc(canvas,(*iter)->get_id()),out,guid_set);
790 Canvas::const_iterator iter;
792 for(iter=canvas->begin();iter!=canvas->end();++iter)
794 Layer::Handle layer(*iter);
796 Layer::DynamicParamList::const_iterator iter;
798 iter=layer->dynamic_param_list().begin();
799 iter!=layer->dynamic_param_list().end();
802 if(!iter->second->is_exported())
803 ret+=_process_value_desc(ValueDesc(layer,iter->first),out,guid_set);
805 ValueBase value(layer->get_param("canvas"));
807 ret+=_process_value_desc(ValueDesc(layer,"canvas"),out,guid_set);
814 CanvasInterface::find_important_value_descs(std::vector<synfigapp::ValueDesc>& out)
817 return find_important_value_descs(get_canvas(),out,tmp);
821 CanvasInterface::seek_frame(int frames)
825 float fps(get_canvas()->rend_desc().get_frame_rate());
826 Time newtime(get_time()+(float)frames/fps);
827 newtime=newtime.round(fps);
829 if(newtime<=get_canvas()->rend_desc().get_time_start())
830 newtime=get_canvas()->rend_desc().get_time_start();
831 if(newtime>=get_canvas()->rend_desc().get_time_end())
832 newtime=get_canvas()->rend_desc().get_time_end();
837 CanvasInterface::seek_time(synfig::Time time)
842 float fps(get_canvas()->rend_desc().get_frame_rate());
844 if(time>=synfig::Time::end())
846 set_time(get_canvas()->rend_desc().get_time_end());
849 if(time<=synfig::Time::begin())
851 set_time(get_canvas()->rend_desc().get_time_start());
855 Time newtime(get_time()+time);
856 newtime=newtime.round(fps);
858 if(newtime<=get_canvas()->rend_desc().get_time_start())
859 newtime=get_canvas()->rend_desc().get_time_start();
860 if(newtime>=get_canvas()->rend_desc().get_time_end())
861 newtime=get_canvas()->rend_desc().get_time_end();