1 /* === S Y N F I G ========================================================= */
2 /*! \file keyframeduplicate.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 ======================================================= */
32 #include "keyframeduplicate.h"
33 #include <synfigapp/canvasinterface.h>
34 #include <synfig/valuenode_dynamiclist.h>
35 #include <synfig/valuenode_animated.h>
36 #include "activepointsetsmart.h"
37 #include "waypointsetsmart.h"
43 using namespace synfig;
44 using namespace synfigapp;
45 using namespace Action;
47 /* === M A C R O S ========================================================= */
49 ACTION_INIT(Action::KeyframeDuplicate);
50 ACTION_SET_NAME(Action::KeyframeDuplicate,"keyframe_duplicate");
51 ACTION_SET_LOCAL_NAME(Action::KeyframeDuplicate,"Duplicate Keyframe");
52 ACTION_SET_TASK(Action::KeyframeDuplicate,"duplicate");
53 ACTION_SET_CATEGORY(Action::KeyframeDuplicate,Action::CATEGORY_KEYFRAME);
54 ACTION_SET_PRIORITY(Action::KeyframeDuplicate,0);
55 ACTION_SET_VERSION(Action::KeyframeDuplicate,"0.0");
56 ACTION_SET_CVS_ID(Action::KeyframeDuplicate,"$Id$");
58 /* === G L O B A L S ======================================================= */
60 /* === P R O C E D U R E S ================================================= */
62 /* === M E T H O D S ======================================================= */
64 Action::KeyframeDuplicate::KeyframeDuplicate()
66 new_keyframe.set_time(Time::begin()-1);
67 keyframe.set_time(Time::begin()-1);
72 Action::KeyframeDuplicate::get_param_vocab()
74 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
76 ret.push_back(ParamDesc("keyframe",Param::TYPE_KEYFRAME)
77 .set_local_name(_("Keyframe"))
78 .set_desc(_("Keyframe to be duplicated"))
81 ret.push_back(ParamDesc("time",Param::TYPE_TIME)
82 .set_local_name(_("Time"))
89 Action::KeyframeDuplicate::is_candidate(const ParamList &x)
91 return candidate_check(get_param_vocab(),x);
95 Action::KeyframeDuplicate::set_param(const synfig::String& name, const Action::Param ¶m)
97 if(name=="keyframe" && param.get_type()==Param::TYPE_KEYFRAME)
99 keyframe=param.get_keyframe();
100 new_keyframe.set_description(keyframe.get_description()+_(" (Duplicate)"));
104 if(name=="time" && param.get_type()==Param::TYPE_TIME)
106 new_keyframe.set_time(param.get_time());
111 return Action::CanvasSpecific::set_param(name,param);
115 Action::KeyframeDuplicate::is_ready()const
117 if(keyframe.get_time()==(Time::begin()-1) || new_keyframe.get_time()==(Time::begin()-1))
119 return Action::CanvasSpecific::is_ready();
123 Action::KeyframeDuplicate::prepare()
127 const synfig::Time old_time=keyframe.get_time();
128 const synfig::Time new_time=new_keyframe.get_time();
130 try { get_canvas()->keyframe_list().find(keyframe);}
131 catch(synfig::Exception::NotFound)
133 throw Error(_("Unable to find the given keyframe"));
136 try { if(get_canvas()->keyframe_list().find(new_time)!=get_canvas()->keyframe_list().end()) throw Error(_("A Keyframe already exists at this point in time"));}
139 // If the times are different, then we
140 // will need to romp through the valuenodes
141 // and add actions to update their values.
142 if(new_time!=old_time)
144 std::vector<synfigapp::ValueDesc> value_desc_list;
145 get_canvas_interface()->find_important_value_descs(value_desc_list);
146 while(!value_desc_list.empty())
148 process_value_desc(value_desc_list.back());
149 value_desc_list.pop_back();
155 Action::KeyframeDuplicate::process_value_desc(const synfigapp::ValueDesc& value_desc)
157 const synfig::Time old_time=keyframe.get_time();
158 const synfig::Time new_time=new_keyframe.get_time();
160 if(value_desc.is_value_node())
162 ValueNode::Handle value_node(value_desc.get_value_node());
164 // If we are a dynamic list, then we need to update the ActivePoints
165 if(ValueNode_DynamicList::Handle::cast_dynamic(value_node))
167 ValueNode_DynamicList::Handle value_node_dynamic(ValueNode_DynamicList::Handle::cast_dynamic(value_node));
170 for(i=0;i<value_node_dynamic->link_count();i++)
172 synfigapp::ValueDesc value_desc(value_node_dynamic,i);
173 Activepoint activepoint(value_node_dynamic->list[i].new_activepoint_at_time(old_time));
174 activepoint.set_time(new_time);
176 Action::Handle action(ActivepointSetSmart::create());
178 action->set_param("canvas",get_canvas());
179 action->set_param("canvas_interface",get_canvas_interface());
180 action->set_param("value_desc",value_desc);
181 action->set_param("activepoint",activepoint);
183 assert(action->is_ready());
184 if(!action->is_ready())
185 throw Error(Error::TYPE_NOTREADY);
187 add_action_front(action);
190 else if(ValueNode_Animated::Handle::cast_dynamic(value_node))
192 ValueNode_Animated::Handle value_node_animated(ValueNode_Animated::Handle::cast_dynamic(value_node));
193 Waypoint waypoint(value_node_animated->new_waypoint_at_time(old_time));
194 waypoint.set_time(new_time);
196 Action::Handle action(WaypointSetSmart::create());
198 action->set_param("canvas",get_canvas());
199 action->set_param("canvas_interface",get_canvas_interface());
200 action->set_param("value_node",ValueNode::Handle(value_node_animated));
201 action->set_param("waypoint",waypoint);
203 assert(action->is_ready());
204 if(!action->is_ready())
205 throw Error(Error::TYPE_NOTREADY);
207 add_action_front(action);
213 Action::KeyframeDuplicate::perform()
215 try { get_canvas()->keyframe_list().find(new_keyframe.get_time()); throw Error(_("A Keyframe already exists at this point in time"));}
216 catch(synfig::Exception::NotFound) { }
218 try { get_canvas()->keyframe_list().find(new_keyframe); throw Error(_("This keyframe is already in the ValueNode"));}
219 catch(synfig::Exception::NotFound) { }
221 Action::Super::perform();
223 get_canvas()->keyframe_list().add(new_keyframe);
225 if(get_canvas_interface())
227 get_canvas_interface()->signal_keyframe_added()(new_keyframe);
229 else synfig::warning("CanvasInterface not set on action");
233 Action::KeyframeDuplicate::undo()
235 Action::Super::undo();
237 if(get_canvas_interface())
239 get_canvas_interface()->signal_keyframe_removed()(new_keyframe);
241 else synfig::warning("CanvasInterface not set on action");
243 get_canvas()->keyframe_list().erase(new_keyframe);