iPlug2 - C++ Audio Plug-in Framework
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 
30 BEGIN_IPLUG_NAMESPACE
31 BEGIN_IGRAPHICS_NAMESPACE
32 
36 class ISkLottieControl : public IControl
37 {
38 public:
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  WDL_String file;
123  WDL_String path {"~/Desktop"};
124  GetUI()->PromptForFile(file, path);
125  LoadFile(file.Get());
126  }
127  else
128  SetDirty(true);
129  }
130 
131 private:
132  sk_sp<skottie::Animation> mAnimation;
133  SkSize mSize;
134  SkSize mAnimationSize;
135  SkMatrix mMatrix;
136 };
137 
138 END_IGRAPHICS_NAMESPACE
139 END_IPLUG_NAMESPACE
The lowest level base class of an IGraphics control.
Definition: IControl.h:42
Used to manage a rectangular area, independent of draw class/platform.
Used to manage mouse modifiers i.e.
void OnMouseDown(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a mouse down event on this control.
IControl * SetActionFunction(IActionFunction actionFunc)
Set an Action Function for this control.
Definition: IControl.h:201
This file contains the base IControl implementation, along with some base classes for specific types ...
void Draw(IGraphics &g) override
Draw the control to the graphics context.
float H() const
double GetAnimationProgress() const
Get the progress in a control&#39;s animation, in the range 0-1.
Definition: IControl.cpp:429
float W() const
virtual void PromptForFile(WDL_String &fileName, WDL_String &path, EFileAction action=EFileAction::Open, const char *extensions=0)=0
Create a platform file prompt dialog to choose a file/directory path for opening/saving a file/direct...
IControl(const IRECT &bounds, int paramIdx=kNoParameter, IActionFunction actionFunc=nullptr)
Constructor.
Definition: IControl.cpp:81
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
void SetAnimation(IAnimationFunction func)
Set the animation function.
Definition: IControl.h:477
IAnimationFunction GetAnimationFunction()
Get the control&#39;s animation function, if it exists.
Definition: IControl.h:485
virtual void * GetDrawContext()=0
Gets a void pointer to underlying drawing context, for the IGraphics backend See draw class implement...
IGraphics * GetUI()
Definition: IControl.h:452
Milliseconds GetAnimationDuration() const
Get the duration of animations applied to the control.
Definition: IControl.h:494
A control that hosts a Lottie animation, via Skia&#39;s Skottie module https://skia.org/user/modules/skot...
virtual void SetDirty(bool triggerAction=true, int valIdx=kNoValIdx)
Mark the control as dirty, i.e.
Definition: IControl.cpp:196