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