Add an option "Browser Command" to allow the user to specify which web browser to...
[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,"value_node_dynamic_list_insert_smart");
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         {
97                 ValueDesc value_desc(x.find("value_desc")->second.get_value_desc());
98                 if(!value_desc.parent_is_value_node() || !ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node()))
99                         return false;
100
101                 return true;
102         }
103         return false;
104 }
105
106 bool
107 Action::ValueNodeDynamicListInsertSmart::set_param(const synfig::String& name, const Action::Param &param)
108 {
109         if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
110         {
111                 ValueDesc value_desc(param.get_value_desc());
112
113                 if(!value_desc.parent_is_value_node())
114                         return false;
115
116                 value_node=ValueNode_DynamicList::Handle::cast_dynamic(value_desc.get_parent_value_node());
117
118                 if(!value_node)
119                         return false;
120
121                 index=value_desc.get_index();
122
123                 return true;
124         }
125         if(name=="time" && param.get_type()==Param::TYPE_TIME)
126         {
127                 time=param.get_time();
128
129                 return true;
130         }
131         if(name=="origin" && param.get_type()==Param::TYPE_REAL)
132         {
133                 origin=param.get_real();
134
135                 return true;
136         }
137
138         return Action::CanvasSpecific::set_param(name,param);
139 }
140
141 bool
142 Action::ValueNodeDynamicListInsertSmart::is_ready()const
143 {
144         if(!value_node)
145                 return false;
146         return Action::CanvasSpecific::is_ready();
147 }
148
149 void
150 Action::ValueNodeDynamicListInsertSmart::prepare()
151 {
152         //clear();
153         // HACK
154         if(!first_time())
155                 return;
156
157         // If we are in animate editing mode
158         if(get_edit_mode()&MODE_ANIMATE)
159         {
160                 int index(ValueNodeDynamicListInsertSmart::index);
161
162                 // In this case we need to first determine if there is
163                 // a currently disabled item in the list that we can
164                 // turn on. If not, then we need to go ahead and create one.
165                 synfig::info("ValueNodeDynamicListInsertSmart: index=%d",index);
166                 synfig::info("ValueNodeDynamicListInsertSmart: value_node->list.size()=%d",value_node->list.size());
167                 if(int(value_node->list.size())<=index && index>0)
168                         synfig::info("ValueNodeDynamicListInsertSmart: value_node->list[index-1].status_at_time(time)=%d",value_node->list[index-1].status_at_time(time));
169
170                 if(int(value_node->list.size())>=index && index>0 && !value_node->list[index-1].status_at_time(time))
171                 {
172                         // Ok, we do not have to create a new
173                         // entry in the dynamic list after all.
174                         // However, we do need to set the
175                         // position and tangent of this point.
176                         ValueNode_DynamicList::ListEntry list_entry(value_node->create_list_entry(index,time,origin));
177                         ValueBase value((*list_entry.value_node)(time));
178                         index--;
179
180                         ValueDesc item_value_desc(value_node,index);
181
182                         Action::Handle action(Action::create("value_desc_set"));
183
184                         if(!action)
185                                 throw Error(_("Unable to find action value_desc_set (bug)"));
186
187                         action->set_param("edit_mode",get_edit_mode());
188                         action->set_param("canvas",get_canvas());
189                         action->set_param("canvas_interface",get_canvas_interface());
190                         action->set_param("time",time);
191                         action->set_param("new_value",value);
192                         action->set_param("value_desc",ValueDesc(value_node,index));
193
194                         if(!action->is_ready())
195                                 throw Error(Error::TYPE_NOTREADY);
196
197                         add_action(action);
198                 }
199                 else
200                 {
201                         // Ok, not a big deal, we just need to
202                         // add a new item
203                         Action::Handle action(Action::create("value_node_dynamic_list_insert"));
204
205                         if(!action)
206                                 throw Error(_("Unable to find action (bug)"));
207
208                         action->set_param("canvas",get_canvas());
209                         action->set_param("canvas_interface",get_canvas_interface());
210                         action->set_param("time",time);
211                         action->set_param("origin",origin);
212                         action->set_param("value_desc",ValueDesc(value_node,index));
213
214                         if(!action->is_ready())
215                                 throw Error(Error::TYPE_NOTREADY);
216
217                         add_action(action);
218
219                         action=Action::create("activepoint_set_off");
220
221                         if(!action)
222                                 throw Error(_("Unable to find action \"activepoint_set_off\""));
223
224                         action->set_param("edit_mode",MODE_ANIMATE);
225                         action->set_param("canvas",get_canvas());
226                         action->set_param("canvas_interface",get_canvas_interface());
227                         action->set_param("time",Time::begin());
228                         action->set_param("origin",origin);
229                         action->set_param("value_desc",ValueDesc(value_node,index));
230
231                         if(!action->is_ready())
232                                 throw Error(Error::TYPE_NOTREADY);
233
234                         add_action(action);
235                 }
236
237                 // Now we set the activepoint up and then we'll be done
238                 Action::Handle action(Action::create("activepoint_set_on"));
239
240                 if(!action)
241                         throw Error(_("Unable to find action \"activepoint_set_on\""));
242
243                 action->set_param("edit_mode",get_edit_mode());
244                 action->set_param("canvas",get_canvas());
245                 action->set_param("canvas_interface",get_canvas_interface());
246                 action->set_param("time",time);
247                 action->set_param("origin",origin);
248                 action->set_param("value_desc",ValueDesc(value_node,index));
249
250                 if(!action->is_ready())
251                         throw Error(Error::TYPE_NOTREADY);
252
253                 add_action(action);
254         }
255         else
256         {
257                 Action::Handle action(Action::create("value_node_dynamic_list_insert"));
258
259                 if(!action)
260                         throw Error(_("Unable to find action (bug)"));
261
262                 action->set_param("canvas",get_canvas());
263                 action->set_param("canvas_interface",get_canvas_interface());
264                 action->set_param("time",time);
265                 action->set_param("origin",origin);
266                 action->set_param("value_desc",ValueDesc(value_node,index));
267
268                 if(!action->is_ready())
269                         throw Error(Error::TYPE_NOTREADY);
270
271                 add_action(action);
272         }
273 }