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 / waypointset.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file waypointset.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 "waypointset.h"
33 #include <synfigapp/canvasinterface.h>
34
35 #endif
36
37 using namespace std;
38 using namespace etl;
39 using namespace synfig;
40 using namespace synfigapp;
41 using namespace Action;
42
43 /* === M A C R O S ========================================================= */
44
45 ACTION_INIT(Action::WaypointSet);
46 ACTION_SET_NAME(Action::WaypointSet,"waypoint_set");
47 ACTION_SET_LOCAL_NAME(Action::WaypointSet,"Set Waypoint");
48 ACTION_SET_TASK(Action::WaypointSet,"set");
49 ACTION_SET_CATEGORY(Action::WaypointSet,Action::CATEGORY_WAYPOINT);
50 ACTION_SET_PRIORITY(Action::WaypointSet,0);
51 ACTION_SET_VERSION(Action::WaypointSet,"0.0");
52 ACTION_SET_CVS_ID(Action::WaypointSet,"$Id$");
53
54 /* === G L O B A L S ======================================================= */
55
56 /* === P R O C E D U R E S ================================================= */
57
58 /* === M E T H O D S ======================================================= */
59
60 Action::WaypointSet::WaypointSet()
61 {
62         set_dirty(true);
63 }
64
65 Action::ParamVocab
66 Action::WaypointSet::get_param_vocab()
67 {
68         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
69
70         ret.push_back(ParamDesc("value_node",Param::TYPE_VALUENODE)
71                 .set_local_name(_("Destination ValueNode (Animated)"))
72         );
73
74         ret.push_back(ParamDesc("waypoint",Param::TYPE_WAYPOINT)
75                 .set_local_name(_("Waypoint"))
76                 .set_desc(_("Waypoint to be changed"))
77                 .set_supports_multiple()
78         );
79
80         return ret;
81 }
82
83 bool
84 Action::WaypointSet::is_candidate(const ParamList &x)
85 {
86         return candidate_check(get_param_vocab(),x);
87 }
88
89 bool
90 Action::WaypointSet::set_param(const synfig::String& name, const Action::Param &param)
91 {
92         if(name=="value_node" && param.get_type()==Param::TYPE_VALUENODE)
93         {
94                 value_node=ValueNode_Animated::Handle::cast_dynamic(param.get_value_node());
95
96                 return static_cast<bool>(value_node);
97         }
98         if(name=="waypoint" && param.get_type()==Param::TYPE_WAYPOINT)
99         {
100                 //NOTE: at the moment there is no error checking for multiple sets!!!
101                 waypoints.push_back(param.get_waypoint());
102
103                 return true;
104         }
105
106         return Action::CanvasSpecific::set_param(name,param);
107 }
108
109 bool
110 Action::WaypointSet::is_ready()const
111 {
112         if(!value_node || waypoints.empty())
113                 return false;
114         return Action::CanvasSpecific::is_ready();
115 }
116
117 void
118 Action::WaypointSet::perform()
119 {
120         WaypointList::iterator iter;
121
122 #if 1
123         vector<WaypointList::iterator>  iters;
124         vector<Waypoint>::iterator i = waypoints.begin(), end = waypoints.end();
125
126         for(; i != end; ++i)
127         {
128                 try { iters.push_back(value_node->find(*i)); }
129                 catch(synfig::Exception::NotFound)
130                 {
131                         throw Error(_("Unable to find waypoint"));
132                 }
133         }
134
135         //check to see which valuenodes are going to override because of the time...
136         ValueNode_Animated::findresult timeiter;
137
138         for(i = waypoints.begin(); i != end; ++i)
139         {
140                 timeiter = value_node->find_time(i->get_time());
141
142                 bool candelete = timeiter.second;
143
144                 //we only want to track overwrites (not waypoints that are also being modified)
145                 if(candelete)
146                 {
147                         for(vector<WaypointList::iterator>::iterator ii = iters.begin(); ii != iters.end(); ++ii)
148                         {
149                                 if(timeiter.first == *ii)
150                                 {
151                                         candelete = false;
152                                         break;
153                                 }
154                         }
155                 }
156
157                 //if we can still delete it after checking, record it, and then remove them all later
158                 if(candelete)
159                 {
160                         Waypoint w = *timeiter.first;
161                         overwritten_waypoints.push_back(w);
162                 }
163         }
164
165         //overwrite all the valuenodes we're supposed to set
166         {
167                 i = waypoints.begin();
168                 for(vector<WaypointList::iterator>::iterator ii = iters.begin(); ii != iters.end() && i != end; ++ii, ++i)
169                 {
170                         old_waypoints.push_back(**ii);
171                         **ii = *i; //set the point to the corresponding point in the normal waypoint list
172                 }
173         }
174
175         //remove all the points we're supposed to be overwritting
176         {
177                 vector<Waypoint>::iterator      oi = overwritten_waypoints.begin(),
178                                                                         oend = overwritten_waypoints.end();
179                 for(; oi != oend; ++oi)
180                 {
181                         value_node->erase(*oi);
182                 }
183         }
184
185 #else
186         try { iter=value_node->find(waypoint); }
187         catch(synfig::Exception::NotFound)
188         {
189                 throw Error(_("Unable to find waypoint"));
190         }
191
192         //find the value at the old time before we replace it
193         ValueNode_Animated::findresult timeiter;
194         timeiter = value_node->find_time(waypoint.get_time());
195
196         //we only want to track overwrites (not inplace modifications)
197         if(timeiter.second && waypoint.get_uid() == timeiter.first->get_uid())
198         {
199                 timeiter.second = false;
200         }
201
202         //copy and overwrite
203         old_waypoint=*iter;
204         *iter=waypoint;
205
206         //if we've found a unique one then we need to erase it, but store it first
207         if(timeiter.second)
208         {
209                 time_overwrite = true;
210                 overwritten_wp = *timeiter.first;
211
212                 value_node->erase(overwritten_wp);
213         }
214 #endif
215
216         // Signal that a valuenode has been changed
217         value_node->changed();
218 }
219
220 void
221 Action::WaypointSet::undo()
222 {
223         WaypointList::iterator iter;
224
225 #if 1
226         vector<Waypoint>::iterator i = old_waypoints.begin(), end = old_waypoints.end();
227
228         for(; i != end; ++i)
229         {
230                 try { iter = value_node->find(*i); }
231                 catch(synfig::Exception::NotFound)
232                 {
233                         throw Error(_("Unable to find waypoint"));
234                 }
235
236                 //overwrite with old one
237                 *iter = *i;
238         }
239
240         //add back in all the points that we removed before...
241         {
242                 vector<Waypoint>::iterator      oi = overwritten_waypoints.begin(),
243                                                                         oend = overwritten_waypoints.end();
244                 for(; oi != oend; ++oi)
245                 {
246                         value_node->add(*oi);
247                 }
248         }
249
250 #else
251         try { iter=value_node->find(old_waypoint); }
252         catch(synfig::Exception::NotFound)
253         {
254                 throw Error(_("Unable to find waypoint"));
255         }
256
257         *iter=old_waypoint;
258
259         if(time_overwrite)
260         {
261                 value_node->add(overwritten_wp);
262         }
263 #endif
264
265         // Signal that a valuenode has been changed
266         value_node->changed();
267 }