For the 'Range' convert type, when working with angles, if the link parameter is...
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_range.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_range.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_range.h"
34 #include "valuenode_const.h"
35 #include <stdexcept>
36 #include "vector.h"
37 #include "angle.h"
38 #include "real.h"
39
40 #endif
41
42 /* === U S I N G =========================================================== */
43
44 using namespace std;
45 using namespace etl;
46 using namespace synfig;
47
48 /* === M A C R O S ========================================================= */
49
50 /* === G L O B A L S ======================================================= */
51
52 /* === P R O C E D U R E S ================================================= */
53
54 /* === M E T H O D S ======================================================= */
55
56 synfig::ValueNode_Range::ValueNode_Range(const ValueBase &value):
57         LinkableValueNode(value.get_type())
58 {
59         ValueBase::Type id(value.get_type());
60
61         switch(id)
62         {
63         case ValueBase::TYPE_ANGLE:
64                 set_link("min",ValueNode_Const::create(value.get(Angle())));
65                 set_link("max",ValueNode_Const::create(value.get(Angle())));
66                 set_link("link",ValueNode_Const::create(value.get(Angle())));
67                 break;
68         case ValueBase::TYPE_INTEGER:
69                 set_link("min",ValueNode_Const::create(value.get(int())));
70                 set_link("max",ValueNode_Const::create(value.get(int())));
71                 set_link("link",ValueNode_Const::create(value.get(int())));
72                 break;
73         case ValueBase::TYPE_REAL:
74                 set_link("min",ValueNode_Const::create(value.get(Real())));
75                 set_link("max",ValueNode_Const::create(value.get(Real())));
76                 set_link("link",ValueNode_Const::create(value.get(Real())));
77                 break;
78         case ValueBase::TYPE_TIME:
79                 set_link("min",ValueNode_Const::create(value.get(Time())));
80                 set_link("max",ValueNode_Const::create(value.get(Time())));
81                 set_link("link",ValueNode_Const::create(value.get(Time())));
82                 break;
83         default:
84                 assert(0);
85                 throw runtime_error("synfig::ValueNode_Range:Bad type "+ValueBase::type_name(id));
86         }
87
88         assert(min_->get_type()==id);
89         assert(max_->get_type()==id);
90         assert(link_->get_type()==id);
91         assert(get_type()==id);
92
93         DCAST_HACK_ENABLE();
94 }
95
96 LinkableValueNode*
97 ValueNode_Range::create_new()const
98 {
99         return new ValueNode_Range(get_type());
100 }
101
102 ValueNode_Range*
103 ValueNode_Range::create(const ValueBase& value)
104 {
105         return new ValueNode_Range(value);
106 }
107
108 synfig::ValueNode_Range::~ValueNode_Range()
109 {
110         unlink_all();
111 }
112
113 #define min(x,y) (x>y ? y : x)
114 #define max(x,y) (x>y ? x : y)
115 #define range(low,high,input) (min(high,max(low,input)))
116
117 synfig::ValueBase
118 synfig::ValueNode_Range::operator()(Time t)const
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
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         case ValueBase::TYPE_INTEGER:
141                 return range((*min_)(t).get(int()),   (*max_)(t).get(int()),   (*link_)(t).get(int()));
142         case ValueBase::TYPE_REAL:
143                 return range((*min_)(t).get(Real()),  (*max_)(t).get(Real()),  (*link_)(t).get(Real()));
144         case ValueBase::TYPE_TIME:
145                 return range((*min_)(t).get(Time()),  (*max_)(t).get(Time()),  (*link_)(t).get(Time()));
146         default:
147                 assert(0);
148                 break;
149         }
150         return ValueBase();
151 }
152
153 bool
154 ValueNode_Range::set_link_vfunc(int i,ValueNode::Handle value)
155 {
156         assert(i>=0 && i<3);
157         switch(i)
158         {
159                 case 0:
160                         min_=value;
161                         signal_child_changed()(i);signal_value_changed()();
162                         return true;
163                 case 1:
164                         max_=value;
165                         signal_child_changed()(i);signal_value_changed()();
166                         return true;
167                 case 2:
168                         link_=value;
169                         signal_child_changed()(i);signal_value_changed()();
170                         return true;
171         }
172
173         return false;
174 }
175
176 ValueNode::LooseHandle
177 ValueNode_Range::get_link_vfunc(int i)const
178 {
179         assert(i>=0 && i<3);
180         switch(i)
181         {
182                 case 0: return min_;
183                 case 1: return max_;
184                 case 2: return link_;
185                 default: return 0;
186         }
187 }
188
189 int
190 ValueNode_Range::link_count()const
191 {
192         return 3;
193 }
194
195 String
196 ValueNode_Range::link_local_name(int i)const
197 {
198         assert(i>=0 && i<3);
199         switch(i)
200         {
201                 case 0: return _("Min");
202                 case 1: return _("Max");
203                 case 2: return _("Link");
204                 default: return String();
205         }
206 }
207
208 String
209 ValueNode_Range::link_name(int i)const
210 {
211         assert(i>=0 && i<3);
212         switch(i)
213         {
214                 case 0: return "min";
215                 case 1: return "max";
216                 case 2: return "link";
217                 default: return String();
218         }
219 }
220
221 int
222 ValueNode_Range::get_link_index_from_name(const String &name)const
223 {
224         if(name=="min") return 0;
225         if(name=="max") return 1;
226         if(name=="link") return 2;
227         throw Exception::BadLinkName(name);
228 }
229
230 String
231 ValueNode_Range::get_name()const
232 {
233         return "range";
234 }
235
236 String
237 ValueNode_Range::get_local_name()const
238 {
239         return _("Range");
240 }
241
242 bool
243 ValueNode_Range::check_type(ValueBase::Type type)
244 {
245         return type==ValueBase::TYPE_ANGLE
246                 || type==ValueBase::TYPE_INTEGER
247                 || type==ValueBase::TYPE_REAL
248                 || type==ValueBase::TYPE_TIME;
249 }