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