Use LinkableValueNode members functions when possible in the derived valuenodes.
[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         Vocab ret(get_children_vocab());
61         set_children_vocab(ret);
62         ValueBase::Type id(value.get_type());
63
64         switch(id)
65         {
66         case ValueBase::TYPE_ANGLE:
67                 set_link("min",ValueNode_Const::create(value.get(Angle())));
68                 set_link("max",ValueNode_Const::create(value.get(Angle())));
69                 set_link("link",ValueNode_Const::create(value.get(Angle())));
70                 break;
71         case ValueBase::TYPE_INTEGER:
72                 set_link("min",ValueNode_Const::create(value.get(int())));
73                 set_link("max",ValueNode_Const::create(value.get(int())));
74                 set_link("link",ValueNode_Const::create(value.get(int())));
75                 break;
76         case ValueBase::TYPE_REAL:
77                 set_link("min",ValueNode_Const::create(value.get(Real())));
78                 set_link("max",ValueNode_Const::create(value.get(Real())));
79                 set_link("link",ValueNode_Const::create(value.get(Real())));
80                 break;
81         case ValueBase::TYPE_TIME:
82                 set_link("min",ValueNode_Const::create(value.get(Time())));
83                 set_link("max",ValueNode_Const::create(value.get(Time())));
84                 set_link("link",ValueNode_Const::create(value.get(Time())));
85                 break;
86         default:
87                 assert(0);
88                 throw runtime_error(get_local_name()+_(":Bad type ")+ValueBase::type_local_name(id));
89         }
90
91         assert(min_->get_type()==id);
92         assert(max_->get_type()==id);
93         assert(link_->get_type()==id);
94         assert(get_type()==id);
95 }
96
97 LinkableValueNode*
98 ValueNode_Range::create_new()const
99 {
100         return new ValueNode_Range(get_type());
101 }
102
103 ValueNode_Range*
104 ValueNode_Range::create(const ValueBase& value)
105 {
106         return new ValueNode_Range(value);
107 }
108
109 synfig::ValueNode_Range::~ValueNode_Range()
110 {
111         unlink_all();
112 }
113
114 synfig::ValueBase
115 synfig::ValueNode_Range::operator()(Time t)const
116 {
117         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
118                 printf("%s:%d operator()\n", __FILE__, __LINE__);
119
120         if(!min_ || !max_ || !link_)
121                 throw runtime_error(strprintf("ValueNode_Range: %s",_("Some of my parameters aren't set!")));
122
123         switch(get_type())
124         {
125         case ValueBase::TYPE_ANGLE:
126         {
127                 Angle minimum = (* min_)(t).get(Angle());
128                 Angle maximum = (* max_)(t).get(Angle());
129                 Angle link    = (*link_)(t).get(Angle());
130 // This code was removed because it didn't work with link < minimum
131 // It is sane to completely delete it if the replacement code is fine.
132 /* ***********************************************
133                 // if link is between min and max, use it
134                 if (Angle::deg((link-minimum).mod()).get() < Angle::deg((maximum-minimum).mod()).get())
135                         return link;
136                 // otherwise use whichever of min and max is closest to link
137                 else if (link.dist(minimum).abs() < link.dist(maximum).abs())
138                         return minimum;
139                 else
140                         return maximum;
141 *********************************************** */
142                 if(Angle::rad(maximum).get()>=Angle::rad(link).get() && Angle::rad(link).get()>=Angle::rad(minimum).get())
143                         return link;
144                 else if (Angle::rad(minimum).get()>Angle::rad(link).get())
145                         return minimum;
146                 else
147                         return maximum;
148         }
149         case ValueBase::TYPE_INTEGER:
150                 return std::max((*min_)(t).get(int()),  std::min((*max_)(t).get(int()),  (*link_)(t).get(int())));
151         case ValueBase::TYPE_REAL:
152                 return std::max((*min_)(t).get(Real()), std::min((*max_)(t).get(Real()), (*link_)(t).get(Real())));
153         case ValueBase::TYPE_TIME:
154                 return std::max((*min_)(t).get(Time()), std::min((*max_)(t).get(Time()), (*link_)(t).get(Time())));
155         default:
156                 assert(0);
157                 break;
158         }
159         return ValueBase();
160 }
161
162 synfig::ValueBase
163 synfig::ValueNode_Range::get_inverse(Time t, const synfig::Vector &target_value) const
164 {
165         switch (get_type())
166         {
167                 case ValueBase::TYPE_INTEGER:
168                         {
169                         int max_value((*max_)(t).get(int()));
170                         int min_value((*min_)(t).get(int()));
171                         return std::max(min_value, std::min(max_value, int(target_value.mag())));
172                         }
173                 case ValueBase::TYPE_REAL:
174                         {
175                         Real max_value((*max_)(t).get(Real()));
176                         Real min_value((*min_)(t).get(Real()));
177                         return std::max(min_value, std::min(max_value, target_value.mag()));
178                         }
179                 case ValueBase::TYPE_ANGLE:
180                         {
181                         Angle max_value((*max_)(t).get(Angle()));
182                         Angle min_value((*min_)(t).get(Angle()));
183                         Angle target_angle(Angle::tan(target_value[1],target_value[0]));
184                         return target_angle>max_value?max_value:target_angle<min_value?min_value:target_angle;
185                         }
186                 case ValueBase::TYPE_TIME:
187                         {
188                         Real max_value((*max_)(t).get(Time()));
189                         Real min_value((*min_)(t).get(Time()));
190                         return std::max(min_value, std::min(max_value, target_value.mag()));
191                         }
192                 default:
193                         return target_value;
194         }
195         return ValueBase();
196 }
197
198 synfig::ValueBase
199 synfig::ValueNode_Range::get_inverse(Time t, const synfig::Angle &target_value) const
200 {
201         Angle minimum = (* min_)(t).get(Angle());
202         Angle maximum = (* max_)(t).get(Angle());
203         Angle link = (*link_)(t).get(Angle());
204                 switch (get_type())
205                 {
206                         default:
207
208                 if(Angle::rad(maximum).get()>=Angle::rad(target_value).get() && Angle::rad(target_value).get()>=Angle::rad(minimum).get())
209                         return target_value;
210                 else if (Angle::rad(minimum).get()>Angle::rad(target_value).get())
211                         return minimum;
212                 else
213                         return maximum;
214                 }
215         return ValueBase();
216 }
217
218
219 bool
220 ValueNode_Range::set_link_vfunc(int i,ValueNode::Handle value)
221 {
222         assert(i>=0 && i<link_count());
223
224         switch(i)
225         {
226         case 0: CHECK_TYPE_AND_SET_VALUE(min_,  get_type());
227         case 1: CHECK_TYPE_AND_SET_VALUE(max_,  get_type());
228         case 2: CHECK_TYPE_AND_SET_VALUE(link_, get_type());
229         }
230         return false;
231 }
232
233 ValueNode::LooseHandle
234 ValueNode_Range::get_link_vfunc(int i)const
235 {
236         assert(i>=0 && i<link_count());
237
238         switch(i)
239         {
240                 case 0: return min_;
241                 case 1: return max_;
242                 case 2: return link_;
243                 default: return 0;
244         }
245 }
246
247 String
248 ValueNode_Range::get_name()const
249 {
250         return "range";
251 }
252
253 String
254 ValueNode_Range::get_local_name()const
255 {
256         return _("Range");
257 }
258
259 bool
260 ValueNode_Range::check_type(ValueBase::Type type)
261 {
262         return type==ValueBase::TYPE_ANGLE
263                 || type==ValueBase::TYPE_INTEGER
264                 || type==ValueBase::TYPE_REAL
265                 || type==ValueBase::TYPE_TIME;
266 }
267
268 LinkableValueNode::Vocab
269 ValueNode_Range::get_children_vocab_vfunc()const
270 {
271         if(children_vocab.size())
272                 return children_vocab;
273
274         LinkableValueNode::Vocab ret;
275
276         ret.push_back(ParamDesc(ValueBase(),"min")
277                 .set_local_name(_("Min"))
278                 .set_description(_("Returned value when 'Link' is smaller"))
279         );
280
281         ret.push_back(ParamDesc(ValueBase(),"max")
282                 .set_local_name(_("Max"))
283                 .set_description(_("Returned value when 'Link' is greater"))
284         );
285
286         ret.push_back(ParamDesc(ValueBase(),"link")
287                 .set_local_name(_("Link"))
288                 .set_description(_("The value node to limit its range"))
289         );
290
291         return ret;
292 }