Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_07_rc1 / src / synfigapp / actions / waypointadd.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file waypointadd.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
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.
14 **
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.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "waypointadd.h"
33 #include <synfigapp/canvasinterface.h>
34 #include <synfigapp/main.h>
35
36 #endif
37
38 using namespace std;
39 using namespace etl;
40 using namespace synfig;
41 using namespace synfigapp;
42 using namespace Action;
43
44 /* === M A C R O S ========================================================= */
45
46 ACTION_INIT(Action::WaypointAdd);
47 ACTION_SET_NAME(Action::WaypointAdd,"waypoint_add");
48 ACTION_SET_LOCAL_NAME(Action::WaypointAdd,"Add Waypoint");
49 ACTION_SET_TASK(Action::WaypointAdd,"add");
50 ACTION_SET_CATEGORY(Action::WaypointAdd,Action::CATEGORY_WAYPOINT);
51 ACTION_SET_PRIORITY(Action::WaypointAdd,0);
52 ACTION_SET_VERSION(Action::WaypointAdd,"0.0");
53 ACTION_SET_CVS_ID(Action::WaypointAdd,"$Id$");
54
55 /* === G L O B A L S ======================================================= */
56
57 /* === P R O C E D U R E S ================================================= */
58
59 /* === M E T H O D S ======================================================= */
60
61 Action::WaypointAdd::WaypointAdd()
62 {
63         waypoint.set_time(Time::begin()-1);
64         time_set=false;
65         set_dirty(true);
66 }
67
68 Action::ParamVocab
69 Action::WaypointAdd::get_param_vocab()
70 {
71         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
72
73         ret.push_back(ParamDesc("value_node",Param::TYPE_VALUENODE)
74                 .set_local_name(_("Destination ValueNode (Animated)"))
75         );
76
77         ret.push_back(ParamDesc("waypoint",Param::TYPE_WAYPOINT)
78                 .set_local_name(_("New Waypoint"))
79                 .set_desc(_("Waypoint to be added"))
80                 .set_optional()
81         );
82
83         ret.push_back(ParamDesc("time",Param::TYPE_TIME)
84                 .set_local_name(_("Time"))
85                 .set_desc(_("Time where waypoint is to be added"))
86                 .set_optional()
87         );
88
89         return ret;
90 }
91
92 bool
93 Action::WaypointAdd::is_candidate(const ParamList &x)
94 {
95         if(candidate_check(get_param_vocab(),x))
96         {
97                 if(!ValueNode_Animated::Handle::cast_dynamic(x.find("value_node")->second.get_value_node()))
98                         return false;
99
100                 // We need either a waypoint or a time.
101                 if(x.count("waypoint") || x.count("time"))
102                         return true;
103         }
104         return false;
105 }
106
107 bool
108 Action::WaypointAdd::set_param(const synfig::String& name, const Action::Param &param)
109 {
110         if(name=="value_node" && param.get_type()==Param::TYPE_VALUENODE)
111         {
112                 value_node=ValueNode_Animated::Handle::cast_dynamic(param.get_value_node());
113                 if(time_set)
114                         calc_waypoint();
115
116                 return static_cast<bool>(value_node);
117         }
118         if(name=="waypoint" && param.get_type()==Param::TYPE_WAYPOINT && !time_set)
119         {
120                 waypoint=param.get_waypoint();
121
122                 return true;
123         }
124         if(name=="time" && param.get_type()==Param::TYPE_TIME && waypoint.get_time()==Time::begin()-1)
125         {
126                 waypoint.set_time(param.get_time());
127                 time_set=true;
128
129                 if(value_node)
130                         calc_waypoint();
131
132                 return true;
133         }
134
135         return Action::CanvasSpecific::set_param(name,param);
136 }
137
138 bool
139 Action::WaypointAdd::is_ready()const
140 {
141         if(!value_node || waypoint.get_time()==(Time::begin()-1))
142                 return false;
143         return Action::CanvasSpecific::is_ready();
144 }
145
146 // This function is called if a time is specified, but not
147 // a waypoint. In this case, we need to calculate the value
148 // of the waypoint
149 void
150 Action::WaypointAdd::calc_waypoint()
151 {
152         Time time=waypoint.get_time();
153         Waypoint original(waypoint);
154         waypoint=value_node->new_waypoint_at_time(time);
155         waypoint.mimic(original);
156         waypoint.set_before(synfigapp::Main::get_interpolation());
157         waypoint.set_after(synfigapp::Main::get_interpolation());
158
159 /*
160         ValueNode_Animated::WaypointList &waypoint_list(value_node->waypoint_list());
161         ValueNode_Animated::WaypointList::iterator iter;
162
163         if(waypoint_list.empty())
164         {
165                 waypoint.set_value((*value_node)(time));
166                 return;
167         }
168
169         ValueNode_Animated::WaypointList::iterator closest=waypoint_list.begin();
170
171         for(iter=waypoint_list.begin();iter!=waypoint_list.end();++iter)
172         {
173                 const Real dist(abs(iter->get_time()-time));
174                 if(dist<abs(closest->get_time()-time))
175                         closest=iter;
176         }
177         if(!closest->is_static())
178                 waypoint.set_value_node(closest->get_value_node());
179         else
180                 waypoint.set_value((*value_node)(time));
181         */
182 }
183
184 void
185 Action::WaypointAdd::perform()
186 {
187         try { value_node->find(waypoint.get_time()); throw Error(_("A Waypoint already exists at this point in time (%s)"),waypoint.get_time().get_string().c_str());}
188         catch(synfig::Exception::NotFound) { }
189
190         try { if(value_node->find(waypoint)!=value_node->waypoint_list().end()) throw Error(_("This waypoint is already in the ValueNode"));}
191         catch(synfig::Exception::NotFound) { }
192
193         value_node->add(waypoint);
194
195         value_node->changed();
196 /*_if(get_canvas_interface())
197         {
198                 get_canvas_interface()->signal_value_node_changed()(value_node);
199         }
200         else synfig::warning("CanvasInterface not set on action");*/
201 }
202
203 void
204 Action::WaypointAdd::undo()
205 {
206         value_node->erase(waypoint);
207
208         value_node->changed();
209 /*_if(get_canvas_interface())
210         {
211                 get_canvas_interface()->signal_value_node_changed()(value_node);
212         }
213         else synfig::warning("CanvasInterface not set on action");*/
214 }