Use translated versions of the type names everywhere other than in the .sif(z) files.
[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 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 #include <ETL/misc>
42
43 #endif
44
45 /* === U S I N G =========================================================== */
46
47 using namespace std;
48 using namespace etl;
49 using namespace synfig;
50
51 /* === M A C R O S ========================================================= */
52
53 /* === G L O B A L S ======================================================= */
54
55 /* === P R O C E D U R E S ================================================= */
56
57 /* === M E T H O D S ======================================================= */
58
59 synfig::ValueNode_Add::ValueNode_Add(const ValueBase &value):
60         LinkableValueNode(value.get_type())
61 {
62         set_link("scalar",ValueNode_Const::create(Real(1.0)));
63         ValueBase::Type id(value.get_type());
64
65         switch(id)
66         {
67         case ValueBase::TYPE_ANGLE:
68                 set_link("lhs",ValueNode_Const::create(value.get(Angle())));
69                 set_link("rhs",ValueNode_Const::create(Angle::deg(0)));
70                 break;
71         case ValueBase::TYPE_COLOR:
72                 set_link("lhs",ValueNode_Const::create(value.get(Color())));
73                 set_link("rhs",ValueNode_Const::create(Color(0,0,0,0)));
74                 break;
75         case ValueBase::TYPE_INTEGER:
76                 set_link("lhs",ValueNode_Const::create(value.get(int())));
77                 set_link("rhs",ValueNode_Const::create(int(0)));
78                 break;
79         case ValueBase::TYPE_REAL:
80                 set_link("lhs",ValueNode_Const::create(value.get(Real())));
81                 set_link("rhs",ValueNode_Const::create(Real(0)));
82                 break;
83         case ValueBase::TYPE_TIME:
84                 set_link("lhs",ValueNode_Const::create(value.get(Time())));
85                 set_link("rhs",ValueNode_Const::create(Time(0)));
86                 break;
87         case ValueBase::TYPE_VECTOR:
88                 set_link("lhs",ValueNode_Const::create(value.get(Vector())));
89                 set_link("rhs",ValueNode_Const::create(Vector(0,0)));
90                 break;
91         default:
92                 assert(0);
93                 throw runtime_error(_("synfig::ValueNode_Add:Bad type ")+ValueBase::type_local_name(id));
94         }
95 }
96
97 LinkableValueNode*
98 ValueNode_Add::create_new()const
99 {
100         return new ValueNode_Add(get_type());
101 }
102
103 ValueNode_Add*
104 ValueNode_Add::create(const ValueBase& value)
105 {
106         return new ValueNode_Add(value);
107 }
108
109 synfig::ValueNode_Add::~ValueNode_Add()
110 {
111         unlink_all();
112 }
113
114 synfig::ValueBase
115 synfig::ValueNode_Add::operator()(Time t)const
116 {
117         if(!ref_a || !ref_b)
118                 throw runtime_error(strprintf("ValueNode_Add: %s",_("One or both of my parameters aren't set!")));
119         switch(get_type())
120         {
121         case ValueBase::TYPE_ANGLE:
122                 return ((*ref_a)(t).get(Angle())+(*ref_b)(t).get(Angle()))*(*scalar)(t).get(Real());
123         case ValueBase::TYPE_COLOR:
124                 return ((*ref_a)(t).get(Color())+(*ref_b)(t).get(Color()))*(*scalar)(t).get(Real());
125         case ValueBase::TYPE_INTEGER:
126                 return round_to_int(((*ref_a)(t).get(int())+(*ref_b)(t).get(int()))*(*scalar)(t).get(Real()));
127         case ValueBase::TYPE_REAL:
128                 return ((*ref_a)(t).get(Vector::value_type())+(*ref_b)(t).get(Vector::value_type()))*(*scalar)(t).get(Real());
129         case ValueBase::TYPE_TIME:
130                 return ((*ref_a)(t).get(Time())+(*ref_b)(t).get(Time()))*(*scalar)(t).get(Real());
131         case ValueBase::TYPE_VECTOR:
132                 return ((*ref_a)(t).get(Vector())+(*ref_b)(t).get(Vector()))*(*scalar)(t).get(Real());
133         default:
134                 assert(0);
135                 break;
136         }
137         return ValueBase();
138 }
139
140 bool
141 ValueNode_Add::set_link_vfunc(int i,ValueNode::Handle value)
142 {
143         assert(i>=0 && i<link_count());
144         switch(i)
145         {
146         case 0: CHECK_TYPE_AND_SET_VALUE(ref_a,  get_type());
147         case 1: CHECK_TYPE_AND_SET_VALUE(ref_b,  get_type());
148         case 2: CHECK_TYPE_AND_SET_VALUE(scalar, ValueBase::TYPE_REAL);
149         }
150         return false;
151 }
152
153 ValueNode::LooseHandle
154 ValueNode_Add::get_link_vfunc(int i)const
155 {
156         assert(i>=0 && i<link_count());
157         switch(i)
158         {
159                 case 0: return ref_a;
160                 case 1: return ref_b;
161                 case 2: return scalar;
162                 default: return 0;
163         }
164 }
165
166 int
167 ValueNode_Add::link_count()const
168 {
169         return 3;
170 }
171
172 String
173 ValueNode_Add::link_local_name(int i)const
174 {
175         assert(i>=0 && i<link_count());
176         switch(i)
177         {
178                 case 0: return _("LHS");
179                 case 1: return _("RHS");
180                 case 2: return _("Scalar");
181                 default: return String();
182         }
183 }
184
185 String
186 ValueNode_Add::link_name(int i)const
187 {
188         assert(i>=0 && i<link_count());
189         switch(i)
190         {
191                 case 0: return "lhs";
192                 case 1: return "rhs";
193                 case 2: return "scalar";
194                 default: return String();
195         }
196 }
197
198 int
199 ValueNode_Add::get_link_index_from_name(const String &name)const
200 {
201         if(name=="lhs") return 0;
202         if(name=="rhs") return 1;
203         if(name=="scalar") return 2;
204         throw Exception::BadLinkName(name);
205 }
206
207 String
208 ValueNode_Add::get_name()const
209 {
210         return "add";
211 }
212
213 String
214 ValueNode_Add::get_local_name()const
215 {
216         return _("Add");
217 }
218
219 bool
220 ValueNode_Add::check_type(ValueBase::Type type)
221 {
222         return type==ValueBase::TYPE_ANGLE
223                 || type==ValueBase::TYPE_COLOR
224                 || type==ValueBase::TYPE_INTEGER
225                 || type==ValueBase::TYPE_REAL
226                 || type==ValueBase::TYPE_TIME
227                 || type==ValueBase::TYPE_VECTOR;
228 }