1 /* === S Y N F I G ========================================================= */
2 /*! \file valuenode_range.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
33 #include "valuenode_range.h"
34 #include "valuenode_const.h"
42 /* === U S I N G =========================================================== */
46 using namespace synfig;
48 /* === M A C R O S ========================================================= */
50 /* === G L O B A L S ======================================================= */
52 /* === P R O C E D U R E S ================================================= */
54 /* === M E T H O D S ======================================================= */
56 synfig::ValueNode_Range::ValueNode_Range(const ValueBase &value):
57 LinkableValueNode(value.get_type())
59 ValueBase::Type id(value.get_type());
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())));
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())));
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())));
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())));
85 throw runtime_error("synfig::ValueNode_Range:Bad type "+ValueBase::type_name(id));
88 assert(min_->get_type()==id);
89 assert(max_->get_type()==id);
90 assert(link_->get_type()==id);
91 assert(get_type()==id);
97 ValueNode_Range::create_new()const
99 return new ValueNode_Range(get_type());
103 ValueNode_Range::create(const ValueBase& value)
105 return new ValueNode_Range(value);
108 synfig::ValueNode_Range::~ValueNode_Range()
114 synfig::ValueNode_Range::operator()(Time t)const
116 if(!min_ || !max_ || !link_)
117 throw runtime_error(strprintf("ValueNode_Range: %s",_("Some of my parameters aren't set!")));
121 case ValueBase::TYPE_ANGLE:
123 Angle minimum = (* min_)(t).get(Angle());
124 Angle maximum = (* max_)(t).get(Angle());
125 Angle link = (*link_)(t).get(Angle());
127 // if link is between min and max, use it
128 if (Angle::deg((link-minimum).mod()).get() < Angle::deg((maximum-minimum).mod()).get())
130 // otherwise use whichever of min and max is closest to link
131 else if (link.dist(minimum).abs() < link.dist(maximum).abs())
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())));
150 ValueNode_Range::set_link_vfunc(int i,ValueNode::Handle value)
157 signal_child_changed()(i);signal_value_changed()();
161 signal_child_changed()(i);signal_value_changed()();
165 signal_child_changed()(i);signal_value_changed()();
172 ValueNode::LooseHandle
173 ValueNode_Range::get_link_vfunc(int i)const
180 case 2: return link_;
186 ValueNode_Range::link_count()const
192 ValueNode_Range::link_local_name(int i)const
197 case 0: return _("Min");
198 case 1: return _("Max");
199 case 2: return _("Link");
200 default: return String();
205 ValueNode_Range::link_name(int i)const
210 case 0: return "min";
211 case 1: return "max";
212 case 2: return "link";
213 default: return String();
218 ValueNode_Range::get_link_index_from_name(const String &name)const
220 if(name=="min") return 0;
221 if(name=="max") return 1;
222 if(name=="link") return 2;
223 throw Exception::BadLinkName(name);
227 ValueNode_Range::get_name()const
233 ValueNode_Range::get_local_name()const
239 ValueNode_Range::check_type(ValueBase::Type type)
241 return type==ValueBase::TYPE_ANGLE
242 || type==ValueBase::TYPE_INTEGER
243 || type==ValueBase::TYPE_REAL
244 || type==ValueBase::TYPE_TIME;