Typo: 'totaly' -> 'totally'.
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / valuenodedynamiclistinsertsmart.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenodedynamiclistinsertsmart.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) 2007 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 "valuenodedynamiclistinsertsmart.h"
34 #include "valuenodedynamiclistinsert.h"
35 #include <synfigapp/canvasinterface.h>
36
37 #endif
38
39 using namespace std;
40 using namespace etl;
41 using namespace synfig;
42 using namespace synfigapp;
43 using namespace Action;
44
45 /* === M A C R O S ========================================================= */
46
47 ACTION_INIT(Action::ValueNodeDynamicListInsertSmart);
48 ACTION_SET_NAME(Action::ValueNodeDynamicListInsertSmart,"value_node_dynamic_list_insert_smart");
49 ACTION_SET_LOCAL_NAME(Action::ValueNodeDynamicListInsertSmart,"Insert Item (Smart)");
50 ACTION_SET_TASK(Action::ValueNodeDynamicListInsertSmart,"insert");
51 ACTION_SET_CATEGORY(Action::ValueNodeDynamicListInsertSmart,Action::CATEGORY_VALUEDESC|Action::CATEGORY_VALUENODE);
52 ACTION_SET_PRIORITY(Action::ValueNodeDynamicListInsertSmart,-20);
53 ACTION_SET_VERSION(Action::ValueNodeDynamicListInsertSmart,"0.0");
54 ACTION_SET_CVS_ID(Action::ValueNodeDynamicListInsertSmart,"$Id$");
55
56 /* === G L O B A L S ======================================================= */
57
58 /* === P R O C E D U R E S ================================================= */
59
60 /* === M E T H O D S ======================================================= */
61
62 Action::ValueNodeDynamicListInsertSmart::ValueNodeDynamicListInsertSmart()
63 {
64         index=0;
65         time=0;
66         origin=0.5f;
67         set_dirty(true);
68 }
69
70 Action::ParamVocab
71 Action::ValueNodeDynamicListInsertSmart::get_param_vocab()
72 {
73         ParamVocab ret(Action::CanvasSpecific::get_param_vocab());
74
75         ret.push_back(ParamDesc("value_desc",Param::TYPE_VALUEDESC)
76                 .set_local_name(_("ValueDesc"))
77         );
78         ret.push_back(ParamDesc("time",Param::TYPE_TIME)
79                 .set_local_name(_("Time"))
80                 .set_optional()
81         );
82         ret.push_back(ParamDesc("origin",Param::TYPE_REAL)
83                 .set_local_name(_("Origin"))
84                 .set_optional()
85         );
86
87         return ret;
88 }
89
90 bool
91 Action::ValueNodeDynamicListInsertSmart::is_candidate(const ParamList &x)
92 {
93         if(candidate_check(get_param_vocab(),x))
94         {
95                 ValueDesc value_desc(x.find("value_desc")->second.get_value_desc());
96                 if(!value_desc.parent_is_value_node() || !ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node()))
97                         return false;
98
99                 return true;
100         }
101         return false;
102 }
103
104 bool
105 Action::ValueNodeDynamicListInsertSmart::set_param(const synfig::String& name, const Action::Param &param)
106 {
107         if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
108         {
109                 ValueDesc value_desc(param.get_value_desc());
110
111                 if(!value_desc.parent_is_value_node())
112                         return false;
113
114                 value_node=ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node());
115
116                 if(!value_node)
117                         return false;
118
119                 index=value_desc.get_index();
120
121                 return true;
122         }
123         if(name=="time" && param.get_type()==Param::TYPE_TIME)
124         {
125                 time=param.get_time();
126
127                 return true;
128         }
129         if(name=="origin" && param.get_type()==Param::TYPE_REAL)
130         {
131                 origin=param.get_real();
132
133                 return true;
134         }
135
136         return Action::CanvasSpecific::set_param(name,param);
137 }
138
139 bool
140 Action::ValueNodeDynamicListInsertSmart::is_ready()const
141 {
142         if(!value_node)
143                 return false;
144         return Action::CanvasSpecific::is_ready();
145 }
146
147 void
148 Action::ValueNodeDynamicListInsertSmart::prepare()
149 {
150         //clear();
151         // HACK
152         if(!first_time())
153                 return;
154
155         // If we are in animate editing mode
156         if(get_edit_mode()&MODE_ANIMATE)
157         {
158                 int index(ValueNodeDynamicListInsertSmart::index);
159
160                 // In this case we need to first determine if there is
161                 // a currently disabled item in the list that we can
162                 // turn on. If not, then we need to go ahead and create one.
163                 synfig::info("ValueNodeDynamicListInsertSmart: index=%d",index);
164                 synfig::info("ValueNodeDynamicListInsertSmart: value_node->list.size()=%d",value_node->list.size());
165                 if(int(value_node->list.size())<=index && index>0)
166                         synfig::info("ValueNodeDynamicListInsertSmart: value_node->list[index-1].status_at_time(time)=%d",value_node->list[index-1].status_at_time(time));
167
168                 if(int(value_node->list.size())>=index && index>0 && !value_node->list[index-1].status_at_time(time))
169                 {
170                         // Ok, we do not have to create a new
171                         // entry in the dynamic list after all.
172                         // However, we do need to set the
173                         // position and tangent of this point.
174                         ValueNode_DynamicList::ListEntry list_entry(value_node->create_list_entry(index,time,origin));
175                         ValueBase value((*list_entry.value_node)(time));
176                         index--;
177
178                         ValueDesc item_value_desc(value_node,index);
179
180                         Action::Handle action(Action::create("value_desc_set"));
181
182                         if(!action)
183                                 throw Error(_("Unable to find action value_desc_set (bug)"));
184
185                         action->set_param("edit_mode",get_edit_mode());
186                         action->set_param("canvas",get_canvas());
187                         action->set_param("canvas_interface",get_canvas_interface());
188                         action->set_param("time",time);
189                         action->set_param("new_value",value);
190                         action->set_param("value_desc",ValueDesc(value_node,index));
191
192                         if(!action->is_ready())
193                                 throw Error(Error::TYPE_NOTREADY);
194
195                         add_action(action);
196                 }
197                 else
198                 {
199                         // Ok, not a big deal, we just need to
200                         // add a new item
201                         Action::Handle action(Action::create("value_node_dynamic_list_insert"));
202
203                         if(!action)
204                                 throw Error(_("Unable to find action (bug)"));
205
206                         action->set_param("canvas",get_canvas());
207                         action->set_param("canvas_interface",get_canvas_interface());
208                         action->set_param("time",time);
209                         action->set_param("origin",origin);
210                         action->set_param("value_desc",ValueDesc(value_node,index));
211
212                         if(!action->is_ready())
213                                 throw Error(Error::TYPE_NOTREADY);
214
215                         add_action(action);
216
217                         action=Action::create("activepoint_set_off");
218
219                         if(!action)
220                                 throw Error(_("Unable to find action \"activepoint_set_off\""));
221
222                         action->set_param("edit_mode",MODE_ANIMATE);
223                         action->set_param("canvas",get_canvas());
224                         action->set_param("canvas_interface",get_canvas_interface());
225                         action->set_param("time",Time::begin());
226                         action->set_param("origin",origin);
227                         action->set_param("value_desc",ValueDesc(value_node,index));
228
229                         if(!action->is_ready())
230                                 throw Error(Error::TYPE_NOTREADY);
231
232                         add_action(action);
233                 }
234
235                 // Now we set the activepoint up and then we'll be done
236                 Action::Handle action(Action::create("activepoint_set_on"));
237
238                 if(!action)
239                         throw Error(_("Unable to find action \"activepoint_set_on\""));
240
241                 action->set_param("edit_mode",get_edit_mode());
242                 action->set_param("canvas",get_canvas());
243                 action->set_param("canvas_interface",get_canvas_interface());
244                 action->set_param("time",time);
245                 action->set_param("origin",origin);
246                 action->set_param("value_desc",ValueDesc(value_node,index));
247
248                 if(!action->is_ready())
249                         throw Error(Error::TYPE_NOTREADY);
250
251                 add_action(action);
252         }
253         else
254         {
255                 Action::Handle action(Action::create("value_node_dynamic_list_insert"));
256
257                 if(!action)
258                         throw Error(_("Unable to find action (bug)"));
259
260                 action->set_param("canvas",get_canvas());
261                 action->set_param("canvas_interface",get_canvas_interface());
262                 action->set_param("time",time);
263                 action->set_param("origin",origin);
264                 action->set_param("value_desc",ValueDesc(value_node,index));
265
266                 if(!action->is_ready())
267                         throw Error(Error::TYPE_NOTREADY);
268
269                 add_action(action);
270         }
271 }