1 /* === S Y N F I G ========================================================= */
2 /*! \file keyframeset.cpp
3 ** \brief Template File
5 ** $Id: keyframeset.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
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 "keyframeset.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::KeyframeSet);
50 ACTION_SET_NAME(Action::KeyframeSet,"keyframe_set");
51 ACTION_SET_LOCAL_NAME(Action::KeyframeSet,"Set Keyframe");
52 ACTION_SET_TASK(Action::KeyframeSet,"set");
53 ACTION_SET_CATEGORY(Action::KeyframeSet,Action::CATEGORY_KEYFRAME|Action::CATEGORY_HIDDEN);
54 ACTION_SET_PRIORITY(Action::KeyframeSet,0);
55 ACTION_SET_VERSION(Action::KeyframeSet,"0.0");
56 ACTION_SET_CVS_ID(Action::KeyframeSet,"$Id: keyframeset.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $");
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::KeyframeSet::KeyframeSet()
66 keyframe.set_time(Time::begin()-1);
71 Action::KeyframeSet::get_param_vocab()
73 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
75 ret.push_back(ParamDesc("keyframe",Param::TYPE_KEYFRAME)
76 .set_local_name(_("New Keyframe"))
77 .set_desc(_("Keyframe to be added"))
84 Action::KeyframeSet::is_canidate(const ParamList &x)
86 return canidate_check(get_param_vocab(),x);
90 Action::KeyframeSet::set_param(const synfig::String& name, const Action::Param ¶m)
92 if(name=="keyframe" && param.get_type()==Param::TYPE_KEYFRAME)
94 synfig::info("KeyframeSet::set_param():old_time: %s",keyframe.get_time().get_string().c_str());
95 keyframe=param.get_keyframe();
96 synfig::info("KeyframeSet::set_param():new_time: %s",keyframe.get_time().get_string().c_str());
97 synfig::info("KeyframeSet::set_param():get_keyframe(): %s",param.get_keyframe().get_time().get_string().c_str());
102 return Action::CanvasSpecific::set_param(name,param);
106 Action::KeyframeSet::is_ready()const
108 if(keyframe.get_time()==(Time::begin()-1))
110 return Action::CanvasSpecific::is_ready();
114 Action::KeyframeSet::prepare()
123 //synfig::info("new_time: %s",new_time.get_string().c_str());
124 //synfig::info("old_time: %s",old_time.get_string().c_str());
126 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"));}
130 // If the times are different, then we
131 // will need to romp through the valuenodes
132 // and add actions to update their values.
133 if(new_time!=old_time)
135 std::vector<synfigapp::ValueDesc> value_desc_list;
136 get_canvas_interface()->find_important_value_descs(value_desc_list);
137 while(!value_desc_list.empty())
139 process_value_desc(value_desc_list.back());
140 value_desc_list.pop_back();
145 #define old_2_new(x) (((x)-old_begin)/(old_end-old_begin)*(new_end-new_begin)+new_begin)
148 Action::KeyframeSet::scale_activepoints(const synfigapp::ValueDesc& value_desc,const Time& old_begin,const Time& old_end,const Time& new_begin,const Time& new_end)
150 ValueNode_DynamicList::Handle value_node(ValueNode_DynamicList::Handle::cast_static(value_desc.get_parent_value_node()));
151 ValueNode_DynamicList::ListEntry& list_entry(value_node->list[value_desc.get_index()]);
153 std::vector<Activepoint*> selected;
154 std::vector<Activepoint*>::iterator iter;
156 if(list_entry.find(old_begin,old_end,selected))
158 // check to make sure this operation is OK
159 for(iter=selected.begin();iter!=selected.end();++iter)
163 Time new_time(old_2_new((*iter)->get_time()));
164 if(new_time>=old_begin && new_time<old_end)
166 list_entry.find(new_time);
167 // If we found a activepoint already at that time, then
169 //throw Exception::BadTime(_("Activepoint Conflict"));
171 catch(Exception::NotFound) { }
175 while(!selected.empty())
177 if(selected.back()->get_time()!=old_2_new(selected.back()->get_time()))
179 Action::Handle action(Action::create("activepoint_set"));
181 action->set_param("canvas",get_canvas());
182 action->set_param("canvas_interface",get_canvas_interface());
183 action->set_param("value_desc",value_desc);
185 Activepoint activepoint(*selected.back());
186 activepoint.set_time(old_2_new(selected.back()->get_time()));
188 action->set_param("activepoint",activepoint);
190 assert(action->is_ready());
191 if(!action->is_ready())
192 throw Error(Error::TYPE_NOTREADY);
194 add_action_front(action);
206 Action::KeyframeSet::scale_waypoints(const synfigapp::ValueDesc& value_desc,const Time& old_begin,const Time& old_end,const Time& new_begin,const Time& new_end)
208 ValueNode_Animated::Handle value_node(ValueNode_Animated::Handle::cast_static(value_desc.get_value_node()));
210 std::vector<Waypoint*> selected;
211 std::vector<Waypoint*>::iterator iter;
213 if(value_node->find(old_begin,old_end,selected))
215 // check to make sure this operation is OK
216 for(iter=selected.begin();iter!=selected.end();++iter)
220 Time new_time(old_2_new((*iter)->get_time()));
221 if(new_time>=old_begin && new_time<old_end)
223 value_node->find(new_time);
224 // If we found a waypoint point already at that time, then
226 //synfig::info(_("old_begin: %s, old_end: %s"),old_begin.get_string().c_str(),old_end.get_string().c_str());
227 //synfig::info(_("new_begin: %s, new_end: %s"),new_begin.get_string().c_str(),new_end.get_string().c_str());
228 //throw Exception::BadTime(strprintf(_("Waypoint Conflict, old: %s, new: %s"),(*iter)->get_time().get_string().c_str(),new_time.get_string().c_str()));
230 catch(Exception::NotFound) { }
234 while(!selected.empty())
236 if(selected.back()->get_time()!=old_2_new(selected.back()->get_time()))
238 Action::Handle action(Action::create("waypoint_set"));
240 action->set_param("canvas",get_canvas());
241 action->set_param("canvas_interface",get_canvas_interface());
242 action->set_param("value_node",ValueNode::Handle::cast_static(value_node));
244 Waypoint waypoint(*selected.back());
245 waypoint.set_time(old_2_new(selected.back()->get_time()));
247 action->set_param("waypoint",waypoint);
249 assert(action->is_ready());
250 if(!action->is_ready())
251 throw Error(Error::TYPE_NOTREADY);
253 add_action_front(action);
265 Action::KeyframeSet::process_value_desc(const synfigapp::ValueDesc& value_desc)
267 if(value_desc.is_value_node())
269 ValueNode::Handle value_node(value_desc.get_value_node());
271 //if(guid_set.count(value_node->get_guid()))
273 //guid_set.insert(value_node->get_guid());
275 // If we are a dynamic list, then we need to update the ActivePoints
276 if(ValueNode_DynamicList::Handle::cast_dynamic(value_node))
278 ValueNode_DynamicList::Handle value_node(ValueNode_DynamicList::Handle::cast_dynamic(value_node));
280 for(i=0;i<value_node->link_count();i++)
282 synfigapp::ValueDesc value_desc(value_node,i);
283 if(new_time>keyframe_prev && new_time<keyframe_next)
285 // In this circumstance, we need to adjust any
286 // activepoints between the previous and next
288 scale_activepoints(value_desc,keyframe_prev,old_time,keyframe_prev,new_time);
289 scale_activepoints(value_desc,old_time,keyframe_next,new_time,keyframe_next);
293 Action::Handle action(ActivepointSetSmart::create());
295 action->set_param("canvas",get_canvas());
296 action->set_param("canvas_interface",get_canvas_interface());
297 action->set_param("value_desc",value_desc);
299 Activepoint activepoint;
302 activepoint=*value_node->list[i].find(old_time);
303 activepoint.set_time(new_time);
307 activepoint.set_time(new_time);
308 activepoint.set_state(value_node->list[i].status_at_time(old_time));
309 activepoint.set_priority(0);
311 action->set_param("activepoint",activepoint);
313 assert(action->is_ready());
314 if(!action->is_ready())
315 throw Error(Error::TYPE_NOTREADY);
317 add_action_front(action);
321 else if(ValueNode_Animated::Handle::cast_dynamic(value_node))
323 if(new_time>keyframe_prev && new_time<keyframe_next)
325 // In this circumstance, we need to adjust any
326 // waypoints between the previous and next
328 scale_waypoints(value_desc,keyframe_prev,old_time,keyframe_prev,new_time);
329 scale_waypoints(value_desc,old_time,keyframe_next,new_time,keyframe_next);
333 ValueNode_Animated::Handle value_node(ValueNode_Animated::Handle::cast_dynamic(value_node));
335 Action::Handle action(WaypointSetSmart::create());
337 action->set_param("canvas",get_canvas());
338 action->set_param("canvas_interface",get_canvas_interface());
339 action->set_param("value_node",ValueNode::Handle(value_node));
344 waypoint=*value_node->find(old_time);
345 waypoint.set_time(new_time);
349 waypoint.set_time(new_time);
350 waypoint.set_value((*value_node)(old_time));
352 action->set_param("waypoint",waypoint);
354 assert(action->is_ready());
355 if(!action->is_ready())
356 throw Error(Error::TYPE_NOTREADY);
358 add_action_front(action);
366 Action::KeyframeSet::perform()
369 old_time=get_canvas()->keyframe_list().find(keyframe)->get_time();
370 new_time=keyframe.get_time();
372 try { get_canvas()->keyframe_list().find(keyframe);}
373 catch(synfig::Exception::NotFound)
375 throw Error(_("Unable to find the given keyframe"));
378 // Check for colisions
379 if(old_time!=new_time)
382 get_canvas()->keyframe_list().find(new_time);
383 throw Error(_("Cannot change keyframe time because another keyframe already exists with that time."));
385 catch(Exception::NotFound) { }
387 try { keyframe_next=get_canvas()->keyframe_list().find_next(old_time)->get_time(); }
388 catch(...) { keyframe_next=Time::end(); }
389 try { keyframe_prev=get_canvas()->keyframe_list().find_prev(old_time)->get_time(); }
390 catch(...) { keyframe_prev=Time::begin(); }
392 old_keyframe=*get_canvas()->keyframe_list().find(keyframe);
393 *get_canvas()->keyframe_list().find(keyframe)=keyframe;
395 get_canvas()->keyframe_list().sync();
398 Action::Super::perform();
401 *get_canvas()->keyframe_list().find(old_keyframe)=old_keyframe;
403 get_canvas()->keyframe_list().sync();
407 // Signal that a layer has been inserted
408 if(get_canvas_interface())
410 get_canvas_interface()->signal_keyframe_changed()(keyframe);
412 else synfig::warning("CanvasInterface not set on action");
416 Action::KeyframeSet::undo()
418 Action::Super::undo();
420 *get_canvas()->keyframe_list().find(old_keyframe)=old_keyframe;
422 get_canvas()->keyframe_list().sync();
424 // Signal that a layer has been inserted
425 if(get_canvas_interface())
427 get_canvas_interface()->signal_keyframe_changed()(keyframe);
429 else synfig::warning("CanvasInterface not set on action");