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