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"
39 #include <synfigapp/general.h>
45 using namespace synfig;
46 using namespace synfigapp;
47 using namespace Action;
49 /* === M A C R O S ========================================================= */
51 ACTION_INIT(Action::KeyframeDuplicate);
52 ACTION_SET_NAME(Action::KeyframeDuplicate,"keyframe_duplicate");
53 ACTION_SET_LOCAL_NAME(Action::KeyframeDuplicate,N_("Duplicate Keyframe"));
54 ACTION_SET_TASK(Action::KeyframeDuplicate,"duplicate");
55 ACTION_SET_CATEGORY(Action::KeyframeDuplicate,Action::CATEGORY_KEYFRAME);
56 ACTION_SET_PRIORITY(Action::KeyframeDuplicate,0);
57 ACTION_SET_VERSION(Action::KeyframeDuplicate,"0.0");
58 ACTION_SET_CVS_ID(Action::KeyframeDuplicate,"$Id$");
60 /* === G L O B A L S ======================================================= */
62 /* === P R O C E D U R E S ================================================= */
64 /* === M E T H O D S ======================================================= */
66 Action::KeyframeDuplicate::KeyframeDuplicate()
68 new_keyframe.set_time(Time::begin()-1);
69 keyframe.set_time(Time::begin()-1);
74 Action::KeyframeDuplicate::get_param_vocab()
76 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
78 ret.push_back(ParamDesc("keyframe",Param::TYPE_KEYFRAME)
79 .set_local_name(_("Keyframe"))
80 .set_desc(_("Keyframe to be duplicated"))
83 ret.push_back(ParamDesc("time",Param::TYPE_TIME)
84 .set_local_name(_("Time"))
91 Action::KeyframeDuplicate::is_candidate(const ParamList &x)
93 return candidate_check(get_param_vocab(),x);
97 Action::KeyframeDuplicate::set_param(const synfig::String& name, const Action::Param ¶m)
99 if(name=="keyframe" && param.get_type()==Param::TYPE_KEYFRAME)
101 keyframe=param.get_keyframe();
102 new_keyframe.set_description(keyframe.get_description()+_(" (Duplicate)"));
106 if(name=="time" && param.get_type()==Param::TYPE_TIME)
108 new_keyframe.set_time(param.get_time());
113 return Action::CanvasSpecific::set_param(name,param);
117 Action::KeyframeDuplicate::is_ready()const
119 if(keyframe.get_time()==(Time::begin()-1) || new_keyframe.get_time()==(Time::begin()-1))
121 return Action::CanvasSpecific::is_ready();
125 Action::KeyframeDuplicate::prepare()
129 const synfig::Time old_time=keyframe.get_time();
130 const synfig::Time new_time=new_keyframe.get_time();
132 try { get_canvas()->keyframe_list().find(keyframe);}
133 catch(synfig::Exception::NotFound)
135 throw Error(_("Unable to find the given keyframe"));
138 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"));}
141 // If the times are different, then we
142 // will need to romp through the valuenodes
143 // and add actions to update their values.
144 if(new_time!=old_time)
146 std::vector<synfigapp::ValueDesc> value_desc_list;
147 get_canvas_interface()->find_important_value_descs(value_desc_list);
148 while(!value_desc_list.empty())
150 process_value_desc(value_desc_list.back());
151 value_desc_list.pop_back();
157 Action::KeyframeDuplicate::process_value_desc(const synfigapp::ValueDesc& value_desc)
159 const synfig::Time old_time=keyframe.get_time();
160 const synfig::Time new_time=new_keyframe.get_time();
162 if(value_desc.is_value_node())
164 ValueNode::Handle value_node(value_desc.get_value_node());
166 // If we are a dynamic list, then we need to update the ActivePoints
167 if(ValueNode_DynamicList::Handle::cast_dynamic(value_node))
169 ValueNode_DynamicList::Handle value_node_dynamic(ValueNode_DynamicList::Handle::cast_dynamic(value_node));
172 for(i=0;i<value_node_dynamic->link_count();i++)
174 synfigapp::ValueDesc value_desc(value_node_dynamic,i);
175 Activepoint activepoint(value_node_dynamic->list[i].new_activepoint_at_time(old_time));
176 activepoint.set_time(new_time);
178 Action::Handle action(ActivepointSetSmart::create());
180 action->set_param("canvas",get_canvas());
181 action->set_param("canvas_interface",get_canvas_interface());
182 action->set_param("value_desc",value_desc);
183 action->set_param("activepoint",activepoint);
185 assert(action->is_ready());
186 if(!action->is_ready())
187 throw Error(Error::TYPE_NOTREADY);
189 add_action_front(action);
192 else if(ValueNode_Animated::Handle::cast_dynamic(value_node))
194 ValueNode_Animated::Handle value_node_animated(ValueNode_Animated::Handle::cast_dynamic(value_node));
195 Waypoint waypoint(value_node_animated->new_waypoint_at_time(old_time));
196 waypoint.set_time(new_time);
198 Action::Handle action(WaypointSetSmart::create());
200 action->set_param("canvas",get_canvas());
201 action->set_param("canvas_interface",get_canvas_interface());
202 action->set_param("value_node",ValueNode::Handle(value_node_animated));
203 action->set_param("waypoint",waypoint);
205 assert(action->is_ready());
206 if(!action->is_ready())
207 throw Error(Error::TYPE_NOTREADY);
209 add_action_front(action);
215 Action::KeyframeDuplicate::perform()
217 try { get_canvas()->keyframe_list().find(new_keyframe.get_time()); throw Error(_("A Keyframe already exists at this point in time"));}
218 catch(synfig::Exception::NotFound) { }
220 try { get_canvas()->keyframe_list().find(new_keyframe); throw Error(_("This keyframe is already in the ValueNode"));}
221 catch(synfig::Exception::NotFound) { }
223 Action::Super::perform();
225 get_canvas()->keyframe_list().add(new_keyframe);
227 if(get_canvas_interface())
229 get_canvas_interface()->signal_keyframe_added()(new_keyframe);
231 else synfig::warning("CanvasInterface not set on action");
235 Action::KeyframeDuplicate::undo()
237 Action::Super::undo();
239 if(get_canvas_interface())
241 get_canvas_interface()->signal_keyframe_removed()(new_keyframe);
243 else synfig::warning("CanvasInterface not set on action");
245 get_canvas()->keyframe_list().erase(new_keyframe);