Use link_count from children vocabulary and return the stored vocabulary if already...
[synfig.git] / synfig-core / src / synfig / valuenode_blinecalctangent.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_blinecalctangent.cpp
3 **      \brief Implementation of the "BLine Tangent" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 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_blinecalctangent.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 #include <ETL/calculus>
41
42 #endif
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_BLineCalcTangent::ValueNode_BLineCalcTangent(const ValueBase::Type &x):
59         LinkableValueNode(x)
60 {
61         Vocab ret(get_children_vocab());
62         set_children_vocab(ret);
63         if(x!=ValueBase::TYPE_ANGLE && x!=ValueBase::TYPE_REAL && x!=ValueBase::TYPE_VECTOR)
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("offset",ValueNode_Const::create(Angle::deg(0)));
71         set_link("scale",ValueNode_Const::create(Real(1.0)));
72         set_link("fixed_length",ValueNode_Const::create(bool(false)));
73 }
74
75 LinkableValueNode*
76 ValueNode_BLineCalcTangent::create_new()const
77 {
78         return new ValueNode_BLineCalcTangent(get_type());
79 }
80
81 ValueNode_BLineCalcTangent*
82 ValueNode_BLineCalcTangent::create(const ValueBase &x)
83 {
84         return new ValueNode_BLineCalcTangent(x.get_type());
85 }
86
87 ValueNode_BLineCalcTangent::~ValueNode_BLineCalcTangent()
88 {
89         unlink_all();
90 }
91
92 ValueBase
93 ValueNode_BLineCalcTangent::operator()(Time t, Real amount)const
94 {
95         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
96                 printf("%s:%d operator()\n", __FILE__, __LINE__);
97
98         const std::vector<ValueBase> bline((*bline_)(t).get_list());
99         handle<ValueNode_BLine> bline_value_node(bline_);
100         const bool looped(bline_value_node->get_loop());
101         int size = bline.size(), from_vertex;
102         bool loop((*loop_)(t).get(bool()));
103         Angle offset((*offset_)(t).get(Angle()));
104         Real scale((*scale_)(t).get(Real()));
105         bool fixed_length((*fixed_length_)(t).get(bool()));
106         BLinePoint blinepoint0, blinepoint1;
107
108         if (!looped) size--;
109         if (size < 1)
110                 switch (get_type())
111                 {
112                         case ValueBase::TYPE_ANGLE:  return Angle();
113                         case ValueBase::TYPE_REAL:   return Real();
114                         case ValueBase::TYPE_VECTOR: return Vector();
115                         default: assert(0); return ValueBase();
116                 }
117         if (loop)
118         {
119                 amount = amount - int(amount);
120                 if (amount < 0) amount++;
121         }
122         else
123         {
124                 if (amount < 0) amount = 0;
125                 if (amount > 1) amount = 1;
126         }
127
128         vector<ValueBase>::const_iterator iter, next(bline.begin());
129
130         iter = looped ? --bline.end() : next++;
131         amount = amount * size;
132         from_vertex = int(amount);
133         if (from_vertex > size-1) from_vertex = size-1;
134         blinepoint0 = from_vertex ? *(next+from_vertex-1) : *iter;
135         blinepoint1 = *(next+from_vertex);
136
137         etl::hermite<Vector> curve(blinepoint0.get_vertex(),   blinepoint1.get_vertex(),
138                                                            blinepoint0.get_tangent2(), blinepoint1.get_tangent1());
139         etl::derivative< etl::hermite<Vector> > deriv(curve);
140
141         switch (get_type())
142         {
143                 case ValueBase::TYPE_ANGLE:  return deriv(amount-from_vertex).angle() + offset;
144                 case ValueBase::TYPE_REAL:
145                 {
146                         if (fixed_length) return scale;
147                         return deriv(amount-from_vertex).mag() * scale;
148                 }
149                 case ValueBase::TYPE_VECTOR:
150                 {
151                         Vector tangent(deriv(amount-from_vertex));
152                         Angle angle(tangent.angle() + offset);
153                         Real mag = fixed_length ? scale : (tangent.mag() * scale);
154                         return Vector(Angle::cos(angle).get()*mag,
155                                                   Angle::sin(angle).get()*mag);
156                 }
157                 default: assert(0); return ValueBase();
158         }
159 }
160
161 ValueBase
162 ValueNode_BLineCalcTangent::operator()(Time t)const
163 {
164         Real amount((*amount_)(t).get(Real()));
165         return (*this)(t, amount);
166 }
167
168 String
169 ValueNode_BLineCalcTangent::get_name()const
170 {
171         return "blinecalctangent";
172 }
173
174 String
175 ValueNode_BLineCalcTangent::get_local_name()const
176 {
177         return _("BLine Tangent");
178 }
179
180 bool
181 ValueNode_BLineCalcTangent::set_link_vfunc(int i,ValueNode::Handle value)
182 {
183         assert(i>=0 && i<link_count());
184
185         switch(i)
186         {
187         case 0: CHECK_TYPE_AND_SET_VALUE(bline_,                ValueBase::TYPE_LIST);
188         case 1: CHECK_TYPE_AND_SET_VALUE(loop_,                 ValueBase::TYPE_BOOL);
189         case 2: CHECK_TYPE_AND_SET_VALUE(amount_,               ValueBase::TYPE_REAL);
190         case 3: CHECK_TYPE_AND_SET_VALUE(offset_,               ValueBase::TYPE_ANGLE);
191         case 4: CHECK_TYPE_AND_SET_VALUE(scale_,                ValueBase::TYPE_REAL);
192         case 5: CHECK_TYPE_AND_SET_VALUE(fixed_length_, ValueBase::TYPE_BOOL);
193         }
194         return false;
195 }
196
197 ValueNode::LooseHandle
198 ValueNode_BLineCalcTangent::get_link_vfunc(int i)const
199 {
200         assert(i>=0 && i<link_count());
201
202         switch(i)
203         {
204                 case 0: return bline_;
205                 case 1: return loop_;
206                 case 2: return amount_;
207                 case 3: return offset_;
208                 case 4: return scale_;
209                 case 5: return fixed_length_;
210         }
211
212         return 0;
213 }
214
215 int
216 ValueNode_BLineCalcTangent::link_count()const
217 {
218         return 6;
219 }
220
221 String
222 ValueNode_BLineCalcTangent::link_name(int i)const
223 {
224         assert(i>=0 && i<link_count());
225
226         switch(i)
227         {
228                 case 0: return "bline";
229                 case 1: return "loop";
230                 case 2: return "amount";
231                 case 3: return "offset";
232                 case 4: return "scale";
233                 case 5: return "fixed_length";
234         }
235         return String();
236 }
237
238 String
239 ValueNode_BLineCalcTangent::link_local_name(int i)const
240 {
241         assert(i>=0 && i<link_count());
242
243         switch(i)
244         {
245                 case 0: return _("BLine");
246                 case 1: return _("Loop");
247                 case 2: return _("Amount");
248                 case 3: return _("Offset");
249                 case 4: return _("Scale");
250                 case 5: return _("Fixed Length");
251         }
252         return String();
253 }
254
255 int
256 ValueNode_BLineCalcTangent::get_link_index_from_name(const String &name)const
257 {
258         if (name=="bline")                return 0;
259         if (name=="loop")                 return 1;
260         if (name=="amount")               return 2;
261         if (name=="offset")               return 3;
262         if (name=="scale")                return 4;
263         if (name=="fixed_length") return 5;
264         throw Exception::BadLinkName(name);
265 }
266
267 bool
268 ValueNode_BLineCalcTangent::check_type(ValueBase::Type type)
269 {
270         return (type==ValueBase::TYPE_ANGLE ||
271                         type==ValueBase::TYPE_REAL  ||
272                         type==ValueBase::TYPE_VECTOR);
273 }
274
275 LinkableValueNode::Vocab
276 ValueNode_BLineCalcTangent::get_children_vocab_vfunc()const
277 {
278         if(children_vocab.size())
279                 return children_vocab;
280
281         LinkableValueNode::Vocab ret;
282
283         ret.push_back(ParamDesc(ValueBase(),"bline")
284                 .set_local_name(_("BLine"))
285                 .set_description(_("The BLine where the tangent is linked to"))
286         );
287
288         ret.push_back(ParamDesc(ValueBase(),"loop")
289                 .set_local_name(_("Loop"))
290                 .set_description(_("When checked, the amount would loop"))
291         );
292
293         ret.push_back(ParamDesc(ValueBase(),"amount")
294                 .set_local_name(_("Amount"))
295                 .set_description(_("The position of the linked tangent on the BLine (0,1]"))
296         );
297
298         ret.push_back(ParamDesc(ValueBase(),"offset")
299                 .set_local_name(_("Offset"))
300                 .set_description(_("Angle offset of the tangent"))
301         );
302
303         ret.push_back(ParamDesc(ValueBase(),"scale")
304                 .set_local_name(_("Scale"))
305                 .set_description(_("Scale of the tangent"))
306         );
307
308         ret.push_back(ParamDesc(ValueBase(),"fixed_length")
309                 .set_local_name(_("Fixed Length"))
310                 .set_description(_("When checked, the tangent's length is fixed"))
311         );
312         return ret;
313 }