Use link_count from children vocabulary and return the stored vocabulary if already...
[synfig.git] / synfig-core / src / synfig / valuenode_timeloop.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_timeloop.cpp
3 **      \brief Implementation of the "Time Loop" 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_timeloop.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_TimeLoop::ValueNode_TimeLoop(const ValueBase::Type &x):
54         LinkableValueNode(x)
55 {
56 }
57
58 ValueNode_TimeLoop::ValueNode_TimeLoop(const ValueNode::Handle &x):
59         LinkableValueNode(x->get_type())
60 {
61         Vocab ret(get_children_vocab());
62         set_children_vocab(ret);
63         set_link("link", x);
64         set_link("link_time",  ValueNode_Const::create(Time(0)));
65         set_link("local_time", ValueNode_Const::create(Time(0)));
66         set_link("duration",   ValueNode_Const::create(Time(1)));
67 }
68
69 ValueNode_TimeLoop*
70 ValueNode_TimeLoop::create(const ValueBase &x)
71 {
72         return new ValueNode_TimeLoop(ValueNode_Const::create(x));
73 }
74
75 LinkableValueNode*
76 ValueNode_TimeLoop::create_new()const
77 {
78         return new ValueNode_TimeLoop(get_type());
79 }
80
81 ValueNode_TimeLoop::~ValueNode_TimeLoop()
82 {
83         unlink_all();
84 }
85
86 bool
87 ValueNode_TimeLoop::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(link_,       get_type());
94         case 1: CHECK_TYPE_AND_SET_VALUE(link_time_,  ValueBase::TYPE_TIME);
95         case 2: CHECK_TYPE_AND_SET_VALUE(local_time_, ValueBase::TYPE_TIME);
96         case 3: CHECK_TYPE_AND_SET_VALUE(duration_,   ValueBase::TYPE_TIME);
97         }
98         return false;
99 }
100
101 ValueNode::LooseHandle
102 ValueNode_TimeLoop::get_link_vfunc(int i)const
103 {
104         assert(i>=0 && i<link_count());
105
106         if(i==0) return link_;
107         if(i==1) return link_time_;
108         if(i==2) return local_time_;
109         if(i==3) return duration_;
110
111         return 0;
112 }
113
114 int
115 ValueNode_TimeLoop::link_count()const
116 {
117         return 4;
118 }
119
120 String
121 ValueNode_TimeLoop::link_local_name(int i)const
122 {
123         assert(i>=0 && i<link_count());
124
125         if(i==0) return _("Link");
126         if(i==1) return _("Link Time");
127         if(i==2) return _("Local Time");
128         if(i==3) return _("Duration");
129         return String();
130 }
131
132 String
133 ValueNode_TimeLoop::link_name(int i)const
134 {
135         assert(i>=0 && i<link_count());
136
137         if(i==0) return "link";
138         if(i==1) return "link_time";
139         if(i==2) return "local_time";
140         if(i==3) return "duration";
141         return String();
142 }
143
144 int
145 ValueNode_TimeLoop::get_link_index_from_name(const String &name)const
146 {
147         if(name=="link")       return 0;
148         if(name=="link_time")  return 1;
149         if(name=="local_time") return 2;
150         if(name=="duration")   return 3;
151
152         throw Exception::BadLinkName(name);
153 }
154
155 ValueBase
156 ValueNode_TimeLoop::operator()(Time t)const
157 {
158         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
159                 printf("%s:%d operator()\n", __FILE__, __LINE__);
160
161         Time link_time  = (*link_time_) (t).get(Time());
162         Time local_time = (*local_time_)(t).get(Time());
163         Time duration   = (*duration_)  (t).get(Time());
164
165         if (duration == 0)
166                 t = link_time;
167         else if (duration > 0)
168         {
169                 t -= local_time;
170                 t -= floor(t / duration) * duration;
171                 t  = link_time + t;
172         }
173         else
174         {
175                 duration = -duration;
176                 t -= local_time;
177                 t -= floor(t / duration) * duration;
178                 t  = link_time - t;
179         }
180
181         return (*link_)(t);
182 }
183
184 String
185 ValueNode_TimeLoop::get_name()const
186 {
187         return "timeloop";
188 }
189
190 String
191 ValueNode_TimeLoop::get_local_name()const
192 {
193         return _("Time Loop");
194 }
195
196 bool
197 ValueNode_TimeLoop::check_type(ValueBase::Type type)
198 {
199         if(type)
200                 return true;
201         return false;
202 }
203
204
205 LinkableValueNode::Vocab
206 ValueNode_TimeLoop::get_children_vocab_vfunc()const
207 {
208         if(children_vocab.size())
209                 return children_vocab;
210
211         LinkableValueNode::Vocab ret;
212
213         ret.push_back(ParamDesc("link")
214                 .set_local_name(_("Link"))
215                 .set_description(_("The value node to time loop"))
216         );
217
218         ret.push_back(ParamDesc("link_time")
219                 .set_local_name(_("Link Time"))
220                 .set_description(_("Start time of the loop for the value node timeline"))
221         );
222
223         ret.push_back(ParamDesc("local_time")
224                 .set_local_name(_("Local Time"))
225                 .set_description(_("The time when the resulted loop starts"))
226         );
227
228         ret.push_back(ParamDesc("duration")
229                 .set_local_name(_("Duration"))
230                 .set_description(_("Lenght of the loop"))
231         );
232         return ret;
233 }