Move reverse manipulation code into ValueDescSet action
[synfig.git] / synfig-core / src / synfig / valuenode_range.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_range.cpp
3 **      \brief Implementation of the "Range" 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_range.h"
35 #include "valuenode_const.h"
36 #include <stdexcept>
37 #include "vector.h"
38 #include "angle.h"
39 #include "real.h"
40
41 #endif
42
43 /* === U S I N G =========================================================== */
44
45 using namespace std;
46 using namespace etl;
47 using namespace synfig;
48
49 /* === M A C R O S ========================================================= */
50
51 /* === G L O B A L S ======================================================= */
52
53 /* === P R O C E D U R E S ================================================= */
54
55 /* === M E T H O D S ======================================================= */
56
57 synfig::ValueNode_Range::ValueNode_Range(const ValueBase &value):
58         LinkableValueNode(value.get_type())
59 {
60         ValueBase::Type id(value.get_type());
61
62         switch(id)
63         {
64         case ValueBase::TYPE_ANGLE:
65                 set_link("min",ValueNode_Const::create(value.get(Angle())));
66                 set_link("max",ValueNode_Const::create(value.get(Angle())));
67                 set_link("link",ValueNode_Const::create(value.get(Angle())));
68                 break;
69         case ValueBase::TYPE_INTEGER:
70                 set_link("min",ValueNode_Const::create(value.get(int())));
71                 set_link("max",ValueNode_Const::create(value.get(int())));
72                 set_link("link",ValueNode_Const::create(value.get(int())));
73                 break;
74         case ValueBase::TYPE_REAL:
75                 set_link("min",ValueNode_Const::create(value.get(Real())));
76                 set_link("max",ValueNode_Const::create(value.get(Real())));
77                 set_link("link",ValueNode_Const::create(value.get(Real())));
78                 break;
79         case ValueBase::TYPE_TIME:
80                 set_link("min",ValueNode_Const::create(value.get(Time())));
81                 set_link("max",ValueNode_Const::create(value.get(Time())));
82                 set_link("link",ValueNode_Const::create(value.get(Time())));
83                 break;
84         default:
85                 assert(0);
86                 throw runtime_error(get_local_name()+_(":Bad type ")+ValueBase::type_local_name(id));
87         }
88
89         assert(min_->get_type()==id);
90         assert(max_->get_type()==id);
91         assert(link_->get_type()==id);
92         assert(get_type()==id);
93 }
94
95 LinkableValueNode*
96 ValueNode_Range::create_new()const
97 {
98         return new ValueNode_Range(get_type());
99 }
100
101 ValueNode_Range*
102 ValueNode_Range::create(const ValueBase& value)
103 {
104         return new ValueNode_Range(value);
105 }
106
107 synfig::ValueNode_Range::~ValueNode_Range()
108 {
109         unlink_all();
110 }
111
112 synfig::ValueBase
113 synfig::ValueNode_Range::operator()(Time t)const
114 {
115         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
116                 printf("%s:%d operator()\n", __FILE__, __LINE__);
117
118         if(!min_ || !max_ || !link_)
119                 throw runtime_error(strprintf("ValueNode_Range: %s",_("Some of my parameters aren't set!")));
120
121         switch(get_type())
122         {
123         case ValueBase::TYPE_ANGLE:
124         {
125                 Angle minimum = (* min_)(t).get(Angle());
126                 Angle maximum = (* max_)(t).get(Angle());
127                 Angle link    = (*link_)(t).get(Angle());
128 // This code was removed because it didn't work with link < minimum
129 // It is sane to completely delete it if the replacement code is fine.
130 /* ***********************************************
131                 // if link is between min and max, use it
132                 if (Angle::deg((link-minimum).mod()).get() < Angle::deg((maximum-minimum).mod()).get())
133                         return link;
134                 // otherwise use whichever of min and max is closest to link
135                 else if (link.dist(minimum).abs() < link.dist(maximum).abs())
136                         return minimum;
137                 else
138                         return maximum;
139 *********************************************** */
140                 if(Angle::rad(maximum).get()>=Angle::rad(link).get() && Angle::rad(link).get()>=Angle::rad(minimum).get())
141                         return link;
142                 else if (Angle::rad(minimum).get()>Angle::rad(link).get())
143                         return minimum;
144                 else
145                         return maximum;
146         }
147         case ValueBase::TYPE_INTEGER:
148                 return std::max((*min_)(t).get(int()),  std::min((*max_)(t).get(int()),  (*link_)(t).get(int())));
149         case ValueBase::TYPE_REAL:
150                 return std::max((*min_)(t).get(Real()), std::min((*max_)(t).get(Real()), (*link_)(t).get(Real())));
151         case ValueBase::TYPE_TIME:
152                 return std::max((*min_)(t).get(Time()), std::min((*max_)(t).get(Time()), (*link_)(t).get(Time())));
153         default:
154                 assert(0);
155                 break;
156         }
157         return ValueBase();
158 }
159
160 synfig::ValueBase
161 synfig::ValueNode_Range::get_inverse(Time t, const synfig::Vector &target_value) const
162 {
163         switch (get_type())
164         {
165                 case ValueBase::TYPE_INTEGER:
166                         {
167                         int max_value((*max_)(t).get(int()));
168                         int min_value((*min_)(t).get(int()));
169                         return std::max(min_value, std::min(max_value, int(target_value.mag())));
170                         }
171                 case ValueBase::TYPE_REAL:
172                         {
173                         Real max_value((*max_)(t).get(Real()));
174                         Real min_value((*min_)(t).get(Real()));
175                         return std::max(min_value, std::min(max_value, target_value.mag()));
176                         }
177                 case ValueBase::TYPE_ANGLE:
178                         {
179                         Angle max_value((*max_)(t).get(Angle()));
180                         Angle min_value((*min_)(t).get(Angle()));
181                         Angle target_angle(Angle::tan(target_value[1],target_value[0]));
182                         return target_angle>max_value?max_value:target_angle<min_value?min_value:target_angle;
183                         }
184                 case ValueBase::TYPE_TIME:
185                         {
186                         Real max_value((*max_)(t).get(Time()));
187                         Real min_value((*min_)(t).get(Time()));
188                         return std::max(min_value, std::min(max_value, target_value.mag()));
189                         }
190                 default:
191                         return target_value;
192         }
193         return ValueBase();
194 }
195
196 synfig::ValueBase
197 synfig::ValueNode_Range::get_inverse(Time t, const synfig::Angle &target_value) const
198 {
199         Angle minimum = (* min_)(t).get(Angle());
200         Angle maximum = (* max_)(t).get(Angle());
201         Angle link = (*link_)(t).get(Angle());
202                 switch (get_type())
203                 {
204                         default:
205
206                 if(Angle::rad(maximum).get()>=Angle::rad(target_value).get() && Angle::rad(target_value).get()>=Angle::rad(minimum).get())
207                         return target_value;
208                 else if (Angle::rad(minimum).get()>Angle::rad(target_value).get())
209                         return minimum;
210                 else
211                         return maximum;
212                 }
213         return ValueBase();
214 }
215
216
217 bool
218 ValueNode_Range::set_link_vfunc(int i,ValueNode::Handle value)
219 {
220         assert(i>=0 && i<link_count());
221
222         switch(i)
223         {
224         case 0: CHECK_TYPE_AND_SET_VALUE(min_,  get_type());
225         case 1: CHECK_TYPE_AND_SET_VALUE(max_,  get_type());
226         case 2: CHECK_TYPE_AND_SET_VALUE(link_, get_type());
227         }
228         return false;
229 }
230
231 ValueNode::LooseHandle
232 ValueNode_Range::get_link_vfunc(int i)const
233 {
234         assert(i>=0 && i<link_count());
235
236         switch(i)
237         {
238                 case 0: return min_;
239                 case 1: return max_;
240                 case 2: return link_;
241                 default: return 0;
242         }
243 }
244
245 int
246 ValueNode_Range::link_count()const
247 {
248         return 3;
249 }
250
251 String
252 ValueNode_Range::link_local_name(int i)const
253 {
254         assert(i>=0 && i<link_count());
255
256         switch(i)
257         {
258                 case 0: return _("Min");
259                 case 1: return _("Max");
260                 case 2: return _("Link");
261                 default: return String();
262         }
263 }
264
265 String
266 ValueNode_Range::link_name(int i)const
267 {
268         assert(i>=0 && i<link_count());
269
270         switch(i)
271         {
272                 case 0: return "min";
273                 case 1: return "max";
274                 case 2: return "link";
275                 default: return String();
276         }
277 }
278
279 int
280 ValueNode_Range::get_link_index_from_name(const String &name)const
281 {
282         if(name=="min") return 0;
283         if(name=="max") return 1;
284         if(name=="link") return 2;
285         throw Exception::BadLinkName(name);
286 }
287
288 String
289 ValueNode_Range::get_name()const
290 {
291         return "range";
292 }
293
294 String
295 ValueNode_Range::get_local_name()const
296 {
297         return _("Range");
298 }
299
300 bool
301 ValueNode_Range::check_type(ValueBase::Type type)
302 {
303         return type==ValueBase::TYPE_ANGLE
304                 || type==ValueBase::TYPE_INTEGER
305                 || type==ValueBase::TYPE_REAL
306                 || type==ValueBase::TYPE_TIME;
307 }