Use LinkableValueNode members functions when possible in the derived valuenodes.
[synfig.git] / synfig-core / src / synfig / valuenode_segcalctangent.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_segcalctangent.cpp
3 **      \brief Implementation of the "Segment Tangent" 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_segcalctangent.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 <ETL/calculus>
40 #include "segment.h"
41
42 #endif
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_SegCalcTangent::ValueNode_SegCalcTangent(const ValueBase::Type &x):
59         LinkableValueNode(x)
60 {
61         Vocab ret(get_children_vocab());
62         set_children_vocab(ret);
63         if(x!=ValueBase::TYPE_VECTOR)
64                 throw Exception::BadType(ValueBase::type_local_name(x));
65
66         set_link("segment",ValueNode_Const::create(ValueBase::TYPE_SEGMENT));
67         set_link("amount",ValueNode_Const::create(Real(0.5)));
68 }
69
70 ValueNode_SegCalcTangent*
71 ValueNode_SegCalcTangent::create(const ValueBase &x)
72 {
73         return new ValueNode_SegCalcTangent(x.get_type());
74 }
75
76 ValueNode_SegCalcTangent::~ValueNode_SegCalcTangent()
77 {
78         unlink_all();
79 }
80
81 ValueBase
82 ValueNode_SegCalcTangent::operator()(Time t)const
83 {
84         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
85                 printf("%s:%d operator()\n", __FILE__, __LINE__);
86
87         Segment segment((*segment_)(t).get(Segment()));
88
89         etl::hermite<Vector> curve(segment.p1,segment.p2,segment.t1,segment.t2);
90         etl::derivative< etl::hermite<Vector> > deriv(curve);
91
92         return deriv((*amount_)(t).get(Real()));
93 }
94
95
96 String
97 ValueNode_SegCalcTangent::get_name()const
98 {
99         return "segcalctangent";
100 }
101
102 String
103 ValueNode_SegCalcTangent::get_local_name()const
104 {
105         return _("Segment Tangent");
106 }
107
108 bool
109 ValueNode_SegCalcTangent::check_type(ValueBase::Type type)
110 {
111         return type==ValueBase::TYPE_VECTOR;
112 }
113
114 bool
115 ValueNode_SegCalcTangent::set_link_vfunc(int i,ValueNode::Handle value)
116 {
117         assert(i>=0 && i<link_count());
118
119         switch(i)
120         {
121         case 0: CHECK_TYPE_AND_SET_VALUE(segment_, ValueBase::TYPE_SEGMENT);
122         case 1: CHECK_TYPE_AND_SET_VALUE(amount_,  ValueBase::TYPE_REAL);
123         }
124         return false;
125 }
126
127 ValueNode::LooseHandle
128 ValueNode_SegCalcTangent::get_link_vfunc(int i)const
129 {
130         assert(i>=0 && i<link_count());
131
132         if(i==0)
133                 return segment_;
134         if(i==1)
135                 return amount_;
136
137         return 0;
138 }
139
140 LinkableValueNode*
141 ValueNode_SegCalcTangent::create_new()const
142 {
143         return new ValueNode_SegCalcTangent(ValueBase::TYPE_VECTOR);
144 }
145
146 LinkableValueNode::Vocab
147 ValueNode_SegCalcTangent::get_children_vocab_vfunc()const
148 {
149         if(children_vocab.size())
150                 return children_vocab;
151
152         LinkableValueNode::Vocab ret;
153
154         ret.push_back(ParamDesc(ValueBase(),"segment")
155                 .set_local_name(_("Segment"))
156                 .set_description(_("The Segment where the tangent is linked to"))
157         );
158
159         ret.push_back(ParamDesc(ValueBase(),"amount")
160                 .set_local_name(_("Amount"))
161                 .set_description(_("The position of the linked tangent on the Segment (0,1]"))
162         );
163         return ret;
164 }