Quote the "Working Draft, Standard for Programming Language C++" on a point I wasn...
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / keyframesetdelta.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file keyframesetdelta.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 "keyframesetdelta.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::KeyframeSetDelta);
50 ACTION_SET_NAME(Action::KeyframeSetDelta,"keyframe_set_delta");
51 ACTION_SET_LOCAL_NAME(Action::KeyframeSetDelta,"Set Keyframe Delta");
52 ACTION_SET_TASK(Action::KeyframeSetDelta,"set");
53 ACTION_SET_CATEGORY(Action::KeyframeSetDelta,Action::CATEGORY_KEYFRAME|Action::CATEGORY_HIDDEN);
54 ACTION_SET_PRIORITY(Action::KeyframeSetDelta,0);
55 ACTION_SET_VERSION(Action::KeyframeSetDelta,"0.0");
56 ACTION_SET_CVS_ID(Action::KeyframeSetDelta,"$Id$");
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::KeyframeSetDelta::KeyframeSetDelta():
65         delta(0)
66 {
67         keyframe.set_time(Time::end());
68         set_dirty(false);
69 }
70
71 Action::ParamVocab
72 Action::KeyframeSetDelta::get_param_vocab()
73 {
74         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
75
76         ret.push_back(ParamDesc("keyframe",Param::TYPE_KEYFRAME)
77                 .set_local_name(_("Keyframe"))
78         );
79         ret.push_back(ParamDesc("delta",Param::TYPE_KEYFRAME)
80                 .set_local_name(_("Delta"))
81         );
82
83         return ret;
84 }
85
86 bool
87 Action::KeyframeSetDelta::is_candidate(const ParamList &x)
88 {
89         return candidate_check(get_param_vocab(),x);
90 }
91
92 bool
93 Action::KeyframeSetDelta::set_param(const synfig::String& name, const Action::Param &param)
94 {
95         if(name=="keyframe" && param.get_type()==Param::TYPE_KEYFRAME)
96         {
97                 keyframe=param.get_keyframe();
98                 return true;
99         }
100         if(name=="delta" && param.get_type()==Param::TYPE_TIME)
101         {
102                 delta=param.get_time();
103                 return true;
104         }
105
106         return Action::CanvasSpecific::set_param(name,param);
107 }
108
109 bool
110 Action::KeyframeSetDelta::is_ready()const
111 {
112         if(keyframe.get_time()==Time::end())
113                 return false;
114         return Action::CanvasSpecific::is_ready();
115 }
116
117 void
118 Action::KeyframeSetDelta::prepare()
119 {
120         clear();
121         value_desc_list.clear();
122         get_canvas_interface()->find_important_value_descs(value_desc_list);
123
124
125         Time time(get_canvas()->keyframe_list().find(keyframe)->get_time());
126
127         std::vector<synfigapp::ValueDesc>::iterator iter;
128                 DEBUGPOINT();
129         for(iter=value_desc_list.begin();iter!=value_desc_list.end();++iter)
130         {
131                 ValueDesc& value_desc(*iter);
132                 ValueNode_Animated::Handle value_node(
133                         ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node())
134                 );
135
136                 if(!value_node)
137                         continue;
138
139                 try{
140                         value_node->find(time);
141                         // if we got to this point, then we know that
142                         // a waypoint already exists here and we don't
143                         // need to add a new one.
144                         continue;
145                 }catch(...)
146                 {
147                         // Make sure there is something previous
148                         try{
149                                 value_node->find_prev(time);
150                         }catch(...)
151                         {
152                                 continue;
153                         }
154                 }
155                 Action::Handle action(Action::create("waypoint_set_smart"));
156
157                 action->set_param("canvas",get_canvas());
158                 action->set_param("canvas_interface",get_canvas_interface());
159                 action->set_param("value_node",ValueNode::Handle::cast_static(value_node));
160
161                 action->set_param("time",time);
162
163                 assert(action->is_ready());
164                 if(!action->is_ready())
165                         throw Error(Error::TYPE_NOTREADY);
166
167                 add_action(action);
168         }
169 }
170
171 void
172 Action::KeyframeSetDelta::perform()
173 {
174         if(!delta)
175                 return;
176         Action::Super::perform();
177
178 //      Time location(keyframe.get_time());
179         Time location(get_canvas()->keyframe_list().find(keyframe)->get_time());
180         Time delta(delta);
181
182         get_canvas()->keyframe_list().insert_time(location,delta);
183
184         std::vector<synfigapp::ValueDesc>::iterator iter;
185         for(iter=value_desc_list.begin();iter!=value_desc_list.end();++iter)
186         {
187                 ValueDesc& value_desc(*iter);
188                 if(!value_desc.is_value_node())
189                         continue;
190                 ValueNode_Animated::Handle animated(
191                         ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node())
192                 );
193                 if(animated)
194                 {
195                         animated->insert_time(location,delta);
196                         continue;
197                 }
198                 ValueNode_DynamicList::Handle dyn_list(
199                         ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_value_node())
200                 );
201                 if(dyn_list)
202                 {
203                         dyn_list->insert_time(location,delta);
204                         continue;
205                 }
206         }
207
208         // Signal that something has changed
209         if(get_canvas_interface())
210         {
211                 get_canvas_interface()->signal_keyframe_changed()(keyframe);
212         }
213         else synfig::warning("CanvasInterface not set on action");
214 }
215
216 void
217 Action::KeyframeSetDelta::undo()
218 {
219         if(!delta)
220                 return;
221
222 //      Time location(keyframe.get_time());
223         Time location(get_canvas()->keyframe_list().find(keyframe)->get_time());
224         Time delta(-delta);
225
226         get_canvas()->keyframe_list().insert_time(location,delta);
227
228         std::vector<synfigapp::ValueDesc>::iterator iter;
229         for(iter=value_desc_list.begin();iter!=value_desc_list.end();++iter)
230         {
231                 ValueDesc& value_desc(*iter);
232                 if(!value_desc.is_value_node())
233                         continue;
234                 ValueNode_Animated::Handle animated(
235                         ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node())
236                 );
237                 if(animated)
238                 {
239                         animated->insert_time(location,delta);
240                         continue;
241                 }
242                 ValueNode_DynamicList::Handle dyn_list(
243                         ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_value_node())
244                 );
245                 if(dyn_list)
246                 {
247                         dyn_list->insert_time(location,delta);
248                         continue;
249                 }
250         }
251
252         Action::Super::undo();
253
254         // Signal that something has changed
255         if(get_canvas_interface())
256         {
257                 get_canvas_interface()->signal_keyframe_changed()(keyframe);
258         }
259         else synfig::warning("CanvasInterface not set on action");
260 }