iPlug2 - C++ Audio Plug-in Framework
ILEDControl.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 
19 #include "IControl.h"
20 #include "Easing.h"
21 
22 BEGIN_IPLUG_NAMESPACE
23 BEGIN_IGRAPHICS_NAMESPACE
24 
27 class ILEDControl : public IControl
28 {
29 public:
30  ILEDControl(const IRECT& bounds, float hue = 0.f)
31  : IControl(bounds)
32  , mHue(hue)
33  {
34  }
35 
36  ILEDControl(const IRECT& bounds, const IColor& color)
37  : IControl(bounds)
38  {
39  float s,l,a;
40  color.GetHSLA(mHue, s, l, a);
41  }
42 
43  void Draw(IGraphics& g) override
44  {
45  const float v = static_cast<float>(GetValue() * 0.65f);
46  const IColor c = IColor::FromHSLA(mHue, 1.f, v);
47  IRECT innerPart = mRECT.GetCentredInside(mRECT.W()/2.f);
48  IRECT flare = innerPart.GetScaledAboutCentre(1.f + v);
49  g.FillEllipse(c, innerPart, nullptr);
50  g.DrawEllipse(COLOR_BLACK, innerPart, nullptr, 1.f);
51  g.PathEllipse(flare);
52  IBlend b = {EBlend::Default, v};
53  g.PathFill(IPattern::CreateRadialGradient(mRECT.MW(), mRECT.MH(), mRECT.W()/2.f, {{c, 0.f}, {COLOR_TRANSPARENT, 1.f}}), {}, &b);
54  }
55 
56  void TriggerWithDecay(int decayTimeMs)
57  {
58  SetAnimation([](IControl* pControl){
59  auto progress = pControl->GetAnimationProgress();
60 
61  if(progress > 1.f) {
62  pControl->OnEndAnimation();
63  return;
64  }
65 
66  pControl->SetValue(EaseCubicIn(1. -progress));
67 
68  }, decayTimeMs);
69  }
70 
71 private:
72  float mHue = 0.f;
73 };
74 
75 END_IGRAPHICS_NAMESPACE
76 END_IPLUG_NAMESPACE
float MW() const
float MH() const
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 composite/blend operations, independent of draw class/platform.
Used to manage color data, independent of draw class/platform.
Glowing LED control.
Definition: ILEDControl.h:27
void GetHSLA(float &h, float &s, float &l, float &a) const
Get the Hue, Saturation and Luminance of the color.
This file contains the base IControl implementation, along with some base classes for specific types ...
static IPattern CreateRadialGradient(float x1, float y1, float r, const std::initializer_list< IColorStop > &stops={})
Create a radial gradient IPattern.
double GetAnimationProgress() const
Get the progress in a control&#39;s animation, in the range 0-1.
Definition: IControl.cpp:429
void PathEllipse(const IRECT &bounds)
Add an ellipse to the current path, specifying the rectangular region.
Definition: IGraphics.cpp:2661
float W() const
IRECT GetScaledAboutCentre(float scale) const
Get a copy of this IRECT where the width and height are multiplied by scale without changing the cent...
IControl(const IRECT &bounds, int paramIdx=kNoParameter, IActionFunction actionFunc=nullptr)
Constructor.
Definition: IControl.cpp:81
virtual void PathFill(const IPattern &pattern, const IFillOptions &options=IFillOptions(), const IBlend *pBlend=0)=0
Fill the current current path.
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
virtual void FillEllipse(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0)
Fill an ellipse within a rectangular region of the graphics context.
Definition: IGraphics.cpp:2591
virtual void DrawEllipse(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0, float thickness=1.f)
Draw an ellipse within a rectangular region of the graphics context.
Definition: IGraphics.cpp:2526
double GetValue(int valIdx=0) const
Get the control&#39;s value.
Definition: IControl.cpp:151
static IColor FromHSLA(float h, float s, float l, float a=1.f)
Create an IColor from Hue Saturation and Luminance values.
IRECT GetCentredInside(const IRECT &sr) const
Get a rectangle the size of sr but with the same center point as this rectangle.
virtual void SetValue(double value, int valIdx=0)
Set one of the control&#39;s values.
Definition: IControl.cpp:145
void Draw(IGraphics &g) override
Draw the control to the graphics context.
Definition: ILEDControl.h:43