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