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