9b9a160f862ab32df820b0ac3e9976da934a4a64
[synfig.git] / synfig-core / 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
86 LinkableValueNode*
87 ValueNode_Step::create_new()const
88 {
89         return new ValueNode_Step(get_type());
90 }
91
92 ValueNode_Step*
93 ValueNode_Step::create(const ValueBase &x)
94 {
95         return new ValueNode_Step(x);
96 }
97
98 ValueNode_Step::~ValueNode_Step()
99 {
100         unlink_all();
101 }
102
103 ValueBase
104 ValueNode_Step::operator()(Time t)const
105 {
106         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
107                 printf("%s:%d operator()\n", __FILE__, __LINE__);
108
109         Time duration    ((*duration_    )(t).get(Time()));
110         Time start_time  ((*start_time_  )(t).get(Time()));
111         Real intersection((*intersection_)(t).get(Real()));
112
113         t = (floor((t - start_time) / duration) + intersection) * duration + start_time;
114
115         switch(get_type())
116         {
117         case ValueBase::TYPE_ANGLE:   return (*link_)(t).get( Angle());
118         case ValueBase::TYPE_COLOR:   return (*link_)(t).get( Color());
119         case ValueBase::TYPE_INTEGER: return (*link_)(t).get(   int());
120         case ValueBase::TYPE_REAL:    return (*link_)(t).get(  Real());
121         case ValueBase::TYPE_TIME:    return (*link_)(t).get(  Time());
122         case ValueBase::TYPE_VECTOR:  return (*link_)(t).get(Vector());
123         default:
124                 assert(0);
125                 return ValueBase();
126         }
127 }
128
129
130 String
131 ValueNode_Step::get_name()const
132 {
133         return "step";
134 }
135
136 String
137 ValueNode_Step::get_local_name()const
138 {
139         return _("Step");
140 }
141
142 bool
143 ValueNode_Step::check_type(ValueBase::Type type)
144 {
145         return
146                 type==ValueBase::TYPE_ANGLE             ||
147                 type==ValueBase::TYPE_COLOR             ||
148                 type==ValueBase::TYPE_INTEGER   ||
149                 type==ValueBase::TYPE_REAL              ||
150                 type==ValueBase::TYPE_TIME              ||
151                 type==ValueBase::TYPE_VECTOR    ;
152 }
153
154 bool
155 ValueNode_Step::set_link_vfunc(int i,ValueNode::Handle value)
156 {
157         assert(i>=0 && i<link_count());
158
159         switch(i)
160         {
161         case 0: CHECK_TYPE_AND_SET_VALUE(link_,         get_type());
162         case 1: CHECK_TYPE_AND_SET_VALUE(duration_,     ValueBase::TYPE_TIME);
163         case 2: CHECK_TYPE_AND_SET_VALUE(start_time_,   ValueBase::TYPE_TIME);
164         case 3: CHECK_TYPE_AND_SET_VALUE(intersection_, ValueBase::TYPE_REAL);
165         }
166         return false;
167 }
168
169 ValueNode::LooseHandle
170 ValueNode_Step::get_link_vfunc(int i)const
171 {
172         assert(i>=0 && i<link_count());
173
174         switch(i)
175         {
176         case 0: return link_;
177         case 1: return duration_;
178         case 2: return start_time_;
179         case 3: return intersection_;
180         default:
181                 return 0;
182         }
183 }
184
185 int
186 ValueNode_Step::link_count()const
187 {
188         return 4;
189 }
190
191 String
192 ValueNode_Step::link_name(int i)const
193 {
194         assert(i>=0 && i<link_count());
195
196         switch(i)
197         {
198         case 0: return "link";
199         case 1: return "duration";
200         case 2: return "start_time";
201         case 3: return "intersection";
202         default:
203                 return String();
204         }
205 }
206
207 String
208 ValueNode_Step::link_local_name(int i)const
209 {
210         assert(i>=0 && i<link_count());
211
212         switch(i)
213         {
214         case 0: return _("Link");
215         case 1: return _("Duration");
216         case 2: return _("Start Time");
217         case 3: return _("Intersection");
218         default:
219                 return String();
220         }
221 }
222
223 int
224 ValueNode_Step::get_link_index_from_name(const String &name)const
225 {
226         if(name=="link")         return 0;
227         if(name=="duration")     return 1;
228         if(name=="start_time")   return 2;
229         if(name=="intersection") return 3;
230
231         throw Exception::BadLinkName(name);
232 }
233
234 LinkableValueNode::Vocab
235 ValueNode_Step::get_children_vocab_vfunc()const
236 {
237         LinkableValueNode::Vocab ret;
238
239         ret.push_back(ParamDesc(ValueBase(),"link")
240                 .set_local_name(_("Link"))
241                 .set_description(_("The value node used to make the step"))
242         );
243
244         ret.push_back(ParamDesc(ValueBase(),"duration")
245                 .set_local_name(_("Duration"))
246                 .set_description(_("The duration of the step"))
247         );
248
249         ret.push_back(ParamDesc(ValueBase(),"start_time")
250                 .set_local_name(_("Start Time"))
251                 .set_description(_("The time when the step conversion starts"))
252         );
253
254                 ret.push_back(ParamDesc(ValueBase(),"intersection")
255                 .set_local_name(_("Intersection"))
256                 .set_description(_("Value that define whether the step is centerd on the value [0,1]"))
257         );
258
259         return ret;
260 }