81525af55048e5fe8042b4b61b00b69ca2c0640c
[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         if(x!=ValueBase::TYPE_VECTOR)
61                 throw Exception::BadType(ValueBase::type_local_name(x));
62
63         set_link("segment",ValueNode_Const::create(ValueBase::TYPE_SEGMENT));
64         set_link("amount",ValueNode_Const::create(Real(0.5)));
65 }
66
67 ValueNode_SegCalcVertex*
68 ValueNode_SegCalcVertex::create(const ValueBase &x)
69 {
70         return new ValueNode_SegCalcVertex(x.get_type());
71 }
72
73 ValueNode_SegCalcVertex::~ValueNode_SegCalcVertex()
74 {
75         unlink_all();
76 }
77
78 ValueBase
79 ValueNode_SegCalcVertex::operator()(Time t)const
80 {
81         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
82                 printf("%s:%d operator()\n", __FILE__, __LINE__);
83
84         Segment segment((*segment_)(t).get(Segment()));
85
86         etl::hermite<Vector> curve(segment.p1,segment.p2,segment.t1,segment.t2);
87
88         return curve((*amount_)(t).get(Real()));
89 }
90
91
92 String
93 ValueNode_SegCalcVertex::get_name()const
94 {
95         return "segcalcvertex";
96 }
97
98 String
99 ValueNode_SegCalcVertex::get_local_name()const
100 {
101         return _("Segment Vertex");
102 }
103
104 bool
105 ValueNode_SegCalcVertex::check_type(ValueBase::Type type)
106 {
107         return type==ValueBase::TYPE_VECTOR;
108 }
109
110 bool
111 ValueNode_SegCalcVertex::set_link_vfunc(int i,ValueNode::Handle value)
112 {
113         assert(i>=0 && i<link_count());
114
115         switch(i)
116         {
117         case 0: CHECK_TYPE_AND_SET_VALUE(segment_, ValueBase::TYPE_SEGMENT);
118         case 1: CHECK_TYPE_AND_SET_VALUE(amount_,  ValueBase::TYPE_REAL);
119         }
120         return false;
121 }
122
123 ValueNode::LooseHandle
124 ValueNode_SegCalcVertex::get_link_vfunc(int i)const
125 {
126         assert(i>=0 && i<link_count());
127
128         if(i==0)
129                 return segment_;
130         if(i==1)
131                 return amount_;
132
133         return 0;
134 }
135
136 int
137 ValueNode_SegCalcVertex::link_count()const
138 {
139         return 2;
140 }
141
142 String
143 ValueNode_SegCalcVertex::link_name(int i)const
144 {
145         assert(i>=0 && i<link_count());
146
147         if(i==0)
148                 return "segment";
149         if(i==1)
150                 return "amount";
151         return String();
152 }
153
154 String
155 ValueNode_SegCalcVertex::link_local_name(int i)const
156 {
157         assert(i>=0 && i<link_count());
158
159         if(i==0)
160                 return _("Segment");
161         if(i==1)
162                 return _("Amount");
163         return String();
164 }
165
166 int
167 ValueNode_SegCalcVertex::get_link_index_from_name(const String &name)const
168 {
169         if(name=="segment")
170                 return 0;
171         if(name=="amount")
172                 return 1;
173
174         throw Exception::BadLinkName(name);
175 }
176
177 LinkableValueNode*
178 ValueNode_SegCalcVertex::create_new()const
179 {
180         return new ValueNode_SegCalcVertex(ValueBase::TYPE_VECTOR);
181 }
182
183 LinkableValueNode::Vocab
184 ValueNode_SegCalcVertex::get_children_vocab_vfunc()const
185 {
186         LinkableValueNode::Vocab ret;
187
188         ret.push_back(ParamDesc(ValueBase(),"segment")
189                 .set_local_name(_("Segment"))
190                 .set_description(_("The Segment where the vertex is linked to"))
191         );
192
193         ret.push_back(ParamDesc(ValueBase(),"amount")
194                 .set_local_name(_("Amount"))
195                 .set_description(_("The position of the linked vertex on the Segment (0,1]"))
196         );
197         return ret;
198 }