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>
45 #include <synfigapp/general.h>
51 using namespace synfig;
52 using namespace synfigapp;
53 using namespace Action;
55 /* === M A C R O S ========================================================= */
57 ACTION_INIT(Action::WaypointSetSmart);
58 ACTION_SET_NAME(Action::WaypointSetSmart,"waypoint_set_smart");
59 ACTION_SET_LOCAL_NAME(Action::WaypointSetSmart,N_("Add Waypoint"));
60 ACTION_SET_TASK(Action::WaypointSetSmart,"set");
61 ACTION_SET_CATEGORY(Action::WaypointSetSmart,Action::CATEGORY_WAYPOINT|Action::CATEGORY_VALUEDESC|Action::CATEGORY_VALUENODE);
62 ACTION_SET_PRIORITY(Action::WaypointSetSmart,0);
63 ACTION_SET_VERSION(Action::WaypointSetSmart,"0.0");
64 ACTION_SET_CVS_ID(Action::WaypointSetSmart,"$Id$");
72 /* === G L O B A L S ======================================================= */
74 /* === P R O C E D U R E S ================================================= */
76 /* === M E T H O D S ======================================================= */
78 Action::WaypointSetSmart::WaypointSetSmart()
80 waypoint.set_time(Time::begin()-1);
86 Action::WaypointSetSmart::get_param_vocab()
88 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
90 ret.push_back(ParamDesc("value_node",Param::TYPE_VALUENODE)
91 .set_local_name(_("Destination ValueNode (Animated)"))
94 ret.push_back(ParamDesc("waypoint",Param::TYPE_WAYPOINT)
95 .set_local_name(_("New Waypoint"))
96 .set_desc(_("Waypoint to be added"))
100 ret.push_back(ParamDesc("waypoint_model",Param::TYPE_WAYPOINTMODEL)
101 .set_local_name(_("Waypoint Model"))
105 ret.push_back(ParamDesc("time",Param::TYPE_TIME)
106 .set_local_name(_("Time"))
107 .set_desc(_("Time where waypoint is to be added"))
115 Action::WaypointSetSmart::is_candidate(const ParamList &x)
117 if(candidate_check(get_param_vocab(),x))
119 if(!ValueNode_Animated::Handle::cast_dynamic(x.find("value_node")->second.get_value_node()))
121 // We need either a waypoint or a time.
122 if(x.count("waypoint") || x.count("time"))
130 Action::WaypointSetSmart::set_param(const synfig::String& name, const Action::Param ¶m)
132 if(name=="value_node" && param.get_type()==Param::TYPE_VALUENODE)
134 value_node=ValueNode_Animated::Handle::cast_dynamic(param.get_value_node());
139 return static_cast<bool>(value_node);
141 if(name=="waypoint" && param.get_type()==Param::TYPE_WAYPOINT && !time_set)
143 waypoint=param.get_waypoint();
149 if(name=="time" && param.get_type()==Param::TYPE_TIME && waypoint.get_time()==(Time::begin()-1))
151 waypoint.set_time(param.get_time());
161 if(name=="model" && param.get_type()==Param::TYPE_WAYPOINTMODEL)
166 waypoint.apply_model(param.get_waypoint_model());
171 return Action::CanvasSpecific::set_param(name,param);
175 Action::WaypointSetSmart::is_ready()const
178 synfig::error("Missing value_node");
180 if(waypoint.get_time()==(Time::begin()-1))
181 synfig::error("Missing waypoint");
183 if(!value_node || waypoint.get_time()==(Time::begin()-1))
185 return Action::CanvasSpecific::is_ready();
188 // This function is called if a time is specified, but not
189 // a waypoint. In this case, we need to calculate the value
192 Action::WaypointSetSmart::calc_waypoint()
195 Time time=waypoint.get_time();
198 // Trivial case, we are sitting on a waypoint
199 waypoint=*value_node->find(waypoint.get_time());
203 waypoint=value_node->new_waypoint_at_time(time);
204 waypoint.set_before(synfigapp::Main::get_interpolation());
205 waypoint.set_after(synfigapp::Main::get_interpolation());
208 Time time=waypoint.get_time();
209 ValueNode_Animated::WaypointList &waypoint_list(value_node->waypoint_list());
210 ValueNode_Animated::WaypointList::iterator iter;
212 if(waypoint_list.empty())
214 waypoint.set_value((*value_node)(time));
218 ValueNode_Animated::WaypointList::iterator closest=waypoint_list.begin();
220 for(iter=waypoint_list.begin();iter!=waypoint_list.end();++iter)
222 const Real dist(abs(iter->get_time()-time));
223 if(dist<abs(closest->get_time()-time))
226 if(!closest->is_static())
227 waypoint.set_value_node(closest->get_value_node());
229 waypoint.set_value((*value_node)(time));
234 Action::WaypointSetSmart::enclose_waypoint(const synfig::Waypoint& waypoint)
236 times.insert(waypoint.get_time());
239 times.insert(value_node->find(waypoint)->get_time());
240 // synfig::info(__FILE__":%d: value_node->find(waypoint)->get_time()=%s",__LINE__,value_node->find(waypoint)->get_time().get_string().c_str());
245 // First we need to to add any waypoints necessary to
246 // maintain the integrity of the keyframes.
247 if(get_edit_mode()&MODE_ANIMATE_PAST) try
249 Time curr_time(waypoint.get_time());
251 //while(value_node->waypoint_list().front().get_time()<=curr_time)
253 // Try to find prev keyframe
254 Keyframe keyframe(*get_canvas()->keyframe_list().find_prev(curr_time));
255 curr_time=keyframe.get_time();
257 // synfig::info(__FILE__":%d: prev_keyframe->time=%s",__LINE__,keyframe.get_time().get_string().c_str());
258 // synfig::info(__FILE__":%d: waypoint->time=%s",__LINE__,waypoint.get_time().get_string().c_str());
261 if(times.count(keyframe.get_time()))
266 if(waypoint.get_time().is_equal(keyframe.get_time()))
272 times.insert(keyframe.get_time());
276 value_node->find(keyframe.get_time());
277 // synfig::info(__FILE__":%d: waypointtime=%s",__LINE__,value_node->find(keyframe.get_time())->get_time().get_string().c_str());
279 catch(synfig::Exception::NotFound)
281 Action::Handle action(WaypointAdd::create());
283 action->set_param("canvas",get_canvas());
284 action->set_param("canvas_interface",get_canvas_interface());
285 action->set_param("value_node",ValueNode::Handle(value_node));
287 if(!value_node->waypoint_list().empty())
289 action->set_param("time",keyframe.get_time());
293 synfig::Waypoint tmp;
295 tmp.set_value(waypoint.get_value());
296 tmp.set_time(keyframe.get_time());
297 action->set_param("waypoint",tmp);
300 assert(action->is_ready());
301 if(!action->is_ready())
302 throw Error(Error::TYPE_NOTREADY);
308 catch(Error x) { throw x; }
309 catch(synfig::Exception::NotFound) { DEBUGPOINT(); }
310 catch(int) { DEBUGPOINT(); }
311 catch(...) { DEBUGPOINT(); }
315 if(get_edit_mode()&MODE_ANIMATE_FUTURE)try
317 Time curr_time(waypoint.get_time());
319 //while(value_node->waypoint_list().back().get_time()>=curr_time)
323 // Try to find next keyframe
324 //synfig::info("FUTURE waypoint.get_time()=%s",waypoint.get_time().get_string().c_str());
325 Keyframe keyframe(*get_canvas()->keyframe_list().find_next(curr_time));
326 //synfig::info("FUTURE keyframe.get_time()=%s",keyframe.get_time().get_string().c_str());
327 curr_time=keyframe.get_time();
330 if(times.count(keyframe.get_time())|| waypoint.get_time().is_equal(keyframe.get_time()))
333 times.insert(keyframe.get_time());
338 value_node->find(keyframe.get_time());
339 synfig::info(__FILE__":%d: time=%s",__LINE__,keyframe.get_time().get_string().c_str());
340 synfig::info(__FILE__":%d: waypointtime=%s",__LINE__,value_node->find(keyframe.get_time())->get_time().get_string().c_str());
343 catch(synfig::Exception::NotFound)
345 Action::Handle action(WaypointAdd::create());
347 action->set_param("canvas",get_canvas());
348 action->set_param("canvas_interface",get_canvas_interface());
349 action->set_param("value_node",ValueNode::Handle(value_node));
351 if(!value_node->waypoint_list().empty())
353 action->set_param("time",keyframe.get_time());
357 synfig::Waypoint tmp;
359 tmp.set_value(waypoint.get_value());
360 tmp.set_time(keyframe.get_time());
361 action->set_param("waypoint",tmp);
364 assert(action->is_ready());
365 if(!action->is_ready())
366 throw Error(Error::TYPE_NOTREADY);
373 catch(Error x) { throw x; }
374 catch(synfig::Exception::NotFound) { DEBUGPOINT(); }
375 catch(int) { DEBUGPOINT(); }
376 catch(...) { DEBUGPOINT(); }
381 Action::WaypointSetSmart::prepare()
387 // First we need to to add any waypoints necessary to
388 // maintain the integrity of the keyframes.
389 enclose_waypoint(waypoint);
393 //synfig::info("WaypointSetSmart: Move/Update?");
394 // Lets try to replace the old waypoint, if it exists
395 WaypointList::iterator iter(value_node->find(waypoint));
397 if(iter == value_node->waypoint_list().end())
400 enclose_waypoint(*iter);
402 Action::Handle action(WaypointSet::create());
404 action->set_param("canvas",get_canvas());
405 action->set_param("canvas_interface",get_canvas_interface());
406 action->set_param("value_node",ValueNode::Handle(value_node));
407 action->set_param("waypoint",waypoint);
409 assert(action->is_ready());
410 if(!action->is_ready())
411 throw Error(Error::TYPE_NOTREADY);
417 catch(synfig::Exception::NotFound){ } catch(int){ }
421 //synfig::info("WaypointSetSmart: Replace?");
423 // Check to see if a waypoint exists at this point in time
424 WaypointList::iterator iter=value_node->find(waypoint.get_time());
426 waypoint.mimic(*iter);
428 enclose_waypoint(*iter);
430 Action::Handle action(WaypointSet::create());
432 action->set_param("canvas",get_canvas());
433 action->set_param("canvas_interface",get_canvas_interface());
434 action->set_param("value_node",ValueNode::Handle(value_node));
435 action->set_param("waypoint",waypoint);
437 assert(action->is_ready());
438 if(!action->is_ready())
439 throw Error(Error::TYPE_NOTREADY);
445 catch(synfig::Exception::NotFound){ } catch(int){ }
449 //synfig::info("WaypointSetSmart: Add?");
451 // At this point we know that the old waypoint doesn't exist,
452 // so we need to create it.
453 Action::Handle action(WaypointAdd::create());
455 action->set_param("canvas",get_canvas());
456 action->set_param("canvas_interface",get_canvas_interface());
457 action->set_param("value_node",ValueNode::Handle(value_node));
458 action->set_param("waypoint",waypoint);
460 assert(action->is_ready());
461 if(!action->is_ready())
462 throw Error(Error::TYPE_NOTREADY);
468 catch(synfig::Exception::NotFound){ } catch(int){ }
470 throw Error(_("Unable to determine how to proceed. This is a bug."));