Made all the assert() lines which check the valuenode sub-parameter index range the...
[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
89         switch(i)
90         {
91         case 0:  link_       = x; break;
92         case 1:  link_time_  = x; break;
93         case 2:  local_time_ = x; break;
94         case 3:  duration_   = x; break;
95         default: return false;
96         }
97
98         signal_child_changed()(i);
99         signal_value_changed()();
100         return true;
101 }
102
103 ValueNode::LooseHandle
104 ValueNode_TimeLoop::get_link_vfunc(int i)const
105 {
106         assert(i>=0 && i<link_count());
107
108         if(i==0) return link_;
109         if(i==1) return link_time_;
110         if(i==2) return local_time_;
111         if(i==3) return duration_;
112
113         return 0;
114 }
115
116 int
117 ValueNode_TimeLoop::link_count()const
118 {
119         return 4;
120 }
121
122 String
123 ValueNode_TimeLoop::link_local_name(int i)const
124 {
125         assert(i>=0 && i<link_count());
126
127         if(i==0) return _("Link");
128         if(i==1) return _("Link Time");
129         if(i==2) return _("Local Time");
130         if(i==3) return _("Duration");
131         return String();
132 }
133
134 String
135 ValueNode_TimeLoop::link_name(int i)const
136 {
137         assert(i>=0 && i<link_count());
138
139         if(i==0) return "link";
140         if(i==1) return "link_time";
141         if(i==2) return "local_time";
142         if(i==3) return "duration";
143         return String();
144 }
145
146 int
147 ValueNode_TimeLoop::get_link_index_from_name(const String &name)const
148 {
149         if(name=="link")       return 0;
150         if(name=="link_time")  return 1;
151         if(name=="local_time") return 2;
152         if(name=="duration")   return 3;
153
154         throw Exception::BadLinkName(name);
155 }
156
157 ValueBase
158 ValueNode_TimeLoop::operator()(Time t)const
159 {
160         Time link_time  = (*link_time_) (t).get(Time());
161         Time local_time = (*local_time_)(t).get(Time());
162         Time duration   = (*duration_)  (t).get(Time());
163
164         if (duration == 0)
165                 t = link_time;
166         else if (duration > 0)
167         {
168                 t -= local_time;
169                 t -= floor(t / duration) * duration;
170                 t  = link_time + t;
171         }
172         else
173         {
174                 duration = -duration;
175                 t -= local_time;
176                 t -= floor(t / duration) * duration;
177                 t  = link_time - t;
178         }
179
180         return (*link_)(t);
181 }
182
183 String
184 ValueNode_TimeLoop::get_name()const
185 {
186         return "timeloop";
187 }
188
189 String
190 ValueNode_TimeLoop::get_local_name()const
191 {
192         return _("Time Loop");
193 }
194
195 bool
196 ValueNode_TimeLoop::check_type(ValueBase::Type type)
197 {
198         if(type)
199                 return true;
200         return false;
201 }