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
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
35 #include <synfig/valuenode_scale.h>
36 #include <synfig/valuenode_timedswap.h>
37 #include <synfig/valuenode_composite.h>
38 #include <synfig/valuenode_subtract.h>
39 #include <synfig/valuenode_linear.h>
40 #include <synfig/valuenode_reference.h>
41 #include <synfig/valuenode_twotone.h>
42 #include <synfig/valuenode_stripes.h>
44 #include <synfig/waypoint.h>
45 #include <synfig/loadcanvas.h>
46 #include <synfig/importer.h>
47 #include <synfig/guidset.h>
49 #include "canvasinterface.h"
52 #include "actions/layeradd.h"
53 #include "actions/valuedescconvert.h"
54 #include "actions/valuenodeadd.h"
55 #include "actions/editmodeset.h"
56 #include "action_system.h"
60 #include <synfig/gradient.h>
64 /* === U S I N G =========================================================== */
68 using namespace synfig;
69 using namespace synfigapp;
71 /* === M A C R O S ========================================================= */
73 /* === G L O B A L S ======================================================= */
75 /* === P R O C E D U R E S ================================================= */
77 /* === M E T H O D S ======================================================= */
79 CanvasInterface::CanvasInterface(loose_handle<Instance> instance,handle<Canvas> canvas):
82 cur_time_(canvas->rend_desc().get_frame_start()),
83 mode_(MODE_NORMAL|MODE_ANIMATE_PAST|MODE_ANIMATE_FUTURE)
85 set_selection_manager(get_instance()->get_selection_manager());
86 set_ui_interface(get_instance()->get_ui_interface());
89 CanvasInterface::~CanvasInterface()
91 synfig::info("synfigapp::CanvasInterface::~CanvasInterface(): Deleted");
95 CanvasInterface::set_time(synfig::Time x)
97 if(get_canvas()->rend_desc().get_frame_rate())
99 float fps(get_canvas()->rend_desc().get_frame_rate());
100 Time r(x.round(fps));
101 //synfig::info("CanvasInterface::set_time(): %s rounded to %s\n",x.get_string(fps).c_str(),r.get_string(fps).c_str());
104 if(cur_time_.is_equal(x))
108 signal_time_changed()();
109 signal_dirty_preview()();
113 CanvasInterface::get_time()const
119 CanvasInterface::refresh_current_values()
121 get_canvas()->set_time(cur_time_);
122 signal_time_changed()();
123 signal_dirty_preview()();
126 etl::handle<CanvasInterface>
127 CanvasInterface::create(loose_handle<Instance> instance,handle<Canvas> canvas)
129 etl::handle<CanvasInterface> intrfc;
130 intrfc=new CanvasInterface(instance,canvas);
131 instance->canvas_interface_list().push_front(intrfc);
136 CanvasInterface::set_mode(Mode x)
138 Action::Handle action(Action::EditModeSet::create());
142 action->set_param("canvas",get_canvas());
143 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
144 action->set_param("edit_mode",x);
146 if(!action->is_ready())
148 get_ui_interface()->error(_("Action Not Ready, unable to change mode"));
153 if(!get_instance()->perform_action(action))
154 get_ui_interface()->error(_("Unable to change mode"));
157 // signal_mode_changed_(x);
160 CanvasInterface::Mode
161 CanvasInterface::get_mode()const
169 CanvasInterface::add_layer_to(String name, Canvas::Handle canvas, int depth)
171 synfigapp::Action::PassiveGrouper group(get_instance().get(),_("Add Layer To"));
173 Layer::Handle layer(Layer::create(name));
180 if(canvas!=get_canvas() && !canvas->is_inline())
182 synfig::error("Bad canvas passed to \"add_layer_to\"");
186 layer->set_canvas(canvas);
188 // Apply some defaults
189 if(layer->set_param("fg",synfigapp::Main::get_foreground_color()))
190 layer->set_param("bg",synfigapp::Main::get_background_color());
192 layer->set_param("color",synfigapp::Main::get_foreground_color());
194 layer->set_param("width",synfigapp::Main::get_bline_width().units(get_canvas()->rend_desc()));
195 layer->set_param("gradient",synfigapp::Main::get_gradient());
197 layer->set_param("amount",synfigapp::Main::get_opacity());
198 layer->set_param("blend_method",synfigapp::Main::get_blend_method());
201 // Grab the layer's list pf parameters
202 Layer::ParamList paramlist=layer->get_param_list();
203 Layer::ParamList::iterator iter;
204 for(iter=paramlist.begin();iter!=paramlist.end();++iter)
206 ValueNode::Handle value_node;
208 if(iter->second.get_type()==ValueBase::TYPE_LIST)
209 value_node=LinkableValueNode::create("dynamic_list",iter->second);
210 else if(LinkableValueNode::check_type("composite",iter->second.get_type()) &&
211 (iter->second.get_type()!=ValueBase::TYPE_COLOR && iter->second.get_type()!=ValueBase::TYPE_VECTOR)
213 value_node=LinkableValueNode::create("composite",iter->second);
216 layer->connect_dynamic_param(iter->first,value_node);
220 // Action to add the layer
221 Action::Handle action(Action::LayerAdd::create());
227 action->set_param("canvas",canvas);
228 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
229 action->set_param("new",layer);
231 if(!action->is_ready())
233 get_ui_interface()->error(_("Action Not Ready"));
237 if(!get_instance()->perform_action(action))
239 get_ui_interface()->error(_("Action Failed."));
243 synfig::info("DEPTH=%d",depth);
244 // Action to move the layer (if necessary)
247 Action::Handle action(Action::create("layer_move"));
253 action->set_param("canvas",canvas);
254 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
255 action->set_param("layer",layer);
256 action->set_param("new_index",depth);
258 if(!action->is_ready())
260 get_ui_interface()->error(_("Move Action Not Ready"));
264 if(!get_instance()->perform_action(action))
266 get_ui_interface()->error(_("Move Action Failed."));
277 CanvasInterface::convert(ValueDesc value_desc, String type)
279 Action::Handle action(Action::ValueDescConvert::create());
285 action->set_param("canvas",get_canvas());
286 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
287 action->set_param("value_desc",value_desc);
288 action->set_param("type",type);
290 if(!action->is_ready())
292 get_ui_interface()->error(_("Action Not Ready"));
296 if(get_instance()->perform_action(action))
299 get_ui_interface()->error(_("Action Failed."));
304 CanvasInterface::add_value_node(synfig::ValueNode::Handle value_node, synfig::String name)
308 get_ui_interface()->error(_("Empty name!"));
312 Action::Handle action(Action::ValueNodeAdd::create());
318 action->set_param("canvas",get_canvas());
319 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
320 action->set_param("new",value_node);
321 action->set_param("name",name);
323 if(!action->is_ready())
325 get_ui_interface()->error(_("Action Not Ready"));
329 if(get_instance()->perform_action(action))
332 get_ui_interface()->error(_("Action Failed."));
337 CanvasInterface::generate_param_list(const ValueDesc &value_desc)
339 synfigapp::Action::ParamList param_list;
340 param_list.add("time",get_time());
341 param_list.add("canvas_interface",etl::handle<CanvasInterface>(this));
342 param_list.add("canvas",get_canvas());
344 param_list.add("value_desc",value_desc);
346 if(value_desc.parent_is_value_node())
347 param_list.add("parent_value_node",value_desc.get_parent_value_node());
349 if(value_desc.is_value_node())
350 param_list.add("value_node",value_desc.get_value_node());
352 if(value_desc.is_const())
353 param_list.add("value",value_desc.get_value());
355 if(value_desc.parent_is_layer_param())
357 param_list.add("parent_layer",value_desc.get_layer());
358 param_list.add("parent_layer_param",value_desc.get_param_name());
362 synfigapp::SelectionManager::ChildrenList children_list;
363 children_list=get_selection_manager()->get_selected_children();
364 if(!value_desc.parent_is_canvas() && children_list.size()==1)
366 param_list.add("dest",value_desc);
367 param_list.add("src",children_list.front().get_value_node());
374 CanvasInterface::generate_param_list(const std::list<synfigapp::ValueDesc> &value_desc_list)
376 synfigapp::Action::ParamList param_list;
377 param_list.add("time",get_time());
378 param_list.add("canvas_interface",etl::handle<CanvasInterface>(this));
379 param_list.add("canvas",get_canvas());
381 std::list<synfigapp::ValueDesc>::const_iterator iter;
382 for(iter=value_desc_list.begin();iter!=value_desc_list.end();++iter)
384 param_list.add("value_desc",*iter);
385 if(iter->is_value_node())
387 param_list.add("value_node",iter->get_value_node());
396 CanvasInterface::set_rend_desc(const synfig::RendDesc &rend_desc)
398 Action::Handle action(Action::create("canvas_rend_desc_set"));
404 action->set_param("canvas",get_canvas());
405 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
406 action->set_param("rend_desc",rend_desc);
408 if(!get_instance()->perform_action(action))
409 get_ui_interface()->error(_("Action Failed."));
413 CanvasInterface::set_name(const String &x)
415 //! \todo This needs to be converted into an action
416 get_canvas()->set_name(x);
417 signal_id_changed_();
422 CanvasInterface::set_description(const String &x)
424 //! \todo This needs to be converted into an action
425 get_canvas()->set_description(x);
430 CanvasInterface::set_id(const String &x)
432 //! \todo This needs to be converted into an action
433 get_canvas()->set_id(x);
434 signal_id_changed_();
440 CanvasInterface::jump_to_next_keyframe()
442 synfig::info("Current time: %s",get_time().get_string().c_str());
445 synfig::Keyframe keyframe(*get_canvas()->keyframe_list().find_next(get_time()));
446 synfig::info("Jumping to keyframe \"%s\" at %s",keyframe.get_description().c_str(),keyframe.get_time().get_string().c_str());
447 set_time(keyframe.get_time());
449 catch(...) { synfig::warning("Unable to find next keyframe"); }
453 CanvasInterface::jump_to_prev_keyframe()
455 synfig::info("Current time: %s",get_time().get_string().c_str());
458 synfig::Keyframe keyframe(*get_canvas()->keyframe_list().find_prev(get_time()));
459 synfig::info("Jumping to keyframe \"%s\" at %s",keyframe.get_description().c_str(),keyframe.get_time().get_string().c_str());
460 set_time(keyframe.get_time());
462 catch(...) { synfig::warning("Unable to find prev keyframe"); }
466 CanvasInterface::import(const synfig::String &filename, bool copy)
468 Action::PassiveGrouper group(get_instance().get(),_("Import Image"));
470 synfig::info("Attempting to import "+filename);
473 if(find(filename.begin(),filename.end(),'.')==filename.end())
475 get_ui_interface()->error("Filename must have an extension!");
479 String ext(String(filename.begin()+filename.find_last_of('.')+1,filename.end()));
480 std::transform(ext.begin(),ext.end(),ext.begin(),&::tolower);
482 // If this is a SIF file, then we need to do things slightly differently
483 if(ext=="sif" || ext=="sifz")try
486 Canvas::Handle outside_canvas(synfig::open_canvas(filename));
488 throw String(_("Unable to open this composition"));
490 Layer::Handle layer(add_layer_to("PasteCanvas",get_canvas()));
492 throw String(_("Unable to create \"PasteCanvas\" layer"));
493 if(!layer->set_param("canvas",ValueBase(outside_canvas)))
496 //layer->set_description(basename(filename));
497 signal_layer_new_description()(layer,filename);
502 get_ui_interface()->error(x+" -- "+filename);
507 get_ui_interface()->error(_("Uncaught exception when attempting\nto open this composition -- ")+filename);
513 if(!Importer::book().count(ext))
515 get_ui_interface()->error(_("I don't know how to open images of this type -- ")+ext);
521 Layer::Handle layer(add_layer_to("Import",get_canvas()));
525 if(!layer->set_param("filename",ValueBase(filename)))
527 w=layer->get_param("_width").get(int());
528 h=layer->get_param("_height").get(int());
531 Vector size=ValueBase(get_canvas()->rend_desc().get_br()-get_canvas()->rend_desc().get_tl());
537 if(size[0]<0 ^ size[1]<0)
544 if(size[0]<0 ^ size[1]<0)
547 if(!layer->set_param("tl",ValueBase(-x/2)))
549 if(!layer->set_param("br",ValueBase(x/2)))
554 if(!layer->set_param("tl",ValueBase(get_canvas()->rend_desc().get_tl())))
556 if(!layer->set_param("br",ValueBase(get_canvas()->rend_desc().get_br())))
560 layer->set_description(basename(filename));
561 signal_layer_new_description()(layer,filename);
567 get_ui_interface()->error("Unable to import "+filename);
575 CanvasInterface::waypoint_duplicate(synfigapp::ValueDesc value_desc,synfig::Waypoint waypoint)
577 Action::Handle action(Action::create("waypoint_set_smart"));
583 waypoint.make_unique();
584 waypoint.set_time(get_time());
586 ValueNode::Handle value_node(value_desc.get_value_node());
588 action->set_param("canvas",get_canvas());
589 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
590 action->set_param("waypoint",waypoint);
591 action->set_param("time",get_time());
592 action->set_param("value_node",value_node);
594 if(!get_instance()->perform_action(action))
595 get_ui_interface()->error(_("Action Failed."));
599 CanvasInterface::waypoint_remove(synfigapp::ValueDesc value_desc,synfig::Waypoint waypoint)
601 Action::Handle action(Action::create("waypoint_remove"));
607 ValueNode::Handle value_node(value_desc.get_value_node());
609 action->set_param("canvas",get_canvas());
610 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
611 action->set_param("waypoint",waypoint);
612 action->set_param("value_node",value_node);
614 if(!get_instance()->perform_action(action))
615 get_ui_interface()->error(_("Action Failed."));
620 CanvasInterface::auto_export(ValueNode::Handle value_node)
623 // Check to see if we are already exported.
624 if(value_node->is_exported())
627 Action::Handle action(Action::create("value_node_add"));
633 String name(strprintf(_("Unnamed%08d"),synfig::UniqueID().get_uid()));
635 action->set_param("canvas",get_canvas());
636 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
637 action->set_param("new",value_node);
638 action->set_param("name",name);
640 if(!get_instance()->perform_action(action))
641 get_ui_interface()->error(_("Action Failed."));
646 CanvasInterface::auto_export(const ValueDesc& value_desc)
648 // THIS FUNCTION IS DEPRECATED, AND IS NOW A STUB.
650 // Check to see if we are already exported.
651 if(value_desc.is_exported())
654 Action::Handle action(Action::create("value_desc_export"));
660 String name(strprintf(_("Unnamed%08d"),synfig::UniqueID().get_uid()));
662 action->set_param("canvas",get_canvas());
663 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
664 action->set_param("value_desc",value_desc);
665 action->set_param("name",name);
667 if(!get_instance()->perform_action(action))
668 get_ui_interface()->error(_("Action Failed."));
673 CanvasInterface::change_value(synfigapp::ValueDesc value_desc,synfig::ValueBase new_value)
675 // If this isn't really a change, then don't bother
676 if(new_value==value_desc.get_value(get_time()))
679 // If this change needs to take place elsewhere, then so be it.
680 if(value_desc.get_canvas() && value_desc.get_canvas()->get_root()!=get_canvas()->get_root())do
682 etl::handle<Instance> instance;
683 instance=find_instance(value_desc.get_canvas()->get_root());
686 return instance->find_canvas_interface(value_desc.get_canvas())->change_value(value_desc,new_value);
689 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."));
695 { synfig::warning("Can't get canvas from value desc...?"); }
698 synfigapp::Action::Handle action(synfigapp::Action::create("value_desc_set"));
704 action->set_param("canvas",get_canvas());
705 action->set_param("canvas_interface",etl::loose_handle<CanvasInterface>(this));
706 action->set_param("time",get_time());
707 action->set_param("value_desc",value_desc);
708 action->set_param("new_value",new_value);
710 return get_instance()->perform_action(action);
714 CanvasInterface::set_meta_data(const synfig::String& key,const synfig::String& data)
716 get_canvas()->set_meta_data(key,data);
720 // this function goes with find_important_value_descs()
722 _process_value_desc(const synfigapp::ValueDesc& value_desc,std::vector<synfigapp::ValueDesc>& out, synfig::GUIDSet& guid_set)
726 if(value_desc.get_value_type()==ValueBase::TYPE_CANVAS)
728 Canvas::Handle canvas;
729 canvas=value_desc.get_value().get(canvas);
730 if(!canvas || !canvas->is_inline())
732 ret+=CanvasInterface::find_important_value_descs(canvas,out,guid_set);
735 if(value_desc.is_value_node())
737 ValueNode::Handle value_node(value_desc.get_value_node());
739 if(guid_set.count(value_node->get_guid()))
741 guid_set.insert(value_node->get_guid());
743 if(LinkableValueNode::Handle::cast_dynamic(value_node))
745 if(ValueNode_DynamicList::Handle::cast_dynamic(value_node))
747 out.push_back(value_desc);
750 // Process the linkable ValueNode's children
751 LinkableValueNode::Handle value_node_copy(LinkableValueNode::Handle::cast_dynamic(value_node));
753 for(i=0;i<value_node_copy->link_count();i++)
755 ValueNode::Handle link(value_node_copy->get_link(i));
756 if(!link->is_exported())
757 ret+=_process_value_desc(ValueDesc(value_node_copy,i),out,guid_set);
760 else if(ValueNode_Animated::Handle::cast_dynamic(value_node))
762 out.push_back(value_desc);
771 CanvasInterface::find_important_value_descs(synfig::Canvas::Handle canvas,std::vector<synfigapp::ValueDesc>& out,synfig::GUIDSet& guid_set)
774 if(!canvas->is_inline())
776 ValueNodeList::const_iterator iter;
779 iter=canvas->value_node_list().begin();
780 iter!=canvas->value_node_list().end();
782 ret+=_process_value_desc(ValueDesc(canvas,(*iter)->get_id()),out,guid_set);
785 Canvas::const_iterator iter;
787 for(iter=canvas->begin();iter!=canvas->end();++iter)
789 Layer::Handle layer(*iter);
791 Layer::DynamicParamList::const_iterator iter;
793 iter=layer->dynamic_param_list().begin();
794 iter!=layer->dynamic_param_list().end();
797 if(!iter->second->is_exported())
798 ret+=_process_value_desc(ValueDesc(layer,iter->first),out,guid_set);
800 ValueBase value(layer->get_param("canvas"));
802 ret+=_process_value_desc(ValueDesc(layer,"canvas"),out,guid_set);
809 CanvasInterface::find_important_value_descs(std::vector<synfigapp::ValueDesc>& out)
812 return find_important_value_descs(get_canvas(),out,tmp);
816 CanvasInterface::seek_frame(int frames)
820 float fps(get_canvas()->rend_desc().get_frame_rate());
821 Time newtime(get_time()+(float)frames/fps);
822 newtime=newtime.round(fps);
824 if(newtime<=get_canvas()->rend_desc().get_frame_start())
825 newtime=get_canvas()->rend_desc().get_frame_start();
826 if(newtime>=get_canvas()->rend_desc().get_frame_end())
827 newtime=get_canvas()->rend_desc().get_frame_end();
832 CanvasInterface::seek_time(synfig::Time time)
837 float fps(get_canvas()->rend_desc().get_frame_rate());
839 if(time>=synfig::Time::end())
841 set_time(get_canvas()->rend_desc().get_time_end());
844 if(time<=synfig::Time::begin())
846 set_time(get_canvas()->rend_desc().get_time_end());
850 Time newtime(get_time()+time);
851 newtime=newtime.round(fps);
853 if(newtime<=get_canvas()->rend_desc().get_time_start())
854 newtime=get_canvas()->rend_desc().get_time_start();
855 if(newtime>=get_canvas()->rend_desc().get_time_end())
856 newtime=get_canvas()->rend_desc().get_time_end();