Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / stable / src / synfig / valuenode_step.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_step.cpp
3 **      \brief Implementation of the "Step" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2008 Chris Moore
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_step.h"
33 #include "valuenode_const.h"
34 #include "general.h"
35 #include "color.h"
36 #include <ETL/misc>
37
38 #endif
39
40 /* === U S I N G =========================================================== */
41
42 using namespace std;
43 using namespace etl;
44 using namespace synfig;
45
46 /* === M A C R O S ========================================================= */
47
48 /* === G L O B A L S ======================================================= */
49
50 /* === P R O C E D U R E S ================================================= */
51
52 /* === M E T H O D S ======================================================= */
53
54 ValueNode_Step::ValueNode_Step(const ValueBase &value):
55         LinkableValueNode(value.get_type())
56 {
57         set_link("duration",     ValueNode_Const::create(Time(1)));
58         set_link("start_time",   ValueNode_Const::create(Time(0)));
59         set_link("intersection", ValueNode_Const::create(Real(0.5)));
60
61         switch(get_type())
62         {
63         case ValueBase::TYPE_ANGLE:
64                 set_link("link",ValueNode_Const::create(value.get(Angle())));
65                 break;
66         case ValueBase::TYPE_COLOR:
67                 set_link("link",ValueNode_Const::create(value.get(Color())));
68                 break;
69         case ValueBase::TYPE_INTEGER:
70                 set_link("link",ValueNode_Const::create(value.get(int())));
71                 break;
72         case ValueBase::TYPE_REAL:
73                 set_link("link",ValueNode_Const::create(value.get(Real())));
74                 break;
75         case ValueBase::TYPE_TIME:
76                 set_link("link",ValueNode_Const::create(value.get(Time())));
77                 break;
78         case ValueBase::TYPE_VECTOR:
79                 set_link("link",ValueNode_Const::create(value.get(Vector())));
80                 break;
81         default:
82                 throw Exception::BadType(ValueBase::type_local_name(get_type()));
83         }
84
85         DCAST_HACK_ENABLE();
86 }
87
88 LinkableValueNode*
89 ValueNode_Step::create_new()const
90 {
91         return new ValueNode_Step(get_type());
92 }
93
94 ValueNode_Step*
95 ValueNode_Step::create(const ValueBase &x)
96 {
97         return new ValueNode_Step(x);
98 }
99
100 ValueNode_Step::~ValueNode_Step()
101 {
102         unlink_all();
103 }
104
105 ValueBase
106 ValueNode_Step::operator()(Time t)const
107 {
108         Time duration    ((*duration_    )(t).get(Time()));
109         Time start_time  ((*start_time_  )(t).get(Time()));
110         Real intersection((*intersection_)(t).get(Real()));
111
112         t = (floor((t - start_time) / duration) + intersection) * duration + start_time;
113
114         switch(get_type())
115         {
116         case ValueBase::TYPE_ANGLE:   return (*link_)(t).get( Angle());
117         case ValueBase::TYPE_COLOR:   return (*link_)(t).get( Color());
118         case ValueBase::TYPE_INTEGER: return (*link_)(t).get(   int());
119         case ValueBase::TYPE_REAL:    return (*link_)(t).get(  Real());
120         case ValueBase::TYPE_TIME:    return (*link_)(t).get(  Time());
121         case ValueBase::TYPE_VECTOR:  return (*link_)(t).get(Vector());
122         default:
123                 assert(0);
124                 return ValueBase();
125         }
126 }
127
128
129 String
130 ValueNode_Step::get_name()const
131 {
132         return "step";
133 }
134
135 String
136 ValueNode_Step::get_local_name()const
137 {
138         return _("Step");
139 }
140
141 bool
142 ValueNode_Step::check_type(ValueBase::Type type)
143 {
144         return
145                 type==ValueBase::TYPE_ANGLE             ||
146                 type==ValueBase::TYPE_COLOR             ||
147                 type==ValueBase::TYPE_INTEGER   ||
148                 type==ValueBase::TYPE_REAL              ||
149                 type==ValueBase::TYPE_TIME              ||
150                 type==ValueBase::TYPE_VECTOR    ;
151 }
152
153 bool
154 ValueNode_Step::set_link_vfunc(int i,ValueNode::Handle value)
155 {
156         assert(i>=0 && i<link_count());
157
158         switch(i)
159         {
160         case 0: CHECK_TYPE_AND_SET_VALUE(link_,         get_type());
161         case 1: CHECK_TYPE_AND_SET_VALUE(duration_,     ValueBase::TYPE_TIME);
162         case 2: CHECK_TYPE_AND_SET_VALUE(start_time_,   ValueBase::TYPE_TIME);
163         case 3: CHECK_TYPE_AND_SET_VALUE(intersection_, ValueBase::TYPE_REAL);
164         }
165         return false;
166 }
167
168 ValueNode::LooseHandle
169 ValueNode_Step::get_link_vfunc(int i)const
170 {
171         assert(i>=0 && i<link_count());
172
173         switch(i)
174         {
175         case 0: return link_;
176         case 1: return duration_;
177         case 2: return start_time_;
178         case 3: return intersection_;
179         default:
180                 return 0;
181         }
182 }
183
184 int
185 ValueNode_Step::link_count()const
186 {
187         return 4;
188 }
189
190 String
191 ValueNode_Step::link_name(int i)const
192 {
193         assert(i>=0 && i<link_count());
194
195         switch(i)
196         {
197         case 0: return "link";
198         case 1: return "duration";
199         case 2: return "start_time";
200         case 3: return "intersection";
201         default:
202                 return String();
203         }
204 }
205
206 String
207 ValueNode_Step::link_local_name(int i)const
208 {
209         assert(i>=0 && i<link_count());
210
211         switch(i)
212         {
213         case 0: return _("Link");
214         case 1: return _("Duration");
215         case 2: return _("Start Time");
216         case 3: return _("Intersection");
217         default:
218                 return String();
219         }
220 }
221
222 int
223 ValueNode_Step::get_link_index_from_name(const String &name)const
224 {
225         if(name=="link")         return 0;
226         if(name=="duration")     return 1;
227         if(name=="start_time")   return 2;
228         if(name=="intersection") return 3;
229
230         throw Exception::BadLinkName(name);
231 }