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