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