Made all the assert() lines which check the valuenode sub-parameter index range the...
[synfig.git] / synfig-core / trunk / 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 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 x)
86 {
87         assert(i>=0 && i<link_count());
88
89         switch(i)
90         {
91         case 0:  from_ = x; break;
92         case 1:  to_   = x; break;
93         case 2:  step_ = x; break;
94         default: return false;
95         }
96
97         signal_child_changed()(i);
98         signal_value_changed()();
99         return true;
100 }
101
102 ValueNode::LooseHandle
103 ValueNode_Duplicate::get_link_vfunc(int i)const
104 {
105         assert(i>=0 && i<link_count());
106
107         if(i==0) return from_;
108         if(i==1) return to_;
109         if(i==2) return step_;
110
111         return 0;
112 }
113
114 int
115 ValueNode_Duplicate::link_count()const
116 {
117         return 3;
118 }
119
120 String
121 ValueNode_Duplicate::link_local_name(int i)const
122 {
123         assert(i>=0 && i<link_count());
124
125         if(i==0) return _("From");
126         if(i==1) return _("To");
127         if(i==2) return _("Step");
128         return String();
129 }
130
131 String
132 ValueNode_Duplicate::link_name(int i)const
133 {
134         assert(i>=0 && i<link_count());
135
136         if(i==0) return "from";
137         if(i==1) return "to";
138         if(i==2) return "step";
139         return String();
140 }
141
142 int
143 ValueNode_Duplicate::get_link_index_from_name(const String &name)const
144 {
145         if(name=="from") return 0;
146         if(name=="to")   return 1;
147         if(name=="step") return 2;
148
149         throw Exception::BadLinkName(name);
150 }
151
152 void
153 ValueNode_Duplicate::reset_index(Time t)const
154 {
155         Real from = (*from_)(t).get(Real());
156         index = from;
157 }
158
159 bool
160 ValueNode_Duplicate::step(Time t)const
161 {
162         Real from = (*from_)(t).get(Real());
163         Real to   = (*to_  )(t).get(Real());
164         Real step = (*step_)(t).get(Real());
165
166         if (step == 0) return false;
167
168         step = abs(step);
169
170         if (from < to)
171         {
172                 index += step;
173                 return index <= to;
174         }
175         else
176         {
177                 index -= step;
178                 return index >= to;
179         }
180 }
181
182 int
183 ValueNode_Duplicate::count_steps(Time t)const
184 {
185         Real from = (*from_)(t).get(Real());
186         Real to   = (*to_  )(t).get(Real());
187         Real step = (*step_)(t).get(Real());
188
189         if (step == 0) return 1;
190
191         return abs((from - to) / step) + 1;
192 }
193
194 ValueBase
195 ValueNode_Duplicate::operator()(Time t)const
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)
214 {
215         // never offer this as a choice.  it's used automatically by the 'Duplicate' layer.
216         return false;
217 }