forked from plugdata-team/plugdata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtonObject.h
More file actions
304 lines (263 loc) · 10.1 KB
/
ButtonObject.h
File metadata and controls
304 lines (263 loc) · 10.1 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
// Copyright (c) 2021-2025 Timothy Schoen
// For information on usage and redistribution, and for a DISCLAIMER OF ALL
// WARRANTIES, see the file, "LICENSE.txt," in this distribution.
*/
#pragma once
class ButtonObject final : public ObjectBase {
bool state = false;
bool alreadyTriggered = false;
Value primaryColour = SynchronousValue();
Value secondaryColour = SynchronousValue();
Value sizeProperty = SynchronousValue();
NVGcolor fgCol;
NVGcolor bgCol;
enum Mode {
Latch,
Toggle,
Bang
};
Mode mode;
public:
ButtonObject(pd::WeakReference obj, Object* parent)
: ObjectBase(obj, parent)
{
objectParameters.addParamSize(&sizeProperty);
objectParameters.addParamColourFG(&primaryColour);
objectParameters.addParamColourBG(&secondaryColour);
updateColours();
}
void onConstrainerCreate() override
{
constrainer->setFixedAspectRatio(1);
constrainer->setMinimumHeight(9);
constrainer->setMinimumWidth(9);
}
void updateColours()
{
bgCol = convertColour(Colour::fromString(secondaryColour.toString()));
fgCol = convertColour(Colour::fromString(primaryColour.toString()));
repaint();
}
void update() override
{
if (auto button = ptr.get<t_fake_button>()) {
primaryColour = Colour(button->x_fgcolor[0], button->x_fgcolor[1], button->x_fgcolor[2]).toString();
secondaryColour = Colour(button->x_bgcolor[0], button->x_bgcolor[1], button->x_bgcolor[2]).toString();
sizeProperty = VarArray(button->x_w, button->x_h);
if (button->x_mode == 0) {
mode = Latch;
} else if (button->x_mode == 1) {
mode = Toggle;
} else {
mode = Bang;
}
}
updateColours();
}
void toggleObject(Point<int> position) override
{
if (!alreadyTriggered) {
if (mode == Latch) {
state = true;
} else if (mode == Toggle) {
state = !state;
}
if (mode == Bang) {
state = true;
if (auto button = ptr.get<t_fake_button>()) {
outlet_bang(button->x_obj.ob_outlet);
}
Timer::callAfterDelay(250,
[_this = SafePointer(this)]() mutable {
// First check if this object still exists
if (!_this)
return;
if (_this->state) {
_this->state = false;
_this->repaint();
}
});
} else {
if (auto button = ptr.get<t_fake_button>()) {
outlet_float(button->x_obj.ob_outlet, state);
}
}
repaint();
alreadyTriggered = true;
}
}
void untoggleObject() override
{
if (alreadyTriggered) {
if (mode == Latch) {
state = false;
if (auto button = ptr.get<t_fake_button>()) {
outlet_float(button->x_obj.ob_outlet, 0);
}
}
repaint();
alreadyTriggered = false;
}
}
Rectangle<int> getPdBounds() override
{
if (auto gobj = ptr.get<t_gobj>()) {
auto* patch = cnv->patch.getRawPointer();
int x = 0, y = 0, w = 0, h = 0;
pd::Interface::getObjectBounds(patch, gobj.get(), &x, &y, &w, &h);
return Rectangle<int>(x, y, w + 1, h + 1);
}
return {};
}
void setPdBounds(Rectangle<int> const b) override
{
if (auto button = ptr.get<t_fake_button>()) {
auto* patch = cnv->patch.getRawPointer();
pd::Interface::moveObject(patch, button.cast<t_gobj>(), b.getX(), b.getY());
button->x_w = b.getWidth() - 1;
button->x_h = b.getHeight() - 1;
}
}
void updateSizeProperty() override
{
setPdBounds(object->getObjectBounds());
if (auto button = ptr.get<t_fake_button>()) {
setParameterExcludingListener(sizeProperty, VarArray(var(button->x_w), var(button->x_h)));
}
}
void mouseDown(MouseEvent const& e) override
{
if (!e.mods.isLeftButtonDown())
return;
if (mode == Latch) {
state = true;
} else if (mode == Toggle) {
state = !state;
}
if (mode == Bang) {
state = true;
if (auto button = ptr.get<t_fake_button>()) {
outlet_bang(button->x_obj.ob_outlet);
}
Timer::callAfterDelay(250,
[_this = SafePointer(this)]() mutable {
// First check if this object still exists
if (!_this)
return;
if (_this->state) {
_this->state = false;
_this->repaint();
}
});
} else {
if (auto button = ptr.get<t_fake_button>()) {
outlet_float(button->x_obj.ob_outlet, state);
}
}
// Make sure we don't re-click with an accidental drag
alreadyTriggered = true;
repaint();
}
void mouseUp(MouseEvent const& e) override
{
alreadyTriggered = false;
if (mode == Latch) {
state = false;
if (auto button = ptr.get<t_fake_button>()) {
outlet_float(button->x_obj.ob_outlet, 0);
}
}
repaint();
}
void render(NVGcontext* nvg) override
{
auto const b = getLocalBounds().toFloat();
nvgDrawRoundedRect(nvg, b.getX(), b.getY(), b.getWidth(), b.getHeight(), bgCol, object->isSelected() ? cnv->selectedOutlineCol : cnv->objectOutlineCol, Corners::objectCornerRadius);
auto const guiBounds = b.reduced(1);
auto const outerBounds = guiBounds.reduced(5);
auto const innerRectBounds = outerBounds.reduced(2.5);
bool spaceToShowRect = false;
if (b.getWidth() >= 25 && b.getHeight() >= 25) {
spaceToShowRect = true;
nvgDrawRoundedRect(nvg, outerBounds.getX(), outerBounds.getY(), outerBounds.getWidth(), outerBounds.getHeight(), cnv->guiObjectInternalOutlineCol, cnv->guiObjectInternalOutlineCol, Corners::objectCornerRadius);
nvgDrawRoundedRect(nvg, innerRectBounds.getX(), innerRectBounds.getY(), innerRectBounds.getWidth(), innerRectBounds.getHeight(), bgCol, bgCol, Corners::objectCornerRadius - 1.0f);
}
// Fill ellipse if bangState is true
if (state) {
auto const innerBounds = spaceToShowRect ? innerRectBounds.reduced(1) : guiBounds;
auto const cornerRadius = spaceToShowRect ? Corners::objectCornerRadius - 1.5f : Corners::objectCornerRadius - 1;
nvgDrawRoundedRect(nvg, innerBounds.getX(), innerBounds.getY(), innerBounds.getWidth(), innerBounds.getHeight(), fgCol, fgCol, cornerRadius);
}
}
bool canEdgeOverrideAspectRatio() override
{
return true;
}
void propertyChanged(Value& value) override
{
if (value.refersToSameSourceAs(sizeProperty)) {
auto* constrainer = getConstrainer();
auto const& arr = *sizeProperty.getValue().getArray();
auto const width = std::max(static_cast<int>(arr[0]), constrainer->getMinimumWidth());
auto height = std::max(static_cast<int>(arr[1]), constrainer->getMinimumHeight());
constrainer->setFixedAspectRatio(static_cast<float>(width) / height);
setParameterExcludingListener(sizeProperty, VarArray(width, height));
if (auto button = ptr.get<t_fake_button>()) {
button->x_w = width;
button->x_h = height;
}
object->updateBounds();
} else if (value.refersToSameSourceAs(primaryColour)) {
auto const col = Colour::fromString(primaryColour.toString());
if (auto button = ptr.get<t_fake_button>()) {
button->x_fgcolor[0] = col.getRed();
button->x_fgcolor[1] = col.getGreen();
button->x_fgcolor[2] = col.getBlue();
}
updateColours();
} else if (value.refersToSameSourceAs(secondaryColour)) {
auto const col = Colour::fromString(secondaryColour.toString());
if (auto button = ptr.get<t_fake_button>()) {
button->x_bgcolor[0] = col.getRed();
button->x_bgcolor[1] = col.getGreen();
button->x_bgcolor[2] = col.getBlue();
}
updateColours();
}
}
void receiveObjectMessage(hash32 const symbol, SmallArray<pd::Atom> const& atoms) override
{
switch (symbol) {
case hash("bgcolor"): {
if (atoms.size() >= 3) {
setParameterExcludingListener(secondaryColour, Colour(atoms[0].getFloat(), atoms[1].getFloat(), atoms[2].getFloat()).toString());
updateColours();
}
break;
}
case hash("fgcolor"): {
if (atoms.size() >= 3) {
setParameterExcludingListener(primaryColour, Colour(atoms[0].getFloat(), atoms[1].getFloat(), atoms[2].getFloat()).toString());
updateColours();
}
break;
}
case hash("float"): {
if (atoms.size()) {
state = !approximatelyEqual(atoms[0].getFloat(), 0.0f);
}
repaint();
break;
}
case hash("latch"):
case hash("bang"):
case hash("toggle"): {
update();
break;
}
default:
break;
}
}
};