From 19814e54d027ea1a98c532218966212298b9b555 Mon Sep 17 00:00:00 2001 From: dooglus Date: Sun, 23 Sep 2007 00:08:23 +0000 Subject: [PATCH] Added a new valuenode conversion type called 'Add'. It's like 'Subtract', only it adds... Previously addition was only possible by first scaling the RHS by -1 and then subtracting, which seems a bit silly. git-svn-id: http://svn.voria.com/code@742 1f10aa63-cdf2-0310-b900-c93c546f37ac --- synfig-core/trunk/src/synfig/Makefile.am | 4 +- synfig-core/trunk/src/synfig/valuenode.cpp | 2 + synfig-core/trunk/src/synfig/valuenode_add.cpp | 283 +++++++++++++++++++++++++ synfig-core/trunk/src/synfig/valuenode_add.h | 109 ++++++++++ 4 files changed, 396 insertions(+), 2 deletions(-) create mode 100644 synfig-core/trunk/src/synfig/valuenode_add.cpp create mode 100644 synfig-core/trunk/src/synfig/valuenode_add.h diff --git a/synfig-core/trunk/src/synfig/Makefile.am b/synfig-core/trunk/src/synfig/Makefile.am index 2c62161..2ae61f3 100644 --- a/synfig-core/trunk/src/synfig/Makefile.am +++ b/synfig-core/trunk/src/synfig/Makefile.am @@ -16,8 +16,8 @@ TARGETSOURCES=target_scanline.cpp target_tile.cpp target_multi.cpp target_null.c IMPORTERHEADERS=listimporter.h IMPORTERSOURCES=listimporter.cpp -VALUENODEHEADERS=valuenode_exp.h valuenode_sine.h valuenode_radialcomposite.h valuenode_bline.h valuenode_segcalcvertex.h valuenode_segcalctangent.h valuenode_twotone.h valuenode_repeat_gradient.h valuenode_stripes.h valuenode_subtract.h valuenode_const.h valuenode_reference.h valuenode_linear.h valuenode_composite.h valuenode_dynamiclist.h valuenode_animated.h valuenode_scale.h valuenode_timedswap.h valuenode_gradientrotate.h -VALUENODESOURCES=valuenode_exp.cpp valuenode_sine.cpp valuenode_radialcomposite.cpp valuenode_bline.cpp valuenode_segcalcvertex.cpp valuenode_segcalctangent.cpp valuenode_twotone.cpp valuenode_repeat_gradient.cpp valuenode_stripes.cpp valuenode_subtract.cpp valuenode_const.cpp valuenode_reference.cpp valuenode_linear.cpp valuenode_composite.cpp valuenode_dynamiclist.cpp valuenode_animated.cpp valuenode_scale.cpp valuenode_timedswap.cpp valuenode_gradientrotate.cpp +VALUENODEHEADERS=valuenode_exp.h valuenode_sine.h valuenode_radialcomposite.h valuenode_bline.h valuenode_segcalcvertex.h valuenode_segcalctangent.h valuenode_twotone.h valuenode_repeat_gradient.h valuenode_stripes.h valuenode_add.h valuenode_subtract.h valuenode_const.h valuenode_reference.h valuenode_linear.h valuenode_composite.h valuenode_dynamiclist.h valuenode_animated.h valuenode_scale.h valuenode_timedswap.h valuenode_gradientrotate.h +VALUENODESOURCES=valuenode_exp.cpp valuenode_sine.cpp valuenode_radialcomposite.cpp valuenode_bline.cpp valuenode_segcalcvertex.cpp valuenode_segcalctangent.cpp valuenode_twotone.cpp valuenode_repeat_gradient.cpp valuenode_stripes.cpp valuenode_add.cpp valuenode_subtract.cpp valuenode_const.cpp valuenode_reference.cpp valuenode_linear.cpp valuenode_composite.cpp valuenode_dynamiclist.cpp valuenode_animated.cpp valuenode_scale.cpp valuenode_timedswap.cpp valuenode_gradientrotate.cpp VALUEHEADERS=blinepoint.h gradient.h value.h VALUESOURCES=blinepoint.cpp gradient.cpp value.cpp diff --git a/synfig-core/trunk/src/synfig/valuenode.cpp b/synfig-core/trunk/src/synfig/valuenode.cpp index 61714a2..77d172a 100644 --- a/synfig-core/trunk/src/synfig/valuenode.cpp +++ b/synfig-core/trunk/src/synfig/valuenode.cpp @@ -46,6 +46,7 @@ #include "valuenode_segcalcvertex.h" #include "valuenode_repeat_gradient.h" #include "valuenode_stripes.h" +#include "valuenode_add.h" #include "valuenode_subtract.h" #include "valuenode_timedswap.h" #include "valuenode_twotone.h" @@ -109,6 +110,7 @@ ValueNode::subsys_init() ADD_VALUENODE(ValueNode_SegCalcTangent, "segcalctangent", _("Segment Tangent") ); ADD_VALUENODE(ValueNode_SegCalcVertex, "segcalcvertex", _("Segment Vertex") ); ADD_VALUENODE(ValueNode_Stripes, "stripes", _("Stripes") ); + ADD_VALUENODE(ValueNode_Subtract, "add", _("Add") ); ADD_VALUENODE(ValueNode_Subtract, "subtract", _("Subtract") ); ADD_VALUENODE(ValueNode_TimedSwap, "timed_swap", _("Timed Swap") ); ADD_VALUENODE(ValueNode_TwoTone, "twotone", _("Two-Tone") ); diff --git a/synfig-core/trunk/src/synfig/valuenode_add.cpp b/synfig-core/trunk/src/synfig/valuenode_add.cpp new file mode 100644 index 0000000..f9ddaf6 --- /dev/null +++ b/synfig-core/trunk/src/synfig/valuenode_add.cpp @@ -0,0 +1,283 @@ +/* === S Y N F I G ========================================================= */ +/*! \file valuenode_add.cpp +** \brief Template File +** +** $Id$ +** +** \legal +** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley +** +** This package is free software; you can redistribute it and/or +** modify it under the terms of the GNU General Public License as +** published by the Free Software Foundation; either version 2 of +** the License, or (at your option) any later version. +** +** This package is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +** General Public License for more details. +** \endlegal +*/ +/* ========================================================================= */ + +/* === H E A D E R S ======================================================= */ + +#ifdef USING_PCH +# include "pch.h" +#else +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "general.h" +#include "valuenode_add.h" +#include "valuenode_const.h" +#include +#include "color.h" +#include "vector.h" +#include "angle.h" +#include "real.h" + +#endif + +/* === U S I N G =========================================================== */ + +using namespace std; +using namespace etl; +using namespace synfig; + +/* === M A C R O S ========================================================= */ + +/* === G L O B A L S ======================================================= */ + +/* === P R O C E D U R E S ================================================= */ + +/* === M E T H O D S ======================================================= */ + +synfig::ValueNode_Add::ValueNode_Add(const ValueBase &value): + LinkableValueNode(value.get_type()) +{ + set_scalar(1.0); + ValueBase::Type id(value.get_type()); + + switch(id) + { + case ValueBase::TYPE_ANGLE: + set_link("lhs",ValueNode_Const::create(value.get(Angle()))); + set_link("rhs",ValueNode_Const::create(Angle::deg(0))); + break; + case ValueBase::TYPE_COLOR: + set_link("lhs",ValueNode_Const::create(value.get(Color()))); + set_link("rhs",ValueNode_Const::create(Color(0,0,0,0))); + break; + case ValueBase::TYPE_INTEGER: + set_link("lhs",ValueNode_Const::create(value.get(int()))); + set_link("rhs",ValueNode_Const::create(int(0))); + break; + case ValueBase::TYPE_REAL: + set_link("lhs",ValueNode_Const::create(value.get(Real()))); + set_link("rhs",ValueNode_Const::create(Real(0))); + break; + case ValueBase::TYPE_VECTOR: + set_link("lhs",ValueNode_Const::create(value.get(Vector()))); + set_link("rhs",ValueNode_Const::create(Vector(0,0))); + break; + default: + assert(0); + throw runtime_error("synfig::ValueNode_Add:Bad type "+ValueBase::type_name(id)); + } + + assert(get_lhs()->get_type()==id); + assert(get_rhs()->get_type()==id); + assert(get_type()==id); + + DCAST_HACK_ENABLE(); +} + +LinkableValueNode* +ValueNode_Add::create_new()const +{ + return new ValueNode_Add(get_type()); +} + +ValueNode_Add* +ValueNode_Add::create(const ValueBase& value) +{ + return new ValueNode_Add(value); +} + +synfig::ValueNode_Add::~ValueNode_Add() +{ + unlink_all(); +} + +void +ValueNode_Add::set_scalar(Real value) +{ + set_link("scalar",ValueNode_Const::create(value)); +} + +bool +synfig::ValueNode_Add::set_scalar(ValueNode::Handle value) +{ + if(value->get_type()!=ValueBase::TYPE_REAL&& !PlaceholderValueNode::Handle::cast_dynamic(value)) + return false; + scalar=value; + return true; +} + +bool +synfig::ValueNode_Add::set_lhs(ValueNode::Handle x) +{ + assert(get_type()); + + if(!x || + (get_type()==ValueBase::TYPE_NIL && !check_type(x->get_type())) || + (get_type()!=ValueBase::TYPE_NIL && x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x))) + return false; + + ref_a=x; + + return true; +} + +bool +synfig::ValueNode_Add::set_rhs(ValueNode::Handle x) +{ + assert(get_type()); + + if(!x || + (get_type()==ValueBase::TYPE_NIL && !check_type(x->get_type())) || + (get_type()!=ValueBase::TYPE_NIL && x->get_type()!=get_type() && !PlaceholderValueNode::Handle::cast_dynamic(x))) + return false; + + ref_b=x; + + return true; +} + +synfig::ValueBase +synfig::ValueNode_Add::operator()(Time t)const +{ + if(!ref_a || !ref_b) + throw runtime_error(strprintf("ValueNode_Add: %s",_("One or both of my parameters aren't set!"))); + switch(get_type()) + { + case ValueBase::TYPE_ANGLE: + return ((*ref_a)(t).get(Angle())+(*ref_b)(t).get(Angle()))*(*scalar)(t).get(Real()); + case ValueBase::TYPE_COLOR: + return ((*ref_a)(t).get(Color())+(*ref_b)(t).get(Color()))*(*scalar)(t).get(Real()); + case ValueBase::TYPE_INTEGER: + { + Real ret = ((*ref_a)(t).get(int())+(*ref_b)(t).get(int()))*(*scalar)(t).get(Real()) + 0.5f; + if (ret < 0) return static_cast(ret-1); + return static_cast(ret); + } + case ValueBase::TYPE_REAL: + return ((*ref_a)(t).get(Vector::value_type())+(*ref_b)(t).get(Vector::value_type()))*(*scalar)(t).get(Real()); + case ValueBase::TYPE_VECTOR: + return ((*ref_a)(t).get(Vector())+(*ref_b)(t).get(Vector()))*(*scalar)(t).get(Real()); + default: + assert(0); + break; + } + return ValueBase(); +} + +bool +ValueNode_Add::set_link_vfunc(int i,ValueNode::Handle value) +{ + assert(i>=0 && i<3); + switch(i) + { + case 0: + if(set_lhs(value)) { signal_child_changed()(i);signal_value_changed()(); return true; } + return false; + case 1: + if(set_rhs(value)) { signal_child_changed()(i);signal_value_changed()(); return true; } + return false; + case 2: + scalar=value; + signal_child_changed()(i);signal_value_changed()(); + return true; + } + + return false; +} + +ValueNode::LooseHandle +ValueNode_Add::get_link_vfunc(int i)const +{ + assert(i>=0 && i<3); + switch(i) + { + case 0: return ref_a; + case 1: return ref_b; + case 2: return scalar; + default: return 0; + } +} + +int +ValueNode_Add::link_count()const +{ + return 3; +} + +String +ValueNode_Add::link_local_name(int i)const +{ + assert(i>=0 && i<3); + switch(i) + { + case 0: return _("LHS"); + case 1: return _("RHS"); + case 2: return _("Scalar"); + default: return String(); + } +} + +String +ValueNode_Add::link_name(int i)const +{ + assert(i>=0 && i<3); + switch(i) + { + case 0: return "lhs"; + case 1: return "rhs"; + case 2: return "scalar"; + default: return String(); + } +} + +int +ValueNode_Add::get_link_index_from_name(const String &name)const +{ + printf("%s:%d link_index_from_name\n", __FILE__, __LINE__); + if(name=="lhs") return 0; + if(name=="rhs") return 1; + if(name=="scalar") return 2; + throw Exception::BadLinkName(name); +} + +String +ValueNode_Add::get_name()const +{ + return "add"; +} + +String +ValueNode_Add::get_local_name()const +{ + return _("Add"); +} + +bool +ValueNode_Add::check_type(ValueBase::Type type) +{ + return type==ValueBase::TYPE_ANGLE + || type==ValueBase::TYPE_COLOR + || type==ValueBase::TYPE_INTEGER + || type==ValueBase::TYPE_REAL + || type==ValueBase::TYPE_VECTOR; +} diff --git a/synfig-core/trunk/src/synfig/valuenode_add.h b/synfig-core/trunk/src/synfig/valuenode_add.h new file mode 100644 index 0000000..4437a03 --- /dev/null +++ b/synfig-core/trunk/src/synfig/valuenode_add.h @@ -0,0 +1,109 @@ +/* === S Y N F I G ========================================================= */ +/*! \file valuenode_add.h +** \brief Template Header +** +** $Id$ +** +** \legal +** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley +** +** This package is free software; you can redistribute it and/or +** modify it under the terms of the GNU General Public License as +** published by the Free Software Foundation; either version 2 of +** the License, or (at your option) any later version. +** +** This package is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +** General Public License for more details. +** \endlegal +*/ +/* ========================================================================= */ + +/* === S T A R T =========================================================== */ + +#ifndef __SYNFIG_VALUENODE_ADD_H +#define __SYNFIG_VALUENODE_ADD_H + +/* === H E A D E R S ======================================================= */ + +#include "valuenode.h" + +/* === M A C R O S ========================================================= */ + +/* === C L A S S E S & S T R U C T S ======================================= */ + +namespace synfig { + +struct ValueNode_Add : public LinkableValueNode +{ + typedef etl::handle Handle; + typedef etl::handle ConstHandle; + +protected: + + ValueNode_Add(const ValueBase &value); + +private: + + ValueNode::RHandle ref_a; + ValueNode::RHandle ref_b; + ValueNode::RHandle scalar; + +public: + + virtual ~ValueNode_Add(); + +// static Handle create(ValueBase::Type id=ValueBase::TYPE_NIL); + + //! Sets the left-hand-side value_node + bool set_lhs(ValueNode::Handle a); + + //! Gets the left-hand-side value_node + ValueNode::Handle get_lhs()const { return ref_a; } + + //! Sets the right-hand-side value_node + bool set_rhs(ValueNode::Handle b); + + //! Gets the right-hand-side value_node + ValueNode::Handle get_rhs()const { return ref_b; } + + //! Sets the scalar value_node + bool set_scalar(ValueNode::Handle x); + + //! Gets the scalar value_node + ValueNode::Handle get_scalar()const { return scalar; } + + void set_scalar(Real x); + + virtual bool set_link_vfunc(int i,ValueNode::Handle x); + + virtual ValueNode::LooseHandle get_link_vfunc(int i)const; + + virtual int link_count()const; + + virtual String link_local_name(int i)const; + virtual String link_name(int i)const; + virtual int get_link_index_from_name(const String &name)const; + + virtual ValueBase operator()(Time t)const; + + virtual String get_name()const; + virtual String get_local_name()const; + +// static bool check_type(const ValueBase::Type &type); + + LinkableValueNode* create_new()const; + +public: + using synfig::LinkableValueNode::get_link_vfunc; + using synfig::LinkableValueNode::set_link_vfunc; + static bool check_type(ValueBase::Type type); + static ValueNode_Add* create(const ValueBase &value=ValueBase()); +}; // END of class ValueNode_Add + +}; // END of namespace synfig + +/* === E N D =============================================================== */ + +#endif -- 2.7.4