1 /* === S Y N F I G ========================================================= */
2 /*! \file valuenode_blinecalcwidth.cpp
3 ** \brief Implementation of the "BLine Width" valuenode conversion.
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 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 ======================================================= */
33 #include "valuenode_blinecalcwidth.h"
34 #include "valuenode_bline.h"
35 #include "valuenode_const.h"
36 #include "valuenode_composite.h"
38 #include "exception.h"
39 #include <ETL/hermite>
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 ValueNode_BLineCalcWidth::ValueNode_BLineCalcWidth(const ValueBase::Type &x):
61 if(x!=ValueBase::TYPE_REAL)
62 throw Exception::BadType(ValueBase::type_local_name(x));
64 ValueNode_BLine* value_node(new ValueNode_BLine());
65 set_link("bline",value_node);
66 set_link("loop",ValueNode_Const::create(bool(false)));
67 set_link("amount",ValueNode_Const::create(Real(0.5)));
68 set_link("scale",ValueNode_Const::create(Real(1.0)));
72 ValueNode_BLineCalcWidth::create_new()const
74 return new ValueNode_BLineCalcWidth(ValueBase::TYPE_REAL);
77 ValueNode_BLineCalcWidth*
78 ValueNode_BLineCalcWidth::create(const ValueBase &x)
80 return new ValueNode_BLineCalcWidth(x.get_type());
83 ValueNode_BLineCalcWidth::~ValueNode_BLineCalcWidth()
89 ValueNode_BLineCalcWidth::operator()(Time t)const
91 const std::vector<ValueBase> bline((*bline_)(t));
92 handle<ValueNode_BLine> bline_value_node(bline_);
93 const bool looped(bline_value_node->get_loop());
94 int size = bline.size(), from_vertex;
95 bool loop((*loop_)(t).get(bool()));
96 Real amount((*amount_)(t).get(Real()));
97 Real scale((*scale_)(t).get(Real()));
98 BLinePoint blinepoint0, blinepoint1;
101 if (size < 1) return Real();
104 amount = amount - int(amount);
105 if (amount < 0) amount++;
109 if (amount < 0) amount = 0;
110 if (amount > 1) amount = 1;
113 vector<ValueBase>::const_iterator iter, next(bline.begin());
115 iter = looped ? --bline.end() : next++;
116 amount = amount * size;
117 from_vertex = int(amount);
118 if (from_vertex > size-1) from_vertex = size-1;
119 blinepoint0 = from_vertex ? *(next+from_vertex-1) : *iter;
120 blinepoint1 = *(next+from_vertex);
122 float width0 = blinepoint0.get_width();
123 float width1 = blinepoint1.get_width();
125 return Real((width0 + (amount-from_vertex) * (width1-width0)) * scale);
129 ValueNode_BLineCalcWidth::get_name()const
131 return "blinecalcwidth";
135 ValueNode_BLineCalcWidth::get_local_name()const
137 return _("BLine Width");
141 ValueNode_BLineCalcWidth::set_link_vfunc(int i,ValueNode::Handle value)
143 assert(i>=0 && i<link_count());
147 case 0: CHECK_TYPE_AND_SET_VALUE(bline_, ValueBase::TYPE_LIST);
148 case 1: CHECK_TYPE_AND_SET_VALUE(loop_, ValueBase::TYPE_BOOL);
149 case 2: CHECK_TYPE_AND_SET_VALUE(amount_, ValueBase::TYPE_REAL);
150 case 3: CHECK_TYPE_AND_SET_VALUE(scale_, ValueBase::TYPE_REAL);
155 ValueNode::LooseHandle
156 ValueNode_BLineCalcWidth::get_link_vfunc(int i)const
158 assert(i>=0 && i<link_count());
162 case 0: return bline_;
163 case 1: return loop_;
164 case 2: return amount_;
165 case 3: return scale_;
172 ValueNode_BLineCalcWidth::link_count()const
178 ValueNode_BLineCalcWidth::link_name(int i)const
180 assert(i>=0 && i<link_count());
184 case 0: return "bline";
185 case 1: return "loop";
186 case 2: return "amount";
187 case 3: return "scale";
193 ValueNode_BLineCalcWidth::link_local_name(int i)const
195 assert(i>=0 && i<link_count());
199 case 0: return _("BLine");
200 case 1: return _("Loop");
201 case 2: return _("Amount");
202 case 3: return _("Scale");
208 ValueNode_BLineCalcWidth::get_link_index_from_name(const String &name)const
210 if(name=="bline") return 0;
211 if(name=="loop") return 1;
212 if(name=="amount") return 2;
213 if(name=="scale") return 3;
214 throw Exception::BadLinkName(name);
218 ValueNode_BLineCalcWidth::check_type(ValueBase::Type type)
220 return type==ValueBase::TYPE_REAL;