Use LinkableValueNode members functions when possible in the derived valuenodes.
[synfig.git] / synfig-core / src / synfig / valuenode_blinecalcwidth.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_blinecalcwidth.cpp
3 **      \brief Implementation of the "BLine Width" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2008 Chris Moore
10 **
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.
15 **
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.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "valuenode_blinecalcwidth.h"
34 #include "valuenode_bline.h"
35 #include "valuenode_const.h"
36 #include "valuenode_composite.h"
37 #include "general.h"
38 #include "exception.h"
39 #include <ETL/hermite>
40
41 #endif
42
43
44 /* === U S I N G =========================================================== */
45
46 using namespace std;
47 using namespace etl;
48 using namespace synfig;
49
50 /* === M A C R O S ========================================================= */
51
52 /* === G L O B A L S ======================================================= */
53
54 /* === P R O C E D U R E S ================================================= */
55
56 /* === M E T H O D S ======================================================= */
57
58 ValueNode_BLineCalcWidth::ValueNode_BLineCalcWidth(const ValueBase::Type &x):
59         LinkableValueNode(x)
60 {
61         Vocab ret(get_children_vocab());
62         set_children_vocab(ret);
63         if(x!=ValueBase::TYPE_REAL)
64                 throw Exception::BadType(ValueBase::type_local_name(x));
65
66         ValueNode_BLine* value_node(new ValueNode_BLine());
67         set_link("bline",value_node);
68         set_link("loop",ValueNode_Const::create(bool(false)));
69         set_link("amount",ValueNode_Const::create(Real(0.5)));
70         set_link("scale",ValueNode_Const::create(Real(1.0)));
71 }
72
73 LinkableValueNode*
74 ValueNode_BLineCalcWidth::create_new()const
75 {
76         return new ValueNode_BLineCalcWidth(ValueBase::TYPE_REAL);
77 }
78
79 ValueNode_BLineCalcWidth*
80 ValueNode_BLineCalcWidth::create(const ValueBase &x)
81 {
82         return new ValueNode_BLineCalcWidth(x.get_type());
83 }
84
85 ValueNode_BLineCalcWidth::~ValueNode_BLineCalcWidth()
86 {
87         unlink_all();
88 }
89
90 ValueBase
91 ValueNode_BLineCalcWidth::operator()(Time t, Real amount)const
92 {
93         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
94                 printf("%s:%d operator()\n", __FILE__, __LINE__);
95
96         const std::vector<ValueBase> bline((*bline_)(t).get_list());
97         handle<ValueNode_BLine> bline_value_node(bline_);
98         const bool looped(bline_value_node->get_loop());
99         int size = bline.size(), from_vertex;
100         bool loop((*loop_)(t).get(bool()));
101         Real scale((*scale_)(t).get(Real()));
102         BLinePoint blinepoint0, blinepoint1;
103
104         if (!looped) size--;
105         if (size < 1) return Real();
106         if (loop)
107         {
108                 amount = amount - int(amount);
109                 if (amount < 0) amount++;
110         }
111         else
112         {
113                 if (amount < 0) amount = 0;
114                 if (amount > 1) amount = 1;
115         }
116
117         vector<ValueBase>::const_iterator iter, next(bline.begin());
118
119         iter = looped ? --bline.end() : next++;
120         amount = amount * size;
121         from_vertex = int(amount);
122         if (from_vertex > size-1) from_vertex = size-1;
123         blinepoint0 = from_vertex ? *(next+from_vertex-1) : *iter;
124         blinepoint1 = *(next+from_vertex);
125
126         float width0 = blinepoint0.get_width();
127         float width1 = blinepoint1.get_width();
128
129         return Real((width0 + (amount-from_vertex) * (width1-width0)) * scale);
130 }
131
132 ValueBase
133 ValueNode_BLineCalcWidth::operator()(Time t)const
134 {
135         Real amount((*amount_)(t).get(Real()));
136         return (*this)(t, amount);
137 }
138
139 String
140 ValueNode_BLineCalcWidth::get_name()const
141 {
142         return "blinecalcwidth";
143 }
144
145 String
146 ValueNode_BLineCalcWidth::get_local_name()const
147 {
148         return _("BLine Width");
149 }
150
151 bool
152 ValueNode_BLineCalcWidth::set_link_vfunc(int i,ValueNode::Handle value)
153 {
154         assert(i>=0 && i<link_count());
155
156         switch(i)
157         {
158         case 0: CHECK_TYPE_AND_SET_VALUE(bline_,  ValueBase::TYPE_LIST);
159         case 1: CHECK_TYPE_AND_SET_VALUE(loop_,   ValueBase::TYPE_BOOL);
160         case 2: CHECK_TYPE_AND_SET_VALUE(amount_, ValueBase::TYPE_REAL);
161         case 3: CHECK_TYPE_AND_SET_VALUE(scale_,  ValueBase::TYPE_REAL);
162         }
163         return false;
164 }
165
166 ValueNode::LooseHandle
167 ValueNode_BLineCalcWidth::get_link_vfunc(int i)const
168 {
169         assert(i>=0 && i<link_count());
170
171         switch(i)
172         {
173                 case 0: return bline_;
174                 case 1: return loop_;
175                 case 2: return amount_;
176                 case 3: return scale_;
177         }
178
179         return 0;
180 }
181
182 bool
183 ValueNode_BLineCalcWidth::check_type(ValueBase::Type type)
184 {
185         return type==ValueBase::TYPE_REAL;
186 }
187
188 LinkableValueNode::Vocab
189 ValueNode_BLineCalcWidth::get_children_vocab_vfunc()const
190 {
191         if(children_vocab.size())
192                 return children_vocab;
193
194         LinkableValueNode::Vocab ret;
195
196         ret.push_back(ParamDesc(ValueBase(),"bline")
197                 .set_local_name(_("BLine"))
198                 .set_description(_("The BLine where the width is linked to"))
199         );
200
201         ret.push_back(ParamDesc(ValueBase(),"loop")
202                 .set_local_name(_("Loop"))
203                 .set_description(_("When checked, the amount would loop"))
204         );
205
206         ret.push_back(ParamDesc(ValueBase(),"amount")
207                 .set_local_name(_("Amount"))
208                 .set_description(_("The position of the linked width on the BLine (0,1]"))
209         );
210
211         ret.push_back(ParamDesc(ValueBase(),"scale")
212                 .set_local_name(_("Scale"))
213                 .set_description(_("Scale of the width"))
214         );
215
216         return ret;
217 }
218