Fix a copy-paste typo when applying the 'scale' convert type to a color. Previously...
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_scale.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_scale.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_scale.h"
34 #include "valuenode_const.h"
35 #include <stdexcept>
36 #include <cassert>
37 #include "color.h"
38 #include "vector.h"
39 #include "time.h"
40 #include "angle.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 ValueNode_Scale::ValueNode_Scale():
59         LinkableValueNode(synfig::ValueBase::TYPE_NIL)
60 {
61         set_scalar(1.0);
62 }
63
64 ValueNode_Scale*
65 ValueNode_Scale::create(const ValueBase& x)
66 {
67         ValueNode_Scale* value_node;
68         switch(x.get_type())
69         {
70         case ValueBase::TYPE_VECTOR:
71         case ValueBase::TYPE_REAL:
72         case ValueBase::TYPE_TIME:
73         case ValueBase::TYPE_INTEGER:
74         case ValueBase::TYPE_ANGLE:
75         case ValueBase::TYPE_COLOR:
76                 value_node=new ValueNode_Scale();
77                 if(!value_node->set_value_node(ValueNode_Const::create(x)))
78                         return 0;
79                 assert(value_node->get_value_node()->get_type()==x.get_type());
80                 break;
81         default:
82                 assert(0);
83                 throw runtime_error("synfig::ValueNode_Scale:Bad type "+ValueBase::type_name(x.get_type()));
84         }
85         assert(value_node);
86         assert(value_node->get_type()==x.get_type());
87
88         return value_node;
89 }
90
91 LinkableValueNode*
92 ValueNode_Scale::create_new()const
93 {
94         return new ValueNode_Scale();
95 }
96
97 synfig::ValueNode_Scale::~ValueNode_Scale()
98 {
99         unlink_all();
100 }
101
102 void
103 ValueNode_Scale::set_scalar(Real x)
104 {
105         set_link("scalar",ValueNode::Handle(ValueNode_Const::create(x)));
106 }
107
108 bool
109 ValueNode_Scale::set_scalar(const ValueNode::Handle &x)
110 {
111         if(!x
112                 || x->get_type()!=ValueBase::TYPE_REAL
113                 && !PlaceholderValueNode::Handle::cast_dynamic(x)
114         )
115                 return false;
116         scalar=x;
117         return true;
118 }
119
120 ValueNode::Handle
121 ValueNode_Scale::get_scalar()const
122 {
123         return scalar;
124 }
125
126 bool
127 ValueNode_Scale::set_value_node(const ValueNode::Handle &x)
128 {
129         if(!x
130                 || ( get_type()==ValueBase::TYPE_NIL
131                         && !check_type(x->get_type()) )
132                 || ( get_type()!=ValueBase::TYPE_NIL
133                         && x->get_type()!=get_type() ) &&
134                 !PlaceholderValueNode::Handle::cast_dynamic(x)
135         )
136                 return false;
137
138         assert(!(PlaceholderValueNode::Handle::cast_dynamic(x) && !get_type()));
139
140         value_node=x;
141
142         if(!get_type())
143                 set_type(x->get_type());
144
145         return true;
146 }
147
148 ValueNode::Handle
149 ValueNode_Scale::get_value_node()const
150 {
151         return value_node;
152 }
153
154
155 synfig::ValueBase
156 synfig::ValueNode_Scale::operator()(Time t)const
157 {
158         if(!value_node || !scalar)
159                 throw runtime_error(strprintf("ValueNode_Scale: %s",_("One or both of my parameters aren't set!")));
160         else
161         if(get_type()==ValueBase::TYPE_VECTOR)
162                 return (*value_node)(t).get(Vector())*(*scalar)(t).get(Real());
163         else
164         if(get_type()==ValueBase::TYPE_REAL)
165                 return (*value_node)(t).get(Real())*(*scalar)(t).get(Real());
166         else
167         if(get_type()==ValueBase::TYPE_TIME)
168                 return (*value_node)(t).get(Time())*(*scalar)(t).get(Time());
169         else
170         if(get_type()==ValueBase::TYPE_INTEGER)
171                 return (*value_node)(t).get(int())*(*scalar)(t).get(Real());
172         else
173         if(get_type()==ValueBase::TYPE_ANGLE)
174                 return (*value_node)(t).get(Angle())*(*scalar)(t).get(Real());
175         else
176         if(get_type()==ValueBase::TYPE_COLOR)
177         {
178                 Color ret((*value_node)(t).get(Color()));
179                 Real s((*scalar)(t).get(Real()));
180                 ret.set_r(ret.get_r()*s);
181                 ret.set_g(ret.get_g()*s);
182                 ret.set_b(ret.get_b()*s);
183                 return ret;
184         }
185
186         assert(0);
187         return ValueBase();
188 }
189
190
191 bool
192 ValueNode_Scale::set_link_vfunc(int i,ValueNode::Handle x)
193 {
194         if(!(i==0 || i==1))
195                 return false;
196
197         if(i==0 && !set_value_node(x))
198                 return false;
199         else
200         if(i==1 && !set_scalar(x))
201                 return false;
202
203         signal_child_changed()(i);signal_value_changed()();
204
205         return true;
206 }
207
208 ValueNode::LooseHandle
209 ValueNode_Scale::get_link_vfunc(int i)const
210 {
211         assert(i==0 || i==1);
212         if(i==0)
213                 return value_node;
214         else if(i==1)
215                 return scalar;
216         return 0;
217 }
218
219 int
220 ValueNode_Scale::link_count()const
221 {
222         return 2;
223 }
224
225 String
226 ValueNode_Scale::link_local_name(int i)const
227 {
228         assert(i==0 || i==1);
229         if(i==0)
230                 return _("Link");
231         else if(i==1)
232                 return _("Scalar");
233         return String();
234 }
235
236 String
237 ValueNode_Scale::link_name(int i)const
238 {
239         assert(i==0 || i==1);
240         if(i==0)
241                 return "link";
242         else if(i==1)
243                 return "scalar";
244         return String();
245 }
246
247 int
248 ValueNode_Scale::get_link_index_from_name(const String &name)const
249 {
250         if(name=="link")
251                 return 0;
252         if(name=="scalar")
253                 return 1;
254
255         throw Exception::BadLinkName(name);
256 }
257
258 String
259 ValueNode_Scale::get_name()const
260 {
261         return "scale";
262 }
263
264 String
265 ValueNode_Scale::get_local_name()const
266 {
267         return _("Scale");
268 }
269
270 bool
271 ValueNode_Scale::check_type(ValueBase::Type type)
272 {
273         return
274                 type==ValueBase::TYPE_VECTOR ||
275                 type==ValueBase::TYPE_REAL ||
276                 type==ValueBase::TYPE_INTEGER ||
277                 type==ValueBase::TYPE_COLOR ||
278                 type==ValueBase::TYPE_ANGLE;
279 }