Don't show 'Duplicate' in the Convert menu. The user doesn't need to see it, since...
[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(int(1)));
62         set_link("to",   ValueNode_Const::create(x.get(int())));
63         set_link("step", ValueNode_Const::create(int(1)));
64         index = 1;
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         switch(i)
89         {
90         case 0:  from_ = x; break;
91         case 1:  to_   = x; break;
92         case 2:  step_ = x; break;
93         default: return false;
94         }
95
96         signal_child_changed()(i);
97         signal_value_changed()();
98         return true;
99 }
100
101 ValueNode::LooseHandle
102 ValueNode_Duplicate::get_link_vfunc(int i)const
103 {
104         assert(i >= 0 && i < link_count());
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         if(i==0) return _("From");
123         if(i==1) return _("To");
124         if(i==2) return _("Step");
125         return String();
126 }
127
128 String
129 ValueNode_Duplicate::link_name(int i)const
130 {
131         assert(i >= 0 && i < link_count());
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         int from = (*from_)(t).get(int());
152         index = from;
153 }
154
155 bool
156 ValueNode_Duplicate::step(Time t)const
157 {
158         int from = (*from_)(t).get(int());
159         int to   = (*to_  )(t).get(int());
160         int step = (*step_)(t).get(int());
161
162         if (step == 0) return false;
163
164         step = abs(step);
165
166         if (from < to)
167         {
168                 index += step;
169                 return index <= to;
170         }
171         else
172         {
173                 index -= step;
174                 return index >= to;
175         }
176 }
177
178 int
179 ValueNode_Duplicate::count_steps(Time t)const
180 {
181         int from = (*from_)(t).get(int());
182         int to   = (*to_  )(t).get(int());
183         int step = (*step_)(t).get(int());
184
185         if (step == 0) return 1;
186
187         return abs((from - to) / step) + 1;
188 }
189
190 ValueBase
191 ValueNode_Duplicate::operator()(Time t)const
192 {
193         return index;
194 }
195
196 String
197 ValueNode_Duplicate::get_name()const
198 {
199         return "duplicate";
200 }
201
202 String
203 ValueNode_Duplicate::get_local_name()const
204 {
205         return _("Duplicate");
206 }
207
208 bool
209 ValueNode_Duplicate::check_type(ValueBase::Type type)
210 {
211         // never offer this as a choice.  it's used automatically by the 'Duplicate' layer.
212         return false;
213 }