From d4612e4267d70142f71bd8b7bef9d59051522949 Mon Sep 17 00:00:00 2001 From: dooglus Date: Thu, 27 Sep 2007 20:43:40 +0000 Subject: [PATCH] Add the 'range' convert type, for limiting a parameter to values between a given minimum and maximum. git-svn-id: http://svn.voria.com/code@776 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_range.cpp | 229 +++++++++++++++++++++++ synfig-core/trunk/src/synfig/valuenode_range.h | 80 ++++++++ 4 files changed, 313 insertions(+), 2 deletions(-) create mode 100644 synfig-core/trunk/src/synfig/valuenode_range.cpp create mode 100644 synfig-core/trunk/src/synfig/valuenode_range.h diff --git a/synfig-core/trunk/src/synfig/Makefile.am b/synfig-core/trunk/src/synfig/Makefile.am index 03658cf..02cbfc9 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_blinecalcvertex.h valuenode_blinecalctangent.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_blinecalcvertex.cpp valuenode_blinecalctangent.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 +VALUENODEHEADERS=valuenode_exp.h valuenode_sine.h valuenode_radialcomposite.h valuenode_bline.h valuenode_blinecalcvertex.h valuenode_blinecalctangent.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_range.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_blinecalcvertex.cpp valuenode_blinecalctangent.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_range.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 eb652c6..713a245 100644 --- a/synfig-core/trunk/src/synfig/valuenode.cpp +++ b/synfig-core/trunk/src/synfig/valuenode.cpp @@ -48,6 +48,7 @@ #include "valuenode_segcalcvertex.h" #include "valuenode_repeat_gradient.h" #include "valuenode_stripes.h" +#include "valuenode_range.h" #include "valuenode_add.h" #include "valuenode_subtract.h" #include "valuenode_timedswap.h" @@ -114,6 +115,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_Range, "range", _("Range") ); ADD_VALUENODE(ValueNode_Add, "add", _("Add") ); ADD_VALUENODE(ValueNode_Subtract, "subtract", _("Subtract") ); ADD_VALUENODE(ValueNode_TimedSwap, "timed_swap", _("Timed Swap") ); diff --git a/synfig-core/trunk/src/synfig/valuenode_range.cpp b/synfig-core/trunk/src/synfig/valuenode_range.cpp new file mode 100644 index 0000000..9c2df59 --- /dev/null +++ b/synfig-core/trunk/src/synfig/valuenode_range.cpp @@ -0,0 +1,229 @@ +/* === S Y N F I G ========================================================= */ +/*! \file valuenode_range.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_range.h" +#include "valuenode_const.h" +#include +#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_Range::ValueNode_Range(const ValueBase &value): + LinkableValueNode(value.get_type()) +{ + ValueBase::Type id(value.get_type()); + + switch(id) + { + case ValueBase::TYPE_ANGLE: + set_link("min",ValueNode_Const::create(value.get(Angle()))); + set_link("max",ValueNode_Const::create(value.get(Angle()))); + set_link("link",ValueNode_Const::create(value.get(Angle()))); + break; + case ValueBase::TYPE_INTEGER: + set_link("min",ValueNode_Const::create(value.get(int()))); + set_link("max",ValueNode_Const::create(value.get(int()))); + set_link("link",ValueNode_Const::create(value.get(int()))); + break; + case ValueBase::TYPE_REAL: + set_link("min",ValueNode_Const::create(value.get(Real()))); + set_link("max",ValueNode_Const::create(value.get(Real()))); + set_link("link",ValueNode_Const::create(value.get(Real()))); + break; + default: + assert(0); + throw runtime_error("synfig::ValueNode_Range:Bad type "+ValueBase::type_name(id)); + } + + assert(min_->get_type()==id); + assert(max_->get_type()==id); + assert(link_->get_type()==id); + assert(get_type()==id); + + DCAST_HACK_ENABLE(); +} + +LinkableValueNode* +ValueNode_Range::create_new()const +{ + return new ValueNode_Range(get_type()); +} + +ValueNode_Range* +ValueNode_Range::create(const ValueBase& value) +{ + return new ValueNode_Range(value); +} + +synfig::ValueNode_Range::~ValueNode_Range() +{ + unlink_all(); +} + +#define min(x,y) (x>y ? y : x) +#define max(x,y) (x>y ? x : y) +#define range(low,high,input) (min(high,max(low,input))) + +synfig::ValueBase +synfig::ValueNode_Range::operator()(Time t)const +{ + if(!min_ || !max_ || !link_) + throw runtime_error(strprintf("ValueNode_Range: %s",_("Some of my parameters aren't set!"))); + + switch(get_type()) + { + case ValueBase::TYPE_ANGLE: + return range((*min_)(t).get(Angle()), (*max_)(t).get(Angle()), (*link_)(t).get(Angle())); + case ValueBase::TYPE_INTEGER: + return range((*min_)(t).get(int()), (*max_)(t).get(int()), (*link_)(t).get(int())); + case ValueBase::TYPE_REAL: + return range((*min_)(t).get(Real()), (*max_)(t).get(Real()), (*link_)(t).get(Real())); + default: + assert(0); + break; + } + return ValueBase(); +} + +bool +ValueNode_Range::set_link_vfunc(int i,ValueNode::Handle value) +{ + assert(i>=0 && i<3); + switch(i) + { + case 0: + min_=value; + signal_child_changed()(i);signal_value_changed()(); + return true; + case 1: + max_=value; + signal_child_changed()(i);signal_value_changed()(); + return true; + case 2: + link_=value; + signal_child_changed()(i);signal_value_changed()(); + return true; + } + + return false; +} + +ValueNode::LooseHandle +ValueNode_Range::get_link_vfunc(int i)const +{ + assert(i>=0 && i<3); + switch(i) + { + case 0: return min_; + case 1: return max_; + case 2: return link_; + default: return 0; + } +} + +int +ValueNode_Range::link_count()const +{ + return 3; +} + +String +ValueNode_Range::link_local_name(int i)const +{ + assert(i>=0 && i<3); + switch(i) + { + case 0: return _("Min"); + case 1: return _("Max"); + case 2: return _("Link"); + default: return String(); + } +} + +String +ValueNode_Range::link_name(int i)const +{ + assert(i>=0 && i<3); + switch(i) + { + case 0: return "min"; + case 1: return "max"; + case 2: return "link"; + default: return String(); + } +} + +int +ValueNode_Range::get_link_index_from_name(const String &name)const +{ + printf("%s:%d link_index_from_name\n", __FILE__, __LINE__); + if(name=="min") return 0; + if(name=="max") return 1; + if(name=="link") return 2; + throw Exception::BadLinkName(name); +} + +String +ValueNode_Range::get_name()const +{ + return "range"; +} + +String +ValueNode_Range::get_local_name()const +{ + return _("Range"); +} + +bool +ValueNode_Range::check_type(ValueBase::Type type) +{ + return type==ValueBase::TYPE_ANGLE + || type==ValueBase::TYPE_INTEGER + || type==ValueBase::TYPE_REAL; +} diff --git a/synfig-core/trunk/src/synfig/valuenode_range.h b/synfig-core/trunk/src/synfig/valuenode_range.h new file mode 100644 index 0000000..06db38f --- /dev/null +++ b/synfig-core/trunk/src/synfig/valuenode_range.h @@ -0,0 +1,80 @@ +/* === S Y N F I G ========================================================= */ +/*! \file valuenode_range.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_RANGE_H +#define __SYNFIG_VALUENODE_RANGE_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 { + +class ValueNode_Range : public LinkableValueNode +{ + ValueNode::RHandle min_; + ValueNode::RHandle max_; + ValueNode::RHandle link_; + + ValueNode_Range(const ValueBase &value); + +public: + + typedef etl::handle Handle; + typedef etl::handle ConstHandle; + + virtual ValueBase operator()(Time t)const; + + virtual ~ValueNode_Range(); + + virtual String get_name()const; + virtual String get_local_name()const; + + virtual ValueNode::LooseHandle get_link_vfunc(int i)const; + virtual int link_count()const; + virtual String link_name(int i)const; + + virtual String link_local_name(int i)const; + virtual int get_link_index_from_name(const String &name)const; + +protected: + virtual bool set_link_vfunc(int i,ValueNode::Handle x); + 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_Range* create(const ValueBase &value=ValueBase()); +}; // END of class ValueNode_Range + +}; // END of namespace synfig + +/* === E N D =============================================================== */ + +#endif -- 2.7.4