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$");
66 /* === G L O B A L S ======================================================= */
68 /* === P R O C E D U R E S ================================================= */
70 /* === M E T H O D S ======================================================= */
72 Action::WaypointSetSmart::WaypointSetSmart()
74 waypoint.set_time(Time::begin()-1);
80 Action::WaypointSetSmart::get_param_vocab()
82 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
84 ret.push_back(ParamDesc("value_node",Param::TYPE_VALUENODE)
85 .set_local_name(_("Destination ValueNode (Animated)"))
88 ret.push_back(ParamDesc("waypoint",Param::TYPE_WAYPOINT)
89 .set_local_name(_("New Waypoint"))
90 .set_desc(_("Waypoint to be added"))
94 ret.push_back(ParamDesc("waypoint_model",Param::TYPE_WAYPOINTMODEL)
95 .set_local_name(_("Waypoint Model"))
99 ret.push_back(ParamDesc("time",Param::TYPE_TIME)
100 .set_local_name(_("Time"))
101 .set_desc(_("Time where waypoint is to be added"))
109 Action::WaypointSetSmart::is_candidate(const ParamList &x)
111 if(candidate_check(get_param_vocab(),x))
113 if(!ValueNode_Animated::Handle::cast_dynamic(x.find("value_node")->second.get_value_node()))
115 // We need either a waypoint or a time.
116 if(x.count("waypoint") || x.count("time"))
124 Action::WaypointSetSmart::set_param(const synfig::String& name, const Action::Param ¶m)
126 if(name=="value_node" && param.get_type()==Param::TYPE_VALUENODE)
128 value_node=ValueNode_Animated::Handle::cast_dynamic(param.get_value_node());
132 return static_cast<bool>(value_node);
134 if(name=="waypoint" && param.get_type()==Param::TYPE_WAYPOINT && !time_set)
136 waypoint=param.get_waypoint();
141 if(name=="time" && param.get_type()==Param::TYPE_TIME && waypoint.get_time()==(Time::begin()-1))
143 waypoint.set_time(param.get_time());
152 if(name=="model" && param.get_type()==Param::TYPE_WAYPOINTMODEL)
157 waypoint.apply_model(param.get_waypoint_model());
162 return Action::CanvasSpecific::set_param(name,param);
166 Action::WaypointSetSmart::is_ready()const
169 synfig::error("Missing value_node");
171 if(waypoint.get_time()==(Time::begin()-1))
172 synfig::error("Missing waypoint");
174 if(!value_node || waypoint.get_time()==(Time::begin()-1))
176 return Action::CanvasSpecific::is_ready();
179 // This function is called if a time is specified, but not
180 // a waypoint. In this case, we need to calculate the value
183 Action::WaypointSetSmart::calc_waypoint()
185 Time time=waypoint.get_time();
188 // Trivial case, we are sitting on a waypoint
189 waypoint=*value_node->find(waypoint.get_time());
193 waypoint=value_node->new_waypoint_at_time(time);
194 waypoint.set_before(synfigapp::Main::get_interpolation());
195 waypoint.set_after(synfigapp::Main::get_interpolation());
198 Time time=waypoint.get_time();
199 ValueNode_Animated::WaypointList &waypoint_list(value_node->waypoint_list());
200 ValueNode_Animated::WaypointList::iterator iter;
202 if(waypoint_list.empty())
204 waypoint.set_value((*value_node)(time));
208 ValueNode_Animated::WaypointList::iterator closest=waypoint_list.begin();
210 for(iter=waypoint_list.begin();iter!=waypoint_list.end();++iter)
212 const Real dist(abs(iter->get_time()-time));
213 if(dist<abs(closest->get_time()-time))
216 if(!closest->is_static())
217 waypoint.set_value_node(closest->get_value_node());
219 waypoint.set_value((*value_node)(time));
224 Action::WaypointSetSmart::enclose_waypoint(const synfig::Waypoint& waypoint)
226 times.insert(waypoint.get_time());
229 times.insert(value_node->find(waypoint)->get_time());
230 // synfig::info(__FILE__":%d: value_node->find(waypoint)->get_time()=%s",__LINE__,value_node->find(waypoint)->get_time().get_string().c_str());
233 // First we need to to add any waypoints necessary to
234 // maintain the integrity of the keyframes.
235 if(get_edit_mode()&MODE_ANIMATE_PAST) try
237 Time curr_time(waypoint.get_time());
239 //while(value_node->waypoint_list().front().get_time()<=curr_time)
241 // Try to find prev keyframe
242 Keyframe keyframe(*get_canvas()->keyframe_list().find_prev(curr_time));
243 curr_time=keyframe.get_time();
245 // synfig::info(__FILE__":%d: prev_keyframe->time=%s",__LINE__,keyframe.get_time().get_string().c_str());
246 // synfig::info(__FILE__":%d: waypoint->time=%s",__LINE__,waypoint.get_time().get_string().c_str());
248 if(times.count(keyframe.get_time()))
252 if(waypoint.get_time().is_equal(keyframe.get_time()))
257 times.insert(keyframe.get_time());
260 value_node->find(keyframe.get_time());
261 // synfig::info(__FILE__":%d: waypointtime=%s",__LINE__,value_node->find(keyframe.get_time())->get_time().get_string().c_str());
263 catch(synfig::Exception::NotFound)
265 Action::Handle action(WaypointAdd::create());
267 action->set_param("canvas",get_canvas());
268 action->set_param("canvas_interface",get_canvas_interface());
269 action->set_param("value_node",ValueNode::Handle(value_node));
271 if(!value_node->waypoint_list().empty())
273 action->set_param("time",keyframe.get_time());
277 synfig::Waypoint tmp;
279 tmp.set_value(waypoint.get_value());
280 tmp.set_time(keyframe.get_time());
281 action->set_param("waypoint",tmp);
284 assert(action->is_ready());
285 if(!action->is_ready())
286 throw Error(Error::TYPE_NOTREADY);
292 catch(Error x) { throw x; }
293 catch(synfig::Exception::NotFound) { }
297 if(get_edit_mode()&MODE_ANIMATE_FUTURE)try
299 Time curr_time(waypoint.get_time());
301 //while(value_node->waypoint_list().back().get_time()>=curr_time)
304 // Try to find next keyframe
305 //synfig::info("FUTURE waypoint.get_time()=%s",waypoint.get_time().get_string().c_str());
306 Keyframe keyframe(*get_canvas()->keyframe_list().find_next(curr_time));
307 //synfig::info("FUTURE keyframe.get_time()=%s",keyframe.get_time().get_string().c_str());
308 curr_time=keyframe.get_time();
310 if(times.count(keyframe.get_time())|| waypoint.get_time().is_equal(keyframe.get_time()))
313 times.insert(keyframe.get_time());
317 value_node->find(keyframe.get_time());
318 synfig::info(__FILE__":%d: time=%s",__LINE__,keyframe.get_time().get_string().c_str());
319 synfig::info(__FILE__":%d: waypointtime=%s",__LINE__,value_node->find(keyframe.get_time())->get_time().get_string().c_str());
322 catch(synfig::Exception::NotFound)
324 Action::Handle action(WaypointAdd::create());
326 action->set_param("canvas",get_canvas());
327 action->set_param("canvas_interface",get_canvas_interface());
328 action->set_param("value_node",ValueNode::Handle(value_node));
330 if(!value_node->waypoint_list().empty())
332 action->set_param("time",keyframe.get_time());
336 synfig::Waypoint tmp;
338 tmp.set_value(waypoint.get_value());
339 tmp.set_time(keyframe.get_time());
340 action->set_param("waypoint",tmp);
343 assert(action->is_ready());
344 if(!action->is_ready())
345 throw Error(Error::TYPE_NOTREADY);
351 catch(Error x) { throw x; }
352 catch(synfig::Exception::NotFound) { }
358 Action::WaypointSetSmart::prepare()
363 // First we need to to add any waypoints necessary to
364 // maintain the integrity of the keyframes.
365 enclose_waypoint(waypoint);
369 //synfig::info("WaypointSetSmart: Move/Update?");
370 // Lets try to replace the old waypoint, if it exists
371 WaypointList::iterator iter(value_node->find(waypoint));
373 if(iter == value_node->waypoint_list().end())
376 enclose_waypoint(*iter);
378 Action::Handle action(WaypointSet::create());
380 action->set_param("canvas",get_canvas());
381 action->set_param("canvas_interface",get_canvas_interface());
382 action->set_param("value_node",ValueNode::Handle(value_node));
383 action->set_param("waypoint",waypoint);
385 assert(action->is_ready());
386 if(!action->is_ready())
387 throw Error(Error::TYPE_NOTREADY);
393 catch(synfig::Exception::NotFound){ } catch(int){ }
397 //synfig::info("WaypointSetSmart: Replace?");
398 // Check to see if a waypoint exists at this point in time
399 WaypointList::iterator iter=value_node->find(waypoint.get_time());
401 waypoint.mimic(*iter);
403 enclose_waypoint(*iter);
405 Action::Handle action(WaypointSet::create());
407 action->set_param("canvas",get_canvas());
408 action->set_param("canvas_interface",get_canvas_interface());
409 action->set_param("value_node",ValueNode::Handle(value_node));
410 action->set_param("waypoint",waypoint);
412 assert(action->is_ready());
413 if(!action->is_ready())
414 throw Error(Error::TYPE_NOTREADY);
420 catch(synfig::Exception::NotFound){ } catch(int){ }
424 //synfig::info("WaypointSetSmart: Add?");
425 // At this point we know that the old waypoint doesn't exist,
426 // so we need to create it.
427 Action::Handle action(WaypointAdd::create());
429 action->set_param("canvas",get_canvas());
430 action->set_param("canvas_interface",get_canvas_interface());
431 action->set_param("value_node",ValueNode::Handle(value_node));
432 action->set_param("waypoint",waypoint);
434 assert(action->is_ready());
435 if(!action->is_ready())
436 throw Error(Error::TYPE_NOTREADY);
442 catch(synfig::Exception::NotFound){ } catch(int){ }
444 throw Error(_("Unable to determine how to proceed. This is a bug."));