1 /* === S Y N F I G ========================================================= */
2 /*! \file valuenode_scale.cpp
3 ** \brief Implementation of the "Scale" valuenode conversion.
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
34 #include "valuenode_scale.h"
35 #include "valuenode_const.h"
45 /* === U S I N G =========================================================== */
49 using namespace synfig;
51 /* === M A C R O S ========================================================= */
53 /* === G L O B A L S ======================================================= */
55 /* === P R O C E D U R E S ================================================= */
57 /* === M E T H O D S ======================================================= */
59 ValueNode_Scale::ValueNode_Scale(const ValueBase &value):
60 LinkableValueNode(value.get_type())
63 ValueBase::Type id(value.get_type());
67 case ValueBase::TYPE_ANGLE:
68 set_link("link",ValueNode_Const::create(value.get(Angle())));
70 case ValueBase::TYPE_COLOR:
71 set_link("link",ValueNode_Const::create(value.get(Color())));
73 case ValueBase::TYPE_INTEGER:
74 set_link("link",ValueNode_Const::create(value.get(int())));
76 case ValueBase::TYPE_REAL:
77 set_link("link",ValueNode_Const::create(value.get(Real())));
79 case ValueBase::TYPE_TIME:
80 set_link("link",ValueNode_Const::create(value.get(Time())));
82 case ValueBase::TYPE_VECTOR:
83 set_link("link",ValueNode_Const::create(value.get(Vector())));
87 throw runtime_error("synfig::ValueNode_Scale:Bad type "+ValueBase::type_name(value.get_type()));
91 assert(get_value_node()->get_type()==id);
92 assert(get_type()==id);
96 ValueNode_Scale::create_new()const
98 return new ValueNode_Scale(get_type());
102 ValueNode_Scale::create(const ValueBase& value)
104 return new ValueNode_Scale(value);
107 synfig::ValueNode_Scale::~ValueNode_Scale()
113 ValueNode_Scale::set_scalar(Real x)
115 set_link("scalar",ValueNode::Handle(ValueNode_Const::create(x)));
119 ValueNode_Scale::set_scalar(const ValueNode::Handle &x)
122 || x->get_type()!=ValueBase::TYPE_REAL
123 && !PlaceholderValueNode::Handle::cast_dynamic(x)
131 ValueNode_Scale::get_scalar()const
137 ValueNode_Scale::set_value_node(const ValueNode::Handle &x)
141 // if this isn't a proper value
143 // or we don't have a type, and this value isn't one of the types we accept
144 (get_type()==ValueBase::TYPE_NIL && !check_type(x->get_type())) ||
145 // or we have a type and this value is a different type and (placeholder?)
146 (get_type()!=ValueBase::TYPE_NIL && x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x)))
147 // then fail to set the value
156 ValueNode_Scale::get_value_node()const
163 synfig::ValueNode_Scale::operator()(Time t)const
165 if(!value_node || !scalar)
166 throw runtime_error(strprintf("ValueNode_Scale: %s",_("One or both of my parameters aren't set!")));
167 else if(get_type()==ValueBase::TYPE_ANGLE)
168 return (*value_node)(t).get(Angle())*(*scalar)(t).get(Real());
169 else if(get_type()==ValueBase::TYPE_COLOR)
171 Color ret((*value_node)(t).get(Color()));
172 Real s((*scalar)(t).get(Real()));
173 ret.set_r(ret.get_r()*s);
174 ret.set_g(ret.get_g()*s);
175 ret.set_b(ret.get_b()*s);
178 else if(get_type()==ValueBase::TYPE_INTEGER)
179 return round_to_int((*value_node)(t).get(int())*(*scalar)(t).get(Real()));
180 else if(get_type()==ValueBase::TYPE_REAL)
181 return (*value_node)(t).get(Real())*(*scalar)(t).get(Real());
182 else if(get_type()==ValueBase::TYPE_TIME)
183 return (*value_node)(t).get(Time())*(*scalar)(t).get(Time());
184 else if(get_type()==ValueBase::TYPE_VECTOR)
185 return (*value_node)(t).get(Vector())*(*scalar)(t).get(Real());
193 ValueNode_Scale::set_link_vfunc(int i,ValueNode::Handle x)
195 assert(i>=0 && i<link_count());
197 if(i==0 && !set_value_node(x))
200 if(i==1 && !set_scalar(x))
203 signal_child_changed()(i);signal_value_changed()();
208 ValueNode::LooseHandle
209 ValueNode_Scale::get_link_vfunc(int i)const
211 assert(i>=0 && i<link_count());
221 ValueNode_Scale::link_count()const
227 ValueNode_Scale::link_local_name(int i)const
229 assert(i>=0 && i<link_count());
239 ValueNode_Scale::link_name(int i)const
241 assert(i>=0 && i<link_count());
251 ValueNode_Scale::get_link_index_from_name(const String &name)const
258 throw Exception::BadLinkName(name);
262 ValueNode_Scale::get_name()const
268 ValueNode_Scale::get_local_name()const
274 ValueNode_Scale::check_type(ValueBase::Type type)
277 type==ValueBase::TYPE_ANGLE ||
278 type==ValueBase::TYPE_COLOR ||
279 type==ValueBase::TYPE_INTEGER ||
280 type==ValueBase::TYPE_REAL ||
281 type==ValueBase::TYPE_TIME ||
282 type==ValueBase::TYPE_VECTOR;