iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
IVDisplayControl.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 "ISender.h"
21
22BEGIN_IPLUG_NAMESPACE
23BEGIN_IGRAPHICS_NAMESPACE
24
27 , public IVectorBase
28{
29public:
30 static constexpr int MAX_BUFFER_SIZE = 2048;
31
32 IVDisplayControl(const IRECT& bounds, const char* label = "", const IVStyle& style = DEFAULT_STYLE, EDirection dir = EDirection::Horizontal, float lo = 0., float hi = 1.f, float defaultVal = 0., uint32_t bufferSize = 100, float strokeThickness = 2.f)
33 : IControl(bounds)
34 , IVectorBase(style)
35 , mBuffer(bufferSize, defaultVal)
36 , mLoValue(lo)
37 , mHiValue(hi)
38 , mStrokeThickness(strokeThickness)
39 , mDirection(dir)
40 {
41 assert(bufferSize > 0 && bufferSize < MAX_BUFFER_SIZE);
42
43 AttachIControl(this, label);
44 }
45
46 void OnResize() override
47 {
49
50 mPlotBounds = mWidgetBounds.GetPadded(mDirection == EDirection::Horizontal ? 0.f : -mStrokeThickness,
51 mDirection == EDirection::Horizontal ? -mStrokeThickness : 0.f,
52 mDirection == EDirection::Horizontal ? 0.f : -mStrokeThickness,
53 mDirection == EDirection::Horizontal ? -mStrokeThickness : 0.f);
54
55 SetDirty(false);
56 }
57
58 void Draw(IGraphics& g) override
59 {
60 DrawBackground(g, mRECT);
61 DrawWidget(g);
62 DrawLabel(g);
63
64 if(mStyle.drawFrame)
65 g.DrawRect(GetColor(kFR), mWidgetBounds, &mBlend, mStyle.frameThickness);
66 }
67
68 void DrawWidget(IGraphics& g) override
69 {
70 float x = mPlotBounds.L;
71 float y = mPlotBounds.T;
72 float w = mPlotBounds.W();
73 float h = mPlotBounds.H();
74
75 const int sz = static_cast<int>(mBuffer.size());
76
77 auto getPlotPos = [&](int pos, float axis, float extrem) {
78 float v = mBuffer[(mReadPos+pos) % sz];
79 v = (v - mLoValue) / (mHiValue - mLoValue);
80 return axis + extrem - (v * extrem);
81 };
82
83 if(mDirection == EDirection::Horizontal)
84 {
85 g.PathMoveTo(x, getPlotPos(0, y, h));
86
87 for (int i = 0; i < sz; i++)
88 {
89 float vx = x + ((float)i/(sz-1)) * w;
90 float vy = getPlotPos(i, y, h);
91 g.PathLineTo(vx, vy);
92 }
93 }
94 else
95 {
96 g.PathMoveTo(getPlotPos(0, x, w), y);
97
98 for (int i = 0; i < sz; i++)
99 {
100 float vx = getPlotPos(i, x, w);
101 float vy = y + ((float)i/(sz-1)) * h;
102 g.PathLineTo(vx, vy);
103 }
104 }
105 IStrokeOptions strokeOptions;
106 strokeOptions.mJoinOption = ELineJoin::Bevel;
107 g.PathStroke(IPattern::CreateLinearGradient(mPlotBounds, mDirection, {{COLOR_TRANSPARENT, 0.f}, {GetColor(kX1), 1.f}}), mStrokeThickness, strokeOptions, &mBlend);
108 }
109
110 void OnMsgFromDelegate(int msgTag, int dataSize, const void* pData) override
111 {
112 auto Update = [&](float v) {
113 mBuffer[mReadPos] = v;
114 mReadPos = (mReadPos+1) % mBuffer.size();
115 SetDirty(false);
116 };
117
118 if (!IsDisabled() && msgTag == ISender<>::kUpdateMessage)
119 {
120 IByteStream stream(pData, dataSize);
121
122 int pos = 0;
124 pos = stream.Get(&d, pos);
125 Update(d.vals[0]);
126 }
127 }
128
129private:
130 std::vector<float> mBuffer;
131 float mLoValue = 0.f;
132 float mHiValue = 1.f;
133 int mReadPos = 0;
134 float mStrokeThickness = 2.f;
135 EDirection mDirection;
136 IRECT mPlotBounds;
137};
138
139END_IGRAPHICS_NAMESPACE
140END_IPLUG_NAMESPACE
This file contains the base IControl implementation, along with some base classes for specific types ...
Manages a non-owned block of memory, for receiving arbitrary message byte streams.
Definition: IPlugStructs.h:268
int Get(T *pDst, int startPos) const
Get arbitary typed data from the stream.
Definition: IPlugStructs.h:289
The lowest level base class of an IGraphics control.
Definition: IControl.h:49
bool IsDisabled() const
Definition: IControl.h:362
void SetTargetRECT(const IRECT &bounds)
Set the rectangular mouse tracking target area, within the graphics context for this control.
Definition: IControl.h:323
virtual void SetDirty(bool triggerAction=true, int valIdx=kNoValIdx)
Mark the control as dirty, i.e.
Definition: IControl.cpp:198
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 PathStroke(const IPattern &pattern, float thickness, const IStrokeOptions &options=IStrokeOptions(), const IBlend *pBlend=0)=0
Stroke the current current path.
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.
ISender is a utility class which can be used to defer data from the realtime audio processing and sen...
Definition: ISender.h:65
A control to display a rolling graphics of historical values.
void OnMsgFromDelegate(int msgTag, int dataSize, const void *pData) override
Implement to receive messages sent to the control, see IEditorDelegate:SendControlMsgFromDelegate()
void Draw(IGraphics &g) override
Draw the control to the graphics context.
void OnResize() override
Called when IControl is constructed or resized using SetRect().
void DrawWidget(IGraphics &g) override
Draw the IVControl main widget (override)
A base interface to be combined with IControl for vectorial controls "IVControls",...
Definition: IControl.h:757
IRECT MakeRects(const IRECT &parent, bool hasHandle=false)
Calculate the rectangles for the various areas, depending on the style.
Definition: IControl.h:1158
virtual void DrawBackground(IGraphics &g, const IRECT &rect)
Draw the IVControl background (usually transparent)
Definition: IControl.h:876
void AttachIControl(IControl *pControl, const char *label)
Call in the constructor of your IVControl to link the IVectorBase and IControl.
Definition: IControl.h:775
virtual void DrawLabel(IGraphics &g)
Draw the IVControl label text.
Definition: IControl.h:889
const IColor & GetColor(EVColor color) const
Get value of a specific EVColor in the IVControl.
Definition: IControl.h:801
Used to manage stroke behaviour for path based drawing back ends.
static IPattern CreateLinearGradient(float x1, float y1, float x2, float y2, const std::initializer_list< IColorStop > &stops={})
Create a linear gradient IPattern.
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.
ISenderData is used to represent a typed data packet, that may contain values for multiple channels.
Definition: ISender.h:34
A struct encapsulating a set of properties used to configure IVControls.