1 /* === S Y N F I G ========================================================= */
2 /*! \file waypointsetsmart.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include "waypointsetsmart.h"
34 #include "valuenodelinkconnect.h"
35 #include "valuenodereplace.h"
37 #include "waypointset.h"
38 #include "waypointadd.h"
40 #include "valuedescconnect.h"
41 #include <synfigapp/canvasinterface.h>
42 #include <synfig/exception.h>
43 #include <synfigapp/main.h>
49 using namespace synfig;
50 using namespace synfigapp;
51 using namespace Action;
53 /* === M A C R O S ========================================================= */
55 ACTION_INIT(Action::WaypointSetSmart);
56 ACTION_SET_NAME(Action::WaypointSetSmart,"waypoint_set_smart");
57 ACTION_SET_LOCAL_NAME(Action::WaypointSetSmart,"Add Waypoint");
58 ACTION_SET_TASK(Action::WaypointSetSmart,"set");
59 ACTION_SET_CATEGORY(Action::WaypointSetSmart,Action::CATEGORY_WAYPOINT|Action::CATEGORY_VALUEDESC|Action::CATEGORY_VALUENODE);
60 ACTION_SET_PRIORITY(Action::WaypointSetSmart,0);
61 ACTION_SET_VERSION(Action::WaypointSetSmart,"0.0");
62 ACTION_SET_CVS_ID(Action::WaypointSetSmart,"$Id$");
70 /* === G L O B A L S ======================================================= */
72 /* === P R O C E D U R E S ================================================= */
74 /* === M E T H O D S ======================================================= */
76 Action::WaypointSetSmart::WaypointSetSmart()
78 waypoint.set_time(Time::begin()-1);
84 Action::WaypointSetSmart::get_param_vocab()
86 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
88 ret.push_back(ParamDesc("value_node",Param::TYPE_VALUENODE)
89 .set_local_name(_("Destination ValueNode (Animated)"))
92 ret.push_back(ParamDesc("waypoint",Param::TYPE_WAYPOINT)
93 .set_local_name(_("New Waypoint"))
94 .set_desc(_("Waypoint to be added"))
98 ret.push_back(ParamDesc("waypoint_model",Param::TYPE_WAYPOINTMODEL)
99 .set_local_name(_("Waypoint Model"))
103 ret.push_back(ParamDesc("time",Param::TYPE_TIME)
104 .set_local_name(_("Time"))
105 .set_desc(_("Time where waypoint is to be added"))
113 Action::WaypointSetSmart::is_candidate(const ParamList &x)
115 if(candidate_check(get_param_vocab(),x))
117 if(!ValueNode_Animated::Handle::cast_dynamic(x.find("value_node")->second.get_value_node()))
119 // We need either a waypoint or a time.
120 if(x.count("waypoint") || x.count("time"))
128 Action::WaypointSetSmart::set_param(const synfig::String& name, const Action::Param ¶m)
130 if(name=="value_node" && param.get_type()==Param::TYPE_VALUENODE)
132 value_node=ValueNode_Animated::Handle::cast_dynamic(param.get_value_node());
137 return static_cast<bool>(value_node);
139 if(name=="waypoint" && param.get_type()==Param::TYPE_WAYPOINT && !time_set)
141 waypoint=param.get_waypoint();
147 if(name=="time" && param.get_type()==Param::TYPE_TIME && waypoint.get_time()==(Time::begin()-1))
149 waypoint.set_time(param.get_time());
159 if(name=="model" && param.get_type()==Param::TYPE_WAYPOINTMODEL)
164 waypoint.apply_model(param.get_waypoint_model());
169 return Action::CanvasSpecific::set_param(name,param);
173 Action::WaypointSetSmart::is_ready()const
176 synfig::error("Missing value_node");
178 if(waypoint.get_time()==(Time::begin()-1))
179 synfig::error("Missing waypoint");
181 if(!value_node || waypoint.get_time()==(Time::begin()-1))
183 return Action::CanvasSpecific::is_ready();
186 // This function is called if a time is specified, but not
187 // a waypoint. In this case, we need to calculate the value
190 Action::WaypointSetSmart::calc_waypoint()
193 Time time=waypoint.get_time();
196 // Trivial case, we are sitting on a waypoint
197 waypoint=*value_node->find(waypoint.get_time());
201 waypoint=value_node->new_waypoint_at_time(time);
202 waypoint.set_before(synfigapp::Main::get_interpolation());
203 waypoint.set_after(synfigapp::Main::get_interpolation());
206 Time time=waypoint.get_time();
207 ValueNode_Animated::WaypointList &waypoint_list(value_node->waypoint_list());
208 ValueNode_Animated::WaypointList::iterator iter;
210 if(waypoint_list.empty())
212 waypoint.set_value((*value_node)(time));
216 ValueNode_Animated::WaypointList::iterator closest=waypoint_list.begin();
218 for(iter=waypoint_list.begin();iter!=waypoint_list.end();++iter)
220 const Real dist(abs(iter->get_time()-time));
221 if(dist<abs(closest->get_time()-time))
224 if(!closest->is_static())
225 waypoint.set_value_node(closest->get_value_node());
227 waypoint.set_value((*value_node)(time));
232 Action::WaypointSetSmart::enclose_waypoint(const synfig::Waypoint& waypoint)
234 times.insert(waypoint.get_time());
237 times.insert(value_node->find(waypoint)->get_time());
238 // synfig::info(__FILE__":%d: value_node->find(waypoint)->get_time()=%s",__LINE__,value_node->find(waypoint)->get_time().get_string().c_str());
243 // First we need to to add any waypoints necessary to
244 // maintain the integrity of the keyframes.
245 if(get_edit_mode()&MODE_ANIMATE_PAST) try
247 Time curr_time(waypoint.get_time());
249 //while(value_node->waypoint_list().front().get_time()<=curr_time)
251 // Try to find prev keyframe
252 Keyframe keyframe(*get_canvas()->keyframe_list().find_prev(curr_time));
253 curr_time=keyframe.get_time();
255 // synfig::info(__FILE__":%d: prev_keyframe->time=%s",__LINE__,keyframe.get_time().get_string().c_str());
256 // synfig::info(__FILE__":%d: waypoint->time=%s",__LINE__,waypoint.get_time().get_string().c_str());
259 if(times.count(keyframe.get_time()))
264 if(waypoint.get_time().is_equal(keyframe.get_time()))
270 times.insert(keyframe.get_time());
274 value_node->find(keyframe.get_time());
275 // synfig::info(__FILE__":%d: waypointtime=%s",__LINE__,value_node->find(keyframe.get_time())->get_time().get_string().c_str());
277 catch(synfig::Exception::NotFound)
279 Action::Handle action(WaypointAdd::create());
281 action->set_param("canvas",get_canvas());
282 action->set_param("canvas_interface",get_canvas_interface());
283 action->set_param("value_node",ValueNode::Handle(value_node));
285 if(!value_node->waypoint_list().empty())
287 action->set_param("time",keyframe.get_time());
291 synfig::Waypoint tmp;
293 tmp.set_value(waypoint.get_value());
294 tmp.set_time(keyframe.get_time());
295 action->set_param("waypoint",tmp);
298 assert(action->is_ready());
299 if(!action->is_ready())
300 throw Error(Error::TYPE_NOTREADY);
306 catch(Error x) { throw x; }
307 catch(synfig::Exception::NotFound) { DEBUGPOINT(); }
308 catch(int) { DEBUGPOINT(); }
309 catch(...) { DEBUGPOINT(); }
313 if(get_edit_mode()&MODE_ANIMATE_FUTURE)try
315 Time curr_time(waypoint.get_time());
317 //while(value_node->waypoint_list().back().get_time()>=curr_time)
321 // Try to find next keyframe
322 //synfig::info("FUTURE waypoint.get_time()=%s",waypoint.get_time().get_string().c_str());
323 Keyframe keyframe(*get_canvas()->keyframe_list().find_next(curr_time));
324 //synfig::info("FUTURE keyframe.get_time()=%s",keyframe.get_time().get_string().c_str());
325 curr_time=keyframe.get_time();
328 if(times.count(keyframe.get_time())|| waypoint.get_time().is_equal(keyframe.get_time()))
331 times.insert(keyframe.get_time());
336 value_node->find(keyframe.get_time());
337 synfig::info(__FILE__":%d: time=%s",__LINE__,keyframe.get_time().get_string().c_str());
338 synfig::info(__FILE__":%d: waypointtime=%s",__LINE__,value_node->find(keyframe.get_time())->get_time().get_string().c_str());
341 catch(synfig::Exception::NotFound)
343 Action::Handle action(WaypointAdd::create());
345 action->set_param("canvas",get_canvas());
346 action->set_param("canvas_interface",get_canvas_interface());
347 action->set_param("value_node",ValueNode::Handle(value_node));
349 if(!value_node->waypoint_list().empty())
351 action->set_param("time",keyframe.get_time());
355 synfig::Waypoint tmp;
357 tmp.set_value(waypoint.get_value());
358 tmp.set_time(keyframe.get_time());
359 action->set_param("waypoint",tmp);
362 assert(action->is_ready());
363 if(!action->is_ready())
364 throw Error(Error::TYPE_NOTREADY);
371 catch(Error x) { throw x; }
372 catch(synfig::Exception::NotFound) { DEBUGPOINT(); }
373 catch(int) { DEBUGPOINT(); }
374 catch(...) { DEBUGPOINT(); }
379 Action::WaypointSetSmart::prepare()
385 // First we need to to add any waypoints necessary to
386 // maintain the integrity of the keyframes.
387 enclose_waypoint(waypoint);
391 //synfig::info("WaypointSetSmart: Move/Update?");
392 // Lets try to replace the old waypoint, if it exists
393 WaypointList::iterator iter(value_node->find(waypoint));
395 if(iter == value_node->waypoint_list().end())
398 enclose_waypoint(*iter);
400 Action::Handle action(WaypointSet::create());
402 action->set_param("canvas",get_canvas());
403 action->set_param("canvas_interface",get_canvas_interface());
404 action->set_param("value_node",ValueNode::Handle(value_node));
405 action->set_param("waypoint",waypoint);
407 assert(action->is_ready());
408 if(!action->is_ready())
409 throw Error(Error::TYPE_NOTREADY);
415 catch(synfig::Exception::NotFound){ } catch(int){ }
419 //synfig::info("WaypointSetSmart: Replace?");
421 // Check to see if a waypoint exists at this point in time
422 WaypointList::iterator iter=value_node->find(waypoint.get_time());
424 waypoint.mimic(*iter);
426 enclose_waypoint(*iter);
428 Action::Handle action(WaypointSet::create());
430 action->set_param("canvas",get_canvas());
431 action->set_param("canvas_interface",get_canvas_interface());
432 action->set_param("value_node",ValueNode::Handle(value_node));
433 action->set_param("waypoint",waypoint);
435 assert(action->is_ready());
436 if(!action->is_ready())
437 throw Error(Error::TYPE_NOTREADY);
443 catch(synfig::Exception::NotFound){ } catch(int){ }
447 //synfig::info("WaypointSetSmart: Add?");
449 // At this point we know that the old waypoint doesn't exist,
450 // so we need to create it.
451 Action::Handle action(WaypointAdd::create());
453 action->set_param("canvas",get_canvas());
454 action->set_param("canvas_interface",get_canvas_interface());
455 action->set_param("value_node",ValueNode::Handle(value_node));
456 action->set_param("waypoint",waypoint);
458 assert(action->is_ready());
459 if(!action->is_ready())
460 throw Error(Error::TYPE_NOTREADY);
466 catch(synfig::Exception::NotFound){ } catch(int){ }
468 throw Error(_("Unable to determine how to proceed. This is a bug."));