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