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