Use link_count from children vocabulary and return the stored vocabulary if already...
[synfig.git] / synfig-core / src / synfig / valuenode_duplicate.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_duplicate.cpp
3 **      \brief Implementation of the "Duplicate" 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_duplicate.h"
34 #include "valuenode_const.h"
35 #include "general.h"
36
37 #endif
38
39 /* === U S I N G =========================================================== */
40
41 using namespace std;
42 using namespace etl;
43 using namespace synfig;
44
45 /* === M A C R O S ========================================================= */
46
47 /* === G L O B A L S ======================================================= */
48
49 /* === P R O C E D U R E S ================================================= */
50
51 /* === M E T H O D S ======================================================= */
52
53 ValueNode_Duplicate::ValueNode_Duplicate(const ValueBase::Type &x):
54         LinkableValueNode(x)
55 {
56 }
57
58 ValueNode_Duplicate::ValueNode_Duplicate(const ValueBase &x):
59         LinkableValueNode(x.get_type())
60 {
61         Vocab ret(get_children_vocab());
62         set_children_vocab(ret);
63         set_link("from", ValueNode_Const::create(Real(1.0)));
64         set_link("to",   ValueNode_Const::create(x.get(Real())));
65         set_link("step", ValueNode_Const::create(Real(1.0)));
66         index = 1.0;
67 }
68
69 ValueNode_Duplicate*
70 ValueNode_Duplicate::create(const ValueBase &x)
71 {
72         return new ValueNode_Duplicate(x);
73 }
74
75 LinkableValueNode*
76 ValueNode_Duplicate::create_new()const
77 {
78         return new ValueNode_Duplicate(get_type());
79 }
80
81 ValueNode_Duplicate::~ValueNode_Duplicate()
82 {
83         unlink_all();
84 }
85
86 bool
87 ValueNode_Duplicate::set_link_vfunc(int i,ValueNode::Handle value)
88 {
89         assert(i>=0 && i<link_count());
90
91         switch(i)
92         {
93         case 0: CHECK_TYPE_AND_SET_VALUE(from_, ValueBase::TYPE_REAL);
94         case 1: CHECK_TYPE_AND_SET_VALUE(to_,   ValueBase::TYPE_REAL);
95         case 2: CHECK_TYPE_AND_SET_VALUE(step_, ValueBase::TYPE_REAL);
96         }
97         return false;
98 }
99
100 ValueNode::LooseHandle
101 ValueNode_Duplicate::get_link_vfunc(int i)const
102 {
103         assert(i>=0 && i<link_count());
104
105         if(i==0) return from_;
106         if(i==1) return to_;
107         if(i==2) return step_;
108
109         return 0;
110 }
111
112 int
113 ValueNode_Duplicate::link_count()const
114 {
115         return 3;
116 }
117
118 String
119 ValueNode_Duplicate::link_local_name(int i)const
120 {
121         assert(i>=0 && i<link_count());
122
123         if(i==0) return _("From");
124         if(i==1) return _("To");
125         if(i==2) return _("Step");
126         return String();
127 }
128
129 String
130 ValueNode_Duplicate::link_name(int i)const
131 {
132         assert(i>=0 && i<link_count());
133
134         if(i==0) return "from";
135         if(i==1) return "to";
136         if(i==2) return "step";
137         return String();
138 }
139
140 int
141 ValueNode_Duplicate::get_link_index_from_name(const String &name)const
142 {
143         if(name=="from") return 0;
144         if(name=="to")   return 1;
145         if(name=="step") return 2;
146
147         throw Exception::BadLinkName(name);
148 }
149
150 void
151 ValueNode_Duplicate::reset_index(Time t)const
152 {
153         Real from = (*from_)(t).get(Real());
154         index = from;
155 }
156
157 bool
158 ValueNode_Duplicate::step(Time t)const
159 {
160         Real from = (*from_)(t).get(Real());
161         Real to   = (*to_  )(t).get(Real());
162         Real step = (*step_)(t).get(Real());
163         Real prev = index;
164
165         if (step == 0) return false;
166
167         step = abs(step);
168
169         if (from < to)
170         {
171                 if ((index += step) <= to) return true;
172         }
173         else
174                 if ((index -= step) >= to) return true;
175
176         // at the end of the loop, leave the index at the last value that was used
177         index = prev;
178         return false;
179 }
180
181 int
182 ValueNode_Duplicate::count_steps(Time t)const
183 {
184         Real from = (*from_)(t).get(Real());
185         Real to   = (*to_  )(t).get(Real());
186         Real step = (*step_)(t).get(Real());
187
188         if (step == 0) return 1;
189
190         return abs((from - to) / step) + 1;
191 }
192
193 ValueBase
194 ValueNode_Duplicate::operator()(Time t __attribute__ ((unused)))const
195 {
196         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
197                 printf("%s:%d operator()\n", __FILE__, __LINE__);
198
199         return index;
200 }
201
202 String
203 ValueNode_Duplicate::get_name()const
204 {
205         return "duplicate";
206 }
207
208 String
209 ValueNode_Duplicate::get_local_name()const
210 {
211         return _("Duplicate");
212 }
213
214 bool
215 ValueNode_Duplicate::check_type(ValueBase::Type type __attribute__ ((unused)))
216 {
217         // never offer this as a choice.  it's used automatically by the 'Duplicate' layer.
218         return false;
219 }
220
221 LinkableValueNode::Vocab
222 ValueNode_Duplicate::get_children_vocab_vfunc()const
223 {
224         if(children_vocab.size())
225                 return children_vocab;
226
227         LinkableValueNode::Vocab ret;
228
229         ret.push_back(ParamDesc(ValueBase(),"from")
230                 .set_local_name(_("From"))
231                 .set_description(_("Initial value of the index "))
232         );
233
234         ret.push_back(ParamDesc(ValueBase(),"to")
235                 .set_local_name(_("To"))
236                 .set_description(_("Final value of the index"))
237         );
238
239         ret.push_back(ParamDesc(ValueBase(),"step")
240                 .set_local_name(_("Step"))
241                 .set_description(_("Amount increment of the index"))
242         );
243
244         return ret;
245 }