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