summaryrefslogtreecommitdiffstats
path: root/src/dm/systems/StudioAnimationSystem.cpp
blob: 4ecae00c122ecba48be6f22ab8265ec458e8d8ee (plain)
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/****************************************************************************
**
** Copyright (C) 1993-2009 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "Qt3DSDMPrefix.h"
#include "StudioAnimationSystem.h"
#include "StudioPropertySystem.h"

using namespace std;

namespace qt3dsdm {

bool AnimationFloatPairContainsAnimation(Qt3DSDMAnimationHandle inAnimation,
                                         const TAnimationFloatPair &inPair)
{
    return inAnimation == inPair.first;
}

CStudioAnimationSystem::CStudioAnimationSystem(TPropertySystemPtr inPropertySystem,
                                               TSlideSystemPtr inSlideSystem,
                                               TSlideCorePtr inSlideCore,
                                               TSlideGraphCorePtr inSlideGraphCore,
                                               TAnimationCorePtr inAnimationCore)
    : m_PropertySystem(inPropertySystem)
    , m_SlideSystem(inSlideSystem)
    , m_SlideCore(inSlideCore)
    , m_SlideGraphCore(inSlideGraphCore)
    , m_AnimationCore(inAnimationCore)
    , m_AutoKeyframe(false)
    , m_SmoothInterpolation(false)
{
    IAnimationCoreSignalProvider *theAnimationSignals =
        dynamic_cast<IAnimationCoreSignalProvider *>(m_AnimationCore.get());
    m_Connections.push_back(theAnimationSignals->ConnectAnimationDeleted(
        std::bind(&CStudioAnimationSystem::OnAnimationDeleted, this, std::placeholders::_1)));
}

void CStudioAnimationSystem::OnAnimationDeleted(Qt3DSDMAnimationHandle inAnimation)
{
    ClearTemporaryAnimationValues(inAnimation);
}

void CStudioAnimationSystem::ClearTemporaryAnimationValues()
{
    m_AnimationFloatPairs.clear();
}

void CStudioAnimationSystem::ClearTemporaryAnimationValues(Qt3DSDMAnimationHandle inAnimation)
{
    erase_if(m_AnimationFloatPairs,
             std::bind(AnimationFloatPairContainsAnimation, inAnimation, std::placeholders::_1));
}

inline bool IsAnimationInfoEqual(const SAnimationInfo &inInfo, Qt3DSDMSlideHandle inSlide,
                                 Qt3DSDMInstanceHandle inInstance, Qt3DSDMPropertyHandle inProperty)
{
    if (inInfo.m_Slide == inSlide && inInfo.m_Instance == inInstance
        && inInfo.m_Property == inProperty)
        return true;
    return false;
}
inline bool IsAnimationInfoEqual(const SAnimationInfo &inInfo, Qt3DSDMSlideHandle inSlide,
                                 Qt3DSDMInstanceHandle inInstance, Qt3DSDMPropertyHandle inProperty,
                                 size_t inIndex)
{
    if (IsAnimationInfoEqual(inInfo, inSlide, inInstance, inProperty) && inInfo.m_Index == inIndex)
        return true;
    return false;
}

inline bool ApplyIfAnimationMatches(TAnimationFloatPair inAnimationFloatPair,
                                    Qt3DSDMSlideHandle inSlide, Qt3DSDMInstanceHandle inInstance,
                                    Qt3DSDMPropertyHandle inProperty,
                                    TAnimationCorePtr inAnimationCore, SValue &outValue)
{
    SAnimationInfo theInfo = inAnimationCore->GetAnimationInfo(inAnimationFloatPair.first);
    if (IsAnimationInfoEqual(theInfo, inSlide, inInstance, inProperty)) {
        SetAnimationValue(inAnimationFloatPair.second, theInfo.m_Index, outValue);
        return true;
    }
    return false;
}

void CStudioAnimationSystem::OverrideChannelIfAnimated(Qt3DSDMSlideHandle inSlide,
                                                       Qt3DSDMInstanceHandle inInstance,
                                                       Qt3DSDMPropertyHandle inProperty,
                                                       size_t inIndex, float inSeconds,
                                                       bool &ioAnimated, SValue &outValue) const
{
    Qt3DSDMAnimationHandle theAnimation =
        m_AnimationCore->GetAnimation(inSlide, inInstance, inProperty, inIndex);
    if (theAnimation.Valid() && m_AnimationCore->GetKeyframeCount(theAnimation)) {
        float theValue = m_AnimationCore->EvaluateAnimation(theAnimation, inSeconds);
        SetAnimationValue(theValue, inIndex, outValue);
        ioAnimated |= true;
    }
}

// Value must be *primed* first.
bool CStudioAnimationSystem::GetAnimatedInstancePropertyValue(Qt3DSDMSlideHandle inSlide,
                                                              Qt3DSDMInstanceHandle inInstance,
                                                              Qt3DSDMPropertyHandle inProperty,
                                                              SValue &outValue) const
{
    bool retval = false;

    size_t arity = GetVariantAnimatableArity(outValue);
    if (arity) {
        for (size_t index = 0; index < m_AnimationFloatPairs.size(); ++index)
            retval |= ApplyIfAnimationMatches(m_AnimationFloatPairs.at(index), inSlide, inInstance,
                                              inProperty, m_AnimationCore, outValue);
    }

    if (!retval) {
        bool animated = false;
        size_t numChannels = GetVariantAnimatableArity(outValue);
        if (numChannels > 0) {
            TGraphSlidePair theGraphSlidePair = m_SlideGraphCore->GetAssociatedGraph(inInstance);
            float theSeconds = m_SlideCore->GetSlideTime(
                m_SlideGraphCore->GetGraphActiveSlide(theGraphSlidePair.first));

            do_times(numChannels, std::bind(&CStudioAnimationSystem::OverrideChannelIfAnimated,
                                            this, inSlide, inInstance, inProperty,
                                            std::placeholders::_1, theSeconds,
                                            std::ref(animated), std::ref(outValue)));
        }

        if (animated)
            retval = true;
    }

    return retval;
}

bool KeyframeNear(const TKeyframe &inKeyframe, float inSeconds)
{
    return fabs(KeyframeTime(inKeyframe) - inSeconds) < .01f;
}

Qt3DSDMKeyframeHandle CStudioAnimationSystem::CreateKeyframeExplicit(
                                Qt3DSDMAnimationHandle inAnimation, float inValue, float inSeconds,
                                TKeyframe keyframeData)
{
    TKeyframeHandleList theKeyframes;
    m_AnimationCore->GetKeyframes(inAnimation, theKeyframes);
    function<TKeyframe(Qt3DSDMKeyframeHandle)> theConverter(
        std::bind(&IAnimationCore::GetKeyframeData, m_AnimationCore, std::placeholders::_1));

   TKeyframeHandleList::iterator theFind =
           std::find_if(theKeyframes.begin(), theKeyframes.end(),
                        [theConverter, inSeconds](const Qt3DSDMKeyframeHandle &handle)
   { return KeyframeNear(theConverter(handle), inSeconds); });

    if (keyframeData.getType()) { // not empty (paste keyframe case)
        // offset control points time for bezier keyframes
        offsetBezier(keyframeData, inSeconds - GetKeyframeSeconds(keyframeData));

        keyframeData = SetKeyframeSeconds(keyframeData, inSeconds);
    } else { // empty (insert new keyframe/change value case
        EAnimationType animType = m_AnimationCore->GetAnimationInfo(inAnimation).m_AnimationType;
        switch (animType) {
        case EAnimationTypeLinear:
            keyframeData = CreateLinearKeyframe(inSeconds, inValue);
            break;

        case EAnimationTypeEaseInOut: {
            float easeIn = m_SmoothInterpolation ? 100 : 0;
            float easeOut = m_SmoothInterpolation ? 100 : 0;
            keyframeData = CreateEaseInEaseOutKeyframe(inSeconds, inValue, easeIn, easeOut);
        } break;

        case EAnimationTypeBezier: {
            float timeIn, valueIn, timeOut, valueOut;
            // if keyframe exists, offset control points values
            if (theFind != theKeyframes.end()) {
                TKeyframe kfData = m_AnimationCore->GetKeyframeData(*theFind);
                getBezierValues(kfData, timeIn, valueIn, timeOut, valueOut);
                float dValue = inValue - KeyframeValueValue(kfData);
                valueIn += dValue;
                valueOut += dValue;
            } else {
                valueIn = inValue;
                valueOut = inValue;
                timeIn = inSeconds - .5f;
                timeOut = inSeconds + .5f;
            }
            keyframeData = CreateBezierKeyframe(inSeconds, inValue, timeIn, valueIn,
                                                                    timeOut, valueOut);
        } break;

        default:
            Q_ASSERT(false);
            break;
        }
    }
    Qt3DSDMKeyframeHandle keyframeHandle;
    if (theFind != theKeyframes.end()) {
        keyframeHandle = *theFind;
        m_AnimationCore->SetKeyframeData(keyframeHandle, keyframeData);
    } else {
        keyframeHandle = m_AnimationCore->InsertKeyframe(inAnimation, keyframeData);
    }

    return keyframeHandle;
}

Qt3DSDMKeyframeHandle CStudioAnimationSystem::CreateKeyframe(Qt3DSDMAnimationHandle inAnimation,
                                    const SValue &inValue, float inSeconds,
                                    TAnimationCorePtr inAnimationCore)
{
    SAnimationInfo theInfo(inAnimationCore->GetAnimationInfo(inAnimation));

    return CreateKeyframeExplicit(inAnimation, GetAnimationValue(theInfo.m_Index, inValue),
                                  inSeconds);
}

void MaybeAddAnimation(Qt3DSDMSlideHandle inSlide, Qt3DSDMInstanceHandle inInstance,
                       Qt3DSDMPropertyHandle inProperty, Qt3DSDMAnimationHandle inAnimation,
                       TAnimationCorePtr inAnimationCore, TAnimationHandleList &outAnimations)
{
    SAnimationInfo theInfo(inAnimationCore->GetAnimationInfo(inAnimation));
    if (IsAnimationInfoEqual(theInfo, inSlide, inInstance, inProperty))
        outAnimations.push_back(inAnimation);
}

bool SortAnimationHandlesByIndex(Qt3DSDMAnimationHandle lhs, Qt3DSDMAnimationHandle rhs,
                                 TAnimationCorePtr inCore)
{
    return inCore->GetAnimationInfo(lhs).m_Index < inCore->GetAnimationInfo(rhs).m_Index;
}

void GetPresentAnimations(Qt3DSDMSlideHandle inSlide, Qt3DSDMInstanceHandle inInstance,
                          Qt3DSDMPropertyHandle inProperty,
                          const TAnimationFloatPairList &inAnimationPairs,
                          TAnimationCorePtr inAnimationCore, TAnimationHandleList &outAnimations)
{
    for (auto animation : inAnimationPairs) {
        MaybeAddAnimation(inSlide, inInstance, inProperty, animation.first, inAnimationCore,
                          std::ref(outAnimations));
    }

    if (outAnimations.empty()) {
        function<void(Qt3DSDMAnimationHandle)> theOperation(
            std::bind(MaybeAddAnimation, inSlide, inInstance, inProperty,
                      std::placeholders::_1, inAnimationCore,
                      std::ref(outAnimations)));

        TAnimationHandleList theAnimationHandles;
        inAnimationCore->GetAnimations(theAnimationHandles);
        do_all(theAnimationHandles, theOperation);
    }
    std::sort(outAnimations.begin(), outAnimations.end(),
              std::bind(SortAnimationHandlesByIndex,
                        std::placeholders::_1, std::placeholders::_2, inAnimationCore));
}

void CreateAnimationAndAdd(Qt3DSDMSlideHandle inSlide, Qt3DSDMInstanceHandle inInstance,
                           Qt3DSDMPropertyHandle inProperty, qt3dsdm::EAnimationType animType,
                           size_t inIndex, TAnimationCorePtr inAnimationCore,
                           TAnimationHandleList &outAnimations)
{
    outAnimations.push_back(inAnimationCore->CreateAnimation(
        inSlide, inInstance, inProperty, inIndex, animType, false));
}

bool AnimationValueDiffers(Qt3DSDMAnimationHandle inAnimation, const SValue &inValue,
                           float inSeconds, TAnimationCorePtr inAnimationCore)
{
    SAnimationInfo theInfo(inAnimationCore->GetAnimationInfo(inAnimation));
    if (inAnimationCore->GetKeyframeCount(inAnimation) == 0)
        return true; // currently there is no keyframe, so return true
    float theValue = GetAnimationValue(theInfo.m_Index, inValue);
    float theAnimatedValue = inAnimationCore->EvaluateAnimation(inAnimation, inSeconds);
    return fabs(theValue - theAnimatedValue) > .001;
}

bool AnimationExistsInPresentAnimations(const TAnimationFloatPair &inAnimation,
                                        TAnimationHandleList &inPresentAnimations)
{
    return exists(inPresentAnimations,
                  std::bind(equal_to<Qt3DSDMAnimationHandle>(), inAnimation.first,
                            std::placeholders::_1));
}

void CStudioAnimationSystem::DoKeyframeProperty(Qt3DSDMSlideHandle inSlide,
                        Qt3DSDMInstanceHandle inInstance, Qt3DSDMPropertyHandle inProperty,
                        const SValue &inValue, bool inDoDiffValue)
{
    TGraphSlidePair theGraphSlidePair = m_SlideGraphCore->GetAssociatedGraph(inInstance);
    float inTimeInSecs = m_SlideCore->GetSlideTime(m_SlideGraphCore
                                                   ->GetGraphActiveSlide(theGraphSlidePair.first));

    size_t arity = GetVariantAnimatableArity(inValue);
    TAnimationHandleList thePresentAnimations;
    GetPresentAnimations(inSlide, inInstance, inProperty, std::cref(m_AnimationFloatPairs),
                         m_AnimationCore, thePresentAnimations);
    if (thePresentAnimations.empty()) {
        // default newly created keyframes to EaseInOut
        do_times(arity, std::bind(CreateAnimationAndAdd, inSlide, inInstance, inProperty,
                                          EAnimationTypeEaseInOut, std::placeholders::_1,
                                          m_AnimationCore, std::ref(thePresentAnimations)));
    }
    if (!inDoDiffValue
        || find_if(thePresentAnimations.begin(), thePresentAnimations.end(),
                   std::bind(AnimationValueDiffers,
                             std::placeholders::_1, inValue, inTimeInSecs, m_AnimationCore))
            != thePresentAnimations.end()) {
        do_all(thePresentAnimations,
               std::bind(&CStudioAnimationSystem::CreateKeyframe, this, std::placeholders::_1,
                         std::cref(inValue), inTimeInSecs, m_AnimationCore));
        erase_if(m_AnimationFloatPairs, std::bind(AnimationExistsInPresentAnimations,
                                                  std::placeholders::_1,
                                                  std::ref(thePresentAnimations)));
    }
}

TAnimationFloatPair CreateAnimationFloatPair(Qt3DSDMAnimationHandle inAnimation,
                                             const SValue &inValue,
                                             TAnimationCorePtr inAnimationCore)
{
    SAnimationInfo theInfo(inAnimationCore->GetAnimationInfo(inAnimation));
    float theValue = GetAnimationValue(theInfo.m_Index, inValue);
    return make_pair(inAnimation, theValue);
}

void InsertUniqueAnimationFloatPair(Qt3DSDMAnimationHandle inAnimation,
                                    TAnimationFloatPairList &inList, const SValue &inValue,
                                    TAnimationCorePtr inAnimationCore)
{
    TAnimationFloatPair thePair = CreateAnimationFloatPair(inAnimation, inValue, inAnimationCore);
    insert_or_update(thePair, inList,
                     std::bind(AnimationFloatPairContainsAnimation, inAnimation, std::placeholders::_1));
}

bool CStudioAnimationSystem::SetAnimatedInstancePropertyValue(Qt3DSDMSlideHandle inSlide,
                                                              Qt3DSDMInstanceHandle inInstance,
                                                              Qt3DSDMPropertyHandle inProperty,
                                                              const SValue &inValue)
{
    size_t arity = GetVariantAnimatableArity(inValue);
    if (arity > 0) {
        // prerequisite for autoset-keyframes is that the property is already animated.
        if (m_AutoKeyframe && IsPropertyAnimated(inInstance, inProperty)) {
            DoKeyframeProperty(inSlide, inInstance, inProperty, inValue, true);
            return true;
        }

        TAnimationHandleList thePresentAnimations;
        GetPresentAnimations(inSlide, inInstance, inProperty, std::cref(m_AnimationFloatPairs),
                             m_AnimationCore, thePresentAnimations);
        if (!thePresentAnimations.empty()) {
            TAnimationFloatPairList thePreList(m_AnimationFloatPairs);
            do_all(thePresentAnimations, std::bind(InsertUniqueAnimationFloatPair, std::placeholders::_1,
                                                     std::ref(m_AnimationFloatPairs),
                                                     std::cref(inValue), m_AnimationCore));

            if (m_Consumer && m_refreshCallback) {
                // Only create a single refresh per transaction stack
                if (((CTransactionConsumer *)m_Consumer.get())->m_TransactionList.size() == 0) {
                    CreateGenericTransactionWithConsumer(
                        __FILE__, __LINE__, m_Consumer,
                        std::bind(m_refreshCallback, inInstance),
                        std::bind(m_refreshCallback, inInstance));
                }
            }

            CreateGenericTransactionWithConsumer(
                __FILE__, __LINE__, m_Consumer,
                std::bind(assign_to<TAnimationFloatPairList>, m_AnimationFloatPairs,
                            std::ref(m_AnimationFloatPairs)),
                std::bind(assign_to<TAnimationFloatPairList>, thePreList,
                            std::ref(m_AnimationFloatPairs)));
            return true;
        }
    }
    return false;
}

void CStudioAnimationSystem::SetAutoKeyframe(bool inAutoKeyframe)
{
    m_AutoKeyframe = inAutoKeyframe;
}
bool CStudioAnimationSystem::GetAutoKeyframe() const
{
    return m_AutoKeyframe;
}

Qt3DSDMSlideHandle CStudioAnimationSystem::GetApplicableGraphAndSlide(
    Qt3DSDMInstanceHandle inInstance, Qt3DSDMPropertyHandle inProperty, const SValue &inValue)
{
    Qt3DSDMSlideHandle theApplicableSlide;
    size_t arity = GetVariantAnimatableArity(inValue);
    if (arity > 0)
        theApplicableSlide = m_SlideSystem->GetApplicableSlide(inInstance, inProperty);
    return theApplicableSlide;
}

void InsertUniqueAnimationKeyframesPair(TAnimationKeyframesPairList &ioList, SAnimationInfo &inInfo,
                                        TKeyframeList &inKeyframes)
{
    bool theFound = false;
    for (TAnimationKeyframesPairList::iterator theAnimationKeyframeIter = ioList.begin();
         theAnimationKeyframeIter < ioList.end(); ++theAnimationKeyframeIter) {
        if (IsAnimationInfoEqual(theAnimationKeyframeIter->first, inInfo.m_Slide, inInfo.m_Instance,
                                 inInfo.m_Property, inInfo.m_Index)) {
            theFound = true;
            // Overwrite the existing value
            SAnimationInfo &theInfo = theAnimationKeyframeIter->first;
            theInfo.m_AnimationType = inInfo.m_AnimationType;
            theInfo.m_DynamicFirstKeyframe = inInfo.m_DynamicFirstKeyframe;
            theAnimationKeyframeIter->second = inKeyframes;
            break;
        }
    }
    if (!theFound) {
        ioList.push_back(std::make_pair(inInfo, inKeyframes));
    }
}

void CStudioAnimationSystem::Animate(Qt3DSDMInstanceHandle inInstance,
                                     Qt3DSDMPropertyHandle inProperty)
{
    SValue theValue;
    m_PropertySystem->GetInstancePropertyValue(inInstance, inProperty, theValue);
    Qt3DSDMSlideHandle theApplicableSlide =
        GetApplicableGraphAndSlide(inInstance, inProperty, theValue.toOldSkool());
    if (theApplicableSlide.Valid()) {
        // Check if previously we have set animation & keyframes
        DataModelDataType::Value theDataType = m_PropertySystem->GetDataType(inProperty);
        size_t theArity = GetDatatypeAnimatableArity(theDataType);
        bool theFound = false;
        for (size_t i = 0; i < theArity; ++i) {
            TAnimationKeyframesPairList::iterator theAnimationKeyframeIter =
                m_DeletedAnimationData.begin();
            for (; theAnimationKeyframeIter < m_DeletedAnimationData.end();
                 ++theAnimationKeyframeIter) {
                if (IsAnimationInfoEqual(theAnimationKeyframeIter->first, theApplicableSlide,
                                         inInstance, inProperty, i)) {
                    theFound = true;

                    // We use previously set animation & keyframes to create new animation &
                    // keyframe
                    SAnimationInfo &theInfo = theAnimationKeyframeIter->first;
                    Qt3DSDMAnimationHandle theAnimation = m_AnimationCore->CreateAnimation(
                        theInfo.m_Slide, theInfo.m_Instance, theInfo.m_Property, theInfo.m_Index,
                        theInfo.m_AnimationType, theInfo.m_DynamicFirstKeyframe);

                    TKeyframeList theKeyframes = theAnimationKeyframeIter->second;
                    for (TKeyframeList::const_iterator theKeyframeIter = theKeyframes.begin();
                         theKeyframeIter < theKeyframes.end(); ++theKeyframeIter)
                        m_AnimationCore->InsertKeyframe(theAnimation, *theKeyframeIter);

                    break;
                }
            }
        }

        if (!theFound)
            DoKeyframeProperty(theApplicableSlide, inInstance, inProperty, theValue.toOldSkool(),
                               true);
    }
}

void CStudioAnimationSystem::Deanimate(Qt3DSDMInstanceHandle inInstance,
                                       Qt3DSDMPropertyHandle inProperty)
{
    DataModelDataType::Value theDataType = m_PropertySystem->GetDataType(inProperty);
    size_t theArity = GetDatatypeAnimatableArity(theDataType);
    for (size_t i = 0; i < theArity; ++i) {
        Qt3DSDMAnimationHandle theAnimationHandle =
            GetControllingAnimation(inInstance, inProperty, i);

        if (theAnimationHandle.Valid()) {
            // Get animation & keyframe data and save it
            SAnimationInfo theInfo(m_AnimationCore->GetAnimationInfo(theAnimationHandle));
            TKeyframeHandleList theKeyframeHandles;
            m_AnimationCore->GetKeyframes(theAnimationHandle, theKeyframeHandles);
            TKeyframeList theKeyframeList;
            for (TKeyframeHandleList::const_iterator theKeyframeIter = theKeyframeHandles.begin();
                 theKeyframeIter < theKeyframeHandles.end(); ++theKeyframeIter)
                theKeyframeList.push_back(m_AnimationCore->GetKeyframeData(*theKeyframeIter));
            InsertUniqueAnimationKeyframesPair(m_DeletedAnimationData, theInfo, theKeyframeList);

            // Delete the animation
            m_AnimationCore->DeleteAnimation(theAnimationHandle);
        }
    }
}

void CStudioAnimationSystem::KeyframeProperty(Qt3DSDMInstanceHandle inInstance,
                                              Qt3DSDMPropertyHandle inProperty, bool inDoDiffValue)
{
    SValue theValue;
    m_PropertySystem->GetInstancePropertyValue(inInstance, inProperty, theValue);
    Qt3DSDMSlideHandle theApplicableSlide =
        GetApplicableGraphAndSlide(inInstance, inProperty, theValue.toOldSkool());
    if (theApplicableSlide.Valid())
        DoKeyframeProperty(theApplicableSlide, inInstance, inProperty, theValue.toOldSkool(),
                           inDoDiffValue);
}
void CStudioAnimationSystem::SetOrCreateKeyframe(Qt3DSDMInstanceHandle inInstance,
                                                 Qt3DSDMPropertyHandle inProperty,
                                                 float inTimeInSeconds,
                                                 SGetOrSetKeyframeInfo *inKeyframeInfo,
                                                 size_t inNumInfos)
{
    qt3dsdm::DataModelDataType::Value thePropertyType = m_PropertySystem->GetDataType(inProperty);
    Qt3DSDMSlideHandle theApplicableSlide;
    size_t arity = GetDatatypeAnimatableArity(thePropertyType);
    if (arity) {
        theApplicableSlide = m_SlideSystem->GetApplicableSlide(inInstance, inProperty);
        if (theApplicableSlide.Valid()) {
            TAnimationHandleList thePresentAnimations;
            GetPresentAnimations(theApplicableSlide, inInstance, inProperty,
                                 std::cref(m_AnimationFloatPairs), m_AnimationCore,
                                 thePresentAnimations);
            size_t numIterations = std::min(inNumInfos, arity);
            if (thePresentAnimations.empty()) {
                qt3dsdm::EAnimationType animType = inKeyframeInfo[0].m_keyframeData.getType();
                for (size_t idx = 0, end = numIterations; idx < end; ++idx) {
                    CreateAnimationAndAdd(theApplicableSlide, inInstance, inProperty, animType, idx,
                                          m_AnimationCore, thePresentAnimations);
                }
            }
            for (size_t idx = 0, end = numIterations; idx < end; ++idx) {
                CreateKeyframeExplicit(thePresentAnimations[idx],
                                       KeyframeValueValue(inKeyframeInfo[idx].m_keyframeData),
                                       inTimeInSeconds, inKeyframeInfo[idx].m_keyframeData);
                if (inKeyframeInfo[idx].m_AnimationTrackIsDynamic)
                    m_AnimationCore->SetFirstKeyframeDynamic(thePresentAnimations[idx], true);
            }
            erase_if(m_AnimationFloatPairs, std::bind(AnimationExistsInPresentAnimations,
                                                      std::placeholders::_1,
                                                      std::ref(thePresentAnimations)));
        }
    }
}

inline bool FindMatchingAnimation(Qt3DSDMAnimationHandle inAnimation,
                                  TAnimationCorePtr inAnimationCore, size_t inIndex)
{
    return inAnimationCore->GetAnimationInfo(inAnimation).m_Index == inIndex;
}

Qt3DSDMAnimationHandle CStudioAnimationSystem::GetControllingAnimation(
    Qt3DSDMInstanceHandle inInstance, Qt3DSDMPropertyHandle inProperty, size_t inIndex) const
{
    Qt3DSDMSlideHandle theApplicableSlide =
        m_SlideSystem->GetApplicableSlide(inInstance, inProperty);
    // first check our anim float pairs
    for (size_t idx = 0, end = m_AnimationFloatPairs.size(); idx < end; ++idx) {
        const SAnimationInfo &theInfo =
            m_AnimationCore->GetAnimationInfo(m_AnimationFloatPairs[idx].first);
        if (IsAnimationInfoEqual(theInfo, theApplicableSlide, inInstance, inProperty, inIndex))
            return m_AnimationFloatPairs[idx].first;
    }
    // Use the cache lookup instead of linear search algorithm
    return m_AnimationCore->GetAnimation(theApplicableSlide, inInstance, inProperty, inIndex);
}

bool CStudioAnimationSystem::IsPropertyAnimatable(Qt3DSDMInstanceHandle inInstance,
                                                  Qt3DSDMPropertyHandle inProperty) const
{
    DataModelDataType::Value dataType = m_PropertySystem->GetDataType(inProperty);
    size_t arity = GetDatatypeAnimatableArity(dataType);
    if (arity)
        return m_SlideGraphCore->GetAssociatedGraph(inInstance).first.Valid();
    return false;
}

bool CStudioAnimationSystem::IsPropertyAnimated(Qt3DSDMInstanceHandle inInstance,
                                                Qt3DSDMPropertyHandle inProperty) const
{
    DataModelDataType::Value dataType = m_PropertySystem->GetDataType(inProperty);
    size_t arity = GetDatatypeAnimatableArity(dataType);
    for (size_t i = 0; i < arity; ++i) {
        if (GetControllingAnimation(inInstance, inProperty, i).Valid())
            return true;
    }
    return false;
}

void CStudioAnimationSystem::SetConsumer(TTransactionConsumerPtr inConsumer)
{
    m_Consumer = inConsumer;
}

void CStudioAnimationSystem::setRefreshCallback(TRefreshCallbackFunc func)
{
    m_refreshCallback = func;
}
}