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