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