1 /* === S Y N F I G ========================================================= */
2 /*! \file valuenode_subtract.cpp
3 ** \brief Implementation of the "Subtract" 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_subtract.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 synfig::ValueNode_Subtract::ValueNode_Subtract(const ValueBase &value):
60 LinkableValueNode(value.get_type())
63 ValueBase::Type id(value.get_type());
67 case ValueBase::TYPE_ANGLE:
68 set_link("lhs",ValueNode_Const::create(value.get(Angle())));
69 set_link("rhs",ValueNode_Const::create(Angle::deg(0)));
71 case ValueBase::TYPE_COLOR:
72 set_link("lhs",ValueNode_Const::create(value.get(Color())));
73 set_link("rhs",ValueNode_Const::create(Color(0,0,0,0)));
75 case ValueBase::TYPE_INTEGER:
76 set_link("lhs",ValueNode_Const::create(value.get(int())));
77 set_link("rhs",ValueNode_Const::create(int(0)));
79 case ValueBase::TYPE_REAL:
80 set_link("lhs",ValueNode_Const::create(value.get(Real())));
81 set_link("rhs",ValueNode_Const::create(Real(0)));
83 case ValueBase::TYPE_TIME:
84 set_link("lhs",ValueNode_Const::create(value.get(Time())));
85 set_link("rhs",ValueNode_Const::create(Time(0)));
87 case ValueBase::TYPE_VECTOR:
88 set_link("lhs",ValueNode_Const::create(value.get(Vector())));
89 set_link("rhs",ValueNode_Const::create(Vector(0,0)));
93 throw runtime_error(_("synfig::ValueNode_Subtract:Bad type ")+ValueBase::type_local_name(id));
96 assert(get_lhs()->get_type()==id);
97 assert(get_rhs()->get_type()==id);
98 assert(get_type()==id);
104 ValueNode_Subtract::create_new()const
106 return new ValueNode_Subtract(get_type());
110 ValueNode_Subtract::create(const ValueBase& value)
112 return new ValueNode_Subtract(value);
115 synfig::ValueNode_Subtract::~ValueNode_Subtract()
121 ValueNode_Subtract::set_scalar(Real value)
123 set_link("scalar",ValueNode_Const::create(value));
127 synfig::ValueNode_Subtract::set_scalar(ValueNode::Handle value)
129 if(value->get_type()!=ValueBase::TYPE_REAL&& !PlaceholderValueNode::Handle::cast_dynamic(value))
136 synfig::ValueNode_Subtract::set_lhs(ValueNode::Handle x)
141 (get_type()==ValueBase::TYPE_NIL && !check_type(x->get_type())) ||
142 (get_type()!=ValueBase::TYPE_NIL && x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x)))
151 synfig::ValueNode_Subtract::set_rhs(ValueNode::Handle x)
156 (get_type()==ValueBase::TYPE_NIL && !check_type(x->get_type())) ||
157 (get_type()!=ValueBase::TYPE_NIL && x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x)))
166 synfig::ValueNode_Subtract::operator()(Time t)const
169 throw runtime_error(strprintf("ValueNode_Subtract: %s",_("One or both of my parameters aren't set!")));
172 case ValueBase::TYPE_ANGLE:
173 return ((*ref_a)(t).get(Angle())-(*ref_b)(t).get(Angle()))*(*scalar)(t).get(Real());
174 case ValueBase::TYPE_COLOR:
175 return ((*ref_a)(t).get(Color())-(*ref_b)(t).get(Color()))*(*scalar)(t).get(Real());
176 case ValueBase::TYPE_INTEGER:
177 return round_to_int(((*ref_a)(t).get(int())-(*ref_b)(t).get(int()))*(*scalar)(t).get(Real()));
178 case ValueBase::TYPE_REAL:
179 return ((*ref_a)(t).get(Vector::value_type())-(*ref_b)(t).get(Vector::value_type()))*(*scalar)(t).get(Real());
180 case ValueBase::TYPE_TIME:
181 return ((*ref_a)(t).get(Time())-(*ref_b)(t).get(Time()))*(*scalar)(t).get(Real());
182 case ValueBase::TYPE_VECTOR:
183 return ((*ref_a)(t).get(Vector())-(*ref_b)(t).get(Vector()))*(*scalar)(t).get(Real());
192 ValueNode_Subtract::set_link_vfunc(int i,ValueNode::Handle value)
194 assert(i>=0 && i<link_count());
199 if(set_lhs(value)) { signal_child_changed()(i);signal_value_changed()(); return true; }
202 if(set_rhs(value)) { signal_child_changed()(i);signal_value_changed()(); return true; }
206 signal_child_changed()(i);signal_value_changed()();
213 ValueNode::LooseHandle
214 ValueNode_Subtract::get_link_vfunc(int i)const
216 assert(i>=0 && i<link_count());
220 case 0: return ref_a;
221 case 1: return ref_b;
222 case 2: return scalar;
228 ValueNode_Subtract::link_count()const
234 ValueNode_Subtract::link_local_name(int i)const
236 assert(i>=0 && i<link_count());
240 case 0: return _("LHS");
241 case 1: return _("RHS");
242 case 2: return _("Scalar");
243 default: return String();
248 ValueNode_Subtract::link_name(int i)const
250 assert(i>=0 && i<link_count());
254 case 0: return "lhs";
255 case 1: return "rhs";
256 case 2: return "scalar";
257 default: return String();
262 ValueNode_Subtract::get_link_index_from_name(const String &name)const
264 if(name=="lhs") return 0;
265 if(name=="rhs") return 1;
266 if(name=="scalar") return 2;
267 throw Exception::BadLinkName(name);
271 ValueNode_Subtract::get_name()const
277 ValueNode_Subtract::get_local_name()const
279 return _("Subtract");
283 ValueNode_Subtract::check_type(ValueBase::Type type)
285 return type==ValueBase::TYPE_ANGLE
286 || type==ValueBase::TYPE_COLOR
287 || type==ValueBase::TYPE_INTEGER
288 || type==ValueBase::TYPE_REAL
289 || type==ValueBase::TYPE_TIME
290 || type==ValueBase::TYPE_VECTOR;