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
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_add.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_Add::ValueNode_Add(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_VECTOR:
82 set_link("lhs",ValueNode_Const::create(value.get(Vector())));
83 set_link("rhs",ValueNode_Const::create(Vector(0,0)));
87 throw runtime_error("synfig::ValueNode_Add:Bad type "+ValueBase::type_name(id));
90 assert(get_lhs()->get_type()==id);
91 assert(get_rhs()->get_type()==id);
92 assert(get_type()==id);
98 ValueNode_Add::create_new()const
100 return new ValueNode_Add(get_type());
104 ValueNode_Add::create(const ValueBase& value)
106 return new ValueNode_Add(value);
109 synfig::ValueNode_Add::~ValueNode_Add()
115 ValueNode_Add::set_scalar(Real value)
117 set_link("scalar",ValueNode_Const::create(value));
121 synfig::ValueNode_Add::set_scalar(ValueNode::Handle value)
123 if(value->get_type()!=ValueBase::TYPE_REAL&& !PlaceholderValueNode::Handle::cast_dynamic(value))
130 synfig::ValueNode_Add::set_lhs(ValueNode::Handle x)
135 (get_type()==ValueBase::TYPE_NIL && !check_type(x->get_type())) ||
136 (get_type()!=ValueBase::TYPE_NIL && x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x)))
145 synfig::ValueNode_Add::set_rhs(ValueNode::Handle x)
150 (get_type()==ValueBase::TYPE_NIL && !check_type(x->get_type())) ||
151 (get_type()!=ValueBase::TYPE_NIL && x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x)))
160 synfig::ValueNode_Add::operator()(Time t)const
163 throw runtime_error(strprintf("ValueNode_Add: %s",_("One or both of my parameters aren't set!")));
166 case ValueBase::TYPE_ANGLE:
167 return ((*ref_a)(t).get(Angle())+(*ref_b)(t).get(Angle()))*(*scalar)(t).get(Real());
168 case ValueBase::TYPE_COLOR:
169 return ((*ref_a)(t).get(Color())+(*ref_b)(t).get(Color()))*(*scalar)(t).get(Real());
170 case ValueBase::TYPE_INTEGER:
172 Real ret = ((*ref_a)(t).get(int())+(*ref_b)(t).get(int()))*(*scalar)(t).get(Real()) + 0.5f;
173 if (ret < 0) return static_cast<int>(ret-1);
174 return static_cast<int>(ret);
176 case ValueBase::TYPE_REAL:
177 return ((*ref_a)(t).get(Vector::value_type())+(*ref_b)(t).get(Vector::value_type()))*(*scalar)(t).get(Real());
178 case ValueBase::TYPE_VECTOR:
179 return ((*ref_a)(t).get(Vector())+(*ref_b)(t).get(Vector()))*(*scalar)(t).get(Real());
188 ValueNode_Add::set_link_vfunc(int i,ValueNode::Handle value)
194 if(set_lhs(value)) { signal_child_changed()(i);signal_value_changed()(); return true; }
197 if(set_rhs(value)) { signal_child_changed()(i);signal_value_changed()(); return true; }
201 signal_child_changed()(i);signal_value_changed()();
208 ValueNode::LooseHandle
209 ValueNode_Add::get_link_vfunc(int i)const
214 case 0: return ref_a;
215 case 1: return ref_b;
216 case 2: return scalar;
222 ValueNode_Add::link_count()const
228 ValueNode_Add::link_local_name(int i)const
233 case 0: return _("LHS");
234 case 1: return _("RHS");
235 case 2: return _("Scalar");
236 default: return String();
241 ValueNode_Add::link_name(int i)const
246 case 0: return "lhs";
247 case 1: return "rhs";
248 case 2: return "scalar";
249 default: return String();
254 ValueNode_Add::get_link_index_from_name(const String &name)const
256 printf("%s:%d link_index_from_name\n", __FILE__, __LINE__);
257 if(name=="lhs") return 0;
258 if(name=="rhs") return 1;
259 if(name=="scalar") return 2;
260 throw Exception::BadLinkName(name);
264 ValueNode_Add::get_name()const
270 ValueNode_Add::get_local_name()const
276 ValueNode_Add::check_type(ValueBase::Type type)
278 return type==ValueBase::TYPE_ANGLE
279 || type==ValueBase::TYPE_COLOR
280 || type==ValueBase::TYPE_INTEGER
281 || type==ValueBase::TYPE_REAL
282 || type==ValueBase::TYPE_VECTOR;