Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_03 / synfig-studio / src / synfigapp / actions / keyframeset.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file keyframeset.cpp
3 **      \brief Template File
4 **
5 **      $Id: keyframeset.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
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 "keyframeset.h"
33 #include <synfigapp/canvasinterface.h>
34 #include <synfig/valuenode_dynamiclist.h>
35 #include <synfig/valuenode_animated.h>
36 #include "activepointsetsmart.h"
37 #include "waypointsetsmart.h"
38
39 #endif
40
41 using namespace std;
42 using namespace etl;
43 using namespace synfig;
44 using namespace synfigapp;
45 using namespace Action;
46
47 /* === M A C R O S ========================================================= */
48
49 ACTION_INIT(Action::KeyframeSet);
50 ACTION_SET_NAME(Action::KeyframeSet,"keyframe_set");
51 ACTION_SET_LOCAL_NAME(Action::KeyframeSet,"Set Keyframe");
52 ACTION_SET_TASK(Action::KeyframeSet,"set");
53 ACTION_SET_CATEGORY(Action::KeyframeSet,Action::CATEGORY_KEYFRAME|Action::CATEGORY_HIDDEN);
54 ACTION_SET_PRIORITY(Action::KeyframeSet,0);
55 ACTION_SET_VERSION(Action::KeyframeSet,"0.0");
56 ACTION_SET_CVS_ID(Action::KeyframeSet,"$Id: keyframeset.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $");
57
58 /* === G L O B A L S ======================================================= */
59
60 /* === P R O C E D U R E S ================================================= */
61
62 /* === M E T H O D S ======================================================= */
63
64 Action::KeyframeSet::KeyframeSet()
65 {
66         keyframe.set_time(Time::begin()-1);
67         set_dirty(false);
68 }
69
70 Action::ParamVocab
71 Action::KeyframeSet::get_param_vocab()
72 {
73         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
74         
75         ret.push_back(ParamDesc("keyframe",Param::TYPE_KEYFRAME)
76                 .set_local_name(_("New Keyframe"))
77                 .set_desc(_("Keyframe to be added"))
78         );
79
80         return ret;
81 }
82
83 bool
84 Action::KeyframeSet::is_canidate(const ParamList &x)
85 {
86         return canidate_check(get_param_vocab(),x);
87 }
88
89 bool
90 Action::KeyframeSet::set_param(const synfig::String& name, const Action::Param &param)
91 {
92         if(name=="keyframe" && param.get_type()==Param::TYPE_KEYFRAME)
93         {
94                 synfig::info("KeyframeSet::set_param():old_time: %s",keyframe.get_time().get_string().c_str());
95                 keyframe=param.get_keyframe();
96                 synfig::info("KeyframeSet::set_param():new_time: %s",keyframe.get_time().get_string().c_str());
97                 synfig::info("KeyframeSet::set_param():get_keyframe(): %s",param.get_keyframe().get_time().get_string().c_str());
98                 
99                 return true;
100         }
101
102         return Action::CanvasSpecific::set_param(name,param);
103 }
104
105 bool
106 Action::KeyframeSet::is_ready()const
107 {
108         if(keyframe.get_time()==(Time::begin()-1))
109                 return false;
110         return Action::CanvasSpecific::is_ready();
111 }
112
113 void
114 Action::KeyframeSet::prepare()
115 {
116         clear();
117         guid_set.clear();
118         
119         
120
121         
122         
123         //synfig::info("new_time: %s",new_time.get_string().c_str());
124         //synfig::info("old_time: %s",old_time.get_string().c_str());
125         
126         try { if(get_canvas()->keyframe_list().find(new_time)!=get_canvas()->keyframe_list().end()) throw Error(_("A Keyframe already exists at this point in time"));}
127         catch(...) { }  
128
129
130         // If the times are different, then we 
131         // will need to romp through the valuenodes
132         // and add actions to update their values.
133         if(new_time!=old_time)
134         {
135                 std::vector<synfigapp::ValueDesc> value_desc_list;
136                 get_canvas_interface()->find_important_value_descs(value_desc_list);
137                 while(!value_desc_list.empty())
138                 {
139                         process_value_desc(value_desc_list.back());
140                         value_desc_list.pop_back();
141                 }
142         }
143 }
144
145 #define old_2_new(x)    (((x)-old_begin)/(old_end-old_begin)*(new_end-new_begin)+new_begin)
146
147 int
148 Action::KeyframeSet::scale_activepoints(const synfigapp::ValueDesc& value_desc,const Time& old_begin,const Time& old_end,const Time& new_begin,const Time& new_end)
149 {
150         ValueNode_DynamicList::Handle value_node(ValueNode_DynamicList::Handle::cast_static(value_desc.get_parent_value_node()));
151         ValueNode_DynamicList::ListEntry& list_entry(value_node->list[value_desc.get_index()]);
152         
153         std::vector<Activepoint*> selected;
154         std::vector<Activepoint*>::iterator iter;
155         
156         if(list_entry.find(old_begin,old_end,selected))
157         {
158                 // check to make sure this operation is OK
159                 for(iter=selected.begin();iter!=selected.end();++iter)
160                 {
161                         try
162                         {
163                                 Time new_time(old_2_new((*iter)->get_time()));
164                                 if(new_time>=old_begin && new_time<old_end)
165                                         continue;
166                                 list_entry.find(new_time);
167                                 // If we found a activepoint already at that time, then
168                                 // we need to abort
169                                 //throw Exception::BadTime(_("Activepoint Conflict"));
170                         }
171                         catch(Exception::NotFound) { }
172                 }
173                 
174                 int ret(0);
175                 while(!selected.empty())
176                 {
177                         if(selected.back()->get_time()!=old_2_new(selected.back()->get_time()))
178                         {
179                                 Action::Handle action(Action::create("activepoint_set"));
180                                 
181                                 action->set_param("canvas",get_canvas());
182                                 action->set_param("canvas_interface",get_canvas_interface());
183                                 action->set_param("value_desc",value_desc);
184                                 
185                                 Activepoint activepoint(*selected.back());
186                                 activepoint.set_time(old_2_new(selected.back()->get_time()));
187
188                                 action->set_param("activepoint",activepoint);
189
190                                 assert(action->is_ready());
191                                 if(!action->is_ready())
192                                         throw Error(Error::TYPE_NOTREADY);
193                         
194                                 add_action_front(action);                                               
195
196                                 ret++;
197                         }
198                         selected.pop_back();
199                 }
200                 return ret;
201         }
202         return 0;
203 }
204
205 int
206 Action::KeyframeSet::scale_waypoints(const synfigapp::ValueDesc& value_desc,const Time& old_begin,const Time& old_end,const Time& new_begin,const Time& new_end)
207 {
208         ValueNode_Animated::Handle value_node(ValueNode_Animated::Handle::cast_static(value_desc.get_value_node()));
209         
210         std::vector<Waypoint*> selected;
211         std::vector<Waypoint*>::iterator iter;
212         
213         if(value_node->find(old_begin,old_end,selected))
214         {
215                 // check to make sure this operation is OK
216                 for(iter=selected.begin();iter!=selected.end();++iter)
217                 {
218                         try
219                         {
220                                 Time new_time(old_2_new((*iter)->get_time()));
221                                 if(new_time>=old_begin && new_time<old_end)
222                                         continue;
223                                 value_node->find(new_time);
224                                 // If we found a waypoint point already at that time, then
225                                 // we need to abort
226                                 //synfig::info(_("old_begin: %s, old_end: %s"),old_begin.get_string().c_str(),old_end.get_string().c_str());
227                                 //synfig::info(_("new_begin: %s, new_end: %s"),new_begin.get_string().c_str(),new_end.get_string().c_str());
228                                 //throw Exception::BadTime(strprintf(_("Waypoint Conflict, old: %s, new: %s"),(*iter)->get_time().get_string().c_str(),new_time.get_string().c_str()));
229                         }
230                         catch(Exception::NotFound) { }
231                 }
232                 
233                 int ret(0);
234                 while(!selected.empty())
235                 {
236                         if(selected.back()->get_time()!=old_2_new(selected.back()->get_time()))
237                         {
238                                 Action::Handle action(Action::create("waypoint_set"));
239                                 
240                                 action->set_param("canvas",get_canvas());
241                                 action->set_param("canvas_interface",get_canvas_interface());
242                                 action->set_param("value_node",ValueNode::Handle::cast_static(value_node));
243                                 
244                                 Waypoint waypoint(*selected.back());
245                                 waypoint.set_time(old_2_new(selected.back()->get_time()));
246
247                                 action->set_param("waypoint",waypoint);
248
249                                 assert(action->is_ready());
250                                 if(!action->is_ready())
251                                         throw Error(Error::TYPE_NOTREADY);
252                         
253                                 add_action_front(action);                                               
254
255                                 ret++;
256                         }
257                         selected.pop_back();
258                 }
259                 return ret;
260         }
261         return 0;
262 }
263
264 void
265 Action::KeyframeSet::process_value_desc(const synfigapp::ValueDesc& value_desc)
266 {       
267         if(value_desc.is_value_node())
268         {               
269                 ValueNode::Handle value_node(value_desc.get_value_node());
270
271                 //if(guid_set.count(value_node->get_guid()))
272                 //      return;
273                 //guid_set.insert(value_node->get_guid());
274
275                 // If we are a dynamic list, then we need to update the ActivePoints
276                 if(ValueNode_DynamicList::Handle::cast_dynamic(value_node))
277                 {
278                         ValueNode_DynamicList::Handle value_node(ValueNode_DynamicList::Handle::cast_dynamic(value_node));
279                         int i;
280                         for(i=0;i<value_node->link_count();i++)
281                         {
282                                 synfigapp::ValueDesc value_desc(value_node,i);
283                                 if(new_time>keyframe_prev && new_time<keyframe_next)
284                                 {
285                                         // In this circumstance, we need to adjust any
286                                         // activepoints between the previous and next
287                                         // keyframes
288                                         scale_activepoints(value_desc,keyframe_prev,old_time,keyframe_prev,new_time);
289                                         scale_activepoints(value_desc,old_time,keyframe_next,new_time,keyframe_next);
290                                 }
291                                 //else
292                                 {       
293                                         Action::Handle action(ActivepointSetSmart::create());
294                                         
295                                         action->set_param("canvas",get_canvas());
296                                         action->set_param("canvas_interface",get_canvas_interface());
297                                         action->set_param("value_desc",value_desc);
298                                         
299                                         Activepoint activepoint;
300                                         try
301                                         {
302                                                 activepoint=*value_node->list[i].find(old_time);
303                                                 activepoint.set_time(new_time);
304                                         }
305                                         catch(...)
306                                         {
307                                                 activepoint.set_time(new_time);
308                                                 activepoint.set_state(value_node->list[i].status_at_time(old_time));
309                                                 activepoint.set_priority(0);
310                                         }
311                                         action->set_param("activepoint",activepoint);
312         
313                                         assert(action->is_ready());
314                                         if(!action->is_ready())
315                                                 throw Error(Error::TYPE_NOTREADY);
316                                 
317                                         add_action_front(action);
318                                 }
319                         }
320                 }
321                 else if(ValueNode_Animated::Handle::cast_dynamic(value_node))
322                 {
323                         if(new_time>keyframe_prev && new_time<keyframe_next)
324                         {
325                                         // In this circumstance, we need to adjust any
326                                         // waypoints between the previous and next
327                                         // keyframes
328                                         scale_waypoints(value_desc,keyframe_prev,old_time,keyframe_prev,new_time);
329                                         scale_waypoints(value_desc,old_time,keyframe_next,new_time,keyframe_next);
330                         }
331                         //else
332                         {
333                                 ValueNode_Animated::Handle value_node(ValueNode_Animated::Handle::cast_dynamic(value_node));
334                                 
335                                 Action::Handle action(WaypointSetSmart::create());
336                                 
337                                 action->set_param("canvas",get_canvas());
338                                 action->set_param("canvas_interface",get_canvas_interface());
339                                 action->set_param("value_node",ValueNode::Handle(value_node));
340                                 
341                                 Waypoint waypoint;
342                                 try
343                                 {
344                                         waypoint=*value_node->find(old_time);
345                                         waypoint.set_time(new_time);
346                                 }
347                                 catch(...)
348                                 {
349                                         waypoint.set_time(new_time);
350                                         waypoint.set_value((*value_node)(old_time));
351                                 }
352                                 action->set_param("waypoint",waypoint);
353                 
354                                 assert(action->is_ready());
355                                 if(!action->is_ready())
356                                         throw Error(Error::TYPE_NOTREADY);
357                         
358                                 add_action_front(action);
359                         }
360                 }
361         }
362 }
363
364
365 void
366 Action::KeyframeSet::perform()
367 {
368
369         old_time=get_canvas()->keyframe_list().find(keyframe)->get_time();
370         new_time=keyframe.get_time();
371         
372         try { get_canvas()->keyframe_list().find(keyframe);}
373         catch(synfig::Exception::NotFound)
374         {
375                 throw Error(_("Unable to find the given keyframe"));
376         }       
377         
378         // Check for colisions
379         if(old_time!=new_time)
380         {
381                 try {
382                         get_canvas()->keyframe_list().find(new_time);
383                         throw Error(_("Cannot change keyframe time because another keyframe already exists with that time."));
384                 }
385                 catch(Exception::NotFound) { }
386         }
387         try { keyframe_next=get_canvas()->keyframe_list().find_next(old_time)->get_time(); }
388         catch(...) { keyframe_next=Time::end(); }
389         try { keyframe_prev=get_canvas()->keyframe_list().find_prev(old_time)->get_time(); }
390         catch(...) { keyframe_prev=Time::begin(); }
391
392         old_keyframe=*get_canvas()->keyframe_list().find(keyframe);
393         *get_canvas()->keyframe_list().find(keyframe)=keyframe;
394         
395         get_canvas()->keyframe_list().sync();
396
397         try{
398                 Action::Super::perform();
399         } catch(...)
400         {
401                 *get_canvas()->keyframe_list().find(old_keyframe)=old_keyframe;
402
403                 get_canvas()->keyframe_list().sync();
404                 throw;
405         }
406         
407         // Signal that a layer has been inserted
408         if(get_canvas_interface())
409         {
410                 get_canvas_interface()->signal_keyframe_changed()(keyframe);
411         }
412         else synfig::warning("CanvasInterface not set on action");
413 }
414
415 void
416 Action::KeyframeSet::undo()
417 {
418         Action::Super::undo();
419
420         *get_canvas()->keyframe_list().find(old_keyframe)=old_keyframe;
421
422         get_canvas()->keyframe_list().sync();
423         
424         // Signal that a layer has been inserted
425         if(get_canvas_interface())
426         {
427                 get_canvas_interface()->signal_keyframe_changed()(keyframe);
428         }
429         else synfig::warning("CanvasInterface not set on action");
430 }