Use LinkableValueNode members functions when possible in the derived valuenodes.
[synfig.git] / synfig-core / src / synfig / valuenode_add.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_add.cpp
3 **      \brief Implementation of the "Add" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 2008 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 "gradient.h"
39 #include "vector.h"
40 #include "angle.h"
41 #include "real.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 synfig::ValueNode_Add::ValueNode_Add(const ValueBase &value):
61         LinkableValueNode(value.get_type())
62 {
63         Vocab ret(get_children_vocab());
64         set_children_vocab(ret);
65         set_link("scalar",ValueNode_Const::create(Real(1.0)));
66         ValueBase::Type id(value.get_type());
67
68         switch(id)
69         {
70         case ValueBase::TYPE_ANGLE:
71                 set_link("lhs",ValueNode_Const::create(value.get(Angle())));
72                 set_link("rhs",ValueNode_Const::create(Angle::deg(0)));
73                 break;
74         case ValueBase::TYPE_COLOR:
75                 set_link("lhs",ValueNode_Const::create(value.get(Color())));
76                 set_link("rhs",ValueNode_Const::create(Color(0,0,0,0)));
77                 break;
78         case ValueBase::TYPE_GRADIENT:
79                 set_link("lhs",ValueNode_Const::create(value.get(Gradient())));
80                 set_link("rhs",ValueNode_Const::create(Gradient()));
81                 break;
82         case ValueBase::TYPE_INTEGER:
83                 set_link("lhs",ValueNode_Const::create(value.get(int())));
84                 set_link("rhs",ValueNode_Const::create(int(0)));
85                 break;
86         case ValueBase::TYPE_REAL:
87                 set_link("lhs",ValueNode_Const::create(value.get(Real())));
88                 set_link("rhs",ValueNode_Const::create(Real(0)));
89                 break;
90         case ValueBase::TYPE_TIME:
91                 set_link("lhs",ValueNode_Const::create(value.get(Time())));
92                 set_link("rhs",ValueNode_Const::create(Time(0)));
93                 break;
94         case ValueBase::TYPE_VECTOR:
95                 set_link("lhs",ValueNode_Const::create(value.get(Vector())));
96                 set_link("rhs",ValueNode_Const::create(Vector(0,0)));
97                 break;
98         default:
99                 assert(0);
100                 throw runtime_error(get_local_name()+_(":Bad type ")+ValueBase::type_local_name(id));
101         }
102 }
103
104 LinkableValueNode*
105 ValueNode_Add::create_new()const
106 {
107         return new ValueNode_Add(get_type());
108 }
109
110 ValueNode_Add*
111 ValueNode_Add::create(const ValueBase& value)
112 {
113         return new ValueNode_Add(value);
114 }
115
116 synfig::ValueNode_Add::~ValueNode_Add()
117 {
118         unlink_all();
119 }
120
121 synfig::ValueBase
122 synfig::ValueNode_Add::operator()(Time t)const
123 {
124         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
125                 printf("%s:%d operator()\n", __FILE__, __LINE__);
126
127         if(!ref_a || !ref_b)
128                 throw runtime_error(strprintf("ValueNode_Add: %s",_("One or both of my parameters aren't set!")));
129         switch(get_type())
130         {
131         case ValueBase::TYPE_ANGLE:
132                 return ((*ref_a)(t).get(Angle())+(*ref_b)(t).get(Angle()))*(*scalar)(t).get(Real());
133         case ValueBase::TYPE_COLOR:
134                 return ((*ref_a)(t).get(Color())+(*ref_b)(t).get(Color()))*(*scalar)(t).get(Real());
135         case ValueBase::TYPE_GRADIENT:
136                 return ((*ref_a)(t).get(Gradient())+(*ref_b)(t).get(Gradient()))*(*scalar)(t).get(Real());
137         case ValueBase::TYPE_INTEGER:
138                 return round_to_int(((*ref_a)(t).get(int())+(*ref_b)(t).get(int()))*(*scalar)(t).get(Real()));
139         case ValueBase::TYPE_REAL:
140                 return ((*ref_a)(t).get(Vector::value_type())+(*ref_b)(t).get(Vector::value_type()))*(*scalar)(t).get(Real());
141         case ValueBase::TYPE_TIME:
142                 return ((*ref_a)(t).get(Time())+(*ref_b)(t).get(Time()))*(*scalar)(t).get(Real());
143         case ValueBase::TYPE_VECTOR:
144                 return ((*ref_a)(t).get(Vector())+(*ref_b)(t).get(Vector()))*(*scalar)(t).get(Real());
145         default:
146                 assert(0);
147                 break;
148         }
149         return ValueBase();
150 }
151
152 bool
153 ValueNode_Add::set_link_vfunc(int i,ValueNode::Handle value)
154 {
155         assert(i>=0 && i<link_count());
156
157         switch(i)
158         {
159         case 0: CHECK_TYPE_AND_SET_VALUE(ref_a,  get_type());
160         case 1: CHECK_TYPE_AND_SET_VALUE(ref_b,  get_type());
161         case 2: CHECK_TYPE_AND_SET_VALUE(scalar, ValueBase::TYPE_REAL);
162         }
163         return false;
164 }
165
166 ValueNode::LooseHandle
167 ValueNode_Add::get_link_vfunc(int i)const
168 {
169         assert(i>=0 && i<link_count());
170
171         switch(i)
172         {
173                 case 0: return ref_a;
174                 case 1: return ref_b;
175                 case 2: return scalar;
176                 default: return 0;
177         }
178 }
179
180 String
181 ValueNode_Add::get_name()const
182 {
183         return "add";
184 }
185
186 String
187 ValueNode_Add::get_local_name()const
188 {
189         return _("Add");
190 }
191
192 bool
193 ValueNode_Add::check_type(ValueBase::Type type)
194 {
195         return type==ValueBase::TYPE_ANGLE
196                 || type==ValueBase::TYPE_COLOR
197                 || type==ValueBase::TYPE_GRADIENT
198                 || type==ValueBase::TYPE_INTEGER
199                 || type==ValueBase::TYPE_REAL
200                 || type==ValueBase::TYPE_TIME
201                 || type==ValueBase::TYPE_VECTOR;
202 }
203
204 LinkableValueNode::Vocab
205 ValueNode_Add::get_children_vocab_vfunc() const
206 {
207         LinkableValueNode::Vocab ret;
208
209         ret.push_back(ParamDesc(ValueBase(),"lhs")
210                 .set_local_name(_("LHS"))
211                 .set_description(_("Left Hand Side of the add"))
212         );
213
214         ret.push_back(ParamDesc(ValueBase(),"rhs")
215                 .set_local_name(_("RHS"))
216                 .set_description(_("Right Hand Side of the add"))
217         );
218
219                 ret.push_back(ParamDesc(ValueBase(),"scalar")
220                 .set_local_name(_("Scalar"))
221                 .set_description(_("Value that multiplies the add"))
222         );
223
224         return ret;
225 }