my log
[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: timepointsmove.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 "timepointsmove.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 "activepointset.h"
38 #include "waypointset.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::TimepointsMove);
54 ACTION_SET_NAME(Action::TimepointsMove,"timepoint_move");
55 ACTION_SET_LOCAL_NAME(Action::TimepointsMove,"Move Time Points");
56 ACTION_SET_TASK(Action::TimepointsMove,"move");
57 ACTION_SET_CATEGORY(Action::TimepointsMove,Action::CATEGORY_WAYPOINT|Action::CATEGORY_ACTIVEPOINT);
58 ACTION_SET_PRIORITY(Action::TimepointsMove,0);
59 ACTION_SET_VERSION(Action::TimepointsMove,"0.0");
60 ACTION_SET_CVS_ID(Action::TimepointsMove,"$Id: timepointsmove.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::TimepointsMove::TimepointsMove()
69 {
70         timemove = 0;
71         set_dirty(false);
72 }
73
74 Action::ParamVocab
75 Action::TimepointsMove::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::TimepointsMove::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::TimepointsMove::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                 timemove = 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::TimepointsMove::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::TimepointsMove::prepare()
187 {
188         clear();
189         
190         //synfig::info("Preparing TimepointsMove by %f secs",(float)timemove);
191         
192         if(sel_times.empty()) return;
193         
194         //all our lists should be set correctly...
195         
196         /*{
197                 std::set<synfig::Time>::iterator i = sel_times.begin(), end = sel_times.end();
198                 
199                 for(; i != end; ++i)
200                 {
201                         synfig::info("Time %f", (float)*i);
202                 }               
203         }*/
204
205         //build our sub-action list
206         //      and yes we do need to store it temporarily so we don't duplicate 
207         //              an operation on a specific valuenode, etc....
208         timepoints_ref  match;
209         
210         Time fps = get_canvas()->rend_desc().get_frame_rate();
211         
212         //std::vector<synfig::Layer::Handle>
213         //synfig::info("Layers %d", sel_layers.size());
214         {
215                 std::vector<synfig::Layer::Handle>::iterator i = sel_layers.begin(),
216                                                                                                         end = sel_layers.end();
217                 
218                 for(; i != end; ++i)
219                 {
220                         //synfig::info("Recurse through a layer");
221                         recurse_layer(*i,sel_times,match);
222                 }
223         }
224         
225         //std::vector<synfig::Canvas::Handle>   sel_canvases;
226         //synfig::info("Canvases %d", sel_canvases.size());
227         {
228                 std::vector<synfig::Canvas::Handle>::iterator   i = sel_canvases.begin(),
229                                                                                                                 end = sel_canvases.end();
230                 
231                 for(; i != end; ++i)
232                 {
233                         //synfig::info("Recurse through a canvas");
234                         recurse_canvas(*i,sel_times,match);
235                 }
236         }
237         
238         //std::vector<synfigapp::ValueDesc>
239         //synfig::info("ValueBasedescs %d", sel_values.size());
240         {
241                 std::vector<synfigapp::ValueDesc>::iterator     i = sel_values.begin(),
242                                                                                                         end = sel_values.end();
243                 
244                 for(; i != end; ++i)
245                 {
246                         //synfig::info("Recurse through a valuedesc");
247                         recurse_valuedesc(*i,sel_times,match);
248                 }
249         }
250         
251         //synfig::info("built list of waypoints/activepoints to modify");
252         //synfig::info("\t There are %d waypoint sets and %d activepointsets", 
253         //                              match.waypointbiglist.size(), match.actpointbiglist.size());
254         //process the hell out of em...
255         {
256                 //must build from both lists
257                 timepoints_ref::waytracker::const_iterator      i = match.waypointbiglist.begin(),
258                                                                                                         end = match.waypointbiglist.end();
259                 for(; i != end; ++i)
260                 {
261                         Action::Handle action(WaypointSet::create());
262                 
263                         action->set_param("canvas",get_canvas());
264                         action->set_param("canvas_interface",get_canvas_interface());
265                         action->set_param("value_node",ValueNode::Handle(i->val));
266                         
267                         //iterate through each waypoint for this specific valuenode
268                         std::set<synfig::Waypoint>::const_iterator      j = i->waypoints.begin(),
269                                                                                                                 end = i->waypoints.end();                       
270                         for(; j != end; ++j)
271                         {                               
272                                 //synfig::info("add waypoint mod...");
273                                 //NOTE: We may want to store the old time for undoing the action...
274                                 Waypoint w = *j;
275                                 w.set_time((w.get_time() + timemove).round(fps));
276                                 action->set_param("waypoint",w);
277                         }
278                         
279                         //run the action now that we've added everything
280                         assert(action->is_ready());
281                         if(!action->is_ready())
282                                 throw Error(Error::TYPE_NOTREADY);
283                         
284                         add_action_front(action);
285                 }
286         }
287         {
288                 //must build from both lists
289                 timepoints_ref::acttracker::const_iterator      i = match.actpointbiglist.begin(),
290                                                                                                         end = match.actpointbiglist.end();
291                 for(; i != end; ++i)
292                 {
293                         Action::Handle action(ActivepointSet::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                         //iterate through each activepoint for this specific valuenode
300                         std::set<synfig::Activepoint>::const_iterator   j = i->activepoints.begin(),
301                                                                                                                         jend = i->activepoints.end();
302                         for(; j != jend; ++j)
303                         {
304                                 //synfig::info("add activepoint mod...");
305                                 
306                                 //NOTE: We may want to store the old time for undoing the action...
307                                 Activepoint a = *j;
308                                 a.set_time((a.get_time() + timemove).round(fps));
309                                 action->set_param("activepoint",a);
310                         }
311                         
312                         assert(action->is_ready());
313                         if(!action->is_ready())
314                         {
315                                 throw Error(Error::TYPE_NOTREADY);
316                         }
317                 
318                         add_action_front(action);
319                 }
320         }
321 }
322
323 void
324 Action::TimepointsMove::perform()
325 {
326         Action::Super::perform();
327 }