1 /* === S I N F G =========================================================== */
2 /*! \file waypointsetsmart.cpp
3 ** \brief Template File
5 ** $Id: waypointsetsmart.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
31 #include "waypointsetsmart.h"
32 #include "valuenodelinkconnect.h"
33 #include "valuenodereplace.h"
35 #include "waypointset.h"
36 #include "waypointadd.h"
38 #include "valuedescconnect.h"
39 #include <sinfgapp/canvasinterface.h>
40 #include <sinfg/exception.h>
41 #include <sinfgapp/main.h>
47 using namespace sinfg;
48 using namespace sinfgapp;
49 using namespace Action;
51 /* === M A C R O S ========================================================= */
53 ACTION_INIT(Action::WaypointSetSmart);
54 ACTION_SET_NAME(Action::WaypointSetSmart,"waypoint_set_smart");
55 ACTION_SET_LOCAL_NAME(Action::WaypointSetSmart,"Connect");
56 ACTION_SET_TASK(Action::WaypointSetSmart,"set");
57 ACTION_SET_CATEGORY(Action::WaypointSetSmart,Action::CATEGORY_WAYPOINT|Action::CATEGORY_VALUEDESC|Action::CATEGORY_VALUENODE);
58 ACTION_SET_PRIORITY(Action::WaypointSetSmart,0);
59 ACTION_SET_VERSION(Action::WaypointSetSmart,"0.0");
60 ACTION_SET_CVS_ID(Action::WaypointSetSmart,"$Id: waypointsetsmart.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $");
68 /* === G L O B A L S ======================================================= */
70 /* === P R O C E D U R E S ================================================= */
72 /* === M E T H O D S ======================================================= */
74 Action::WaypointSetSmart::WaypointSetSmart()
76 waypoint.set_time(Time::begin()-1);
82 Action::WaypointSetSmart::get_param_vocab()
84 ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
86 ret.push_back(ParamDesc("value_node",Param::TYPE_VALUENODE)
87 .set_local_name(_("Destination ValueNode (Animated)"))
90 ret.push_back(ParamDesc("waypoint",Param::TYPE_WAYPOINT)
91 .set_local_name(_("New Waypoint"))
92 .set_desc(_("Waypoint to be added"))
96 ret.push_back(ParamDesc("waypoint_model",Param::TYPE_WAYPOINTMODEL)
97 .set_local_name(_("Waypoint Model"))
101 ret.push_back(ParamDesc("time",Param::TYPE_TIME)
102 .set_local_name(_("Time"))
103 .set_desc(_("Time where waypoint is to be added"))
111 Action::WaypointSetSmart::is_canidate(const ParamList &x)
113 if(canidate_check(get_param_vocab(),x))
115 if(!ValueNode_Animated::Handle::cast_dynamic(x.find("value_node")->second.get_value_node()))
117 // We need either a waypoint or a time.
118 if(x.count("waypoint") || x.count("time"))
126 Action::WaypointSetSmart::set_param(const sinfg::String& name, const Action::Param ¶m)
128 if(name=="value_node" && param.get_type()==Param::TYPE_VALUENODE)
130 value_node=ValueNode_Animated::Handle::cast_dynamic(param.get_value_node());
135 return static_cast<bool>(value_node);
137 if(name=="waypoint" && param.get_type()==Param::TYPE_WAYPOINT && !time_set)
139 waypoint=param.get_waypoint();
145 if(name=="time" && param.get_type()==Param::TYPE_TIME && waypoint.get_time()==(Time::begin()-1))
147 waypoint.set_time(param.get_time());
157 if(name=="model" && param.get_type()==Param::TYPE_WAYPOINTMODEL)
162 waypoint.apply_model(param.get_waypoint_model());
167 return Action::CanvasSpecific::set_param(name,param);
171 Action::WaypointSetSmart::is_ready()const
174 sinfg::error("Missing value_node");
176 if(waypoint.get_time()==(Time::begin()-1))
177 sinfg::error("Missing waypoint");
179 if(!value_node || waypoint.get_time()==(Time::begin()-1))
181 return Action::CanvasSpecific::is_ready();
184 // This function is called if a time is specified, but not
185 // a waypoint. In this case, we need to calculate the value
188 Action::WaypointSetSmart::calc_waypoint()
191 Time time=waypoint.get_time();
194 // Trivial case, we are sitting on a waypoint
195 waypoint=*value_node->find(waypoint.get_time());
199 waypoint=value_node->new_waypoint_at_time(time);
200 waypoint.set_before(sinfgapp::Main::get_interpolation());
201 waypoint.set_after(sinfgapp::Main::get_interpolation());
204 Time time=waypoint.get_time();
205 ValueNode_Animated::WaypointList &waypoint_list(value_node->waypoint_list());
206 ValueNode_Animated::WaypointList::iterator iter;
208 if(waypoint_list.empty())
210 waypoint.set_value((*value_node)(time));
214 ValueNode_Animated::WaypointList::iterator closest=waypoint_list.begin();
216 for(iter=waypoint_list.begin();iter!=waypoint_list.end();++iter)
218 const Real dist(abs(iter->get_time()-time));
219 if(dist<abs(closest->get_time()-time))
222 if(!closest->is_static())
223 waypoint.set_value_node(closest->get_value_node());
225 waypoint.set_value((*value_node)(time));
230 Action::WaypointSetSmart::enclose_waypoint(const sinfg::Waypoint& waypoint)
232 times.insert(waypoint.get_time());
235 times.insert(value_node->find(waypoint)->get_time());
236 // sinfg::info(__FILE__":%d: value_node->find(waypoint)->get_time()=%s",__LINE__,value_node->find(waypoint)->get_time().get_string().c_str());
241 // First we need to to add any waypoints necessary to
242 // maintain the integrity of the keyframes.
243 if(get_edit_mode()&MODE_ANIMATE_PAST) try
245 Time curr_time(waypoint.get_time());
247 //while(value_node->waypoint_list().front().get_time()<=curr_time)
249 // Try to find prev keyframe
250 Keyframe keyframe(*get_canvas()->keyframe_list().find_prev(curr_time));
251 curr_time=keyframe.get_time();
253 // sinfg::info(__FILE__":%d: prev_keyframe->time=%s",__LINE__,keyframe.get_time().get_string().c_str());
254 // sinfg::info(__FILE__":%d: waypoint->time=%s",__LINE__,waypoint.get_time().get_string().c_str());
257 if(times.count(keyframe.get_time()))
262 if(waypoint.get_time().is_equal(keyframe.get_time()))
268 times.insert(keyframe.get_time());
272 value_node->find(keyframe.get_time());
273 // sinfg::info(__FILE__":%d: waypointtime=%s",__LINE__,value_node->find(keyframe.get_time())->get_time().get_string().c_str());
275 catch(sinfg::Exception::NotFound)
277 Action::Handle action(WaypointAdd::create());
279 action->set_param("canvas",get_canvas());
280 action->set_param("canvas_interface",get_canvas_interface());
281 action->set_param("value_node",ValueNode::Handle(value_node));
283 if(!value_node->waypoint_list().empty())
285 action->set_param("time",keyframe.get_time());
291 tmp.set_value(waypoint.get_value());
292 tmp.set_time(keyframe.get_time());
293 action->set_param("waypoint",tmp);
296 assert(action->is_ready());
297 if(!action->is_ready())
298 throw Error(Error::TYPE_NOTREADY);
304 catch(Error x) { throw x; }
305 catch(sinfg::Exception::NotFound) { DEBUGPOINT(); }
306 catch(int) { DEBUGPOINT(); }
307 catch(...) { DEBUGPOINT(); }
311 if(get_edit_mode()&MODE_ANIMATE_FUTURE)try
313 Time curr_time(waypoint.get_time());
315 //while(value_node->waypoint_list().back().get_time()>=curr_time)
319 // Try to find next keyframe
320 //sinfg::info("FUTURE waypoint.get_time()=%s",waypoint.get_time().get_string().c_str());
321 Keyframe keyframe(*get_canvas()->keyframe_list().find_next(curr_time));
322 //sinfg::info("FUTURE keyframe.get_time()=%s",keyframe.get_time().get_string().c_str());
323 curr_time=keyframe.get_time();
326 if(times.count(keyframe.get_time())|| waypoint.get_time().is_equal(keyframe.get_time()))
329 times.insert(keyframe.get_time());
334 value_node->find(keyframe.get_time());
335 sinfg::info(__FILE__":%d: time=%s",__LINE__,keyframe.get_time().get_string().c_str());
336 sinfg::info(__FILE__":%d: waypointtime=%s",__LINE__,value_node->find(keyframe.get_time())->get_time().get_string().c_str());
339 catch(sinfg::Exception::NotFound)
341 Action::Handle action(WaypointAdd::create());
343 action->set_param("canvas",get_canvas());
344 action->set_param("canvas_interface",get_canvas_interface());
345 action->set_param("value_node",ValueNode::Handle(value_node));
347 if(!value_node->waypoint_list().empty())
349 action->set_param("time",keyframe.get_time());
355 tmp.set_value(waypoint.get_value());
356 tmp.set_time(keyframe.get_time());
357 action->set_param("waypoint",tmp);
360 assert(action->is_ready());
361 if(!action->is_ready())
362 throw Error(Error::TYPE_NOTREADY);
369 catch(Error x) { throw x; }
370 catch(sinfg::Exception::NotFound) { DEBUGPOINT(); }
371 catch(int) { DEBUGPOINT(); }
372 catch(...) { DEBUGPOINT(); }
377 Action::WaypointSetSmart::prepare()
383 // First we need to to add any waypoints necessary to
384 // maintain the integrity of the keyframes.
385 enclose_waypoint(waypoint);
389 //sinfg::info("WaypointSetSmart: Move/Update?");
390 // Lets try to replace the old waypoint, if it exists
391 WaypointList::iterator iter(value_node->find(waypoint));
393 if(iter == value_node->waypoint_list().end())
396 enclose_waypoint(*iter);
398 Action::Handle action(WaypointSet::create());
400 action->set_param("canvas",get_canvas());
401 action->set_param("canvas_interface",get_canvas_interface());
402 action->set_param("value_node",ValueNode::Handle(value_node));
403 action->set_param("waypoint",waypoint);
405 assert(action->is_ready());
406 if(!action->is_ready())
407 throw Error(Error::TYPE_NOTREADY);
413 catch(sinfg::Exception::NotFound){ } catch(int){ }
417 //sinfg::info("WaypointSetSmart: Replace?");
419 // Check to see if a waypoint exists at this point in time
420 WaypointList::iterator iter=value_node->find(waypoint.get_time());
422 waypoint.mimic(*iter);
424 enclose_waypoint(*iter);
426 Action::Handle action(WaypointSet::create());
428 action->set_param("canvas",get_canvas());
429 action->set_param("canvas_interface",get_canvas_interface());
430 action->set_param("value_node",ValueNode::Handle(value_node));
431 action->set_param("waypoint",waypoint);
433 assert(action->is_ready());
434 if(!action->is_ready())
435 throw Error(Error::TYPE_NOTREADY);
441 catch(sinfg::Exception::NotFound){ } catch(int){ }
445 //sinfg::info("WaypointSetSmart: Add?");
447 // At this point we know that the old waypoint doesn't exist,
448 // so we need to create it.
449 Action::Handle action(WaypointAdd::create());
451 action->set_param("canvas",get_canvas());
452 action->set_param("canvas_interface",get_canvas_interface());
453 action->set_param("value_node",ValueNode::Handle(value_node));
454 action->set_param("waypoint",waypoint);
456 assert(action->is_ready());
457 if(!action->is_ready())
458 throw Error(Error::TYPE_NOTREADY);
464 catch(sinfg::Exception::NotFound){ } catch(int){ }
466 throw Error(_("Unable to determine how to procede. This is a bug."));