Use link_count from children vocabulary and return the stored vocabulary if already...
[synfig.git] / synfig-core / src / synfig / valuenode_segcalcvertex.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_segcalcvertex.cpp
3 **      \brief Implementation of the "Segment Vertex" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 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_segcalcvertex.h"
34 #include "valuenode_const.h"
35 #include "valuenode_composite.h"
36 #include "general.h"
37 #include "exception.h"
38 #include <ETL/hermite>
39 #include "segment.h"
40
41 #endif
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_SegCalcVertex::ValueNode_SegCalcVertex(const ValueBase::Type &x):
58         LinkableValueNode(x)
59 {
60         Vocab ret(get_children_vocab());
61         set_children_vocab(ret);
62         if(x!=ValueBase::TYPE_VECTOR)
63                 throw Exception::BadType(ValueBase::type_local_name(x));
64
65         set_link("segment",ValueNode_Const::create(ValueBase::TYPE_SEGMENT));
66         set_link("amount",ValueNode_Const::create(Real(0.5)));
67 }
68
69 ValueNode_SegCalcVertex*
70 ValueNode_SegCalcVertex::create(const ValueBase &x)
71 {
72         return new ValueNode_SegCalcVertex(x.get_type());
73 }
74
75 ValueNode_SegCalcVertex::~ValueNode_SegCalcVertex()
76 {
77         unlink_all();
78 }
79
80 ValueBase
81 ValueNode_SegCalcVertex::operator()(Time t)const
82 {
83         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
84                 printf("%s:%d operator()\n", __FILE__, __LINE__);
85
86         Segment segment((*segment_)(t).get(Segment()));
87
88         etl::hermite<Vector> curve(segment.p1,segment.p2,segment.t1,segment.t2);
89
90         return curve((*amount_)(t).get(Real()));
91 }
92
93
94 String
95 ValueNode_SegCalcVertex::get_name()const
96 {
97         return "segcalcvertex";
98 }
99
100 String
101 ValueNode_SegCalcVertex::get_local_name()const
102 {
103         return _("Segment Vertex");
104 }
105
106 bool
107 ValueNode_SegCalcVertex::check_type(ValueBase::Type type)
108 {
109         return type==ValueBase::TYPE_VECTOR;
110 }
111
112 bool
113 ValueNode_SegCalcVertex::set_link_vfunc(int i,ValueNode::Handle value)
114 {
115         assert(i>=0 && i<link_count());
116
117         switch(i)
118         {
119         case 0: CHECK_TYPE_AND_SET_VALUE(segment_, ValueBase::TYPE_SEGMENT);
120         case 1: CHECK_TYPE_AND_SET_VALUE(amount_,  ValueBase::TYPE_REAL);
121         }
122         return false;
123 }
124
125 ValueNode::LooseHandle
126 ValueNode_SegCalcVertex::get_link_vfunc(int i)const
127 {
128         assert(i>=0 && i<link_count());
129
130         if(i==0)
131                 return segment_;
132         if(i==1)
133                 return amount_;
134
135         return 0;
136 }
137
138 int
139 ValueNode_SegCalcVertex::link_count()const
140 {
141         return 2;
142 }
143
144 String
145 ValueNode_SegCalcVertex::link_name(int i)const
146 {
147         assert(i>=0 && i<link_count());
148
149         if(i==0)
150                 return "segment";
151         if(i==1)
152                 return "amount";
153         return String();
154 }
155
156 String
157 ValueNode_SegCalcVertex::link_local_name(int i)const
158 {
159         assert(i>=0 && i<link_count());
160
161         if(i==0)
162                 return _("Segment");
163         if(i==1)
164                 return _("Amount");
165         return String();
166 }
167
168 int
169 ValueNode_SegCalcVertex::get_link_index_from_name(const String &name)const
170 {
171         if(name=="segment")
172                 return 0;
173         if(name=="amount")
174                 return 1;
175
176         throw Exception::BadLinkName(name);
177 }
178
179 LinkableValueNode*
180 ValueNode_SegCalcVertex::create_new()const
181 {
182         return new ValueNode_SegCalcVertex(ValueBase::TYPE_VECTOR);
183 }
184
185 LinkableValueNode::Vocab
186 ValueNode_SegCalcVertex::get_children_vocab_vfunc()const
187 {
188         if(children_vocab.size())
189                 return children_vocab;
190
191         LinkableValueNode::Vocab ret;
192
193         ret.push_back(ParamDesc(ValueBase(),"segment")
194                 .set_local_name(_("Segment"))
195                 .set_description(_("The Segment where the vertex is linked to"))
196         );
197
198         ret.push_back(ParamDesc(ValueBase(),"amount")
199                 .set_local_name(_("Amount"))
200                 .set_description(_("The position of the linked vertex on the Segment (0,1]"))
201         );
202         return ret;
203 }