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