Add new convert types 'From Integer' and 'Duplicate' and new layer 'Duplicate'.
[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 ValueNode::Handle &x):
59         LinkableValueNode(x->get_type())
60 {
61         set_link("from", ValueNode_Const::create(int(0)));
62         set_link("to",   ValueNode_Const::create(int(3)));
63         set_link("step", ValueNode_Const::create(int(1)));
64 }
65
66 ValueNode_Duplicate*
67 ValueNode_Duplicate::create(const ValueBase &x)
68 {
69         return new ValueNode_Duplicate(ValueNode_Const::create(x));
70 }
71
72 LinkableValueNode*
73 ValueNode_Duplicate::create_new()const
74 {
75         return new ValueNode_Duplicate(get_type());
76 }
77
78 ValueNode_Duplicate::~ValueNode_Duplicate()
79 {
80         unlink_all();
81 }
82
83 bool
84 ValueNode_Duplicate::set_link_vfunc(int i,ValueNode::Handle x)
85 {
86         assert(i >= 0 && i < link_count());
87         switch(i)
88         {
89         case 0:  from_ = x; break;
90         case 1:  to_   = x; break;
91         case 2:  step_ = x; break;
92         default: return false;
93         }
94
95         signal_child_changed()(i);
96         signal_value_changed()();
97         return true;
98 }
99
100 ValueNode::LooseHandle
101 ValueNode_Duplicate::get_link_vfunc(int i)const
102 {
103         assert(i >= 0 && i < link_count());
104         if(i==0) return from_;
105         if(i==1) return to_;
106         if(i==2) return step_;
107
108         return 0;
109 }
110
111 int
112 ValueNode_Duplicate::link_count()const
113 {
114         return 3;
115 }
116
117 String
118 ValueNode_Duplicate::link_local_name(int i)const
119 {
120         assert(i >= 0 && i < link_count());
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         if(i==0) return "from";
132         if(i==1) return "to";
133         if(i==2) return "step";
134         return String();
135 }
136
137 int
138 ValueNode_Duplicate::get_link_index_from_name(const String &name)const
139 {
140         if(name=="from") return 0;
141         if(name=="to")   return 1;
142         if(name=="step") return 2;
143
144         throw Exception::BadLinkName(name);
145 }
146
147 void
148 ValueNode_Duplicate::reset_index(Time t)const
149 {
150         int from = (*from_)(t).get(int());
151         index = from;
152 }
153
154 bool
155 ValueNode_Duplicate::step(Time t)const
156 {
157         int from = (*from_)(t).get(int());
158         int to   = (*to_  )(t).get(int());
159         int step = (*step_)(t).get(int());
160
161         if (step == 0) return false;
162
163         step = abs(step);
164
165         if (from < to)
166         {
167                 index += step;
168                 return index <= to;
169         }
170         else
171         {
172                 index -= step;
173                 return index >= to;
174         }
175 }
176
177 int
178 ValueNode_Duplicate::count_steps(Time t)const
179 {
180         int from = (*from_)(t).get(int());
181         int to   = (*to_  )(t).get(int());
182         int step = (*step_)(t).get(int());
183
184         if (step == 0) return 1;
185
186         return abs((from - to) / step) + 1;
187 }
188
189 ValueBase
190 ValueNode_Duplicate::operator()(Time t)const
191 {
192         return index;
193 }
194
195 String
196 ValueNode_Duplicate::get_name()const
197 {
198         return "duplicate";
199 }
200
201 String
202 ValueNode_Duplicate::get_local_name()const
203 {
204         return _("Duplicate");
205 }
206
207 bool
208 ValueNode_Duplicate::check_type(ValueBase::Type type)
209 {
210         return type==ValueBase::TYPE_INTEGER;
211 }