forked from NatronGitHub/Natron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringAnimationManager.cpp
349 lines (286 loc) · 10.3 KB
/
StringAnimationManager.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* ***** BEGIN LICENSE BLOCK *****
* This file is part of Natron <https://natrongithub.github.io/>,
* (C) 2018-2021 The Natron developers
* (C) 2013-2018 INRIA and Alexandre Gauthier-Foichat
*
* Natron 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.
*
* Natron 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.
*
* You should have received a copy of the GNU General Public License
* along with Natron. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>
* ***** END LICENSE BLOCK ***** */
// ***** BEGIN PYTHON BLOCK *****
// from <https://docs.python.org/3/c-api/intro.html#include-files>:
// "Since Python may define some pre-processor definitions which affect the standard headers on some systems, you must include Python.h before any standard headers are included."
#include <Python.h>
// ***** END PYTHON BLOCK *****
#include "StringAnimationManager.h"
#include <set>
#include <cmath>
#include <cassert>
#include <stdexcept>
#include <QtCore/QMutex>
#include <ofxParam.h>
#include <ofxhPropertySuite.h> // PropSpec
#include "Engine/Knob.h"
NATRON_NAMESPACE_ENTER
NATRON_NAMESPACE_ANONYMOUS_ENTER
struct StringKeyFrame
{
std::string value;
double time;
};
struct StringKeyFrame_compare_time
{
bool operator() (const StringKeyFrame & lhs,
const StringKeyFrame & rhs) const
{
return lhs.time < rhs.time;
}
};
typedef std::set<StringKeyFrame, StringKeyFrame_compare_time> Keyframes;
NATRON_NAMESPACE_ANONYMOUS_EXIT
struct StringAnimationManagerPrivate
{
StringAnimationManager::customParamInterpolationV1Entry_t customInterpolation;
void* ofxParamHandle;
mutable QMutex keyframesMutex;
Keyframes keyframes;
const KnobI* knob;
StringAnimationManagerPrivate(const KnobI* knob)
: customInterpolation(NULL)
, ofxParamHandle(NULL)
, keyframesMutex()
, keyframes()
, knob(knob)
{
}
};
StringAnimationManager::StringAnimationManager(const KnobI* knob)
: _imp( new StringAnimationManagerPrivate(knob) )
{
}
StringAnimationManager::~StringAnimationManager()
{
}
bool
StringAnimationManager::hasCustomInterp() const
{
return _imp->customInterpolation != NULL;
}
void
StringAnimationManager::setCustomInterpolation(customParamInterpolationV1Entry_t func,
void* ofxParamHandle)
{
_imp->customInterpolation = func;
_imp->ofxParamHandle = ofxParamHandle;
}
bool
StringAnimationManager::customInterpolation(double time,
std::string* ret) const
{
QMutexLocker l(&_imp->keyframesMutex);
assert(_imp->customInterpolation);
///if there's a single keyframe, return it
if ( _imp->keyframes.empty() ) {
return false;
}
if (_imp->keyframes.size() == 1) {
*ret = _imp->keyframes.begin()->value;
return true;
}
/// get the keyframes surrounding the time
Keyframes::const_iterator upper = _imp->keyframes.end();
Keyframes::const_iterator lower = _imp->keyframes.end();
for (Keyframes::const_iterator it = _imp->keyframes.begin(); it != _imp->keyframes.end(); ++it) {
if (it->time > time) {
upper = it;
break;
} else if (it->time == time) {
///if there's a keyframe exactly at this time, return its value
*ret = it->value;
return true;
}
}
if ( upper == _imp->keyframes.end() ) {
///if the time is greater than the time of all keyframes return the last
--upper;
*ret = upper->value;
return true;
} else if ( upper == _imp->keyframes.begin() ) {
///if the time is lesser than the time of all keyframes, return the first
*ret = upper->value;
return true;
} else {
///general case, we're in-between 2 keyframes
lower = upper;
--lower;
}
OFX::Host::Property::PropSpec inArgsSpec[] = {
{ kOfxPropName, OFX::Host::Property::eString, 1, true, "" },
{ kOfxPropTime, OFX::Host::Property::eDouble, 1, true, "" },
{ kOfxParamPropCustomValue, OFX::Host::Property::eString, 2, true, ""},
{ kOfxParamPropInterpolationTime, OFX::Host::Property::eDouble, 2, true, "" },
{ kOfxParamPropInterpolationAmount, OFX::Host::Property::eDouble, 1, true, "" },
OFX::Host::Property::propSpecEnd
};
OFX::Host::Property::Set inArgs(inArgsSpec);
inArgs.setStringProperty( kOfxPropName, _imp->knob->getName() );
inArgs.setDoubleProperty(kOfxPropTime, time);
inArgs.setStringProperty(kOfxParamPropCustomValue, lower->value, 0);
inArgs.setStringProperty(kOfxParamPropCustomValue, upper->value, 1);
inArgs.setDoubleProperty(kOfxParamPropInterpolationTime, lower->time, 0);
inArgs.setDoubleProperty(kOfxParamPropInterpolationTime, upper->time, 1);
inArgs.setDoubleProperty( kOfxParamPropInterpolationAmount, (time - lower->time) / (double)(upper->time - lower->time) );
OFX::Host::Property::PropSpec outArgsSpec[] = {
{ kOfxParamPropCustomValue, OFX::Host::Property::eString, 1, false, ""},
OFX::Host::Property::propSpecEnd
};
OFX::Host::Property::Set outArgs(outArgsSpec);
l.unlock();
_imp->customInterpolation( _imp->ofxParamHandle, inArgs.getHandle(), outArgs.getHandle() );
*ret = outArgs.getStringProperty(kOfxParamPropCustomValue, 0).c_str();
return true;
} // customInterpolation
void
StringAnimationManager::insertKeyFrame(double time,
const std::string & v,
double* index)
{
StringKeyFrame k;
k.time = time;
k.value = v;
QMutexLocker l(&_imp->keyframesMutex);
std::pair<Keyframes::iterator, bool> ret = _imp->keyframes.insert(k);
if (!ret.second) {
_imp->keyframes.erase(ret.first);
ret = _imp->keyframes.insert(k);
assert(ret.second);
}
*index = std::distance(_imp->keyframes.begin(), ret.first);
}
void
StringAnimationManager::removeKeyFrame(double time)
{
QMutexLocker l(&_imp->keyframesMutex);
for (Keyframes::iterator it = _imp->keyframes.begin(); it != _imp->keyframes.end(); ++it) {
if (it->time == time) {
_imp->keyframes.erase(it);
return;
}
}
}
void
StringAnimationManager::clearKeyFrames()
{
QMutexLocker l(&_imp->keyframesMutex);
_imp->keyframes.clear();
}
void
StringAnimationManager::stringFromInterpolatedIndex(double interpolated,
std::string* returnValue) const
{
int index = std::floor(interpolated + 0.5);
QMutexLocker l(&_imp->keyframesMutex);
if ( _imp->keyframes.empty() ) {
return;
}
///if the index is not in the range, just return the last
if ( index >= (int)_imp->keyframes.size() ) {
Keyframes::const_iterator it = _imp->keyframes.end();
--it;
*returnValue = it->value;
return;
}
int i = 0;
for (Keyframes::const_iterator it = _imp->keyframes.begin(); it != _imp->keyframes.end(); ++it) {
if (i == index) {
*returnValue = it->value;
return;
}
++i;
}
}
void
StringAnimationManager::clone(const StringAnimationManager & other)
{
QMutexLocker l(&_imp->keyframesMutex);
QMutexLocker l2(&other._imp->keyframesMutex);
_imp->keyframes = other._imp->keyframes;
}
bool
StringAnimationManager::cloneAndCheckIfChanged(const StringAnimationManager & other)
{
QMutexLocker l(&_imp->keyframesMutex);
QMutexLocker l2(&other._imp->keyframesMutex);
bool hasChanged = false;
if ( _imp->keyframes.size() != other._imp->keyframes.size() ) {
hasChanged = true;
}
if (!hasChanged) {
Keyframes::const_iterator oit = other._imp->keyframes.begin();
for (Keyframes::const_iterator it = _imp->keyframes.begin(); it != _imp->keyframes.end(); ++it, ++oit) {
if ( (it->time != oit->time) || (it->value != oit->value) ) {
hasChanged = true;
break;
}
}
}
if (hasChanged) {
_imp->keyframes = other._imp->keyframes;
}
return hasChanged;
}
void
StringAnimationManager::clone(const StringAnimationManager & other,
SequenceTime offset,
const RangeD* range)
{
// The range=[0,0] case is obviously a bug in the spec of paramCopy() from the parameter suite:
// it prevents copying the value of frame 0.
bool copyRange = range != NULL /*&& (range->min != 0 || range->max != 0)*/;
QMutexLocker l(&_imp->keyframesMutex);
_imp->keyframes.clear();
QMutexLocker l2(&other._imp->keyframesMutex);
for (Keyframes::const_iterator it = other._imp->keyframes.begin(); it != other._imp->keyframes.end(); ++it) {
if ( copyRange && ( (it->time < range->min) || (it->time > range->max) ) ) {
continue;
}
StringKeyFrame k;
k.time = it->time + offset;
k.value = it->value;
_imp->keyframes.insert(k);
}
}
void
StringAnimationManager::load(const std::map<int, std::string> & keyframes)
{
QMutexLocker l(&_imp->keyframesMutex);
assert( _imp->keyframes.empty() );
for (std::map<int, std::string>::const_iterator it = keyframes.begin(); it != keyframes.end(); ++it) {
StringKeyFrame k;
k.time = it->first;
k.value = it->second;
std::pair<Keyframes::iterator, bool> ret = _imp->keyframes.insert(k);
assert(ret.second);
Q_UNUSED(ret);
}
}
void
StringAnimationManager::save(std::map<int, std::string>* keyframes) const
{
QMutexLocker l(&_imp->keyframesMutex);
for (Keyframes::const_iterator it = _imp->keyframes.begin(); it != _imp->keyframes.end(); ++it) {
std::pair<std::map<int, std::string>::iterator, bool> success = keyframes->insert( std::make_pair(it->time, it->value) );
assert(success.second);
Q_UNUSED(success);
}
}
NATRON_NAMESPACE_EXIT