f896af0614aa217609a19a38eef50efc66a91370
[synfig.git] / synfig-core / src / modules / lyr_std / timeloop.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file timeloop.cpp
3 **      \brief Implementation of the "Time Loop" layer
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 ** === N O T E S ===========================================================
23 **
24 ** ========================================================================= */
25
26 /* === H E A D E R S ======================================================= */
27
28 #ifdef USING_PCH
29 #       include "pch.h"
30 #else
31 #ifdef HAVE_CONFIG_H
32 #       include <config.h>
33 #endif
34
35 #include "timeloop.h"
36 #include <synfig/valuenode.h>
37 #include <synfig/valuenode_const.h>
38 #include <synfig/valuenode_subtract.h>
39 #include <synfig/time.h>
40 #include <synfig/context.h>
41 #include <synfig/paramdesc.h>
42 #include <synfig/renddesc.h>
43 #include <synfig/value.h>
44
45 #endif
46
47 using namespace synfig;
48 using namespace std;
49 using namespace etl;
50
51 /* === M A C R O S ========================================================= */
52
53 /* === G L O B A L S ======================================================= */
54
55 SYNFIG_LAYER_INIT(Layer_TimeLoop);
56 SYNFIG_LAYER_SET_NAME(Layer_TimeLoop,"timeloop");
57 SYNFIG_LAYER_SET_LOCAL_NAME(Layer_TimeLoop,N_("Time Loop"));
58 SYNFIG_LAYER_SET_CATEGORY(Layer_TimeLoop,N_("Other"));
59 SYNFIG_LAYER_SET_VERSION(Layer_TimeLoop,"0.2");
60 SYNFIG_LAYER_SET_CVS_ID(Layer_TimeLoop,"$Id$");
61
62 /* === P R O C E D U R E S ================================================= */
63
64 /* === M E T H O D S ======================================================= */
65
66 Layer_TimeLoop::Layer_TimeLoop()
67 {
68         old_version=false;
69         only_for_positive_duration=false;
70         symmetrical=true;
71         link_time=0;
72         local_time=0;
73         duration=1;
74         Layer::Vocab voc(get_param_vocab());
75         Layer::fill_static(voc);
76 }
77
78 Layer_TimeLoop::~Layer_TimeLoop()
79 {
80 }
81
82 bool
83 Layer_TimeLoop::set_param(const String & param, const ValueBase &value)
84 {
85         if(old_version)
86         {
87                 IMPORT(start_time);
88                 IMPORT(end_time);
89         }
90         else
91         {
92                 IMPORT(local_time);
93                 IMPORT(link_time);
94                 IMPORT(duration);
95                 IMPORT(only_for_positive_duration);
96                 IMPORT(symmetrical);
97         }
98
99         return Layer::set_param(param,value);
100 }
101
102 ValueBase
103 Layer_TimeLoop::get_param(const String & param)const
104 {
105         EXPORT(link_time);
106         EXPORT(local_time);
107         EXPORT(duration);
108         EXPORT(only_for_positive_duration);
109         EXPORT(symmetrical);
110         EXPORT_NAME();
111         EXPORT_VERSION();
112
113         return Layer::get_param(param);
114 }
115
116 Layer::Vocab
117 Layer_TimeLoop::get_param_vocab()const
118 {
119         Layer::Vocab ret(Layer::get_param_vocab());
120
121         ret.push_back(ParamDesc("link_time")
122                 .set_local_name(_("Link Time"))
123         );
124
125         ret.push_back(ParamDesc("local_time")
126                 .set_local_name(_("Local Time"))
127         );
128
129         ret.push_back(ParamDesc("duration")
130                 .set_local_name(_("Duration"))
131         );
132
133         ret.push_back(ParamDesc("only_for_positive_duration")
134                 .set_local_name(_("Only For Positive Duration"))
135         );
136
137         ret.push_back(ParamDesc("symmetrical")
138                 .set_local_name(_("Symmetrical"))
139         );
140
141         return ret;
142 }
143
144 bool
145 Layer_TimeLoop::set_version(const String &ver)
146 {
147         if (ver=="0.1")
148                 old_version = true;
149
150         return true;
151 }
152
153 void
154 Layer_TimeLoop::reset_version()
155 {
156         // if we're not converting from an old version of the layer, there's nothing to do
157         if (!old_version)
158                 return;
159
160         old_version = false;
161
162         // these are the conversions to go from 0.1 to 0.2:
163         //
164         //       local_time = start_time
165         //       duration = end_time - start_time
166         //       if (time < start_time)
167         //         link_time = -duration : if we want to reproduce the old behaviour - do we?
168         //       else
169         //               link_time = 0
170
171         // convert the static parameters
172         local_time = start_time;
173         duration = end_time - start_time;
174         only_for_positive_duration = true;
175         symmetrical = false;
176
177         //! \todo layer version 0.1 acted differently before start_time was reached - possibly due to a bug
178         link_time = 0;
179
180         // convert the dynamic parameters
181         const DynamicParamList &dpl = dynamic_param_list();
182
183         // if neither start_time nor end_time are dynamic, there's nothing more to do
184         if (dpl.count("start_time") == 0 && dpl.count("end_time") == 0)
185                 return;
186
187         etl::rhandle<ValueNode> start_time_value_node, end_time_value_node;
188         LinkableValueNode* duration_value_node;
189
190         if (dpl.count("start_time"))
191         {
192                 start_time_value_node = dpl.find("start_time")->second;
193                 disconnect_dynamic_param("start_time");
194         }
195         else
196                 start_time_value_node = ValueNode_Const::create(start_time);
197
198         if (dpl.count("end_time"))
199         {
200                 end_time_value_node = dpl.find("end_time")->second;
201                 disconnect_dynamic_param("end_time");
202         }
203         else
204                 end_time_value_node = ValueNode_Const::create(end_time);
205
206         duration_value_node = ValueNode_Subtract::create(Time(0));
207         duration_value_node->set_link("lhs", end_time_value_node);
208         duration_value_node->set_link("rhs", start_time_value_node);
209
210         connect_dynamic_param("local_time", start_time_value_node);
211         connect_dynamic_param("duration",   duration_value_node);
212 }
213
214 void
215 Layer_TimeLoop::set_time(Context context, Time t)const
216 {
217         Time time = t;
218
219         if (!only_for_positive_duration || duration > 0)
220         {
221                 if (duration == 0)
222                         t = link_time;
223                 else if (duration > 0)
224                 {
225                         t -= local_time;
226                         t -= floor(t / duration) * duration;
227                         t  = link_time + t;
228                 }
229                 else
230                 {
231                         t -= local_time;
232                         t -= floor(t / -duration) * -duration;
233                         t  = link_time - t;
234                 }
235
236                 // for compatibility with v0.1 layers; before local_time is reached, take a step back
237                 if (!symmetrical && time < local_time)
238                         t -= duration;
239         }
240
241         context.set_time(t);
242 }
243
244 Color
245 Layer_TimeLoop::get_color(Context context, const Point &pos)const
246 {
247         return context.get_color(pos);
248 }
249
250 bool
251 Layer_TimeLoop::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
252 {
253         return context.accelerated_render(surface,quality,renddesc,cb);
254 }