Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07_rc2 / src / synfig / valuenode_blinecalcvertex.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_blinecalcvertex.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "valuenode_blinecalcvertex.h"
33 #include "valuenode_bline.h"
34 #include "valuenode_const.h"
35 #include "valuenode_composite.h"
36 #include "general.h"
37 #include "exception.h"
38 #include <ETL/hermite>
39
40 #endif
41
42
43 /* === U S I N G =========================================================== */
44
45 using namespace std;
46 using namespace etl;
47 using namespace synfig;
48
49 /* === M A C R O S ========================================================= */
50
51 /* === G L O B A L S ======================================================= */
52
53 /* === P R O C E D U R E S ================================================= */
54
55 /* === M E T H O D S ======================================================= */
56
57 ValueNode_BLineCalcVertex::ValueNode_BLineCalcVertex(const ValueBase::Type &x):
58         LinkableValueNode(x)
59 {
60         if(x!=ValueBase::TYPE_VECTOR)
61                 throw Exception::BadType(ValueBase::type_name(x));
62
63         ValueNode_BLine* value_node(new ValueNode_BLine());
64         set_link("bline",value_node);
65         set_link("loop",ValueNode_Const::create(bool(false)));
66         set_link("amount",ValueNode_Const::create(Real(0.5)));
67 }
68
69 LinkableValueNode*
70 ValueNode_BLineCalcVertex::create_new()const
71 {
72         return new ValueNode_BLineCalcVertex(ValueBase::TYPE_VECTOR);
73 }
74
75 ValueNode_BLineCalcVertex*
76 ValueNode_BLineCalcVertex::create(const ValueBase &x)
77 {
78         return new ValueNode_BLineCalcVertex(x.get_type());
79 }
80
81 ValueNode_BLineCalcVertex::~ValueNode_BLineCalcVertex()
82 {
83         unlink_all();
84 }
85
86 ValueBase
87 ValueNode_BLineCalcVertex::operator()(Time t)const
88 {
89         const std::vector<ValueBase> bline((*bline_)(t));
90         handle<ValueNode_BLine> bline_value_node(bline_);
91         const bool looped(bline_value_node->get_loop());
92         int size = bline.size(), from_vertex;
93         bool loop((*loop_)(t).get(bool()));
94         Real amount((*amount_)(t).get(Real()));
95         BLinePoint blinepoint0, blinepoint1;
96
97         if (!looped) size--;
98         if (size < 1) return Vector();
99         if (loop)
100         {
101                 amount = amount - int(amount);
102                 if (amount < 0) amount++;
103         }
104         else
105         {
106                 if (amount < 0) amount = 0;
107                 if (amount > 1) amount = 1;
108         }
109
110         vector<ValueBase>::const_iterator iter, next(bline.begin());
111
112         iter = looped ? --bline.end() : next++;
113         amount = amount * size;
114         from_vertex = int(amount);
115         if (from_vertex > size-1) from_vertex = size-1;
116         blinepoint0 = from_vertex ? *(next+from_vertex-1) : *iter;
117         blinepoint1 = *(next+from_vertex);
118
119         etl::hermite<Vector> curve(blinepoint0.get_vertex(),   blinepoint1.get_vertex(),
120                                                            blinepoint0.get_tangent2(), blinepoint1.get_tangent1());
121         return curve(amount-from_vertex);
122 }
123
124
125
126
127
128
129
130 String
131 ValueNode_BLineCalcVertex::get_name()const
132 {
133         return "blinecalcvertex";
134 }
135
136 String
137 ValueNode_BLineCalcVertex::get_local_name()const
138 {
139         return _("BLine Vertex");
140 }
141
142 bool
143 ValueNode_BLineCalcVertex::set_link_vfunc(int i,ValueNode::Handle x)
144 {
145         assert(i>=0 && i<link_count());
146         switch(i)
147         {
148                 case 0:
149                         bline_=x;
150                         signal_child_changed()(i);signal_value_changed()();
151                         return true;
152                 case 1:
153                         loop_=x;
154                         signal_child_changed()(i);signal_value_changed()();
155                         return true;
156                 case 2:
157                         amount_=x;
158                         signal_child_changed()(i);signal_value_changed()();
159                         return true;
160         }
161         return false;
162 }
163
164 ValueNode::LooseHandle
165 ValueNode_BLineCalcVertex::get_link_vfunc(int i)const
166 {
167         assert(i>=0 && i<link_count());
168         switch(i)
169         {
170                 case 0: return bline_;
171                 case 1: return loop_;
172                 case 2: return amount_;
173         }
174
175         return 0;
176 }
177
178 int
179 ValueNode_BLineCalcVertex::link_count()const
180 {
181         return 3;
182 }
183
184 String
185 ValueNode_BLineCalcVertex::link_name(int i)const
186 {
187         assert(i>=0 && i<link_count());
188         switch(i)
189         {
190                 case 0: return "bline";
191                 case 1: return "loop";
192                 case 2: return "amount";
193         }
194         return String();
195 }
196
197 String
198 ValueNode_BLineCalcVertex::link_local_name(int i)const
199 {
200         assert(i>=0 && i<link_count());
201         switch(i)
202         {
203                 case 0: return _("BLine");
204                 case 1: return _("Loop");
205                 case 2: return _("Amount");
206         }
207         return String();
208 }
209
210 int
211 ValueNode_BLineCalcVertex::get_link_index_from_name(const String &name)const
212 {
213         if(name=="bline")  return 0;
214         if(name=="loop")   return 1;
215         if(name=="amount") return 2;
216         throw Exception::BadLinkName(name);
217 }
218
219 bool
220 ValueNode_BLineCalcVertex::check_type(ValueBase::Type type)
221 {
222         return type==ValueBase::TYPE_VECTOR;
223 }