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