iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
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
21BEGIN_IPLUG_NAMESPACE
22BEGIN_IGRAPHICS_NAMESPACE
23
28 , public IVectorBase
29{
30private:
31 static constexpr int MAXBUF = 100;
32public:
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 }
158private:
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
171END_IGRAPHICS_NAMESPACE
172END_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
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.
void OnMouseDown(float x, float y, const IMouseMod &mod) override
Implement this method to respond to a mouse down event on this control.
bool IsDirty() override
Called at each display refresh by the IGraphics draw loop, after IControl::Animate(),...
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
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:2497
virtual void PathFill(const IPattern &pattern, const IFillOptions &options=IFillOptions(), const IBlend *pBlend=0)=0
Fill the current current path.
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:670
virtual const char * GetDrawingAPIStr()=0
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:2569
virtual void PathMoveTo(float x, float y)=0
Move the current point in the current path.
virtual void PathLineTo(float x, float y)=0
Add a line to the current path from the current point to the specified location.
A base interface to be combined with IControl for vectorial controls "IVControls",...
Definition: IControl.h:757
void SetColor(EVColor colorIdx, const IColor &color)
Set one of the IVColors that style the IVControl.
Definition: IControl.h:787
void AttachIControl(IControl *pControl, const char *label)
Call in the constructor of your IVControl to link the IVectorBase and IControl.
Definition: IControl.h:775
const IColor & GetColor(EVColor color) const
Get value of a specific EVColor in the IVControl.
Definition: IControl.h:801
Used to manage mouse modifiers i.e.
Used to manage a rectangular area, independent of draw class/platform.
float W() const
float H() const
IRECT GetPadded(float padding) const
Get a copy of this IRECT with each value padded by padding N.B.
IText is used to manage font and text/text entry style for a piece of text on the UI,...