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