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, 2008 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"
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 synfig::ValueNode_Subtract::ValueNode_Subtract(const ValueBase &value):
61 LinkableValueNode(value.get_type())
63 set_link("scalar",ValueNode_Const::create(Real(1.0)));
64 ValueBase::Type id(value.get_type());
68 case ValueBase::TYPE_ANGLE:
69 set_link("lhs",ValueNode_Const::create(value.get(Angle())));
70 set_link("rhs",ValueNode_Const::create(Angle::deg(0)));
72 case ValueBase::TYPE_COLOR:
73 set_link("lhs",ValueNode_Const::create(value.get(Color())));
74 set_link("rhs",ValueNode_Const::create(Color(0,0,0,0)));
76 case ValueBase::TYPE_GRADIENT:
77 set_link("lhs",ValueNode_Const::create(value.get(Gradient())));
78 set_link("rhs",ValueNode_Const::create(Gradient()));
80 case ValueBase::TYPE_INTEGER:
81 set_link("lhs",ValueNode_Const::create(value.get(int())));
82 set_link("rhs",ValueNode_Const::create(int(0)));
84 case ValueBase::TYPE_REAL:
85 set_link("lhs",ValueNode_Const::create(value.get(Real())));
86 set_link("rhs",ValueNode_Const::create(Real(0)));
88 case ValueBase::TYPE_TIME:
89 set_link("lhs",ValueNode_Const::create(value.get(Time())));
90 set_link("rhs",ValueNode_Const::create(Time(0)));
92 case ValueBase::TYPE_VECTOR:
93 set_link("lhs",ValueNode_Const::create(value.get(Vector())));
94 set_link("rhs",ValueNode_Const::create(Vector(0,0)));
98 throw runtime_error(get_local_name()+_(":Bad type ")+ValueBase::type_local_name(id));
101 assert(ref_a->get_type()==id);
102 assert(ref_b->get_type()==id);
103 assert(get_type()==id);
107 ValueNode_Subtract::create_new()const
109 return new ValueNode_Subtract(get_type());
113 ValueNode_Subtract::create(const ValueBase& value)
115 return new ValueNode_Subtract(value);
118 synfig::ValueNode_Subtract::~ValueNode_Subtract()
124 synfig::ValueNode_Subtract::operator()(Time t)const
126 if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
127 printf("%s:%d operator()\n", __FILE__, __LINE__);
130 throw runtime_error(strprintf("ValueNode_Subtract: %s",_("One or both of my parameters aren't set!")));
133 case ValueBase::TYPE_ANGLE:
134 return ((*ref_a)(t).get(Angle())-(*ref_b)(t).get(Angle()))*(*scalar)(t).get(Real());
135 case ValueBase::TYPE_COLOR:
136 return ((*ref_a)(t).get(Color())-(*ref_b)(t).get(Color()))*(*scalar)(t).get(Real());
137 case ValueBase::TYPE_GRADIENT:
138 return ((*ref_a)(t).get(Gradient())-(*ref_b)(t).get(Gradient()))*(*scalar)(t).get(Real());
139 case ValueBase::TYPE_INTEGER:
140 return round_to_int(((*ref_a)(t).get(int())-(*ref_b)(t).get(int()))*(*scalar)(t).get(Real()));
141 case ValueBase::TYPE_REAL:
142 return ((*ref_a)(t).get(Vector::value_type())-(*ref_b)(t).get(Vector::value_type()))*(*scalar)(t).get(Real());
143 case ValueBase::TYPE_TIME:
144 return ((*ref_a)(t).get(Time())-(*ref_b)(t).get(Time()))*(*scalar)(t).get(Real());
145 case ValueBase::TYPE_VECTOR:
146 return ((*ref_a)(t).get(Vector())-(*ref_b)(t).get(Vector()))*(*scalar)(t).get(Real());
155 ValueNode_Subtract::set_link_vfunc(int i,ValueNode::Handle value)
157 assert(i>=0 && i<link_count());
161 case 0: CHECK_TYPE_AND_SET_VALUE(ref_a, get_type());
162 case 1: CHECK_TYPE_AND_SET_VALUE(ref_b, get_type());
163 case 2: CHECK_TYPE_AND_SET_VALUE(scalar, ValueBase::TYPE_REAL);
168 ValueNode::LooseHandle
169 ValueNode_Subtract::get_link_vfunc(int i)const
171 assert(i>=0 && i<link_count());
175 case 0: return ref_a;
176 case 1: return ref_b;
177 case 2: return scalar;
183 ValueNode_Subtract::link_count()const
189 ValueNode_Subtract::link_local_name(int i)const
191 assert(i>=0 && i<link_count());
195 case 0: return _("LHS");
196 case 1: return _("RHS");
197 case 2: return _("Scalar");
198 default: return String();
203 ValueNode_Subtract::link_name(int i)const
205 assert(i>=0 && i<link_count());
209 case 0: return "lhs";
210 case 1: return "rhs";
211 case 2: return "scalar";
212 default: return String();
217 ValueNode_Subtract::get_link_index_from_name(const String &name)const
219 if(name=="lhs") return 0;
220 if(name=="rhs") return 1;
221 if(name=="scalar") return 2;
222 throw Exception::BadLinkName(name);
226 ValueNode_Subtract::get_name()const
232 ValueNode_Subtract::get_local_name()const
234 return _("Subtract");
238 ValueNode_Subtract::check_type(ValueBase::Type type)
240 return type==ValueBase::TYPE_ANGLE
241 || type==ValueBase::TYPE_COLOR
242 || type==ValueBase::TYPE_GRADIENT
243 || type==ValueBase::TYPE_INTEGER
244 || type==ValueBase::TYPE_REAL
245 || type==ValueBase::TYPE_TIME
246 || type==ValueBase::TYPE_VECTOR;