Fix 1501383: "del key deletes object instead of character". Changed the LayerRemove...
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / timepointsmove.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file timepointsmove.cpp
3 **      \brief Move the Time Points File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2004 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 "timepointsmove.h"
33 #include <synfig/layer_pastecanvas.h>
34 #include <synfigapp/canvasinterface.h>
35 #include <synfig/valuenode_dynamiclist.h>
36 #include <synfig/valuenode_animated.h>
37
38 #include "activepointset.h"
39 #include "waypointset.h"
40 #include <synfigapp/timegather.h>
41
42 #include <typeinfo>
43
44 #include <synfigapp/general.h>
45
46 #endif
47
48 using namespace std;
49 using namespace etl;
50 using namespace synfig;
51 using namespace synfigapp;
52 using namespace Action;
53
54 /* === M A C R O S ========================================================= */
55
56 ACTION_INIT(Action::TimepointsMove);
57 ACTION_SET_NAME(Action::TimepointsMove,"TimepointsMove");
58 ACTION_SET_LOCAL_NAME(Action::TimepointsMove,N_("Move Time Points"));
59 ACTION_SET_TASK(Action::TimepointsMove,"move");
60 ACTION_SET_CATEGORY(Action::TimepointsMove,Action::CATEGORY_WAYPOINT|Action::CATEGORY_ACTIVEPOINT);
61 ACTION_SET_PRIORITY(Action::TimepointsMove,0);
62 ACTION_SET_VERSION(Action::TimepointsMove,"0.0");
63 ACTION_SET_CVS_ID(Action::TimepointsMove,"$Id$");
64
65 /* === G L O B A L S ======================================================= */
66
67 /* === P R O C E D U R E S ================================================= */
68
69 /* === M E T H O D S ======================================================= */
70
71 Action::TimepointsMove::TimepointsMove()
72 {
73         timemove = 0;
74         set_dirty(false);
75 }
76
77 Action::ParamVocab
78 Action::TimepointsMove::get_param_vocab()
79 {
80         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
81
82         ret.push_back(ParamDesc("addlayer",Param::TYPE_VALUE)
83                 .set_local_name(_("New Selected Layer"))
84                 .set_desc(_("A layer to add to our selected list"))
85                 .set_supports_multiple()
86                 .set_optional()
87         );
88
89         ret.push_back(ParamDesc("addcanvas",Param::TYPE_CANVAS)
90                 .set_local_name(_("New Selected Canvas"))
91                 .set_desc(_("A canvas to add to our selected list"))
92                 .set_supports_multiple()
93                 .set_optional()
94         );
95
96         ret.push_back(ParamDesc("addvaluedesc",Param::TYPE_VALUEDESC)
97                 .set_local_name(_("New Selected ValueBase"))
98                 .set_desc(_("A valuenode's description to add to our selected list"))
99                 .set_supports_multiple()
100                 .set_optional()
101         );
102
103         ret.push_back(ParamDesc("addtime",Param::TYPE_TIME)
104                 .set_local_name(_("New Selected Time Point"))
105                 .set_desc(_("A time point to add to our selected list"))
106                 .set_supports_multiple()
107         );
108
109         ret.push_back(ParamDesc("deltatime",Param::TYPE_TIME)
110                 .set_local_name(_("Time adjustment"))
111                 .set_desc(_("The amount of time to adjust all the selected points"))
112         );
113
114         return ret;
115 }
116
117 bool
118 Action::TimepointsMove::is_candidate(const ParamList &x)
119 {
120         if(!candidate_check(get_param_vocab(),x))
121                 return false;
122
123         if(     x.find("addlayer") == x.end() &&
124                 x.find("addcanvas") == x.end() &&
125                 x.find("addvaluedesc") == x.end())
126                 return false;
127         return true;
128 }
129
130 bool
131 Action::TimepointsMove::set_param(const synfig::String& name, const Action::Param &param)
132 {
133         if(name=="addlayer" && param.get_type()==Param::TYPE_LAYER)
134         {
135                 //add a layer to the list
136                 sel_layers.push_back(param.get_layer());
137                 //synfig::info("action got layer");
138
139                 return true;
140         }
141
142         if(name=="addcanvas" && param.get_type()==Param::TYPE_CANVAS)
143         {
144                 //add a layer to the list
145                 sel_canvases.push_back(param.get_canvas());
146                 //synfig::info("action got canvas");
147
148                 return true;
149         }
150
151         if(name=="addvaluedesc" && param.get_type()==Param::TYPE_VALUEDESC)
152         {
153                 //add a layer to the list
154                 sel_values.push_back(param.get_value_desc());
155                 //synfig::info("action got valuedesc");
156
157                 return true;
158         }
159
160         if(name=="addtime" && param.get_type()==Param::TYPE_TIME)
161         {
162                 //add a layer to the list
163                 sel_times.insert(param.get_time());
164                 //synfig::info("action got time");
165
166                 return true;
167         }
168
169         if(name=="deltatime" && param.get_type()==Param::TYPE_TIME)
170         {
171                 timemove = param.get_time();
172                 //synfig::info("action got time to move");
173
174                 return true;
175         }
176
177         return Action::CanvasSpecific::set_param(name,param);
178 }
179
180 bool
181 Action::TimepointsMove::is_ready()const
182 {
183         if((sel_layers.empty() && sel_canvases.empty() && sel_values.empty()) || sel_times.empty())
184                 return false;
185         return Action::CanvasSpecific::is_ready();
186 }
187
188 void
189 Action::TimepointsMove::prepare()
190 {
191         clear();
192
193         //synfig::info("Preparing TimepointsMove by %f secs",(float)timemove);
194
195         if(sel_times.empty()) return;
196
197         //all our lists should be set correctly...
198
199         /*{
200                 std::set<synfig::Time>::iterator i = sel_times.begin(), end = sel_times.end();
201
202                 for(; i != end; ++i)
203                 {
204                         synfig::info("Time %f", (float)*i);
205                 }
206         }*/
207
208         //build our sub-action list
209         //      and yes we do need to store it temporarily so we don't duplicate
210         //              an operation on a specific valuenode, etc....
211         timepoints_ref  match;
212
213         Time fps = get_canvas()->rend_desc().get_frame_rate();
214
215         //std::vector<synfig::Layer::Handle>
216         //synfig::info("Layers %d", sel_layers.size());
217         {
218                 std::vector<synfig::Layer::Handle>::iterator i = sel_layers.begin(),
219                                                                                                         end = sel_layers.end();
220
221                 for(; i != end; ++i)
222                 {
223                         //synfig::info("Recurse through a layer");
224                         recurse_layer(*i,sel_times,match);
225                 }
226         }
227
228         //std::vector<synfig::Canvas::Handle>   sel_canvases;
229         //synfig::info("Canvases %d", sel_canvases.size());
230         {
231                 std::vector<synfig::Canvas::Handle>::iterator   i = sel_canvases.begin(),
232                                                                                                                 end = sel_canvases.end();
233
234                 for(; i != end; ++i)
235                 {
236                         //synfig::info("Recurse through a canvas");
237                         recurse_canvas(*i,sel_times,match);
238                 }
239         }
240
241         //std::vector<synfigapp::ValueDesc>
242         //synfig::info("ValueBasedescs %d", sel_values.size());
243         {
244                 std::vector<synfigapp::ValueDesc>::iterator     i = sel_values.begin(),
245                                                                                                         end = sel_values.end();
246
247                 for(; i != end; ++i)
248                 {
249                         //synfig::info("Recurse through a valuedesc");
250                         recurse_valuedesc(*i,sel_times,match);
251                 }
252         }
253
254         //synfig::info("built list of waypoints/activepoints to modify");
255         //synfig::info("\t There are %d waypoint sets and %d activepointsets",
256         //                              match.waypointbiglist.size(), match.actpointbiglist.size());
257         //process them...
258         {
259                 //must build from both lists
260                 timepoints_ref::waytracker::const_iterator      i = match.waypointbiglist.begin(),
261                                                                                                         end = match.waypointbiglist.end();
262                 for(; i != end; ++i)
263                 {
264                         Action::Handle action(WaypointSet::create());
265
266                         action->set_param("canvas",get_canvas());
267                         action->set_param("canvas_interface",get_canvas_interface());
268                         action->set_param("value_node",ValueNode::Handle(i->val));
269
270                         //iterate through each waypoint for this specific valuenode
271                         std::set<synfig::Waypoint>::const_iterator      j = i->waypoints.begin(),
272                                                                                                                 end = i->waypoints.end();
273                         for(; j != end; ++j)
274                         {
275                                 //synfig::info("add waypoint mod...");
276                                 //NOTE: We may want to store the old time for undoing the action...
277                                 Waypoint w = *j;
278                                 w.set_time((w.get_time() + timemove).round(fps));
279                                 action->set_param("waypoint",w);
280                         }
281
282                         //run the action now that we've added everything
283                         assert(action->is_ready());
284                         if(!action->is_ready())
285                                 throw Error(Error::TYPE_NOTREADY);
286
287                         add_action_front(action);
288                 }
289         }
290         {
291                 //must build from both lists
292                 timepoints_ref::acttracker::const_iterator      i = match.actpointbiglist.begin(),
293                                                                                                         end = match.actpointbiglist.end();
294                 for(; i != end; ++i)
295                 {
296                         Action::Handle action(ActivepointSet::create());
297
298                         action->set_param("canvas",get_canvas());
299                         action->set_param("canvas_interface",get_canvas_interface());
300                         action->set_param("value_desc",i->val);
301
302                         //iterate through each activepoint for this specific valuenode
303                         std::set<synfig::Activepoint>::const_iterator   j = i->activepoints.begin(),
304                                                                                                                         jend = i->activepoints.end();
305                         for(; j != jend; ++j)
306                         {
307                                 //synfig::info("add activepoint mod...");
308
309                                 //NOTE: We may want to store the old time for undoing the action...
310                                 Activepoint a = *j;
311                                 a.set_time((a.get_time() + timemove).round(fps));
312                                 action->set_param("activepoint",a);
313                         }
314
315                         assert(action->is_ready());
316                         if(!action->is_ready())
317                         {
318                                 throw Error(Error::TYPE_NOTREADY);
319                         }
320
321                         add_action_front(action);
322                 }
323         }
324 }
325
326 void
327 Action::TimepointsMove::perform()
328 {
329         Action::Super::perform();
330 }