Use std::min and std::max rather than my ad-hoc #defines.
[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 synfig::ValueBase
114 synfig::ValueNode_Range::operator()(Time t)const
115 {
116         if(!min_ || !max_ || !link_)
117                 throw runtime_error(strprintf("ValueNode_Range: %s",_("Some of my parameters aren't set!")));
118
119         switch(get_type())
120         {
121         case ValueBase::TYPE_ANGLE:
122         {
123                 Angle minimum = (* min_)(t).get(Angle());
124                 Angle maximum = (* max_)(t).get(Angle());
125                 Angle link    = (*link_)(t).get(Angle());
126
127                 // if link is between min and max, use it
128                 if (Angle::deg((link-minimum).mod()).get() < Angle::deg((maximum-minimum).mod()).get())
129                         return link;
130                 // otherwise use whichever of min and max is closest to link
131                 else if (link.dist(minimum).abs() < link.dist(maximum).abs())
132                         return minimum;
133                 else
134                         return maximum;
135         }
136         case ValueBase::TYPE_INTEGER:
137                 return std::max((*min_)(t).get(int()),  std::min((*max_)(t).get(int()),  (*link_)(t).get(int())));
138         case ValueBase::TYPE_REAL:
139                 return std::max((*min_)(t).get(Real()), std::min((*max_)(t).get(Real()), (*link_)(t).get(Real())));
140         case ValueBase::TYPE_TIME:
141                 return std::max((*min_)(t).get(Time()), std::min((*max_)(t).get(Time()), (*link_)(t).get(Time())));
142         default:
143                 assert(0);
144                 break;
145         }
146         return ValueBase();
147 }
148
149 bool
150 ValueNode_Range::set_link_vfunc(int i,ValueNode::Handle value)
151 {
152         assert(i>=0 && i<3);
153         switch(i)
154         {
155                 case 0:
156                         min_=value;
157                         signal_child_changed()(i);signal_value_changed()();
158                         return true;
159                 case 1:
160                         max_=value;
161                         signal_child_changed()(i);signal_value_changed()();
162                         return true;
163                 case 2:
164                         link_=value;
165                         signal_child_changed()(i);signal_value_changed()();
166                         return true;
167         }
168
169         return false;
170 }
171
172 ValueNode::LooseHandle
173 ValueNode_Range::get_link_vfunc(int i)const
174 {
175         assert(i>=0 && i<3);
176         switch(i)
177         {
178                 case 0: return min_;
179                 case 1: return max_;
180                 case 2: return link_;
181                 default: return 0;
182         }
183 }
184
185 int
186 ValueNode_Range::link_count()const
187 {
188         return 3;
189 }
190
191 String
192 ValueNode_Range::link_local_name(int i)const
193 {
194         assert(i>=0 && i<3);
195         switch(i)
196         {
197                 case 0: return _("Min");
198                 case 1: return _("Max");
199                 case 2: return _("Link");
200                 default: return String();
201         }
202 }
203
204 String
205 ValueNode_Range::link_name(int i)const
206 {
207         assert(i>=0 && i<3);
208         switch(i)
209         {
210                 case 0: return "min";
211                 case 1: return "max";
212                 case 2: return "link";
213                 default: return String();
214         }
215 }
216
217 int
218 ValueNode_Range::get_link_index_from_name(const String &name)const
219 {
220         if(name=="min") return 0;
221         if(name=="max") return 1;
222         if(name=="link") return 2;
223         throw Exception::BadLinkName(name);
224 }
225
226 String
227 ValueNode_Range::get_name()const
228 {
229         return "range";
230 }
231
232 String
233 ValueNode_Range::get_local_name()const
234 {
235         return _("Range");
236 }
237
238 bool
239 ValueNode_Range::check_type(ValueBase::Type type)
240 {
241         return type==ValueBase::TYPE_ANGLE
242                 || type==ValueBase::TYPE_INTEGER
243                 || type==ValueBase::TYPE_REAL
244                 || type==ValueBase::TYPE_TIME;
245 }