-
Notifications
You must be signed in to change notification settings - Fork 7
/
dfxguicontrol.h
273 lines (215 loc) · 7.24 KB
/
dfxguicontrol.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*------------------------------------------------------------------------
Destroy FX Library is a collection of foundation code
for creating audio processing plug-ins.
Copyright (C) 2003-2022 Sophia Poirier
This file is part of the Destroy FX Library (version 1.0).
Destroy FX Library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Destroy FX Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Destroy FX Library. If not, see <http:https://www.gnu.org/licenses/>.
To contact the author, use the contact form at http:https://destroyfx.org/
------------------------------------------------------------------------*/
#pragma once
#include <type_traits>
#include <unordered_set>
#include <vector>
#include "dfxgui-base.h"
#include "dfxguimisc.h"
#include "idfxguicontrol.h"
#if TARGET_OS_MAC
#include <CoreFoundation/CoreFoundation.h>
#endif
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
//-----------------------------------------------------------------------------
template <class T>
class DGControl : public IDGControl, public T
{
static_assert(std::is_base_of_v<VSTGUI::CControl, T>);
public:
template <typename... Args>
explicit DGControl(Args&&... args);
VSTGUI::CControl* asCControl() final
{
return this;
}
VSTGUI::CControl const* asCControl() const final
{
return this;
}
DfxGuiEditor* getOwnerEditor() const noexcept final
{
return mOwnerEditor;
}
void setValue_gen(float inValue) final;
void setDefaultValue_gen(float inValue) final;
long getValue_i() final;
void setValue_i(long inValue) final;
void redraw() final;
long getParameterID() const final;
void setParameterID(long inParameterID) final;
bool isParameterAttached() const final;
long getNumStates() const final
{
return mNumStates;
}
void setNumStates(long inNumStates);
float getFineTuneFactor() const override
{
return kDefaultFineTuneFactor;
}
bool notifyIfChanged() final;
bool onWheel(VSTGUI::CPoint const& inPos, VSTGUI::CMouseWheelAxis const& inAxis, float const& inDistance, VSTGUI::CButtonState const& inButtons) override;
void onMouseWheelEditing() final
{
mMouseWheelEditingSupport.onMouseWheelEditing();
}
void invalidateMouseWheelEditingTimer() final
{
mMouseWheelEditingSupport.invalidateMouseWheelEditingTimer();
}
void setDrawAlpha(float inAlpha) final;
float getDrawAlpha() const final;
void setHelpText(char const* inText) final;
#if TARGET_OS_MAC
void setHelpText(CFStringRef inText);
#endif
std::vector<IDGControl*> getChildren() override { return {}; }
std::vector<IDGControl const*> getChildren() const override { return {}; }
#if TARGET_PLUGIN_USES_MIDI
void setMidiLearner(bool inEnable) override {}
#endif
protected:
static constexpr float kDefaultFineTuneFactor = 10.0f;
//static constexpr float kDefaultMouseDragRange = 200.0f; // pixels
//static constexpr float kDefaultMouseDragValueIncrement = 0.1f;
//static constexpr float kDefaultMouseDragValueIncrement_descrete = 15.0f;
class DiscreteValueConstrainer
{
public:
explicit DiscreteValueConstrainer(IDGControl* inControl)
: mControl(inControl),
mEntryValue(inControl ? inControl->asCControl()->getValue() : 0.0f),
mEntryEditing(inControl && inControl->asCControl()->isEditing())
{
// the editing state is a counter, so this is essentially us bumping the editing reference count
if (mEntryEditing)
{
mControl->asCControl()->beginEdit();
}
}
~DiscreteValueConstrainer()
{
if (mControl && (mControl->getNumStates() > 0) && (mControl->asCControl()->getValue() != mEntryValue))
{
mControl->setValue_i(mControl->getValue_i());
if (mControl->asCControl()->isDirty())
{
mControl->asCControl()->valueChanged();
mControl->asCControl()->invalid();
}
}
// and by bumping that reference count, here we extend the edit cycle lifespan through the preceding value change
if (mEntryEditing)
{
mControl->asCControl()->endEdit();
}
}
private:
IDGControl* const mControl;
float const mEntryValue;
bool const mEntryEditing;
};
DGRect rectLocalToFrame(DGRect rect) const
{
return rect.offset(T::getViewSize().getTopLeft());
}
// Apple on Mac, Control on others.
static bool isPlatformMetaSet(const VSTGUI::CButtonState &button)
{
#if TARGET_OS_MAC
return button.isAppleSet();
#else
return button.isControlSet();
#endif
}
private:
void initValues();
void pullNumStatesFromParameter();
DfxGuiEditor* const mOwnerEditor;
long mNumStates = 0;
class MouseWheelEditingSupport : protected VSTGUI::CMouseWheelEditingSupport
{
public:
explicit MouseWheelEditingSupport(IDGControl* inControl)
: mControl(inControl)
{
}
void onMouseWheelEditing()
{
VSTGUI::CMouseWheelEditingSupport::onMouseWheelEditing(mControl->asCControl());
}
void invalidateMouseWheelEditingTimer()
{
VSTGUI::CMouseWheelEditingSupport::invalidMouseWheelEditTimer(mControl->asCControl());
}
private:
IDGControl* const mControl;
} mMouseWheelEditingSupport;
};
#pragma clang diagnostic pop
//-----------------------------------------------------------------------------
template <class T>
class DGMultiControl : /*public IDGMultiControl,*/ public DGControl<T>
{
public:
using DGControl<T>::DGControl;
std::vector<IDGControl*> getChildren() final
{
return {mChildren.cbegin(), mChildren.cend()};
}
std::vector<IDGControl const*> getChildren() const final
{
return {mChildren.cbegin(), mChildren.cend()};
}
void setViewSize(VSTGUI::CRect const& inPos, bool inInvalidate = true) override;
bool onWheel(VSTGUI::CPoint const& inPos, VSTGUI::CMouseWheelAxis const& inAxis, float const& inDistance, VSTGUI::CButtonState const& inButtons) override;
void setDirty_all(bool inValue);
bool isDirty() const override;
bool checkDefaultValue_all(VSTGUI::CButtonState inButtons);
void beginEdit_all();
void endEdit_all();
bool isEditing_any() const;
IDGControl* getControlByParameterID(long inParameterID);
protected:
IDGControl* addChild(long inParameterID);
// TODO: C++23 use std::span?
void addChildren(std::vector<long> const& inParameterID);
template <typename Proc>
void forEachChild(Proc inProc);
void notifyIfChanged_all();
private:
class DGMultiControlChild final : public DGControl<VSTGUI::CControl>
{
public:
DGMultiControlChild(DfxGuiEditor* inOwnerEditor, DGRect const& inRegion, long inParameterID);
void draw(VSTGUI::CDrawContext*) override {}
CLASS_METHODS(DGMultiControlChild, VSTGUI::CControl)
};
std::unordered_set<IDGControl*> mChildren;
};
#include "dfxguicontrol.hpp"
//-----------------------------------------------------------------------------
class DGNullControl final : public DGControl<VSTGUI::CControl>
{
public:
DGNullControl(DfxGuiEditor* inOwnerEditor, DGRect const& inRegion, DGImage* inBackgroundImage = nullptr);
void draw(VSTGUI::CDrawContext* inContext) override;
CLASS_METHODS(DGNullControl, VSTGUI::CControl)
};