1 /* === S Y N F I G ========================================================= */
2 /*! \file keyframeset.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 "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"
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::KeyframeSet);
52 ACTION_SET_NAME(Action::KeyframeSet,"keyframe_set");
53 ACTION_SET_LOCAL_NAME(Action::KeyframeSet,N_("Set Keyframe"));
54 ACTION_SET_TASK(Action::KeyframeSet,"set");
55 ACTION_SET_CATEGORY(Action::KeyframeSet,Action::CATEGORY_KEYFRAME|Action::CATEGORY_HIDDEN);
56 ACTION_SET_PRIORITY(Action::KeyframeSet,0);
57 ACTION_SET_VERSION(Action::KeyframeSet,"0.0");
58 ACTION_SET_CVS_ID(Action::KeyframeSet,"$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::KeyframeSet::KeyframeSet()
68 keyframe.set_time(Time::begin()-1);
73 Action::KeyframeSet::get_param_vocab()
75 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
77 ret.push_back(ParamDesc("keyframe",Param::TYPE_KEYFRAME)
78 .set_local_name(_("New Keyframe"))
79 .set_desc(_("Keyframe to be added"))
86 Action::KeyframeSet::is_candidate(const ParamList &x)
88 return candidate_check(get_param_vocab(),x);
92 Action::KeyframeSet::set_param(const synfig::String& name, const Action::Param ¶m)
94 if(name=="keyframe" && param.get_type()==Param::TYPE_KEYFRAME)
96 synfig::info("KeyframeSet::set_param():old_time: %s",keyframe.get_time().get_string().c_str());
97 keyframe=param.get_keyframe();
98 synfig::info("KeyframeSet::set_param():new_time: %s",keyframe.get_time().get_string().c_str());
99 synfig::info("KeyframeSet::set_param():get_keyframe(): %s",param.get_keyframe().get_time().get_string().c_str());
104 return Action::CanvasSpecific::set_param(name,param);
108 Action::KeyframeSet::is_ready()const
110 if(keyframe.get_time()==(Time::begin()-1))
112 return Action::CanvasSpecific::is_ready();
116 Action::KeyframeSet::prepare()
125 //synfig::info("new_time: %s",new_time.get_string().c_str());
126 //synfig::info("old_time: %s",old_time.get_string().c_str());
128 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"));}
132 // If the times are different, then we
133 // will need to romp through the valuenodes
134 // and add actions to update their values.
135 if(new_time!=old_time)
137 std::vector<synfigapp::ValueDesc> value_desc_list;
138 get_canvas_interface()->find_important_value_descs(value_desc_list);
139 while(!value_desc_list.empty())
141 process_value_desc(value_desc_list.back());
142 value_desc_list.pop_back();
147 #define old_2_new(x) (((x)-old_begin)/(old_end-old_begin)*(new_end-new_begin)+new_begin)
150 Action::KeyframeSet::scale_activepoints(const synfigapp::ValueDesc& value_desc,const synfig::Time& old_begin,const synfig::Time& old_end,const synfig::Time& new_begin,const synfig::Time& new_end)
152 ValueNode_DynamicList::Handle value_node(ValueNode_DynamicList::Handle::cast_static(value_desc.get_parent_value_node()));
153 ValueNode_DynamicList::ListEntry& list_entry(value_node->list[value_desc.get_index()]);
155 std::vector<Activepoint*> selected;
156 std::vector<Activepoint*>::iterator iter;
158 if(list_entry.find(old_begin,old_end,selected))
160 // check to make sure this operation is OK
161 for(iter=selected.begin();iter!=selected.end();++iter)
165 Time new_time(old_2_new((*iter)->get_time()));
166 if(new_time>=old_begin && new_time<old_end)
168 list_entry.find(new_time);
169 // If we found a activepoint already at that time, then
171 //throw Exception::BadTime(_("Activepoint Conflict"));
173 catch(Exception::NotFound) { }
177 while(!selected.empty())
179 if(selected.back()->get_time()!=old_2_new(selected.back()->get_time()))
181 Action::Handle action(Action::create("activepoint_set"));
183 action->set_param("canvas",get_canvas());
184 action->set_param("canvas_interface",get_canvas_interface());
185 action->set_param("value_desc",value_desc);
187 Activepoint activepoint(*selected.back());
188 activepoint.set_time(old_2_new(selected.back()->get_time()));
190 action->set_param("activepoint",activepoint);
192 assert(action->is_ready());
193 if(!action->is_ready())
194 throw Error(Error::TYPE_NOTREADY);
196 add_action_front(action);
208 Action::KeyframeSet::scale_waypoints(const synfigapp::ValueDesc& value_desc,const synfig::Time& old_begin,const synfig::Time& old_end,const synfig::Time& new_begin,const synfig::Time& new_end)
210 ValueNode_Animated::Handle value_node(ValueNode_Animated::Handle::cast_static(value_desc.get_value_node()));
212 std::vector<Waypoint*> selected;
213 std::vector<Waypoint*>::iterator iter;
215 if(value_node->find(old_begin,old_end,selected))
217 // check to make sure this operation is OK
218 for(iter=selected.begin();iter!=selected.end();++iter)
222 Time new_time(old_2_new((*iter)->get_time()));
223 if(new_time>=old_begin && new_time<old_end)
225 value_node->find(new_time);
226 // If we found a waypoint point already at that time, then
228 //synfig::info(_("old_begin: %s, old_end: %s"),old_begin.get_string().c_str(),old_end.get_string().c_str());
229 //synfig::info(_("new_begin: %s, new_end: %s"),new_begin.get_string().c_str(),new_end.get_string().c_str());
230 //throw Exception::BadTime(strprintf(_("Waypoint Conflict, old: %s, new: %s"),(*iter)->get_time().get_string().c_str(),new_time.get_string().c_str()));
232 catch(Exception::NotFound) { }
236 while(!selected.empty())
238 if(selected.back()->get_time()!=old_2_new(selected.back()->get_time()))
240 Action::Handle action(Action::create("waypoint_set"));
242 action->set_param("canvas",get_canvas());
243 action->set_param("canvas_interface",get_canvas_interface());
244 action->set_param("value_node",ValueNode::Handle::cast_static(value_node));
246 Waypoint waypoint(*selected.back());
247 waypoint.set_time(old_2_new(selected.back()->get_time()));
249 action->set_param("waypoint",waypoint);
251 assert(action->is_ready());
252 if(!action->is_ready())
253 throw Error(Error::TYPE_NOTREADY);
255 add_action_front(action);
267 Action::KeyframeSet::process_value_desc(const synfigapp::ValueDesc& value_desc)
269 if(value_desc.is_value_node())
271 ValueNode::Handle value_node(value_desc.get_value_node());
273 //if(guid_set.count(value_node->get_guid()))
275 //guid_set.insert(value_node->get_guid());
277 // If we are a dynamic list, then we need to update the ActivePoints
278 if(ValueNode_DynamicList::Handle::cast_dynamic(value_node))
280 ValueNode_DynamicList::Handle value_node_dynamic(ValueNode_DynamicList::Handle::cast_dynamic(value_node));
282 for(i=0;i<value_node_dynamic->link_count();i++)
284 synfigapp::ValueDesc value_desc(value_node_dynamic,i);
285 if(new_time>keyframe_prev && new_time<keyframe_next)
287 // In this circumstance, we need to adjust any
288 // activepoints between the previous and next
290 scale_activepoints(value_desc,keyframe_prev,old_time,keyframe_prev,new_time);
291 scale_activepoints(value_desc,old_time,keyframe_next,new_time,keyframe_next);
295 Action::Handle action(ActivepointSetSmart::create());
297 action->set_param("canvas",get_canvas());
298 action->set_param("canvas_interface",get_canvas_interface());
299 action->set_param("value_desc",value_desc);
301 Activepoint activepoint;
304 activepoint=*value_node_dynamic->list[i].find(old_time);
305 activepoint.set_time(new_time);
309 activepoint.set_time(new_time);
310 activepoint.set_state(value_node_dynamic->list[i].status_at_time(old_time));
311 activepoint.set_priority(0);
313 action->set_param("activepoint",activepoint);
315 assert(action->is_ready());
316 if(!action->is_ready())
317 throw Error(Error::TYPE_NOTREADY);
319 add_action_front(action);
323 else if(ValueNode_Animated::Handle::cast_dynamic(value_node))
325 if(new_time>keyframe_prev && new_time<keyframe_next)
327 // In this circumstance, we need to adjust any
328 // waypoints between the previous and next
330 scale_waypoints(value_desc,keyframe_prev,old_time,keyframe_prev,new_time);
331 scale_waypoints(value_desc,old_time,keyframe_next,new_time,keyframe_next);
335 ValueNode_Animated::Handle value_node_animated(ValueNode_Animated::Handle::cast_dynamic(value_node));
337 Action::Handle action(WaypointSetSmart::create());
339 action->set_param("canvas",get_canvas());
340 action->set_param("canvas_interface",get_canvas_interface());
341 action->set_param("value_node",ValueNode::Handle(value_node_animated));
346 waypoint=*value_node_animated->find(old_time);
347 waypoint.set_time(new_time);
351 waypoint.set_time(new_time);
352 waypoint.set_value((*value_node_animated)(old_time));
354 action->set_param("waypoint",waypoint);
356 assert(action->is_ready());
357 if(!action->is_ready())
358 throw Error(Error::TYPE_NOTREADY);
360 add_action_front(action);
368 Action::KeyframeSet::perform()
371 old_time=get_canvas()->keyframe_list().find(keyframe)->get_time();
372 new_time=keyframe.get_time();
374 try { get_canvas()->keyframe_list().find(keyframe);}
375 catch(synfig::Exception::NotFound)
377 throw Error(_("Unable to find the given keyframe"));
380 // Check for collisions
381 if(old_time!=new_time)
384 get_canvas()->keyframe_list().find(new_time);
385 throw Error(_("Cannot change keyframe time because another keyframe already exists with that time."));
387 catch(Exception::NotFound) { }
389 try { keyframe_next=get_canvas()->keyframe_list().find_next(old_time)->get_time(); }
390 catch(...) { keyframe_next=Time::end(); }
391 try { keyframe_prev=get_canvas()->keyframe_list().find_prev(old_time)->get_time(); }
392 catch(...) { keyframe_prev=Time::begin(); }
394 old_keyframe=*get_canvas()->keyframe_list().find(keyframe);
395 *get_canvas()->keyframe_list().find(keyframe)=keyframe;
397 get_canvas()->keyframe_list().sync();
400 Action::Super::perform();
403 *get_canvas()->keyframe_list().find(old_keyframe)=old_keyframe;
405 get_canvas()->keyframe_list().sync();
409 // Signal that a layer has been inserted
410 if(get_canvas_interface())
412 get_canvas_interface()->signal_keyframe_changed()(keyframe);
414 else synfig::warning("CanvasInterface not set on action");
418 Action::KeyframeSet::undo()
420 Action::Super::undo();
422 *get_canvas()->keyframe_list().find(old_keyframe)=old_keyframe;
424 get_canvas()->keyframe_list().sync();
426 // Signal that a layer has been inserted
427 if(get_canvas_interface())
429 get_canvas_interface()->signal_keyframe_changed()(keyframe);
431 else synfig::warning("CanvasInterface not set on action");