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_subtract.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_subtract.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_subtract.h"
35 #include "valuenode_const.h"
36 #include <stdexcept>
37 #include "color.h"
38 #include "vector.h"
39 #include "angle.h"
40 #include "real.h"
41 #include <ETL/misc>
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 synfig::ValueNode_Subtract::ValueNode_Subtract(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("lhs",ValueNode_Const::create(value.get(Angle())));
69                 set_link("rhs",ValueNode_Const::create(Angle::deg(0)));
70                 break;
71         case ValueBase::TYPE_COLOR:
72                 set_link("lhs",ValueNode_Const::create(value.get(Color())));
73                 set_link("rhs",ValueNode_Const::create(Color(0,0,0,0)));
74                 break;
75         case ValueBase::TYPE_INTEGER:
76                 set_link("lhs",ValueNode_Const::create(value.get(int())));
77                 set_link("rhs",ValueNode_Const::create(int(0)));
78                 break;
79         case ValueBase::TYPE_REAL:
80                 set_link("lhs",ValueNode_Const::create(value.get(Real())));
81                 set_link("rhs",ValueNode_Const::create(Real(0)));
82                 break;
83         case ValueBase::TYPE_TIME:
84                 set_link("lhs",ValueNode_Const::create(value.get(Time())));
85                 set_link("rhs",ValueNode_Const::create(Time(0)));
86                 break;
87         case ValueBase::TYPE_VECTOR:
88                 set_link("lhs",ValueNode_Const::create(value.get(Vector())));
89                 set_link("rhs",ValueNode_Const::create(Vector(0,0)));
90                 break;
91         default:
92                 assert(0);
93                 throw runtime_error("synfig::ValueNode_Subtract:Bad type "+ValueBase::type_name(id));
94         }
95
96         assert(get_lhs()->get_type()==id);
97         assert(get_rhs()->get_type()==id);
98         assert(get_type()==id);
99
100         DCAST_HACK_ENABLE();
101 }
102
103 LinkableValueNode*
104 ValueNode_Subtract::create_new()const
105 {
106         return new ValueNode_Subtract(get_type());
107 }
108
109 ValueNode_Subtract*
110 ValueNode_Subtract::create(const ValueBase& value)
111 {
112         return new ValueNode_Subtract(value);
113 }
114
115 synfig::ValueNode_Subtract::~ValueNode_Subtract()
116 {
117         unlink_all();
118 }
119
120 void
121 ValueNode_Subtract::set_scalar(Real value)
122 {
123         set_link("scalar",ValueNode_Const::create(value));
124 }
125
126 bool
127 synfig::ValueNode_Subtract::set_scalar(ValueNode::Handle value)
128 {
129         if(value->get_type()!=ValueBase::TYPE_REAL&& !PlaceholderValueNode::Handle::cast_dynamic(value))
130                 return false;
131         scalar=value;
132         return true;
133 }
134
135 bool
136 synfig::ValueNode_Subtract::set_lhs(ValueNode::Handle x)
137 {
138         assert(get_type());
139
140         if(!x ||
141            (get_type()==ValueBase::TYPE_NIL && !check_type(x->get_type())) ||
142            (get_type()!=ValueBase::TYPE_NIL && x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x)))
143                 return false;
144
145         ref_a=x;
146
147         return true;
148 }
149
150 bool
151 synfig::ValueNode_Subtract::set_rhs(ValueNode::Handle x)
152 {
153         assert(get_type());
154
155         if(!x ||
156            (get_type()==ValueBase::TYPE_NIL && !check_type(x->get_type())) ||
157            (get_type()!=ValueBase::TYPE_NIL && x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x)))
158                 return false;
159
160         ref_b=x;
161
162         return true;
163 }
164
165 synfig::ValueBase
166 synfig::ValueNode_Subtract::operator()(Time t)const
167 {
168         if(!ref_a || !ref_b)
169                 throw runtime_error(strprintf("ValueNode_Subtract: %s",_("One or both of my parameters aren't set!")));
170         switch(get_type())
171         {
172         case ValueBase::TYPE_ANGLE:
173                 return ((*ref_a)(t).get(Angle())-(*ref_b)(t).get(Angle()))*(*scalar)(t).get(Real());
174         case ValueBase::TYPE_COLOR:
175                 return ((*ref_a)(t).get(Color())-(*ref_b)(t).get(Color()))*(*scalar)(t).get(Real());
176         case ValueBase::TYPE_INTEGER:
177                 return round_to_int(((*ref_a)(t).get(int())-(*ref_b)(t).get(int()))*(*scalar)(t).get(Real()));
178         case ValueBase::TYPE_REAL:
179                 return ((*ref_a)(t).get(Vector::value_type())-(*ref_b)(t).get(Vector::value_type()))*(*scalar)(t).get(Real());
180         case ValueBase::TYPE_TIME:
181                 return ((*ref_a)(t).get(Time())-(*ref_b)(t).get(Time()))*(*scalar)(t).get(Real());
182         case ValueBase::TYPE_VECTOR:
183                 return ((*ref_a)(t).get(Vector())-(*ref_b)(t).get(Vector()))*(*scalar)(t).get(Real());
184         default:
185                 assert(0);
186                 break;
187         }
188         return ValueBase();
189 }
190
191 bool
192 ValueNode_Subtract::set_link_vfunc(int i,ValueNode::Handle value)
193 {
194         assert(i>=0 && i<3);
195         switch(i)
196         {
197                 case 0:
198                         if(set_lhs(value)) { signal_child_changed()(i);signal_value_changed()(); return true; }
199                         return false;
200                 case 1:
201                         if(set_rhs(value)) { signal_child_changed()(i);signal_value_changed()(); return true; }
202                         return false;
203                 case 2:
204                         scalar=value;
205                         signal_child_changed()(i);signal_value_changed()();
206                         return true;
207         }
208
209         return false;
210 }
211
212 ValueNode::LooseHandle
213 ValueNode_Subtract::get_link_vfunc(int i)const
214 {
215         assert(i>=0 && i<3);
216         switch(i)
217         {
218                 case 0: return ref_a;
219                 case 1: return ref_b;
220                 case 2: return scalar;
221                 default: return 0;
222         }
223 }
224
225 int
226 ValueNode_Subtract::link_count()const
227 {
228         return 3;
229 }
230
231 String
232 ValueNode_Subtract::link_local_name(int i)const
233 {
234         assert(i>=0 && i<3);
235         switch(i)
236         {
237                 case 0: return _("LHS");
238                 case 1: return _("RHS");
239                 case 2: return _("Scalar");
240                 default: return String();
241         }
242 }
243
244 String
245 ValueNode_Subtract::link_name(int i)const
246 {
247         assert(i>=0 && i<3);
248         switch(i)
249         {
250                 case 0: return "lhs";
251                 case 1: return "rhs";
252                 case 2: return "scalar";
253                 default: return String();
254         }
255 }
256
257 int
258 ValueNode_Subtract::get_link_index_from_name(const String &name)const
259 {
260         if(name=="lhs") return 0;
261         if(name=="rhs") return 1;
262         if(name=="scalar") return 2;
263         throw Exception::BadLinkName(name);
264 }
265
266 String
267 ValueNode_Subtract::get_name()const
268 {
269         return "subtract";
270 }
271
272 String
273 ValueNode_Subtract::get_local_name()const
274 {
275         return _("Subtract");
276 }
277
278 bool
279 ValueNode_Subtract::check_type(ValueBase::Type type)
280 {
281         return type==ValueBase::TYPE_ANGLE
282                 || type==ValueBase::TYPE_COLOR
283                 || type==ValueBase::TYPE_INTEGER
284                 || type==ValueBase::TYPE_REAL
285                 || type==ValueBase::TYPE_TIME
286                 || type==ValueBase::TYPE_VECTOR;
287 }