forked from synfig/synfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeloop.cpp
293 lines (251 loc) · 8.03 KB
/
timeloop.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* === S Y N F I G ========================================================= */
/*! \file timeloop.cpp
** \brief Implementation of the "Time Loop" layer
**
** $Id$
**
** \legal
** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
** Copyright (c) 2007, 2008 Chris Moore
** Copyright (c) 2011-2013 Carlos López
**
** This package is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License as
** published by the Free Software Foundation; either version 2 of
** the License, or (at your option) any later version.
**
** This package is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** General Public License for more details.
** \endlegal
**
** === N O T E S ===========================================================
**
** ========================================================================= */
/* === H E A D E R S ======================================================= */
#ifdef USING_PCH
# include "pch.h"
#else
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <synfig/localization.h>
#include <synfig/general.h>
#include "timeloop.h"
#include <synfig/valuenode.h>
#include <synfig/valuenodes/valuenode_const.h>
#include <synfig/valuenodes/valuenode_subtract.h>
#include <synfig/time.h>
#include <synfig/context.h>
#include <synfig/paramdesc.h>
#include <synfig/renddesc.h>
#include <synfig/value.h>
#include <synfig/canvas.h>
#endif
using namespace etl;
using namespace synfig;
using namespace modules;
using namespace lyr_std;
/* === M A C R O S ========================================================= */
/* === G L O B A L S ======================================================= */
SYNFIG_LAYER_INIT(Layer_TimeLoop);
SYNFIG_LAYER_SET_NAME(Layer_TimeLoop,"timeloop");
SYNFIG_LAYER_SET_LOCAL_NAME(Layer_TimeLoop,N_("Time Loop"));
SYNFIG_LAYER_SET_CATEGORY(Layer_TimeLoop,N_("Time"));
SYNFIG_LAYER_SET_VERSION(Layer_TimeLoop,"0.2");
/* === P R O C E D U R E S ================================================= */
/* === M E T H O D S ======================================================= */
Layer_TimeLoop::Layer_TimeLoop()
{
old_version=false;
param_only_for_positive_duration=ValueBase(false);
param_symmetrical=ValueBase(true);
param_link_time=ValueBase(Time(0));
param_local_time=ValueBase(Time(0));
param_duration=ValueBase(Time(1));
SET_INTERPOLATION_DEFAULTS();
SET_STATIC_DEFAULTS();
}
Layer_TimeLoop::~Layer_TimeLoop()
{
}
bool
Layer_TimeLoop::set_param(const String & param, const ValueBase &value)
{
if(old_version)
{
if(param=="start_time" && value.same_type_as(start_time))
{
value.put(&start_time);
return true;
}
if(param=="end_time" && value.same_type_as(end_time))
{
value.put(&end_time);
return true;
}
}
else
{
IMPORT_VALUE(param_local_time);
IMPORT_VALUE(param_link_time);
IMPORT_VALUE(param_duration);
IMPORT_VALUE(param_only_for_positive_duration);
IMPORT_VALUE(param_symmetrical);
}
return Layer::set_param(param,value);
}
ValueBase
Layer_TimeLoop::get_param(const String & param)const
{
EXPORT_VALUE(param_link_time);
EXPORT_VALUE(param_local_time);
EXPORT_VALUE(param_duration);
EXPORT_VALUE(param_only_for_positive_duration);
EXPORT_VALUE(param_symmetrical);
EXPORT_NAME();
EXPORT_VERSION();
return Layer::get_param(param);
}
Layer::Vocab
Layer_TimeLoop::get_param_vocab()const
{
Layer::Vocab ret(Layer::get_param_vocab());
ret.push_back(ParamDesc("link_time")
.set_local_name(_("Link Time"))
.set_description(_("Start time of the loop for the cycled context"))
.set_static(true)
);
ret.push_back(ParamDesc("local_time")
.set_local_name(_("Local Time"))
.set_description(_("The time when the resulted loop starts"))
.set_static(true)
);
ret.push_back(ParamDesc("duration")
.set_local_name(_("Duration"))
.set_description(_("Length of the loop"))
.set_static(true)
);
ret.push_back(ParamDesc("only_for_positive_duration")
.set_local_name(_("Only For Positive Duration"))
.set_description(_("When checked, loops only positive durations"))
.set_static(true)
);
ret.push_back(ParamDesc("symmetrical")
.set_local_name(_("Symmetrical"))
.set_description(_("When checked, loops are mirrored centered at Local Time"))
.set_static(true)
);
return ret;
}
bool
Layer_TimeLoop::set_version(const String &ver)
{
if (ver=="0.1")
old_version = true;
return true;
}
void
Layer_TimeLoop::reset_version()
{
Time link_time, local_time, duration;
bool only_for_positive_duration, symmetrical;
// if we're not converting from an old version of the layer, there's nothing to do
if (!old_version)
return;
old_version = false;
// these are the conversions to go from 0.1 to 0.2:
//
// local_time = start_time
// duration = end_time - start_time
// if (time < start_time)
// link_time = -duration : if we want to reproduce the old behaviour - do we?
// else
// link_time = 0
// convert the static parameters
local_time = start_time;
duration = end_time - start_time;
only_for_positive_duration = true;
symmetrical = false;
param_local_time.set(local_time);
param_duration.set(duration);
param_only_for_positive_duration.set(only_for_positive_duration);
param_symmetrical.set(symmetrical);
//! \todo layer version 0.1 acted differently before start_time was reached - possibly due to a bug
link_time = 0;
param_link_time.set(link_time);
// convert the dynamic parameters
const DynamicParamList &dpl = dynamic_param_list();
// if neither start_time nor end_time are dynamic, there's nothing more to do
if (dpl.count("start_time") == 0 && dpl.count("end_time") == 0)
return;
etl::rhandle<ValueNode> start_time_value_node, end_time_value_node;
LinkableValueNode* duration_value_node;
if (dpl.count("start_time"))
{
start_time_value_node = dpl.find("start_time")->second;
disconnect_dynamic_param("start_time");
}
else
start_time_value_node = ValueNode_Const::create(start_time);
if (dpl.count("end_time"))
{
end_time_value_node = dpl.find("end_time")->second;
disconnect_dynamic_param("end_time");
}
else
end_time_value_node = ValueNode_Const::create(end_time);
duration_value_node = ValueNode_Subtract::create(Time(0));
duration_value_node->set_link("lhs", end_time_value_node);
duration_value_node->set_link("rhs", start_time_value_node);
connect_dynamic_param("local_time", start_time_value_node);
connect_dynamic_param("duration", duration_value_node);
}
void
Layer_TimeLoop::set_time_vfunc(IndependentContext context, Time t)const
{
Time link_time=param_link_time.get(Time());
Time local_time=param_local_time.get(Time());
Time duration=param_duration.get(Time());
bool only_for_positive_duration=param_only_for_positive_duration.get(bool());
bool symmetrical=param_symmetrical.get(bool());
Time time = t;
float document_fps=get_canvas()->rend_desc().get_frame_rate();
if (!only_for_positive_duration || duration > 0)
{
if (duration == 0)
t = link_time;
else {
float local_time_frames = round(local_time*document_fps);
float t_frames = round(t*document_fps);
float duration_frames = round(duration*document_fps);
if (duration > 0)
{
t_frames -= local_time_frames;
// Simple formula looks like this:
// t -= floor(t / duration) * duration;
// but we should make all calculations in frames to avoid round errors
t_frames -= floor(t_frames / duration_frames) * duration_frames;
// converting back to seconds:
t = t_frames / document_fps;
t = link_time + t;
}
else
{
t_frames -= local_time_frames;
// Simple formula looks like this:
// t -= floor(t / -duration) * -duration;
// but we should make all calculations in frames to avoid round errors
t_frames -= floor(t_frames / -duration_frames) * -duration_frames;
// converting back to seconds:
t = t_frames / document_fps;
t = link_time - t;
}
}
// for compatibility with v0.1 layers; before local_time is reached, take a step back
if (!symmetrical && time < local_time)
t -= duration;
}
context.set_time(t);
}