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"
46 /* === U S I N G =========================================================== */
50 using namespace synfig;
52 /* === M A C R O S ========================================================= */
54 /* === G L O B A L S ======================================================= */
56 /* === P R O C E D U R E S ================================================= */
58 /* === M E T H O D S ======================================================= */
60 ValueNode_Scale::ValueNode_Scale(const ValueBase &value):
61 LinkableValueNode(value.get_type())
64 ValueBase::Type id(value.get_type());
68 case ValueBase::TYPE_ANGLE:
69 set_link("link",ValueNode_Const::create(value.get(Angle())));
71 case ValueBase::TYPE_COLOR:
72 set_link("link",ValueNode_Const::create(value.get(Color())));
74 case ValueBase::TYPE_INTEGER:
75 set_link("link",ValueNode_Const::create(value.get(int())));
77 case ValueBase::TYPE_REAL:
78 set_link("link",ValueNode_Const::create(value.get(Real())));
80 case ValueBase::TYPE_TIME:
81 set_link("link",ValueNode_Const::create(value.get(Time())));
83 case ValueBase::TYPE_VECTOR:
84 set_link("link",ValueNode_Const::create(value.get(Vector())));
88 throw runtime_error("synfig::ValueNode_Scale:Bad type "+ValueBase::type_name(value.get_type()));
92 assert(get_value_node()->get_type()==id);
93 assert(get_type()==id);
97 ValueNode_Scale::create_new()const
99 return new ValueNode_Scale(get_type());
103 ValueNode_Scale::create(const ValueBase& value)
105 return new ValueNode_Scale(value);
108 synfig::ValueNode_Scale::~ValueNode_Scale()
114 ValueNode_Scale::set_scalar(Real x)
116 set_link("scalar",ValueNode::Handle(ValueNode_Const::create(x)));
120 ValueNode_Scale::set_scalar(const ValueNode::Handle &x)
123 || x->get_type()!=ValueBase::TYPE_REAL
124 && !PlaceholderValueNode::Handle::cast_dynamic(x)
132 ValueNode_Scale::get_scalar()const
138 ValueNode_Scale::set_value_node(const ValueNode::Handle &x)
142 // if this isn't a proper value
144 // or we don't have a type, and this value isn't one of the types we accept
145 (get_type()==ValueBase::TYPE_NIL && !check_type(x->get_type())) ||
146 // or we have a type and this value is a different type and (placeholder?)
147 (get_type()!=ValueBase::TYPE_NIL && x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x)))
148 // then fail to set the value
157 ValueNode_Scale::get_value_node()const
164 synfig::ValueNode_Scale::operator()(Time t)const
166 if(!value_node || !scalar)
167 throw runtime_error(strprintf("ValueNode_Scale: %s",_("One or both of my parameters aren't set!")));
168 else if(get_type()==ValueBase::TYPE_ANGLE)
169 return (*value_node)(t).get(Angle())*(*scalar)(t).get(Real());
170 else if(get_type()==ValueBase::TYPE_COLOR)
172 Color ret((*value_node)(t).get(Color()));
173 Real s((*scalar)(t).get(Real()));
174 ret.set_r(ret.get_r()*s);
175 ret.set_g(ret.get_g()*s);
176 ret.set_b(ret.get_b()*s);
179 else if(get_type()==ValueBase::TYPE_INTEGER)
180 return round_to_int((*value_node)(t).get(int())*(*scalar)(t).get(Real()));
181 else if(get_type()==ValueBase::TYPE_REAL)
182 return (*value_node)(t).get(Real())*(*scalar)(t).get(Real());
183 else if(get_type()==ValueBase::TYPE_TIME)
184 return (*value_node)(t).get(Time())*(*scalar)(t).get(Time());
185 else if(get_type()==ValueBase::TYPE_VECTOR)
186 return (*value_node)(t).get(Vector())*(*scalar)(t).get(Real());
194 ValueNode_Scale::set_link_vfunc(int i,ValueNode::Handle x)
199 if(i==0 && !set_value_node(x))
202 if(i==1 && !set_scalar(x))
205 signal_child_changed()(i);signal_value_changed()();
210 ValueNode::LooseHandle
211 ValueNode_Scale::get_link_vfunc(int i)const
213 assert(i==0 || i==1);
222 ValueNode_Scale::link_count()const
228 ValueNode_Scale::link_local_name(int i)const
230 assert(i==0 || i==1);
239 ValueNode_Scale::link_name(int i)const
241 assert(i==0 || i==1);
250 ValueNode_Scale::get_link_index_from_name(const String &name)const
257 throw Exception::BadLinkName(name);
261 ValueNode_Scale::get_name()const
267 ValueNode_Scale::get_local_name()const
273 ValueNode_Scale::check_type(ValueBase::Type type)
276 type==ValueBase::TYPE_ANGLE ||
277 type==ValueBase::TYPE_COLOR ||
278 type==ValueBase::TYPE_INTEGER ||
279 type==ValueBase::TYPE_REAL ||
280 type==ValueBase::TYPE_TIME ||
281 type==ValueBase::TYPE_VECTOR;