Added copyright lines for files I've edited this year.
[synfig.git] / synfig-core / trunk / 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         set_link("scalar",ValueNode_Const::create(Real(1.0)));
64         ValueBase::Type id(value.get_type());
65
66         switch(id)
67         {
68         case ValueBase::TYPE_ANGLE:
69                 set_link("lhs",ValueNode_Const::create(value.get(Angle())));
70                 set_link("rhs",ValueNode_Const::create(Angle::deg(0)));
71                 break;
72         case ValueBase::TYPE_COLOR:
73                 set_link("lhs",ValueNode_Const::create(value.get(Color())));
74                 set_link("rhs",ValueNode_Const::create(Color(0,0,0,0)));
75                 break;
76         case ValueBase::TYPE_GRADIENT:
77                 set_link("lhs",ValueNode_Const::create(value.get(Gradient())));
78                 set_link("rhs",ValueNode_Const::create(Gradient()));
79                 break;
80         case ValueBase::TYPE_INTEGER:
81                 set_link("lhs",ValueNode_Const::create(value.get(int())));
82                 set_link("rhs",ValueNode_Const::create(int(0)));
83                 break;
84         case ValueBase::TYPE_REAL:
85                 set_link("lhs",ValueNode_Const::create(value.get(Real())));
86                 set_link("rhs",ValueNode_Const::create(Real(0)));
87                 break;
88         case ValueBase::TYPE_TIME:
89                 set_link("lhs",ValueNode_Const::create(value.get(Time())));
90                 set_link("rhs",ValueNode_Const::create(Time(0)));
91                 break;
92         case ValueBase::TYPE_VECTOR:
93                 set_link("lhs",ValueNode_Const::create(value.get(Vector())));
94                 set_link("rhs",ValueNode_Const::create(Vector(0,0)));
95                 break;
96         default:
97                 assert(0);
98                 throw runtime_error(get_local_name()+_(":Bad type ")+ValueBase::type_local_name(id));
99         }
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 synfig::ValueBase
120 synfig::ValueNode_Add::operator()(Time t)const
121 {
122         if(!ref_a || !ref_b)
123                 throw runtime_error(strprintf("ValueNode_Add: %s",_("One or both of my parameters aren't set!")));
124         switch(get_type())
125         {
126         case ValueBase::TYPE_ANGLE:
127                 return ((*ref_a)(t).get(Angle())+(*ref_b)(t).get(Angle()))*(*scalar)(t).get(Real());
128         case ValueBase::TYPE_COLOR:
129                 return ((*ref_a)(t).get(Color())+(*ref_b)(t).get(Color()))*(*scalar)(t).get(Real());
130         case ValueBase::TYPE_GRADIENT:
131                 return ((*ref_a)(t).get(Gradient())+(*ref_b)(t).get(Gradient()))*(*scalar)(t).get(Real());
132         case ValueBase::TYPE_INTEGER:
133                 return round_to_int(((*ref_a)(t).get(int())+(*ref_b)(t).get(int()))*(*scalar)(t).get(Real()));
134         case ValueBase::TYPE_REAL:
135                 return ((*ref_a)(t).get(Vector::value_type())+(*ref_b)(t).get(Vector::value_type()))*(*scalar)(t).get(Real());
136         case ValueBase::TYPE_TIME:
137                 return ((*ref_a)(t).get(Time())+(*ref_b)(t).get(Time()))*(*scalar)(t).get(Real());
138         case ValueBase::TYPE_VECTOR:
139                 return ((*ref_a)(t).get(Vector())+(*ref_b)(t).get(Vector()))*(*scalar)(t).get(Real());
140         default:
141                 assert(0);
142                 break;
143         }
144         return ValueBase();
145 }
146
147 bool
148 ValueNode_Add::set_link_vfunc(int i,ValueNode::Handle value)
149 {
150         assert(i>=0 && i<link_count());
151
152         switch(i)
153         {
154         case 0: CHECK_TYPE_AND_SET_VALUE(ref_a,  get_type());
155         case 1: CHECK_TYPE_AND_SET_VALUE(ref_b,  get_type());
156         case 2: CHECK_TYPE_AND_SET_VALUE(scalar, ValueBase::TYPE_REAL);
157         }
158         return false;
159 }
160
161 ValueNode::LooseHandle
162 ValueNode_Add::get_link_vfunc(int i)const
163 {
164         assert(i>=0 && i<link_count());
165
166         switch(i)
167         {
168                 case 0: return ref_a;
169                 case 1: return ref_b;
170                 case 2: return scalar;
171                 default: return 0;
172         }
173 }
174
175 int
176 ValueNode_Add::link_count()const
177 {
178         return 3;
179 }
180
181 String
182 ValueNode_Add::link_local_name(int i)const
183 {
184         assert(i>=0 && i<link_count());
185
186         switch(i)
187         {
188                 case 0: return _("LHS");
189                 case 1: return _("RHS");
190                 case 2: return _("Scalar");
191                 default: return String();
192         }
193 }
194
195 String
196 ValueNode_Add::link_name(int i)const
197 {
198         assert(i>=0 && i<link_count());
199
200         switch(i)
201         {
202                 case 0: return "lhs";
203                 case 1: return "rhs";
204                 case 2: return "scalar";
205                 default: return String();
206         }
207 }
208
209 int
210 ValueNode_Add::get_link_index_from_name(const String &name)const
211 {
212         if(name=="lhs") return 0;
213         if(name=="rhs") return 1;
214         if(name=="scalar") return 2;
215         throw Exception::BadLinkName(name);
216 }
217
218 String
219 ValueNode_Add::get_name()const
220 {
221         return "add";
222 }
223
224 String
225 ValueNode_Add::get_local_name()const
226 {
227         return _("Add");
228 }
229
230 bool
231 ValueNode_Add::check_type(ValueBase::Type type)
232 {
233         return type==ValueBase::TYPE_ANGLE
234                 || type==ValueBase::TYPE_COLOR
235                 || type==ValueBase::TYPE_GRADIENT
236                 || type==ValueBase::TYPE_INTEGER
237                 || type==ValueBase::TYPE_REAL
238                 || type==ValueBase::TYPE_TIME
239                 || type==ValueBase::TYPE_VECTOR;
240 }