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