Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_05 / synfig-studio / src / synfigapp / actions / activepointsetsmart.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file activepointsetsmart.cpp
3 **      \brief Template File
4 **
5 **      $Id: activepointsetsmart.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 "activepointsetsmart.h"
33 #include "valuenodelinkconnect.h"
34 #include "valuenodereplace.h"
35
36 #include "activepointset.h"
37 #include "activepointadd.h"
38
39 #include "valuedescconnect.h"
40 #include <synfigapp/canvasinterface.h>
41
42 #endif
43
44 using namespace std;
45 using namespace etl;
46 using namespace synfig;
47 using namespace synfigapp;
48 using namespace Action;
49
50 /* === M A C R O S ========================================================= */
51
52 ACTION_INIT(Action::ActivepointSetSmart);
53 ACTION_SET_NAME(Action::ActivepointSetSmart,"activepoint_set_smart");
54 ACTION_SET_LOCAL_NAME(Action::ActivepointSetSmart,_("Set Activepoint (Smart)"));
55 ACTION_SET_TASK(Action::ActivepointSetSmart,"set");
56 ACTION_SET_CATEGORY(Action::ActivepointSetSmart,Action::CATEGORY_ACTIVEPOINT);
57 ACTION_SET_PRIORITY(Action::ActivepointSetSmart,0);
58 ACTION_SET_VERSION(Action::ActivepointSetSmart,"0.0");
59 ACTION_SET_CVS_ID(Action::ActivepointSetSmart,"$Id: activepointsetsmart.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $");
60
61 /* === G L O B A L S ======================================================= */
62
63 /* === P R O C E D U R E S ================================================= */
64
65 /* === M E T H O D S ======================================================= */
66
67 Action::ActivepointSetSmart::ActivepointSetSmart()
68 {
69         activepoint.set_time(Time::begin()-1);
70         time_set=false;
71         set_dirty(true);
72 }
73
74 Action::ParamVocab
75 Action::ActivepointSetSmart::get_param_vocab()
76 {
77         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
78         
79         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
80                 .set_local_name(_("ValueDesc"))
81         );
82
83         ret.push_back(ParamDesc("activepoint",Param::TYPE_ACTIVEPOINT)
84                 .set_local_name(_("New Activepoint"))
85                 .set_desc(_("Activepoint to be added"))
86                 .set_optional()
87         );
88
89         ret.push_back(ParamDesc("time",Param::TYPE_TIME)
90                 .set_local_name(_("Time"))
91                 .set_desc(_("Time where activepoint is to be added"))
92                 .set_optional()
93         );
94         
95         return ret;
96 }
97
98 bool
99 Action::ActivepointSetSmart::is_canidate(const ParamList &x)
100 {
101         if(canidate_check(get_param_vocab(),x))
102         {
103                 ValueDesc value_desc(x.find("value_desc")->second.get_value_desc());
104                 if(!value_desc.parent_is_value_node() || !ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node()))
105                         return false;
106
107                 // We need either a activepoint or a time.
108                 if(x.count("activepoint") || x.count("time"))
109                         return true;
110         }
111         return false;
112 }
113
114 bool
115 Action::ActivepointSetSmart::set_param(const synfig::String& name, const Action::Param &param)
116 {
117         if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
118         {
119                 value_desc=param.get_value_desc();
120                 
121                 if(!value_desc.parent_is_value_node())
122                         return false;
123                 
124                 value_node=ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node());
125                 
126                 if(!value_node)
127                         return false;
128                 
129                 index=value_desc.get_index();
130                 
131                 if(time_set)
132                         calc_activepoint();
133                 
134                 return true;
135         }
136         if(name=="activepoint" && param.get_type()==Param::TYPE_ACTIVEPOINT && !time_set)
137         {
138                 activepoint=param.get_activepoint();
139                 
140                 return true;
141         }
142         if(name=="time" && param.get_type()==Param::TYPE_TIME && activepoint.get_time()==Time::begin()-1)
143         {
144                 activepoint.set_time(param.get_time());
145                 time_set=true;
146
147                 if(value_node)
148                         calc_activepoint();
149                 
150                 return true;
151         }
152
153         return Action::CanvasSpecific::set_param(name,param);
154
155         return Action::CanvasSpecific::set_param(name,param);
156 }
157
158 bool
159 Action::ActivepointSetSmart::is_ready()const
160 {
161         if(!value_node)
162                 synfig::error("Missing value_node");
163
164         if(activepoint.get_time()==(Time::begin()-1))
165                 synfig::error("Missing activepoint");
166         
167         if(!value_node || activepoint.get_time()==(Time::begin()-1))
168                 return false;
169         return Action::CanvasSpecific::is_ready();
170 }
171
172 // This function is called if a time is specified, but not 
173 // a activepoint. In this case, we need to calculate the value
174 // of the activepoint
175 void
176 Action::ActivepointSetSmart::calc_activepoint()
177 {       
178 /*
179         const Time time(activepoint.get_time());
180         activepoint.set_state(value_node->list[index].status_at_time(time));
181         activepoint.set_priority(0);
182 */
183
184         activepoint=value_node->list[index].new_activepoint_at_time(activepoint.get_time());
185
186         // In this case, nothing is really changing, so there will be
187         // no need to redraw the window
188         set_dirty(false);
189 }
190
191 void
192 Action::ActivepointSetSmart::enclose_activepoint(const synfig::Activepoint& activepoint)
193 {
194         times.insert(activepoint.get_time());                   
195
196         if(get_edit_mode()&MODE_ANIMATE_PAST) try
197         {
198                 // Try to find prev keyframe
199                 Keyframe keyframe(*get_canvas()->keyframe_list().find_prev(activepoint.get_time()));
200
201                 if(times.count(keyframe.get_time()))
202                         throw int();
203                 else
204                         times.insert(keyframe.get_time());                      
205                 
206                 try { value_node->list[index].find(keyframe.get_time()); }
207                 catch(synfig::Exception::NotFound)
208                 {
209                         Action::Handle action(ActivepointAdd::create());
210                         
211                         action->set_param("canvas",get_canvas());
212                         action->set_param("canvas_interface",get_canvas_interface());
213                         action->set_param("value_desc",value_desc);
214                 
215                         if(!value_node->list[index].timing_info.empty())
216                         {       
217                                 action->set_param("time",keyframe.get_time());
218                         }
219                         else
220                         {
221                                 synfig::Activepoint tmp;
222                                 
223                                 tmp.set_state(true);
224                                 tmp.set_time(keyframe.get_time());
225                                 action->set_param("activepoint",tmp);
226                         }
227
228                         assert(action->is_ready());
229                         if(!action->is_ready())
230                                 throw Error(Error::TYPE_NOTREADY);
231                 
232                         add_action_front(action);
233                 }                                               
234         }
235         catch(int) { }
236         catch(synfig::Exception::NotFound) { }
237
238         if(get_edit_mode()&MODE_ANIMATE_FUTURE)try
239         {
240                 // Try to find next keyframe
241                 Keyframe keyframe(*get_canvas()->keyframe_list().find_next(activepoint.get_time()));
242
243                 if(times.count(keyframe.get_time()))
244                         throw int();
245                 else
246                         times.insert(keyframe.get_time());                      
247                 
248                 try { value_node->list[index].find(keyframe.get_time()); }
249                 catch(synfig::Exception::NotFound)
250                 {
251                         Action::Handle action(ActivepointAdd::create());
252                         
253                         action->set_param("canvas",get_canvas());
254                         action->set_param("canvas_interface",get_canvas_interface());
255                         action->set_param("value_desc",value_desc);
256                 
257                         if(!value_node->list[index].timing_info.empty())
258                         {       
259                                 action->set_param("time",keyframe.get_time());
260                         }
261                         else
262                         {
263                                 synfig::Activepoint tmp;
264                                 
265                                 tmp.set_state(true);
266                                 tmp.set_time(keyframe.get_time());
267                                 action->set_param("activepoint",tmp);
268                         }
269
270                         assert(action->is_ready());
271                         if(!action->is_ready())
272                                 throw Error(Error::TYPE_NOTREADY);
273                 
274                         add_action_front(action);
275                 }                                               
276         }
277         catch(int) { }
278         catch(synfig::Exception::NotFound) { }
279 }
280
281 void
282 Action::ActivepointSetSmart::prepare()
283 {
284         clear();
285         times.clear();
286         
287         // First, we need to to add any activepoints necessary to
288         // maintain the integrity of the keyframes.
289         enclose_activepoint(activepoint);
290         
291         try
292         {
293                 if(value_node->list[index].find(activepoint)==value_node->list[index].timing_info.end())
294                         throw int();
295                 
296                 // Then, lets try to replace the old activepoint, if it exists
297                 enclose_activepoint(*value_node->list[index].find(activepoint));
298
299                 Action::Handle action(ActivepointSet::create());
300                 
301                 action->set_param("canvas",get_canvas());
302                 action->set_param("canvas_interface",get_canvas_interface());
303                 action->set_param("value_desc",value_desc);
304                 action->set_param("activepoint",activepoint);
305         
306                 assert(action->is_ready());
307                 if(!action->is_ready())
308                         throw Error(Error::TYPE_NOTREADY);
309         
310                 add_action_front(action);
311                 
312                 return;
313         }
314         catch(int){}
315         catch(Exception::NotFound){}
316
317         try
318         {
319                 // Check to see if a activepoint exists at this point in time
320                 activepoint.mimic(*value_node->list[index].find(activepoint.get_time()));
321
322                 enclose_activepoint(*value_node->list[index].find(activepoint.get_time()));
323                 
324                 Action::Handle action(ActivepointSet::create());
325                 
326                 action->set_param("canvas",get_canvas());
327                 action->set_param("canvas_interface",get_canvas_interface());
328                 action->set_param("value_desc",value_desc);
329                 action->set_param("activepoint",activepoint);
330         
331                 assert(action->is_ready());
332                 if(!action->is_ready())
333                         throw Error(Error::TYPE_NOTREADY);
334         
335                 add_action_front(action);
336                 
337                 return;
338         }
339         catch(int){}
340         catch(Exception::NotFound){}
341         
342         try
343         {
344                 // At this point we know that the old activepoint doesn't exist,
345                 // so we need to create it.
346                 Action::Handle action(ActivepointAdd::create());
347                 
348                 action->set_param("canvas",get_canvas());
349                 action->set_param("canvas_interface",get_canvas_interface());
350                 action->set_param("value_desc",value_desc);
351                 action->set_param("activepoint",activepoint);
352         
353                 assert(action->is_ready());
354                 if(!action->is_ready())
355                         throw Error(Error::TYPE_NOTREADY);
356         
357                 add_action_front(action);
358                 
359                 return;
360         }
361         catch(int){}
362         catch(Exception::NotFound){}
363         
364         throw Error(_("Unable to determine how to procede. This is a bug."));
365 }