iPlug2 - C++ Audio Plug-in Framework
IFPSDisplayControl.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 
21 BEGIN_IPLUG_NAMESPACE
22 BEGIN_IGRAPHICS_NAMESPACE
23 
28  , public IVectorBase
29 {
30 private:
31  static constexpr int MAXBUF = 100;
32 public:
33  enum EStyle
34  {
35  kFPS,
36  kMS,
37  kPercentage,
38  kNumStyles
39  };
40 
41  IFPSDisplayControl(const IRECT& bounds, EStyle style = EStyle::kFPS, const char* label = "Frame Time")
42  : IControl(bounds)
43  , IVectorBase(DEFAULT_STYLE)
44  , mStyle(style)
45  , mNameLabel(label)
46  {
47  AttachIControl(this, label);
48 
49  SetColor(kBG, COLOR_WHITE);
50 
51  mNameLabelText = IText(14, GetColor(kFR), DEFAULT_FONT, EAlign::Near, EVAlign::Bottom);
52  }
53 
54  void OnMouseDown(float x, float y, const IMouseMod& mod) override
55  {
56  mStyle++;
57 
58  if(mStyle == kNumStyles)
59  mStyle = kFPS;
60  }
61 
62  bool IsDirty() override
63  {
64  return true;
65  }
66 
67  void Update(float frameTime)
68  {
69  mReadPos = (mReadPos+1) % MAXBUF;
70  mBuffer[mReadPos] = frameTime;
71  }
72 
73  void Draw(IGraphics& g) override
74  {
75  float avg = 0.f;
76  for (int i = 0; i < MAXBUF; i++)
77  avg += mBuffer[i];
78 
79  avg = avg / (float)MAXBUF;
80 
81  g.FillRect(GetColor(kBG), mRECT);
82  g.DrawRect(COLOR_BLACK, mRECT);
83 
84  IRECT padded = mRECT.GetPadded(-2);
85 
86  float x = padded.L;
87  float y = padded.T;
88  float w = padded.W();
89  float h = padded.H();
90 
91  // TODO: replace with IGraphics::DrawData, make it work with lice
92 
93  g.PathMoveTo(x, y+h);
94 
95  if (mStyle == kFPS)
96  {
97  for (int i = 0; i < MAXBUF; i++) {
98  float v = 1.0f / (0.00001f + mBuffer[(mReadPos+i) % MAXBUF]);
99  float vx, vy;
100  if (v > 80.0f) v = 80.0f;
101  vx = x + ((float)i/(MAXBUF-1)) * w;
102  vy = y + h - ((v / 80.0f) * h);
103  g.PathLineTo(vx, vy);
104  }
105  }
106  else if (mStyle == kPercentage)
107  {
108  for (int i = 0; i < MAXBUF; i++) {
109  float v = mBuffer[(mReadPos+i) % MAXBUF] * 1.0f;
110  float vx, vy;
111  if (v > 100.0f) v = 100.0f;
112  vx = x + ((float)i/(MAXBUF-1)) * w;
113  vy = y + h - ((v / 100.0f) * h);
114  g.PathLineTo(vx, vy);
115  }
116  }
117  else
118  {
119  for (int i = 0; i < MAXBUF; i++) {
120  float v = mBuffer[(mReadPos+i) % MAXBUF] * 1000.0f;
121  float vx, vy;
122  if (v > 20.0f) v = 20.0f;
123  vx = x + ((float)i/(MAXBUF-1)) * w;
124  vy = y + h - ((v / 20.0f) * h);
125  g.PathLineTo(vx, vy);
126  }
127  }
128 
129  g.PathLineTo(mRECT.R, mRECT.B);
130  g.PathFill(GetColor(kFG));
131 
132  g.DrawText(mAPILabelText, g.GetDrawingAPIStr(), padded);
133 
134  if (mNameLabel.GetLength())
135  g.DrawText(mNameLabelText, mNameLabel.Get(), padded);
136 
137  WDL_String str;
138 
139  if (mStyle == kFPS)
140  {
141  str.SetFormatted(32, "%.2f FPS", 1.0f / avg);
142  g.DrawText(mTopLabelText, str.Get(), padded);
143 
144  str.SetFormatted(32, "%.2f ms", avg * 1000.0f);
145  g.DrawText(mBottomLabelText, str.Get(), padded);
146  }
147  else if (mStyle == kPercentage)
148  {
149  str.SetFormatted(32, "%.1f %%", avg * 1.0f);
150  g.DrawText(mTopLabelText, str.Get(), padded);
151  }
152  else
153  {
154  str.SetFormatted(32, "%.2f ms", avg * 1000.0f);
155  g.DrawText(mTopLabelText, str.Get(), padded);
156  }
157  }
158 private:
159  int mStyle;
160  WDL_String mNameLabel;
161  float mBuffer[MAXBUF] = {};
162  int mReadPos = 0;
163 
164  float mPadding = 1.f;
165  IText& mNameLabelText = mText;
166  IText mAPILabelText = IText(14, GetColor(kFR), DEFAULT_FONT, EAlign::Near, EVAlign::Top);
167  IText mTopLabelText = IText(18, GetColor(kFR), DEFAULT_FONT, EAlign::Far, EVAlign::Top);
168  IText mBottomLabelText = IText(15, GetColor(kFR), DEFAULT_FONT, EAlign::Far, EVAlign::Bottom);
169 };
170 
171 END_IGRAPHICS_NAMESPACE
172 END_IPLUG_NAMESPACE
virtual void PathMoveTo(float x, float y)=0
Move the current point in the current path.
The lowest level base class of an IGraphics control.
Definition: IControl.h:42
Used to manage a rectangular area, independent of draw class/platform.
void SetColor(EVColor colorIdx, const IColor &color)
Set one of the IVColors that style the IVControl.
Definition: IControl.h:650
virtual void DrawRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0, float thickness=1.f)
Draw a rectangle to the graphics context.
Definition: IGraphics.cpp:2475
Used to manage mouse modifiers i.e.
virtual void PathLineTo(float x, float y)=0
Add a line to the current path from the current point to the specified location.
void OnMouseDown(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a mouse down event on this control.
void DrawText(const IText &text, const char *str, const IRECT &bounds, const IBlend *pBlend=0)
Draw some text to the graphics context in a specific rectangle.
Definition: IGraphics.cpp:631
void AttachIControl(IControl *pControl, const char *label)
Call in the constructor of your IVControl to link the IVectorBase and IControl.
Definition: IControl.h:641
This file contains the base IControl implementation, along with some base classes for specific types ...
float R
Right side of the rectangle (X + W)
float H() const
bool IsDirty() override
Called at each display refresh by the IGraphics draw loop, after IControl::Animate(), to determine if the control is marked as dirty.
float W() const
IText is used to manage font and text/text entry style for a piece of text on the UI...
const IColor & GetColor(EVColor color) const
Get value of a specific EVColor in the IVControl.
Definition: IControl.h:664
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.
Performance display meter, based on code from NanoVG This is a special control that lives outside the...
void Draw(IGraphics &g) override
Draw the control to the graphics context.
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
IVectorBase(const IVStyle &style, bool labelInWidget=false, bool valueInWidget=false)
IVectorBase Constructor.
Definition: IControl.h:631
virtual void FillRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0)
Fill a rectangular region of the graphics context with a color.
Definition: IGraphics.cpp:2547
float L
Left side of the rectangle (X)
IRECT GetPadded(float padding) const
Get a copy of this IRECT with each value padded by padding N.B.
A base interface to be combined with IControl for vectorial controls "IVControls", in order for them to share a common style If you need more flexibility, you&#39;re on your own!
Definition: IControl.h:624
float T
Top of the rectangle (Y)
virtual const char * GetDrawingAPIStr()=0
float B
Bottom of the rectangle (Y + H)