1 /* === S Y N F I G ========================================================= */
2 /*! \file valuenode_subtract.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_subtract.h"
34 #include "valuenode_const.h"
43 /* === U S I N G =========================================================== */
47 using namespace synfig;
49 /* === M A C R O S ========================================================= */
51 /* === G L O B A L S ======================================================= */
53 /* === P R O C E D U R E S ================================================= */
55 /* === M E T H O D S ======================================================= */
57 synfig::ValueNode_Subtract::ValueNode_Subtract(const ValueBase &value):
58 LinkableValueNode(value.get_type())
61 ValueBase::Type id(value.get_type());
65 case ValueBase::TYPE_ANGLE:
66 set_link("lhs",ValueNode_Const::create(value.get(Angle())));
67 set_link("rhs",ValueNode_Const::create(Angle::deg(0)));
69 case ValueBase::TYPE_COLOR:
70 set_link("lhs",ValueNode_Const::create(value.get(Color())));
71 set_link("rhs",ValueNode_Const::create(Color(0,0,0,0)));
73 case ValueBase::TYPE_INTEGER:
74 set_link("lhs",ValueNode_Const::create(value.get(int())));
75 set_link("rhs",ValueNode_Const::create(int(0)));
77 case ValueBase::TYPE_REAL:
78 set_link("lhs",ValueNode_Const::create(value.get(Real())));
79 set_link("rhs",ValueNode_Const::create(Real(0)));
81 case ValueBase::TYPE_TIME:
82 set_link("lhs",ValueNode_Const::create(value.get(Time())));
83 set_link("rhs",ValueNode_Const::create(Time(0)));
85 case ValueBase::TYPE_VECTOR:
86 set_link("lhs",ValueNode_Const::create(value.get(Vector())));
87 set_link("rhs",ValueNode_Const::create(Vector(0,0)));
91 throw runtime_error("synfig::ValueNode_Subtract:Bad type "+ValueBase::type_name(id));
94 assert(get_lhs()->get_type()==id);
95 assert(get_rhs()->get_type()==id);
96 assert(get_type()==id);
102 ValueNode_Subtract::create_new()const
104 return new ValueNode_Subtract(get_type());
108 ValueNode_Subtract::create(const ValueBase& value)
110 return new ValueNode_Subtract(value);
113 synfig::ValueNode_Subtract::~ValueNode_Subtract()
119 ValueNode_Subtract::set_scalar(Real value)
121 set_link("scalar",ValueNode_Const::create(value));
125 synfig::ValueNode_Subtract::set_scalar(ValueNode::Handle value)
127 if(value->get_type()!=ValueBase::TYPE_REAL&& !PlaceholderValueNode::Handle::cast_dynamic(value))
134 synfig::ValueNode_Subtract::set_lhs(ValueNode::Handle x)
139 (get_type()==ValueBase::TYPE_NIL && !check_type(x->get_type())) ||
140 (get_type()!=ValueBase::TYPE_NIL && x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x)))
149 synfig::ValueNode_Subtract::set_rhs(ValueNode::Handle x)
154 (get_type()==ValueBase::TYPE_NIL && !check_type(x->get_type())) ||
155 (get_type()!=ValueBase::TYPE_NIL && x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x)))
164 synfig::ValueNode_Subtract::operator()(Time t)const
167 throw runtime_error(strprintf("ValueNode_Subtract: %s",_("One or both of my parameters aren't set!")));
170 case ValueBase::TYPE_ANGLE:
171 return ((*ref_a)(t).get(Angle())-(*ref_b)(t).get(Angle()))*(*scalar)(t).get(Real());
172 case ValueBase::TYPE_COLOR:
173 return ((*ref_a)(t).get(Color())-(*ref_b)(t).get(Color()))*(*scalar)(t).get(Real());
174 case ValueBase::TYPE_INTEGER:
176 Real ret = ((*ref_a)(t).get(int())-(*ref_b)(t).get(int()))*(*scalar)(t).get(Real()) + 0.5f;
177 if (ret < 0) return static_cast<int>(ret-1);
178 return static_cast<int>(ret);
180 case ValueBase::TYPE_REAL:
181 return ((*ref_a)(t).get(Vector::value_type())-(*ref_b)(t).get(Vector::value_type()))*(*scalar)(t).get(Real());
182 case ValueBase::TYPE_TIME:
183 return ((*ref_a)(t).get(Time())-(*ref_b)(t).get(Time()))*(*scalar)(t).get(Real());
184 case ValueBase::TYPE_VECTOR:
185 return ((*ref_a)(t).get(Vector())-(*ref_b)(t).get(Vector()))*(*scalar)(t).get(Real());
194 ValueNode_Subtract::set_link_vfunc(int i,ValueNode::Handle value)
200 if(set_lhs(value)) { signal_child_changed()(i);signal_value_changed()(); return true; }
203 if(set_rhs(value)) { signal_child_changed()(i);signal_value_changed()(); return true; }
207 signal_child_changed()(i);signal_value_changed()();
214 ValueNode::LooseHandle
215 ValueNode_Subtract::get_link_vfunc(int i)const
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
239 case 0: return _("LHS");
240 case 1: return _("RHS");
241 case 2: return _("Scalar");
242 default: return String();
247 ValueNode_Subtract::link_name(int i)const
252 case 0: return "lhs";
253 case 1: return "rhs";
254 case 2: return "scalar";
255 default: return String();
260 ValueNode_Subtract::get_link_index_from_name(const String &name)const
262 if(name=="lhs") return 0;
263 if(name=="rhs") return 1;
264 if(name=="scalar") return 2;
265 throw Exception::BadLinkName(name);
269 ValueNode_Subtract::get_name()const
275 ValueNode_Subtract::get_local_name()const
277 return _("Subtract");
281 ValueNode_Subtract::check_type(ValueBase::Type type)
283 return type==ValueBase::TYPE_ANGLE
284 || type==ValueBase::TYPE_COLOR
285 || type==ValueBase::TYPE_INTEGER
286 || type==ValueBase::TYPE_REAL
287 || type==ValueBase::TYPE_TIME
288 || type==ValueBase::TYPE_VECTOR;