my log
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / keyframeduplicate.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file keyframeduplicate.cpp
3 **      \brief Template File
4 **
5 **      $Id: keyframeduplicate.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
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 "keyframeduplicate.h"
32 #include <synfigapp/canvasinterface.h>
33 #include <synfig/valuenode_dynamiclist.h>
34 #include <synfig/valuenode_animated.h>
35 #include "activepointsetsmart.h"
36 #include "waypointsetsmart.h"
37
38 #endif
39
40 using namespace std;
41 using namespace etl;
42 using namespace synfig;
43 using namespace synfigapp;
44 using namespace Action;
45
46 /* === M A C R O S ========================================================= */
47
48 ACTION_INIT(Action::KeyframeDuplicate);
49 ACTION_SET_NAME(Action::KeyframeDuplicate,"keyframe_duplicate");
50 ACTION_SET_LOCAL_NAME(Action::KeyframeDuplicate,"Duplicate Keyframe");
51 ACTION_SET_TASK(Action::KeyframeDuplicate,"duplicate");
52 ACTION_SET_CATEGORY(Action::KeyframeDuplicate,Action::CATEGORY_KEYFRAME);
53 ACTION_SET_PRIORITY(Action::KeyframeDuplicate,0);
54 ACTION_SET_VERSION(Action::KeyframeDuplicate,"0.0");
55 ACTION_SET_CVS_ID(Action::KeyframeDuplicate,"$Id: keyframeduplicate.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $");
56
57 /* === G L O B A L S ======================================================= */
58
59 /* === P R O C E D U R E S ================================================= */
60
61 /* === M E T H O D S ======================================================= */
62
63 Action::KeyframeDuplicate::KeyframeDuplicate()
64 {
65         new_keyframe.set_time(Time::begin()-1);
66         keyframe.set_time(Time::begin()-1);
67         set_dirty(true);
68 }
69
70 Action::ParamVocab
71 Action::KeyframeDuplicate::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(_("Keyframe"))
77                 .set_desc(_("Keyframe to be duplicated"))
78         );
79
80         ret.push_back(ParamDesc("time",Param::TYPE_TIME)
81                 .set_local_name(_("Time"))
82         );
83
84         return ret;
85 }
86
87 bool
88 Action::KeyframeDuplicate::is_canidate(const ParamList &x)
89 {
90         return canidate_check(get_param_vocab(),x);
91 }
92
93 bool
94 Action::KeyframeDuplicate::set_param(const synfig::String& name, const Action::Param &param)
95 {
96         if(name=="keyframe" && param.get_type()==Param::TYPE_KEYFRAME)
97         {
98                 keyframe=param.get_keyframe();
99                 new_keyframe.set_description(keyframe.get_description()+_(" (Duplicate)"));
100                 
101                 return true;
102         }
103         if(name=="time" && param.get_type()==Param::TYPE_TIME)
104         {
105                 new_keyframe.set_time(param.get_time());
106                 
107                 return true;
108         }
109
110         return Action::CanvasSpecific::set_param(name,param);
111 }
112
113 bool
114 Action::KeyframeDuplicate::is_ready()const
115 {
116         if(keyframe.get_time()==(Time::begin()-1) || new_keyframe.get_time()==(Time::begin()-1))
117                 return false;
118         return Action::CanvasSpecific::is_ready();
119 }
120
121 void
122 Action::KeyframeDuplicate::prepare()
123 {
124         clear();
125
126         const synfig::Time old_time=keyframe.get_time();
127         const synfig::Time new_time=new_keyframe.get_time();
128
129         try { get_canvas()->keyframe_list().find(keyframe);}
130         catch(synfig::Exception::NotFound)
131         {
132                 throw Error(_("Unable to find the given keyframe"));
133         }       
134
135         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"));}
136         catch(...) { }                  
137
138         // If the times are different, then we 
139         // will need to romp through the valuenodes
140         // and add actions to update their values.
141         if(new_time!=old_time)
142         {
143                 std::vector<synfigapp::ValueDesc> value_desc_list;
144                 get_canvas_interface()->find_important_value_descs(value_desc_list);
145                 while(!value_desc_list.empty())
146                 {
147                         process_value_desc(value_desc_list.back());
148                         value_desc_list.pop_back();
149                 }
150         }
151 }
152
153 void
154 Action::KeyframeDuplicate::process_value_desc(const synfigapp::ValueDesc& value_desc)
155 {       
156         const synfig::Time old_time=keyframe.get_time();
157         const synfig::Time new_time=new_keyframe.get_time();
158
159         if(value_desc.is_value_node())
160         {
161                 ValueNode::Handle value_node(value_desc.get_value_node());
162         
163                 // If we are a dynamic list, then we need to update the ActivePoints
164                 if(ValueNode_DynamicList::Handle::cast_dynamic(value_node))
165                 {
166                         ValueNode_DynamicList::Handle value_node(ValueNode_DynamicList::Handle::cast_dynamic(value_node));
167                         int i;
168                         
169                         for(i=0;i<value_node->link_count();i++)
170                         {
171                                 synfigapp::ValueDesc value_desc(value_node,i);
172                                 Activepoint activepoint(value_node->list[i].new_activepoint_at_time(old_time));
173                                 activepoint.set_time(new_time);
174
175                                 Action::Handle action(ActivepointSetSmart::create());
176                                 
177                                 action->set_param("canvas",get_canvas());
178                                 action->set_param("canvas_interface",get_canvas_interface());
179                                 action->set_param("value_desc",value_desc);
180                                 action->set_param("activepoint",activepoint);
181
182                                 assert(action->is_ready());
183                                 if(!action->is_ready())
184                                         throw Error(Error::TYPE_NOTREADY);
185                         
186                                 add_action_front(action);                                               
187                         }
188                 }
189                 else if(ValueNode_Animated::Handle::cast_dynamic(value_node))
190                 {
191                         ValueNode_Animated::Handle value_node(ValueNode_Animated::Handle::cast_dynamic(value_node));
192                         Waypoint waypoint(value_node->new_waypoint_at_time(old_time));
193                         waypoint.set_time(new_time);
194                         
195                         Action::Handle action(WaypointSetSmart::create());
196                         
197                         action->set_param("canvas",get_canvas());
198                         action->set_param("canvas_interface",get_canvas_interface());
199                         action->set_param("value_node",ValueNode::Handle(value_node));
200                         action->set_param("waypoint",waypoint);
201         
202                         assert(action->is_ready());
203                         if(!action->is_ready())
204                                 throw Error(Error::TYPE_NOTREADY);
205                 
206                         add_action_front(action);                                               
207                 }
208         }
209 }
210
211 void
212 Action::KeyframeDuplicate::perform()
213 {
214         try { get_canvas()->keyframe_list().find(new_keyframe.get_time()); throw Error(_("A Keyframe already exists at this point in time"));}
215         catch(synfig::Exception::NotFound) { }  
216
217         try { get_canvas()->keyframe_list().find(new_keyframe); throw Error(_("This keyframe is already in the ValueNode"));}
218         catch(synfig::Exception::NotFound) { }  
219
220         Action::Super::perform();
221         
222         get_canvas()->keyframe_list().add(new_keyframe);
223         
224         if(get_canvas_interface())
225         {
226                 get_canvas_interface()->signal_keyframe_added()(new_keyframe);
227         }
228         else synfig::warning("CanvasInterface not set on action");
229 }
230
231 void
232 Action::KeyframeDuplicate::undo()
233 {
234         Action::Super::undo();
235         
236         if(get_canvas_interface())
237         {
238                 get_canvas_interface()->signal_keyframe_removed()(new_keyframe);
239         }
240         else synfig::warning("CanvasInterface not set on action");
241
242         get_canvas()->keyframe_list().erase(new_keyframe);      
243 }