Use the current value of a parameter when converting it, rather than its value at...
[synfig.git] / synfig-studio / trunk / src / synfigapp / actions / valuedescconvert.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuedescconvert.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 /*
33 #include "valuenodereplace.h"
34 #include "layerparamconnect.h"
35 #include "valuenodelinkconnect.h"
36 */
37
38 #include "valuedescconnect.h"
39
40 #include "valuedescconvert.h"
41
42 #include <synfigapp/canvasinterface.h>
43 #include <synfig/valuenode_const.h>
44
45 #include <synfigapp/general.h>
46
47 #endif
48
49 using namespace std;
50 using namespace etl;
51 using namespace synfig;
52 using namespace synfigapp;
53 using namespace Action;
54
55 /* === M A C R O S ========================================================= */
56
57 ACTION_INIT(Action::ValueDescConvert);
58 ACTION_SET_NAME(Action::ValueDescConvert,"value_desc_convert");
59 ACTION_SET_LOCAL_NAME(Action::ValueDescConvert,N_("Convert"));
60 ACTION_SET_TASK(Action::ValueDescConvert,"convert");
61 ACTION_SET_CATEGORY(Action::ValueDescConvert,Action::CATEGORY_VALUEDESC);
62 ACTION_SET_PRIORITY(Action::ValueDescConvert,0);
63 ACTION_SET_VERSION(Action::ValueDescConvert,"0.0");
64 ACTION_SET_CVS_ID(Action::ValueDescConvert,"$Id$");
65
66 /* === G L O B A L S ======================================================= */
67
68 /* === P R O C E D U R E S ================================================= */
69
70 /* === M E T H O D S ======================================================= */
71
72 Action::ValueDescConvert::ValueDescConvert()
73 {
74         time=(Time::begin()-1);
75 }
76
77 Action::ParamVocab
78 Action::ValueDescConvert::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("type",Param::TYPE_STRING)
87                 .set_local_name(_("Type"))
88                 .set_desc(_("The type of ValueNode that you want to be converted to"))
89         );
90
91         ret.push_back(ParamDesc("time",Param::TYPE_TIME)
92                 .set_local_name(_("Time"))
93         );
94
95         return ret;
96 }
97
98 bool
99 Action::ValueDescConvert::is_candidate(const ParamList &x)
100 {
101         return candidate_check(get_param_vocab(),x);
102 }
103
104 bool
105 Action::ValueDescConvert::set_param(const synfig::String& name, const Action::Param &param)
106 {
107         if(name=="value_desc" && param.get_type()==Param::TYPE_VALUEDESC)
108         {
109                 value_desc=param.get_value_desc();
110
111                 return true;
112         }
113
114         if(name=="type" && param.get_type()==Param::TYPE_STRING)
115         {
116                 type=param.get_string();
117
118                 return true;
119         }
120
121         if(name=="time" && param.get_type()==Param::TYPE_TIME)
122         {
123                 time=param.get_time();
124
125                 return true;
126         }
127
128         return Action::CanvasSpecific::set_param(name,param);
129 }
130
131 bool
132 Action::ValueDescConvert::is_ready()const
133 {
134         if(!value_desc || type.empty())
135                 return false;
136         if(time==(Time::begin()-1))
137         {
138                 synfig::error("Missing time");
139                 return false;
140         }
141         return Action::CanvasSpecific::is_ready();
142 }
143
144 void
145 Action::ValueDescConvert::prepare()
146 {
147         clear();
148
149         ValueBase value;
150
151         if(value_desc.is_const())
152                 value=value_desc.get_value();
153         else if(value_desc.is_value_node())
154                 value=(*value_desc.get_value_node())(time);
155         else
156                 throw Error(_("Unable to decipher ValueDesc (Bug?)"));
157
158         ValueNode::Handle src_value_node(LinkableValueNode::create(type,value));
159
160         if(!src_value_node)
161                 throw Error(_("Unable to create new value node"));
162
163
164         ValueNode::Handle dest_value_node;
165         dest_value_node=value_desc.get_value_node();
166
167         Action::Handle action(ValueDescConnect::create());
168
169         action->set_param("canvas",get_canvas());
170         action->set_param("canvas_interface",get_canvas_interface());
171         action->set_param("src",src_value_node);
172         action->set_param("dest",value_desc);
173
174         assert(action->is_ready());
175         if(!action->is_ready())
176                 throw Error(Error::TYPE_NOTREADY);
177
178         add_action_front(action);
179
180 /*
181         return;
182
183
184         if(value_desc.parent_is_canvas())
185         {
186                 ValueNode::Handle dest_value_node;
187                 dest_value_node=value_desc.get_value_node();
188
189                 Action::Handle action(ValueNodeReplace::create());
190
191                 action->set_param("canvas",get_canvas());
192                 action->set_param("canvas_interface",get_canvas_interface());
193                 action->set_param("src",src_value_node);
194                 action->set_param("dest",dest_value_node);
195
196                 assert(action->is_ready());
197                 if(!action->is_ready())
198                         throw Error(Error::TYPE_NOTREADY);
199
200                 add_action_front(action);
201                 return;
202         }
203         else
204         if(value_desc.parent_is_linkable_value_node())
205         {
206                 Action::Handle action(ValueNodeLinkConnect::create());
207
208                 action->set_param("canvas",get_canvas());
209                 action->set_param("canvas_interface",get_canvas_interface());
210                 action->set_param("parent_value_node",value_desc.get_parent_value_node());
211                 action->set_param("value_node", src_value_node);
212                 action->set_param("index",value_desc.get_index());
213
214                 assert(action->is_ready());
215                 if(!action->is_ready())
216                         throw Error(Error::TYPE_NOTREADY);
217
218                 add_action_front(action);
219                 return;
220         }
221         else
222         if(value_desc.parent_is_layer_param())
223         {
224                 Action::Handle action(LayerParamConnect::create());
225
226                 action->set_param("canvas",get_canvas());
227                 action->set_param("canvas_interface",get_canvas_interface());
228                 action->set_param("layer",value_desc.get_layer());
229                 action->set_param("param",value_desc.get_param_name());
230                 action->set_param("value_node",src_value_node);
231
232                 assert(action->is_ready());
233                 if(!action->is_ready())
234                         throw Error(Error::TYPE_NOTREADY);
235
236                 add_action_front(action);
237                 return;
238         }
239
240
241
242         throw Error(_("ValueDesc is not recognized or supported."));
243 */
244 }