1 /* === S Y N F I G ========================================================= */
2 /*! \file valuenode_add.cpp
3 ** \brief Template File
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_add.h"
35 #include "valuenode_const.h"
44 /* === U S I N G =========================================================== */
48 using namespace synfig;
50 /* === M A C R O S ========================================================= */
52 /* === G L O B A L S ======================================================= */
54 /* === P R O C E D U R E S ================================================= */
56 /* === M E T H O D S ======================================================= */
58 synfig::ValueNode_Add::ValueNode_Add(const ValueBase &value):
59 LinkableValueNode(value.get_type())
62 ValueBase::Type id(value.get_type());
66 case ValueBase::TYPE_ANGLE:
67 set_link("lhs",ValueNode_Const::create(value.get(Angle())));
68 set_link("rhs",ValueNode_Const::create(Angle::deg(0)));
70 case ValueBase::TYPE_COLOR:
71 set_link("lhs",ValueNode_Const::create(value.get(Color())));
72 set_link("rhs",ValueNode_Const::create(Color(0,0,0,0)));
74 case ValueBase::TYPE_INTEGER:
75 set_link("lhs",ValueNode_Const::create(value.get(int())));
76 set_link("rhs",ValueNode_Const::create(int(0)));
78 case ValueBase::TYPE_REAL:
79 set_link("lhs",ValueNode_Const::create(value.get(Real())));
80 set_link("rhs",ValueNode_Const::create(Real(0)));
82 case ValueBase::TYPE_TIME:
83 set_link("lhs",ValueNode_Const::create(value.get(Time())));
84 set_link("rhs",ValueNode_Const::create(Time(0)));
86 case ValueBase::TYPE_VECTOR:
87 set_link("lhs",ValueNode_Const::create(value.get(Vector())));
88 set_link("rhs",ValueNode_Const::create(Vector(0,0)));
92 throw runtime_error("synfig::ValueNode_Add:Bad type "+ValueBase::type_name(id));
95 assert(get_lhs()->get_type()==id);
96 assert(get_rhs()->get_type()==id);
97 assert(get_type()==id);
103 ValueNode_Add::create_new()const
105 return new ValueNode_Add(get_type());
109 ValueNode_Add::create(const ValueBase& value)
111 return new ValueNode_Add(value);
114 synfig::ValueNode_Add::~ValueNode_Add()
120 ValueNode_Add::set_scalar(Real value)
122 set_link("scalar",ValueNode_Const::create(value));
126 synfig::ValueNode_Add::set_scalar(ValueNode::Handle value)
128 if(value->get_type()!=ValueBase::TYPE_REAL&& !PlaceholderValueNode::Handle::cast_dynamic(value))
135 synfig::ValueNode_Add::set_lhs(ValueNode::Handle x)
140 (get_type()==ValueBase::TYPE_NIL && !check_type(x->get_type())) ||
141 (get_type()!=ValueBase::TYPE_NIL && x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x)))
150 synfig::ValueNode_Add::set_rhs(ValueNode::Handle x)
155 (get_type()==ValueBase::TYPE_NIL && !check_type(x->get_type())) ||
156 (get_type()!=ValueBase::TYPE_NIL && x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x)))
165 synfig::ValueNode_Add::operator()(Time t)const
168 throw runtime_error(strprintf("ValueNode_Add: %s",_("One or both of my parameters aren't set!")));
171 case ValueBase::TYPE_ANGLE:
172 return ((*ref_a)(t).get(Angle())+(*ref_b)(t).get(Angle()))*(*scalar)(t).get(Real());
173 case ValueBase::TYPE_COLOR:
174 return ((*ref_a)(t).get(Color())+(*ref_b)(t).get(Color()))*(*scalar)(t).get(Real());
175 case ValueBase::TYPE_INTEGER:
177 Real ret = ((*ref_a)(t).get(int())+(*ref_b)(t).get(int()))*(*scalar)(t).get(Real()) + 0.5f;
178 if (ret < 0) return static_cast<int>(ret-1);
179 return static_cast<int>(ret);
181 case ValueBase::TYPE_REAL:
182 return ((*ref_a)(t).get(Vector::value_type())+(*ref_b)(t).get(Vector::value_type()))*(*scalar)(t).get(Real());
183 case ValueBase::TYPE_TIME:
184 return ((*ref_a)(t).get(Time())+(*ref_b)(t).get(Time()))*(*scalar)(t).get(Real());
185 case ValueBase::TYPE_VECTOR:
186 return ((*ref_a)(t).get(Vector())+(*ref_b)(t).get(Vector()))*(*scalar)(t).get(Real());
195 ValueNode_Add::set_link_vfunc(int i,ValueNode::Handle value)
201 if(set_lhs(value)) { signal_child_changed()(i);signal_value_changed()(); return true; }
204 if(set_rhs(value)) { signal_child_changed()(i);signal_value_changed()(); return true; }
208 signal_child_changed()(i);signal_value_changed()();
215 ValueNode::LooseHandle
216 ValueNode_Add::get_link_vfunc(int i)const
221 case 0: return ref_a;
222 case 1: return ref_b;
223 case 2: return scalar;
229 ValueNode_Add::link_count()const
235 ValueNode_Add::link_local_name(int i)const
240 case 0: return _("LHS");
241 case 1: return _("RHS");
242 case 2: return _("Scalar");
243 default: return String();
248 ValueNode_Add::link_name(int i)const
253 case 0: return "lhs";
254 case 1: return "rhs";
255 case 2: return "scalar";
256 default: return String();
261 ValueNode_Add::get_link_index_from_name(const String &name)const
263 if(name=="lhs") return 0;
264 if(name=="rhs") return 1;
265 if(name=="scalar") return 2;
266 throw Exception::BadLinkName(name);
270 ValueNode_Add::get_name()const
276 ValueNode_Add::get_local_name()const
282 ValueNode_Add::check_type(ValueBase::Type type)
284 return type==ValueBase::TYPE_ANGLE
285 || type==ValueBase::TYPE_COLOR
286 || type==ValueBase::TYPE_INTEGER
287 || type==ValueBase::TYPE_REAL
288 || type==ValueBase::TYPE_TIME
289 || type==ValueBase::TYPE_VECTOR;