Use LinkableValueNode members functions when possible in the derived valuenodes.
[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         Vocab ret(get_children_vocab());
58         set_children_vocab(ret);
59         set_link("duration",     ValueNode_Const::create(Time(1)));
60         set_link("start_time",   ValueNode_Const::create(Time(0)));
61         set_link("intersection", ValueNode_Const::create(Real(0.5)));
62
63         switch(get_type())
64         {
65         case ValueBase::TYPE_ANGLE:
66                 set_link("link",ValueNode_Const::create(value.get(Angle())));
67                 break;
68         case ValueBase::TYPE_COLOR:
69                 set_link("link",ValueNode_Const::create(value.get(Color())));
70                 break;
71         case ValueBase::TYPE_INTEGER:
72                 set_link("link",ValueNode_Const::create(value.get(int())));
73                 break;
74         case ValueBase::TYPE_REAL:
75                 set_link("link",ValueNode_Const::create(value.get(Real())));
76                 break;
77         case ValueBase::TYPE_TIME:
78                 set_link("link",ValueNode_Const::create(value.get(Time())));
79                 break;
80         case ValueBase::TYPE_VECTOR:
81                 set_link("link",ValueNode_Const::create(value.get(Vector())));
82                 break;
83         default:
84                 throw Exception::BadType(ValueBase::type_local_name(get_type()));
85         }
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         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
109                 printf("%s:%d operator()\n", __FILE__, __LINE__);
110
111         Time duration    ((*duration_    )(t).get(Time()));
112         Time start_time  ((*start_time_  )(t).get(Time()));
113         Real intersection((*intersection_)(t).get(Real()));
114
115         t = (floor((t - start_time) / duration) + intersection) * duration + start_time;
116
117         switch(get_type())
118         {
119         case ValueBase::TYPE_ANGLE:   return (*link_)(t).get( Angle());
120         case ValueBase::TYPE_COLOR:   return (*link_)(t).get( Color());
121         case ValueBase::TYPE_INTEGER: return (*link_)(t).get(   int());
122         case ValueBase::TYPE_REAL:    return (*link_)(t).get(  Real());
123         case ValueBase::TYPE_TIME:    return (*link_)(t).get(  Time());
124         case ValueBase::TYPE_VECTOR:  return (*link_)(t).get(Vector());
125         default:
126                 assert(0);
127                 return ValueBase();
128         }
129 }
130
131
132 String
133 ValueNode_Step::get_name()const
134 {
135         return "step";
136 }
137
138 String
139 ValueNode_Step::get_local_name()const
140 {
141         return _("Step");
142 }
143
144 bool
145 ValueNode_Step::check_type(ValueBase::Type type)
146 {
147         return
148                 type==ValueBase::TYPE_ANGLE             ||
149                 type==ValueBase::TYPE_COLOR             ||
150                 type==ValueBase::TYPE_INTEGER   ||
151                 type==ValueBase::TYPE_REAL              ||
152                 type==ValueBase::TYPE_TIME              ||
153                 type==ValueBase::TYPE_VECTOR    ;
154 }
155
156 bool
157 ValueNode_Step::set_link_vfunc(int i,ValueNode::Handle value)
158 {
159         assert(i>=0 && i<link_count());
160
161         switch(i)
162         {
163         case 0: CHECK_TYPE_AND_SET_VALUE(link_,         get_type());
164         case 1: CHECK_TYPE_AND_SET_VALUE(duration_,     ValueBase::TYPE_TIME);
165         case 2: CHECK_TYPE_AND_SET_VALUE(start_time_,   ValueBase::TYPE_TIME);
166         case 3: CHECK_TYPE_AND_SET_VALUE(intersection_, ValueBase::TYPE_REAL);
167         }
168         return false;
169 }
170
171 ValueNode::LooseHandle
172 ValueNode_Step::get_link_vfunc(int i)const
173 {
174         assert(i>=0 && i<link_count());
175
176         switch(i)
177         {
178         case 0: return link_;
179         case 1: return duration_;
180         case 2: return start_time_;
181         case 3: return intersection_;
182         default:
183                 return 0;
184         }
185 }
186
187 LinkableValueNode::Vocab
188 ValueNode_Step::get_children_vocab_vfunc()const
189 {
190         if(children_vocab.size())
191                 return children_vocab;
192
193         LinkableValueNode::Vocab ret;
194
195         ret.push_back(ParamDesc(ValueBase(),"link")
196                 .set_local_name(_("Link"))
197                 .set_description(_("The value node used to make the step"))
198         );
199
200         ret.push_back(ParamDesc(ValueBase(),"duration")
201                 .set_local_name(_("Duration"))
202                 .set_description(_("The duration of the step"))
203         );
204
205         ret.push_back(ParamDesc(ValueBase(),"start_time")
206                 .set_local_name(_("Start Time"))
207                 .set_description(_("The time when the step conversion starts"))
208         );
209
210                 ret.push_back(ParamDesc(ValueBase(),"intersection")
211                 .set_local_name(_("Intersection"))
212                 .set_description(_("Value that define whether the step is centerd on the value [0,1]"))
213         );
214
215         return ret;
216 }