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