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