Use LinkableValueNode members functions when possible in the derived valuenodes.
[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         Vocab ret(get_children_vocab());
62         set_children_vocab(ret);
63         if(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 }
71
72 LinkableValueNode*
73 ValueNode_BLineCalcVertex::create_new()const
74 {
75         return new ValueNode_BLineCalcVertex(ValueBase::TYPE_VECTOR);
76 }
77
78 ValueNode_BLineCalcVertex*
79 ValueNode_BLineCalcVertex::create(const ValueBase &x)
80 {
81         return new ValueNode_BLineCalcVertex(x.get_type());
82 }
83
84 ValueNode_BLineCalcVertex::~ValueNode_BLineCalcVertex()
85 {
86         unlink_all();
87 }
88
89 ValueBase
90 ValueNode_BLineCalcVertex::operator()(Time t)const
91 {
92         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
93                 printf("%s:%d operator()\n", __FILE__, __LINE__);
94
95         const std::vector<ValueBase> bline((*bline_)(t).get_list());
96         handle<ValueNode_BLine> bline_value_node(bline_);
97         const bool looped(bline_value_node->get_loop());
98         int size = bline.size(), from_vertex;
99         bool loop((*loop_)(t).get(bool()));
100         Real amount((*amount_)(t).get(Real()));
101         BLinePoint blinepoint0, blinepoint1;
102
103         if (!looped) size--;
104         if (size < 1) return Vector();
105         if (loop)
106         {
107                 amount = amount - int(amount);
108                 if (amount < 0) amount++;
109         }
110         else
111         {
112                 if (amount < 0) amount = 0;
113                 if (amount > 1) amount = 1;
114         }
115
116         vector<ValueBase>::const_iterator iter, next(bline.begin());
117
118         iter = looped ? --bline.end() : next++;
119         amount = amount * size;
120         from_vertex = int(amount);
121         if (from_vertex > size-1) from_vertex = size-1;
122         blinepoint0 = from_vertex ? *(next+from_vertex-1) : *iter;
123         blinepoint1 = *(next+from_vertex);
124
125         etl::hermite<Vector> curve(blinepoint0.get_vertex(),   blinepoint1.get_vertex(),
126                                                            blinepoint0.get_tangent2(), blinepoint1.get_tangent1());
127         return curve(amount-from_vertex);
128 }
129
130
131
132
133
134
135
136 String
137 ValueNode_BLineCalcVertex::get_name()const
138 {
139         return "blinecalcvertex";
140 }
141
142 String
143 ValueNode_BLineCalcVertex::get_local_name()const
144 {
145         return _("BLine Vertex");
146 }
147
148 bool
149 ValueNode_BLineCalcVertex::set_link_vfunc(int i,ValueNode::Handle value)
150 {
151         assert(i>=0 && i<link_count());
152
153         switch(i)
154         {
155         case 0: CHECK_TYPE_AND_SET_VALUE(bline_,  ValueBase::TYPE_LIST);
156         case 1: CHECK_TYPE_AND_SET_VALUE(loop_,   ValueBase::TYPE_BOOL);
157         case 2: CHECK_TYPE_AND_SET_VALUE(amount_, ValueBase::TYPE_REAL);
158         }
159         return false;
160 }
161
162 ValueNode::LooseHandle
163 ValueNode_BLineCalcVertex::get_link_vfunc(int i)const
164 {
165         assert(i>=0 && i<link_count());
166
167         switch(i)
168         {
169                 case 0: return bline_;
170                 case 1: return loop_;
171                 case 2: return amount_;
172         }
173
174         return 0;
175 }
176
177 bool
178 ValueNode_BLineCalcVertex::check_type(ValueBase::Type type)
179 {
180         return type==ValueBase::TYPE_VECTOR;
181 }
182
183 LinkableValueNode::Vocab
184 ValueNode_BLineCalcVertex::get_children_vocab_vfunc()const
185 {
186         if(children_vocab.size())
187                 return children_vocab;
188
189         LinkableValueNode::Vocab ret;
190
191         ret.push_back(ParamDesc(ValueBase(),"bline")
192                 .set_local_name(_("BLine"))
193                 .set_description(_("The BLine where the vertex is linked to"))
194         );
195
196         ret.push_back(ParamDesc(ValueBase(),"loop")
197                 .set_local_name(_("Loop"))
198                 .set_description(_("When checked, the amount would loop"))
199         );
200
201         ret.push_back(ParamDesc(ValueBase(),"amount")
202                 .set_local_name(_("Amount"))
203                 .set_description(_("The position of the linked vertex on the BLine (0,1]"))
204         );
205
206         return ret;
207 }