Initialise 'scale' valuenodes when converting, rather than using preset values.
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_scale.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_scale.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 #include "general.h"
33 #include "valuenode_scale.h"
34 #include "valuenode_const.h"
35 #include <stdexcept>
36 #include <cassert>
37 #include "color.h"
38 #include "vector.h"
39 #include "time.h"
40 #include "angle.h"
41
42 #endif
43
44 /* === U S I N G =========================================================== */
45
46 using namespace std;
47 using namespace etl;
48 using namespace synfig;
49
50 /* === M A C R O S ========================================================= */
51
52 /* === G L O B A L S ======================================================= */
53
54 /* === P R O C E D U R E S ================================================= */
55
56 /* === M E T H O D S ======================================================= */
57
58 ValueNode_Scale::ValueNode_Scale(const ValueBase &value):
59         LinkableValueNode(value.get_type())
60 {
61         set_scalar(1.0);
62         ValueBase::Type id(value.get_type());
63
64         switch(id)
65         {
66         case ValueBase::TYPE_ANGLE:
67                 set_link("link",ValueNode_Const::create(value.get(Angle())));
68                 break;
69         case ValueBase::TYPE_COLOR:
70                 set_link("link",ValueNode_Const::create(value.get(Color())));
71                 break;
72         case ValueBase::TYPE_INTEGER:
73                 set_link("link",ValueNode_Const::create(value.get(int())));
74                 break;
75         case ValueBase::TYPE_REAL:
76                 set_link("link",ValueNode_Const::create(value.get(Real())));
77                 break;
78         case ValueBase::TYPE_TIME:
79                 set_link("link",ValueNode_Const::create(value.get(Time())));
80                 break;
81         case ValueBase::TYPE_VECTOR:
82                 set_link("link",ValueNode_Const::create(value.get(Vector())));
83                 break;
84         default:
85                 assert(0);
86                 throw runtime_error("synfig::ValueNode_Scale:Bad type "+ValueBase::type_name(value.get_type()));
87         }
88
89         assert(value_node);
90         assert(get_value_node()->get_type()==id);
91         assert(get_type()==id);
92 }
93
94 LinkableValueNode*
95 ValueNode_Scale::create_new()const
96 {
97         return new ValueNode_Scale(get_type());
98 }
99
100 ValueNode_Scale*
101 ValueNode_Scale::create(const ValueBase& value)
102 {
103         return new ValueNode_Scale(value);
104 }
105
106 synfig::ValueNode_Scale::~ValueNode_Scale()
107 {
108         unlink_all();
109 }
110
111 void
112 ValueNode_Scale::set_scalar(Real x)
113 {
114         set_link("scalar",ValueNode::Handle(ValueNode_Const::create(x)));
115 }
116
117 bool
118 ValueNode_Scale::set_scalar(const ValueNode::Handle &x)
119 {
120         if(!x
121                 || x->get_type()!=ValueBase::TYPE_REAL
122                 && !PlaceholderValueNode::Handle::cast_dynamic(x)
123         )
124                 return false;
125         scalar=x;
126         return true;
127 }
128
129 ValueNode::Handle
130 ValueNode_Scale::get_scalar()const
131 {
132         return scalar;
133 }
134
135 bool
136 ValueNode_Scale::set_value_node(const ValueNode::Handle &x)
137 {
138         assert(get_type());
139
140         // if this isn't a proper value
141         if(!x ||
142            // or we don't have a type, and this value isn't one of the types we accept
143            (get_type()==ValueBase::TYPE_NIL && !check_type(x->get_type())) ||
144            // or we have a type and this value is a different type and (placeholder?)
145            (get_type()!=ValueBase::TYPE_NIL && x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x)))
146                 // then fail to set the value
147                 return false;
148
149         value_node=x;
150
151         return true;
152 }
153
154 ValueNode::Handle
155 ValueNode_Scale::get_value_node()const
156 {
157         return value_node;
158 }
159
160
161 synfig::ValueBase
162 synfig::ValueNode_Scale::operator()(Time t)const
163 {
164         if(!value_node || !scalar)
165                 throw runtime_error(strprintf("ValueNode_Scale: %s",_("One or both of my parameters aren't set!")));
166         else
167         if(get_type()==ValueBase::TYPE_VECTOR)
168                 return (*value_node)(t).get(Vector())*(*scalar)(t).get(Real());
169         else
170         if(get_type()==ValueBase::TYPE_REAL)
171                 return (*value_node)(t).get(Real())*(*scalar)(t).get(Real());
172         else
173         if(get_type()==ValueBase::TYPE_TIME)
174                 return (*value_node)(t).get(Time())*(*scalar)(t).get(Time());
175         else
176         if(get_type()==ValueBase::TYPE_INTEGER)
177                 return (*value_node)(t).get(int())*(*scalar)(t).get(Real());
178         else
179         if(get_type()==ValueBase::TYPE_ANGLE)
180                 return (*value_node)(t).get(Angle())*(*scalar)(t).get(Real());
181         else
182         if(get_type()==ValueBase::TYPE_COLOR)
183         {
184                 Color ret((*value_node)(t).get(Color()));
185                 Real s((*scalar)(t).get(Real()));
186                 ret.set_r(ret.get_r()*s);
187                 ret.set_g(ret.get_g()*s);
188                 ret.set_b(ret.get_b()*s);
189                 return ret;
190         }
191
192         assert(0);
193         return ValueBase();
194 }
195
196
197 bool
198 ValueNode_Scale::set_link_vfunc(int i,ValueNode::Handle x)
199 {
200         if(!(i==0 || i==1))
201                 return false;
202
203         if(i==0 && !set_value_node(x))
204                 return false;
205         else
206         if(i==1 && !set_scalar(x))
207                 return false;
208
209         signal_child_changed()(i);signal_value_changed()();
210
211         return true;
212 }
213
214 ValueNode::LooseHandle
215 ValueNode_Scale::get_link_vfunc(int i)const
216 {
217         assert(i==0 || i==1);
218         if(i==0)
219                 return value_node;
220         else if(i==1)
221                 return scalar;
222         return 0;
223 }
224
225 int
226 ValueNode_Scale::link_count()const
227 {
228         return 2;
229 }
230
231 String
232 ValueNode_Scale::link_local_name(int i)const
233 {
234         assert(i==0 || i==1);
235         if(i==0)
236                 return _("Link");
237         else if(i==1)
238                 return _("Scalar");
239         return String();
240 }
241
242 String
243 ValueNode_Scale::link_name(int i)const
244 {
245         assert(i==0 || i==1);
246         if(i==0)
247                 return "link";
248         else if(i==1)
249                 return "scalar";
250         return String();
251 }
252
253 int
254 ValueNode_Scale::get_link_index_from_name(const String &name)const
255 {
256         if(name=="link")
257                 return 0;
258         if(name=="scalar")
259                 return 1;
260
261         throw Exception::BadLinkName(name);
262 }
263
264 String
265 ValueNode_Scale::get_name()const
266 {
267         return "scale";
268 }
269
270 String
271 ValueNode_Scale::get_local_name()const
272 {
273         return _("Scale");
274 }
275
276 bool
277 ValueNode_Scale::check_type(ValueBase::Type type)
278 {
279         return
280                 type==ValueBase::TYPE_VECTOR ||
281                 type==ValueBase::TYPE_REAL ||
282                 type==ValueBase::TYPE_INTEGER ||
283                 type==ValueBase::TYPE_COLOR ||
284                 type==ValueBase::TYPE_ANGLE;
285 }