1 /* === S Y N F I G ========================================================= */
2 /*! \file valuenode_scale.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_scale.h"
34 #include "valuenode_const.h"
44 /* === U S I N G =========================================================== */
48 using namespace synfig;
50 /* === M A C R O S ========================================================= */
52 /* === G L O B A L S ======================================================= */
54 /* === P R O C E D U R E S ================================================= */
56 /* === M E T H O D S ======================================================= */
58 ValueNode_Scale::ValueNode_Scale(const ValueBase &value):
59 LinkableValueNode(value.get_type())
62 ValueBase::Type id(value.get_type());
66 case ValueBase::TYPE_ANGLE:
67 set_link("link",ValueNode_Const::create(value.get(Angle())));
69 case ValueBase::TYPE_COLOR:
70 set_link("link",ValueNode_Const::create(value.get(Color())));
72 case ValueBase::TYPE_INTEGER:
73 set_link("link",ValueNode_Const::create(value.get(int())));
75 case ValueBase::TYPE_REAL:
76 set_link("link",ValueNode_Const::create(value.get(Real())));
78 case ValueBase::TYPE_TIME:
79 set_link("link",ValueNode_Const::create(value.get(Time())));
81 case ValueBase::TYPE_VECTOR:
82 set_link("link",ValueNode_Const::create(value.get(Vector())));
86 throw runtime_error("synfig::ValueNode_Scale:Bad type "+ValueBase::type_name(value.get_type()));
90 assert(get_value_node()->get_type()==id);
91 assert(get_type()==id);
95 ValueNode_Scale::create_new()const
97 return new ValueNode_Scale(get_type());
101 ValueNode_Scale::create(const ValueBase& value)
103 return new ValueNode_Scale(value);
106 synfig::ValueNode_Scale::~ValueNode_Scale()
112 ValueNode_Scale::set_scalar(Real x)
114 set_link("scalar",ValueNode::Handle(ValueNode_Const::create(x)));
118 ValueNode_Scale::set_scalar(const ValueNode::Handle &x)
121 || x->get_type()!=ValueBase::TYPE_REAL
122 && !PlaceholderValueNode::Handle::cast_dynamic(x)
130 ValueNode_Scale::get_scalar()const
136 ValueNode_Scale::set_value_node(const ValueNode::Handle &x)
140 // if this isn't a proper value
142 // or we don't have a type, and this value isn't one of the types we accept
143 (get_type()==ValueBase::TYPE_NIL && !check_type(x->get_type())) ||
144 // or we have a type and this value is a different type and (placeholder?)
145 (get_type()!=ValueBase::TYPE_NIL && x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x)))
146 // then fail to set the value
155 ValueNode_Scale::get_value_node()const
162 synfig::ValueNode_Scale::operator()(Time t)const
164 if(!value_node || !scalar)
165 throw runtime_error(strprintf("ValueNode_Scale: %s",_("One or both of my parameters aren't set!")));
167 if(get_type()==ValueBase::TYPE_VECTOR)
168 return (*value_node)(t).get(Vector())*(*scalar)(t).get(Real());
170 if(get_type()==ValueBase::TYPE_REAL)
171 return (*value_node)(t).get(Real())*(*scalar)(t).get(Real());
173 if(get_type()==ValueBase::TYPE_TIME)
174 return (*value_node)(t).get(Time())*(*scalar)(t).get(Time());
176 if(get_type()==ValueBase::TYPE_INTEGER)
178 Real ret = (*value_node)(t).get(int())*(*scalar)(t).get(Real()) + 0.5f;
179 if (ret < 0) return static_cast<int>(ret-1);
180 return static_cast<int>(ret);
183 if(get_type()==ValueBase::TYPE_ANGLE)
184 return (*value_node)(t).get(Angle())*(*scalar)(t).get(Real());
186 if(get_type()==ValueBase::TYPE_COLOR)
188 Color ret((*value_node)(t).get(Color()));
189 Real s((*scalar)(t).get(Real()));
190 ret.set_r(ret.get_r()*s);
191 ret.set_g(ret.get_g()*s);
192 ret.set_b(ret.get_b()*s);
202 ValueNode_Scale::set_link_vfunc(int i,ValueNode::Handle x)
207 if(i==0 && !set_value_node(x))
210 if(i==1 && !set_scalar(x))
213 signal_child_changed()(i);signal_value_changed()();
218 ValueNode::LooseHandle
219 ValueNode_Scale::get_link_vfunc(int i)const
221 assert(i==0 || i==1);
230 ValueNode_Scale::link_count()const
236 ValueNode_Scale::link_local_name(int i)const
238 assert(i==0 || i==1);
247 ValueNode_Scale::link_name(int i)const
249 assert(i==0 || i==1);
258 ValueNode_Scale::get_link_index_from_name(const String &name)const
265 throw Exception::BadLinkName(name);
269 ValueNode_Scale::get_name()const
275 ValueNode_Scale::get_local_name()const
281 ValueNode_Scale::check_type(ValueBase::Type type)
284 type==ValueBase::TYPE_VECTOR ||
285 type==ValueBase::TYPE_REAL ||
286 type==ValueBase::TYPE_INTEGER ||
287 type==ValueBase::TYPE_COLOR ||
288 type==ValueBase::TYPE_ANGLE;