Fix 1797488: use set_link() rather than modifying the members directly.
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_segcalcvertex.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_segcalcvertex.cpp
3 **      \brief Template File
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_segcalcvertex.h"
33 #include "valuenode_const.h"
34 #include "valuenode_composite.h"
35 #include "general.h"
36 #include "exception.h"
37 #include <ETL/hermite>
38 #include "segment.h"
39
40 #endif
41
42 /* === U S I N G =========================================================== */
43
44 using namespace std;
45 using namespace etl;
46 using namespace synfig;
47
48 /* === M A C R O S ========================================================= */
49
50 /* === G L O B A L S ======================================================= */
51
52 /* === P R O C E D U R E S ================================================= */
53
54 /* === M E T H O D S ======================================================= */
55
56 ValueNode_SegCalcVertex::ValueNode_SegCalcVertex(const ValueBase::Type &x):
57         LinkableValueNode(x)
58 {
59         if(x!=ValueBase::TYPE_VECTOR)
60                 throw Exception::BadType(ValueBase::type_name(x));
61
62         set_link("segment",ValueNode_Const::create(ValueBase::TYPE_SEGMENT));
63         set_link("amount",ValueNode_Const::create(Real(0.5)));
64 }
65
66 ValueNode_SegCalcVertex*
67 ValueNode_SegCalcVertex::create(const ValueBase &x)
68 {
69         return new ValueNode_SegCalcVertex(x.get_type());
70 }
71
72 ValueNode_SegCalcVertex::~ValueNode_SegCalcVertex()
73 {
74         unlink_all();
75 }
76
77 ValueBase
78 ValueNode_SegCalcVertex::operator()(Time t)const
79 {
80         Segment segment((*segment_)(t).get(Segment()));
81
82         etl::hermite<Vector> curve(segment.p1,segment.p2,segment.t1,segment.t2);
83
84         return curve((*amount_)(t).get(Real()));
85 }
86
87
88 String
89 ValueNode_SegCalcVertex::get_name()const
90 {
91         return "segcalcvertex";
92 }
93
94 String
95 ValueNode_SegCalcVertex::get_local_name()const
96 {
97         return _("Segment Vertex");
98 }
99
100 bool
101 ValueNode_SegCalcVertex::check_type(ValueBase::Type type)
102 {
103         return type==ValueBase::TYPE_VECTOR;
104 }
105
106 bool
107 ValueNode_SegCalcVertex::set_link_vfunc(int i,ValueNode::Handle x)
108 {
109         assert(i==0 || i==1);
110         if(i==0)
111         {
112                 segment_=x;
113                 signal_child_changed()(i);signal_value_changed()();
114                 return true;
115         }
116         if(i==1)
117         {
118                 amount_=x;
119                 signal_child_changed()(i);signal_value_changed()();
120                 return true;
121         }
122         return false;
123 }
124
125 ValueNode::LooseHandle
126 ValueNode_SegCalcVertex::get_link_vfunc(int i)const
127 {
128         assert(i==0 || i==1);
129         if(i==0)
130                 return segment_;
131         if(i==1)
132                 return amount_;
133
134         return 0;
135 }
136
137 int
138 ValueNode_SegCalcVertex::link_count()const
139 {
140         return 2;
141 }
142
143 String
144 ValueNode_SegCalcVertex::link_name(int i)const
145 {
146         assert(i==0 || i==1);
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==1);
158         if(i==0)
159                 return _("Segment");
160         if(i==1)
161                 return _("Amount");
162         return String();
163 }
164
165 int
166 ValueNode_SegCalcVertex::get_link_index_from_name(const String &name)const
167 {
168         if(name=="segment")
169                 return 0;
170         if(name=="amount")
171                 return 1;
172
173         throw Exception::BadLinkName(name);
174 }
175
176 LinkableValueNode*
177 ValueNode_SegCalcVertex::create_new()const
178 {
179         return new ValueNode_SegCalcVertex(ValueBase::TYPE_VECTOR);
180 }