b2144e3797e72feb0bafa7d66cb364dde79490e4
[synfig.git] / synfig-core / trunk / 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 **
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_local_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 value)
144 {
145         assert(i>=0 && i<link_count());
146
147         switch(i)
148         {
149         case 0: CHECK_TYPE_AND_SET_VALUE(bline_,  ValueBase::TYPE_LIST);
150         case 1: CHECK_TYPE_AND_SET_VALUE(loop_,   ValueBase::TYPE_BOOL);
151         case 2: CHECK_TYPE_AND_SET_VALUE(amount_, ValueBase::TYPE_REAL);
152         }
153         return false;
154 }
155
156 ValueNode::LooseHandle
157 ValueNode_BLineCalcVertex::get_link_vfunc(int i)const
158 {
159         assert(i>=0 && i<link_count());
160
161         switch(i)
162         {
163                 case 0: return bline_;
164                 case 1: return loop_;
165                 case 2: return amount_;
166         }
167
168         return 0;
169 }
170
171 int
172 ValueNode_BLineCalcVertex::link_count()const
173 {
174         return 3;
175 }
176
177 String
178 ValueNode_BLineCalcVertex::link_name(int i)const
179 {
180         assert(i>=0 && i<link_count());
181
182         switch(i)
183         {
184                 case 0: return "bline";
185                 case 1: return "loop";
186                 case 2: return "amount";
187         }
188         return String();
189 }
190
191 String
192 ValueNode_BLineCalcVertex::link_local_name(int i)const
193 {
194         assert(i>=0 && i<link_count());
195
196         switch(i)
197         {
198                 case 0: return _("BLine");
199                 case 1: return _("Loop");
200                 case 2: return _("Amount");
201         }
202         return String();
203 }
204
205 int
206 ValueNode_BLineCalcVertex::get_link_index_from_name(const String &name)const
207 {
208         if(name=="bline")  return 0;
209         if(name=="loop")   return 1;
210         if(name=="amount") return 2;
211         throw Exception::BadLinkName(name);
212 }
213
214 bool
215 ValueNode_BLineCalcVertex::check_type(ValueBase::Type type)
216 {
217         return type==ValueBase::TYPE_VECTOR;
218 }