friz
An animation control system for JUCE
Loading...
Searching...
No Matches
chain.h
1/*
2 Copyright (c) 2019-2023 Brett g Porter
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21*/
22#pragma once
23
24#include "animation.h"
25
26namespace friz
27{
28
40class Chain : public AnimationType
41{
42public:
43 Chain (int id = 0)
44 : AnimationType (id)
45 {
46 }
47
48 bool isFinished () override { return (currentEffect >= sequence.size ()); }
49
50 bool isReady () const override
51 {
52 for (const auto& effect : sequence)
53 {
54 if ((nullptr == effect) || (!effect->isReady ()))
55 return false;
56 }
57 return true;
58 }
59
60 AnimationType::Status gotoTime (juce::int64 timeInMs) override
61 {
62 auto effect = getEffect (currentEffect);
63 if (effect)
64 {
65 if (AnimationType::Status::finished == effect->gotoTime (timeInMs))
67
70 }
72 }
73
74 void cancel (bool moveToEndPosition) override
75 {
76 currentEffect = static_cast<int> (sequence.size () - 1);
77 if (moveToEndPosition)
78 {
79 auto lastEffectPtr = getEffect (currentEffect);
80 if (lastEffectPtr)
81 lastEffectPtr->cancel (moveToEndPosition);
82 }
83 }
84
85 void addAnimation (std::unique_ptr<AnimationType> effect)
86 {
87 sequence.push_back (std::move (effect));
88 }
89
90 AnimatedValue* getValue (size_t index) override
91 {
92 if (auto effect = getEffect (currentEffect); effect != nullptr)
93 return effect->getValue (index);
94 return nullptr;
95 }
96
97private:
104 {
105 if (juce::isPositiveAndBelow (index, sequence.size ()))
106 return sequence[index].get ();
107
108 jassertfalse;
109 return nullptr;
110 }
111
112protected:
114 std::vector<std::unique_ptr<AnimationType>> sequence;
115
118 int currentEffect { 0 };
119};
120
121} // namespace friz
Abstract base class for objects that can generate a useful series of values to drive UI animations.
Definition: animatedValue.h:34
Abstract base class; all the real action happens in the derived templated Animation class,...
Definition: animation.h:39
Status
Definition: animation.h:42
@ processing
The animation is running right now.
@ finished
Finished running, okay to clean up.
A container animation object that holds several animations and executes them in order.
Definition: chain.h:41
bool isFinished() override
Definition: chain.h:48
AnimationType::Status gotoTime(juce::int64 timeInMs) override
Advance all active animations to this point in time.
Definition: chain.h:60
AnimationType * getEffect(int index)
Definition: chain.h:103
AnimatedValue * getValue(size_t index) override
Retrieve a pointer to one of this animation's value objects.
Definition: chain.h:90
bool isReady() const override
Definition: chain.h:50
void cancel(bool moveToEndPosition) override
Cancel an in-progress animation, optionally moving directly to its end value.
Definition: chain.h:74
std::vector< std::unique_ptr< AnimationType > > sequence
the vector that owns our chain of effects.
Definition: chain.h:114
int currentEffect
index (into the sequence vector) of the effect that we are currently processing.
Definition: chain.h:118