iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
ISkLottieControl.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5
6 See LICENSE.txt for more info.
7
8 ==============================================================================
9*/
10
11#pragma once
12
13#ifndef IGRAPHICS_SKIA
14#error This IControl only works with the Skia graphics backend
15#endif
16
22#ifndef IGRAPHICS_SKIA
23#error ISkLottieControl requires the IGRAPHICS_SKIA backend
24#endif
25
26#include "IControl.h"
27#include "SkMatrix.h"
28#include "modules/skottie/include/Skottie.h"
29
30BEGIN_IPLUG_NAMESPACE
31BEGIN_IGRAPHICS_NAMESPACE
32
37{
38public:
39 ISkLottieControl(const IRECT& bounds)
40 : IControl(bounds)
41 {
42 }
43
44 void Draw(IGraphics& g) override
45 {
46 SkCanvas* pCanvas = static_cast<SkCanvas*>(g.GetDrawContext());
47
48 if(mAnimation && GetAnimationFunction())
49 DoDraw(SkSize::Make(mRECT.W(), mRECT.H()), pCanvas);
50 }
51
52 void DoDraw(SkSize size, SkCanvas* canvas)
53 {
54 if (size.width() != mSize.width() || size.height() != mSize.height())
55 {
56 // Cache the current matrix; change only if size changes.
57 if (mAnimationSize.width() > 0 && mAnimationSize.height() > 0)
58 {
59 float scale = std::min(size.width() / mAnimationSize.width(),
60 size.height() / mAnimationSize.height());
61 mMatrix.setScaleTranslate( scale, scale,
62 (size.width() - mAnimationSize.width() * scale) * 0.5f,
63 (size.height() - mAnimationSize.height() * scale) * 0.5f);
64 }
65 else
66 {
67 mMatrix = SkMatrix();
68 }
69 mSize = size;
70 }
71 canvas->concat(mMatrix);
72 mAnimation->render(canvas);
73 }
74
75 void LoadFile(const char* path)
76 {
77 skottie::Animation::Builder builder;
78 mAnimation = builder.makeFromFile(path);
79 mSize = {0, 0};
80
81 if(mAnimation) {
82 mAnimationSize = mAnimation->size();
83
84 SetActionFunction([&](IControl* pCaller) {
85 if(mAnimation) {
86 SetAnimation([&](IControl* pCaller) {
87 auto progress = pCaller->GetAnimationProgress();
88 mAnimation->seekFrameTime((pCaller->GetAnimationDuration().count() / 1000.) * progress, nullptr);
89
90 if(progress > 1.f)
91 pCaller->OnEndAnimation();
92 }, mAnimation->duration() * 1000.);
93 }
94 });
95 }
96 else {
97 mAnimationSize = {0, 0};
98 }
99 }
100
101 void LoadData(const void* data, size_t length)
102 {
103 skottie::Animation::Builder builder;
104 mAnimation = builder.make((const char*) data, (size_t)length);
105 mSize = {0, 0};
106 mAnimationSize = mAnimation ? mAnimation->size() : SkSize{0, 0};
107
108 SetActionFunction([&](IControl* pCaller) {
109 SetAnimation([&](IControl* pCaller) {
110 auto progress = pCaller->GetAnimationProgress();
111 mAnimation->seekFrameTime((pCaller->GetAnimationDuration().count() / 1000.) * progress, nullptr);
112
113 if(progress > 1.f)
114 pCaller->OnEndAnimation();
115 }, mAnimation->duration() * 1000.);
116 });
117 }
118
119 void OnMouseDown(float x, float y, const IMouseMod& mod) override
120 {
121 if (mod.S)
122 {
123 WDL_String fileName, path {"~/Desktop"};
124
125 GetUI()->PromptForFile(fileName, path, EFileAction::Open, "svg",
126 [this](const WDL_String& fileName, const WDL_String& path) {
127 if (fileName.GetLength())
128 LoadFile(fileName.Get());
129 SetDirty(false);
130 });
131 }
132 else
133 SetDirty(true);
134 }
135
136private:
137 sk_sp<skottie::Animation> mAnimation;
138 SkSize mSize;
139 SkSize mAnimationSize;
140 SkMatrix mMatrix;
141};
142
143END_IGRAPHICS_NAMESPACE
144END_IPLUG_NAMESPACE
This file contains the base IControl implementation, along with some base classes for specific types ...
The lowest level base class of an IGraphics control.
Definition: IControl.h:49
Milliseconds GetAnimationDuration() const
Get the duration of animations applied to the control.
Definition: IControl.h:509
IGraphics * GetUI()
Definition: IControl.h:467
double GetAnimationProgress() const
Get the progress in a control's animation, in the range 0-1.
Definition: IControl.cpp:431
IAnimationFunction GetAnimationFunction()
Get the control's animation function, if it exists.
Definition: IControl.h:500
virtual void SetDirty(bool triggerAction=true, int valIdx=kNoValIdx)
Mark the control as dirty, i.e.
Definition: IControl.cpp:198
IControl * SetActionFunction(IActionFunction actionFunc)
Set an Action Function for this control.
Definition: IControl.h:206
void SetAnimation(IAnimationFunction func)
Set the animation function.
Definition: IControl.h:492
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
virtual void * GetDrawContext()=0
Gets a void pointer to underlying drawing context, for the IGraphics backend See draw class implement...
virtual void PromptForFile(WDL_String &fileName, WDL_String &path, EFileAction action=EFileAction::Open, const char *ext="", IFileDialogCompletionHandlerFunc completionHandler=nullptr)=0
Create a platform file prompt dialog to choose a path for opening/saving a single file.
A control that hosts a Lottie animation, via Skia's Skottie module https://skia.org/user/modules/skot...
void OnMouseDown(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a mouse down event on this control.
void Draw(IGraphics &g) override
Draw the control to the graphics context.
Used to manage mouse modifiers i.e.
Used to manage a rectangular area, independent of draw class/platform.
float W() const
float H() const