Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / 0.61.08 / 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         if(x!=ValueBase::TYPE_REAL)
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_BLineCalcWidth::create_new()const
72 {
73         return new ValueNode_BLineCalcWidth(ValueBase::TYPE_REAL);
74 }
75
76 ValueNode_BLineCalcWidth*
77 ValueNode_BLineCalcWidth::create(const ValueBase &x)
78 {
79         return new ValueNode_BLineCalcWidth(x.get_type());
80 }
81
82 ValueNode_BLineCalcWidth::~ValueNode_BLineCalcWidth()
83 {
84         unlink_all();
85 }
86
87 ValueBase
88 ValueNode_BLineCalcWidth::operator()(Time t)const
89 {
90         const std::vector<ValueBase> bline((*bline_)(t));
91         handle<ValueNode_BLine> bline_value_node(bline_);
92         const bool looped(bline_value_node->get_loop());
93         int size = bline.size(), from_vertex;
94         bool loop((*loop_)(t).get(bool()));
95         Real amount((*amount_)(t).get(Real()));
96         BLinePoint blinepoint0, blinepoint1;
97
98         if (!looped) size--;
99         if (size < 1) return Real();
100         if (loop)
101         {
102                 amount = amount - int(amount);
103                 if (amount < 0) amount++;
104         }
105         else
106         {
107                 if (amount < 0) amount = 0;
108                 if (amount > 1) amount = 1;
109         }
110
111         vector<ValueBase>::const_iterator iter, next(bline.begin());
112
113         iter = looped ? --bline.end() : next++;
114         amount = amount * size;
115         from_vertex = int(amount);
116         if (from_vertex > size-1) from_vertex = size-1;
117         blinepoint0 = from_vertex ? *(next+from_vertex-1) : *iter;
118         blinepoint1 = *(next+from_vertex);
119
120         float width0 = blinepoint0.get_width();
121         float width1 = blinepoint1.get_width();
122
123         return Real(width0 + (amount-from_vertex) * (width1-width0));
124 }
125
126
127
128
129
130
131
132 String
133 ValueNode_BLineCalcWidth::get_name()const
134 {
135         return "blinecalcwidth";
136 }
137
138 String
139 ValueNode_BLineCalcWidth::get_local_name()const
140 {
141         return _("BLine Width");
142 }
143
144 bool
145 ValueNode_BLineCalcWidth::set_link_vfunc(int i,ValueNode::Handle value)
146 {
147         assert(i>=0 && i<link_count());
148
149         switch(i)
150         {
151         case 0: CHECK_TYPE_AND_SET_VALUE(bline_,  ValueBase::TYPE_LIST);
152         case 1: CHECK_TYPE_AND_SET_VALUE(loop_,   ValueBase::TYPE_BOOL);
153         case 2: CHECK_TYPE_AND_SET_VALUE(amount_, ValueBase::TYPE_REAL);
154         }
155         return false;
156 }
157
158 ValueNode::LooseHandle
159 ValueNode_BLineCalcWidth::get_link_vfunc(int i)const
160 {
161         assert(i>=0 && i<link_count());
162
163         switch(i)
164         {
165                 case 0: return bline_;
166                 case 1: return loop_;
167                 case 2: return amount_;
168         }
169
170         return 0;
171 }
172
173 int
174 ValueNode_BLineCalcWidth::link_count()const
175 {
176         return 3;
177 }
178
179 String
180 ValueNode_BLineCalcWidth::link_name(int i)const
181 {
182         assert(i>=0 && i<link_count());
183
184         switch(i)
185         {
186                 case 0: return "bline";
187                 case 1: return "loop";
188                 case 2: return "amount";
189         }
190         return String();
191 }
192
193 String
194 ValueNode_BLineCalcWidth::link_local_name(int i)const
195 {
196         assert(i>=0 && i<link_count());
197
198         switch(i)
199         {
200                 case 0: return _("BLine");
201                 case 1: return _("Loop");
202                 case 2: return _("Amount");
203         }
204         return String();
205 }
206
207 int
208 ValueNode_BLineCalcWidth::get_link_index_from_name(const String &name)const
209 {
210         if(name=="bline")  return 0;
211         if(name=="loop")   return 1;
212         if(name=="amount") return 2;
213         throw Exception::BadLinkName(name);
214 }
215
216 bool
217 ValueNode_BLineCalcWidth::check_type(ValueBase::Type type)
218 {
219         return type==ValueBase::TYPE_REAL;
220 }