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