d371d75bd2f5ad06f6f1f8e149fac23fe44826b7
[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         set_link("from", ValueNode_Const::create(Real(1.0)));
62         set_link("to",   ValueNode_Const::create(x.get(Real())));
63         set_link("step", ValueNode_Const::create(Real(1.0)));
64         index = 1.0;
65 }
66
67 ValueNode_Duplicate*
68 ValueNode_Duplicate::create(const ValueBase &x)
69 {
70         return new ValueNode_Duplicate(x);
71 }
72
73 LinkableValueNode*
74 ValueNode_Duplicate::create_new()const
75 {
76         return new ValueNode_Duplicate(get_type());
77 }
78
79 ValueNode_Duplicate::~ValueNode_Duplicate()
80 {
81         unlink_all();
82 }
83
84 bool
85 ValueNode_Duplicate::set_link_vfunc(int i,ValueNode::Handle value)
86 {
87         assert(i>=0 && i<link_count());
88
89         switch(i)
90         {
91         case 0: CHECK_TYPE_AND_SET_VALUE(from_, ValueBase::TYPE_REAL);
92         case 1: CHECK_TYPE_AND_SET_VALUE(to_,   ValueBase::TYPE_REAL);
93         case 2: CHECK_TYPE_AND_SET_VALUE(step_, ValueBase::TYPE_REAL);
94         }
95         return false;
96 }
97
98 ValueNode::LooseHandle
99 ValueNode_Duplicate::get_link_vfunc(int i)const
100 {
101         assert(i>=0 && i<link_count());
102
103         if(i==0) return from_;
104         if(i==1) return to_;
105         if(i==2) return step_;
106
107         return 0;
108 }
109
110 int
111 ValueNode_Duplicate::link_count()const
112 {
113         return 3;
114 }
115
116 String
117 ValueNode_Duplicate::link_local_name(int i)const
118 {
119         assert(i>=0 && i<link_count());
120
121         if(i==0) return _("From");
122         if(i==1) return _("To");
123         if(i==2) return _("Step");
124         return String();
125 }
126
127 String
128 ValueNode_Duplicate::link_name(int i)const
129 {
130         assert(i>=0 && i<link_count());
131
132         if(i==0) return "from";
133         if(i==1) return "to";
134         if(i==2) return "step";
135         return String();
136 }
137
138 int
139 ValueNode_Duplicate::get_link_index_from_name(const String &name)const
140 {
141         if(name=="from") return 0;
142         if(name=="to")   return 1;
143         if(name=="step") return 2;
144
145         throw Exception::BadLinkName(name);
146 }
147
148 void
149 ValueNode_Duplicate::reset_index(Time t)const
150 {
151         Real from = (*from_)(t).get(Real());
152         index = from;
153 }
154
155 bool
156 ValueNode_Duplicate::step(Time t)const
157 {
158         Real from = (*from_)(t).get(Real());
159         Real to   = (*to_  )(t).get(Real());
160         Real step = (*step_)(t).get(Real());
161         Real prev = index;
162
163         if (step == 0) return false;
164
165         step = abs(step);
166
167         if (from < to)
168         {
169                 if ((index += step) <= to) return true;
170         }
171         else
172                 if ((index -= step) >= to) return true;
173
174         // at the end of the loop, leave the index at the last value that was used
175         index = prev;
176         return false;
177 }
178
179 int
180 ValueNode_Duplicate::count_steps(Time t)const
181 {
182         Real from = (*from_)(t).get(Real());
183         Real to   = (*to_  )(t).get(Real());
184         Real step = (*step_)(t).get(Real());
185
186         if (step == 0) return 1;
187
188         return abs((from - to) / step) + 1;
189 }
190
191 ValueBase
192 ValueNode_Duplicate::operator()(Time t __attribute__ ((unused)))const
193 {
194         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
195                 printf("%s:%d operator()\n", __FILE__, __LINE__);
196
197         return index;
198 }
199
200 String
201 ValueNode_Duplicate::get_name()const
202 {
203         return "duplicate";
204 }
205
206 String
207 ValueNode_Duplicate::get_local_name()const
208 {
209         return _("Duplicate");
210 }
211
212 bool
213 ValueNode_Duplicate::check_type(ValueBase::Type type __attribute__ ((unused)))
214 {
215         // never offer this as a choice.  it's used automatically by the 'Duplicate' layer.
216         return false;
217 }
218
219 LinkableValueNode::Vocab
220 ValueNode_Duplicate::get_children_vocab_vfunc()const
221 {
222         LinkableValueNode::Vocab ret;
223
224         ret.push_back(ParamDesc(ValueBase(),"from")
225                 .set_local_name(_("From"))
226                 .set_description(_("Initial value of the index "))
227         );
228
229         ret.push_back(ParamDesc(ValueBase(),"to")
230                 .set_local_name(_("To"))
231                 .set_description(_("Final value of the index"))
232         );
233
234         ret.push_back(ParamDesc(ValueBase(),"step")
235                 .set_local_name(_("Step"))
236                 .set_description(_("Amount increment of the index"))
237         );
238
239         return ret;
240 }