Rename get_param_vocab to get_children_vocab and use a wrapper for the pure virtual...
[synfig.git] / synfig-core / src / synfig / valuenode_blinecalcvertex.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_blinecalcvertex.cpp
3 **      \brief Implementation of the "BLine Vertex" 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_blinecalcvertex.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_BLineCalcVertex::ValueNode_BLineCalcVertex(const ValueBase::Type &x):
59         LinkableValueNode(x)
60 {
61         if(x!=ValueBase::TYPE_VECTOR)
62                 throw Exception::BadType(ValueBase::type_local_name(x));
63
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 }
69
70 LinkableValueNode*
71 ValueNode_BLineCalcVertex::create_new()const
72 {
73         return new ValueNode_BLineCalcVertex(ValueBase::TYPE_VECTOR);
74 }
75
76 ValueNode_BLineCalcVertex*
77 ValueNode_BLineCalcVertex::create(const ValueBase &x)
78 {
79         return new ValueNode_BLineCalcVertex(x.get_type());
80 }
81
82 ValueNode_BLineCalcVertex::~ValueNode_BLineCalcVertex()
83 {
84         unlink_all();
85 }
86
87 ValueBase
88 ValueNode_BLineCalcVertex::operator()(Time t)const
89 {
90         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
91                 printf("%s:%d operator()\n", __FILE__, __LINE__);
92
93         const std::vector<ValueBase> bline((*bline_)(t).get_list());
94         handle<ValueNode_BLine> bline_value_node(bline_);
95         const bool looped(bline_value_node->get_loop());
96         int size = bline.size(), from_vertex;
97         bool loop((*loop_)(t).get(bool()));
98         Real amount((*amount_)(t).get(Real()));
99         BLinePoint blinepoint0, blinepoint1;
100
101         if (!looped) size--;
102         if (size < 1) return Vector();
103         if (loop)
104         {
105                 amount = amount - int(amount);
106                 if (amount < 0) amount++;
107         }
108         else
109         {
110                 if (amount < 0) amount = 0;
111                 if (amount > 1) amount = 1;
112         }
113
114         vector<ValueBase>::const_iterator iter, next(bline.begin());
115
116         iter = looped ? --bline.end() : next++;
117         amount = amount * size;
118         from_vertex = int(amount);
119         if (from_vertex > size-1) from_vertex = size-1;
120         blinepoint0 = from_vertex ? *(next+from_vertex-1) : *iter;
121         blinepoint1 = *(next+from_vertex);
122
123         etl::hermite<Vector> curve(blinepoint0.get_vertex(),   blinepoint1.get_vertex(),
124                                                            blinepoint0.get_tangent2(), blinepoint1.get_tangent1());
125         return curve(amount-from_vertex);
126 }
127
128
129
130
131
132
133
134 String
135 ValueNode_BLineCalcVertex::get_name()const
136 {
137         return "blinecalcvertex";
138 }
139
140 String
141 ValueNode_BLineCalcVertex::get_local_name()const
142 {
143         return _("BLine Vertex");
144 }
145
146 bool
147 ValueNode_BLineCalcVertex::set_link_vfunc(int i,ValueNode::Handle value)
148 {
149         assert(i>=0 && i<link_count());
150
151         switch(i)
152         {
153         case 0: CHECK_TYPE_AND_SET_VALUE(bline_,  ValueBase::TYPE_LIST);
154         case 1: CHECK_TYPE_AND_SET_VALUE(loop_,   ValueBase::TYPE_BOOL);
155         case 2: CHECK_TYPE_AND_SET_VALUE(amount_, ValueBase::TYPE_REAL);
156         }
157         return false;
158 }
159
160 ValueNode::LooseHandle
161 ValueNode_BLineCalcVertex::get_link_vfunc(int i)const
162 {
163         assert(i>=0 && i<link_count());
164
165         switch(i)
166         {
167                 case 0: return bline_;
168                 case 1: return loop_;
169                 case 2: return amount_;
170         }
171
172         return 0;
173 }
174
175 int
176 ValueNode_BLineCalcVertex::link_count()const
177 {
178         return 3;
179 }
180
181 String
182 ValueNode_BLineCalcVertex::link_name(int i)const
183 {
184         assert(i>=0 && i<link_count());
185
186         switch(i)
187         {
188                 case 0: return "bline";
189                 case 1: return "loop";
190                 case 2: return "amount";
191         }
192         return String();
193 }
194
195 String
196 ValueNode_BLineCalcVertex::link_local_name(int i)const
197 {
198         assert(i>=0 && i<link_count());
199
200         switch(i)
201         {
202                 case 0: return _("BLine");
203                 case 1: return _("Loop");
204                 case 2: return _("Amount");
205         }
206         return String();
207 }
208
209 int
210 ValueNode_BLineCalcVertex::get_link_index_from_name(const String &name)const
211 {
212         if(name=="bline")  return 0;
213         if(name=="loop")   return 1;
214         if(name=="amount") return 2;
215         throw Exception::BadLinkName(name);
216 }
217
218 bool
219 ValueNode_BLineCalcVertex::check_type(ValueBase::Type type)
220 {
221         return type==ValueBase::TYPE_VECTOR;
222 }
223
224 LinkableValueNode::Vocab
225 ValueNode_BLineCalcVertex::get_children_vocab_vfunc()const
226 {
227         LinkableValueNode::Vocab ret;
228
229         ret.push_back(ParamDesc(ValueBase(),"bline")
230                 .set_local_name(_("BLine"))
231                 .set_description(_("The BLine where the vertex is linked to"))
232         );
233
234         ret.push_back(ParamDesc(ValueBase(),"loop")
235                 .set_local_name(_("Loop"))
236                 .set_description(_("When checked, the amount would loop"))
237         );
238
239         ret.push_back(ParamDesc(ValueBase(),"amount")
240                 .set_local_name(_("Amount"))
241                 .set_description(_("The position of the linked vertex on the BLine (0,1]"))
242         );
243
244         return ret;
245 }