Change the default sub-parameters for the Time Loop convert so that the first second...
[synfig.git] / synfig-core / trunk / 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 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 x)
86 {
87         assert(i >= 0 && i < link_count());
88         switch(i)
89         {
90         case 0:  link_       = x; break;
91         case 1:  link_time_  = x; break;
92         case 2:  local_time_ = x; break;
93         case 3:  duration_   = 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_TimeLoop::get_link_vfunc(int i)const
104 {
105         assert(i >= 0 && i < link_count());
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         if(i==0) return _("Link");
125         if(i==1) return _("Link Time");
126         if(i==2) return _("Local Time");
127         if(i==3) return _("Duration");
128         return String();
129 }
130
131 String
132 ValueNode_TimeLoop::link_name(int i)const
133 {
134         assert(i >= 0 && i < link_count());
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         Time link_time  = (*link_time_) (t).get(Time());
157         Time local_time = (*local_time_)(t).get(Time());
158         Time duration   = (*duration_)  (t).get(Time());
159
160         if (duration == 0)
161                 t = link_time;
162         else if (duration > 0)
163         {
164                 t -= local_time;
165                 t -= floor(t / duration) * duration;
166                 t  = link_time + t;
167         }
168         else
169         {
170                 duration = -duration;
171                 t -= local_time;
172                 t -= floor(t / duration) * duration;
173                 t  = link_time - t;
174         }
175
176         return (*link_)(t);
177 }
178
179 String
180 ValueNode_TimeLoop::get_name()const
181 {
182         return "timeloop";
183 }
184
185 String
186 ValueNode_TimeLoop::get_local_name()const
187 {
188         return _("Time Loop");
189 }
190
191 bool
192 ValueNode_TimeLoop::check_type(ValueBase::Type type)
193 {
194         if(type)
195                 return true;
196         return false;
197 }