forked from plugdata-team/plugdata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageObject.h
More file actions
381 lines (299 loc) · 11.9 KB
/
MessageObject.h
File metadata and controls
381 lines (299 loc) · 11.9 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
// 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 MessageObject final : public ObjectBase
, public KeyListener
, public TextEditor::Listener {
Value sizeProperty = SynchronousValue();
std::unique_ptr<TextEditor> editor;
BorderSize<int> border = BorderSize<int>(1, 6, 1, 1);
String objectText;
CachedTextRender textRenderer;
bool isDown = false;
bool isLocked = false;
public:
MessageObject(pd::WeakReference obj, Object* parent)
: ObjectBase(obj, parent)
{
objectParameters.addParamInt("Width (chars)", cDimensions, &sizeProperty, var(), true, 0);
lookAndFeelChanged();
}
void update() override
{
objectText = getSymbol();
if (auto obj = ptr.get<t_text>()) {
sizeProperty = TextObjectHelper::getWidthInChars(obj.get());
}
updateTextLayout();
}
Rectangle<int> getPdBounds() override
{
updateTextLayout(); // make sure layout height is updated
auto const textBounds = getTextSize();
int x = 0, y = 0, w, h;
if (auto obj = ptr.get<t_gobj>()) {
auto* cnvPtr = cnv->patch.getRawPointer();
pd::Interface::getObjectBounds(cnvPtr, obj.get(), &x, &y, &w, &h);
}
return { x, y, textBounds.getWidth(), std::max<int>(textBounds.getHeight() + 5, 20) };
}
Rectangle<int> getTextSize()
{
auto objText = editor ? editor->getText() : objectText;
if (editor && cnv->suggestor) {
cnv->suggestor->updateSuggestions(objText);
if (cnv->suggestor->getText().isNotEmpty()) {
objText = cnv->suggestor->getText();
}
}
int fontWidth = 7;
int charWidth = 0;
if (auto obj = ptr.get<void>()) {
charWidth = TextObjectHelper::getWidthInChars(obj.get());
fontWidth = glist_fontwidth(cnv->patch.getRawPointer());
}
auto const textSize = textRenderer.getTextBounds();
// Calculating string width is expensive, so we cache all the strings that we already calculated the width for
int const idealWidth = CachedStringWidth<15>::calculateStringWidth(objText) + 14;
// We want to adjust the width so ideal text with aligns with fontWidth
int const offset = idealWidth % fontWidth;
int textWidth;
if (objText.isEmpty()) { // If text is empty, set to minimum width
textWidth = std::max(charWidth, 6) * fontWidth;
} else if (charWidth == 0) { // If width is set to automatic, calculate based on text width
textWidth = std::clamp(idealWidth, 2 * fontWidth, fontWidth * 60);
} else { // If width was set manually, calculate what the width is
textWidth = std::max(charWidth, 2) * fontWidth + offset;
}
return { textWidth, textSize.getHeight() };
}
void updateTextLayout()
{
if (cnv->isGraph)
return; // Text layouting is expensive, so skip if it's not necessary
auto objText = editor ? editor->getText() : objectText;
if (editor && cnv->suggestor && cnv->suggestor->getText().isNotEmpty()) {
objText = cnv->suggestor->getText();
}
auto const colour = cnv->editor->getLookAndFeel().findColour(PlugDataColour::canvasTextColourId);
int const textWidth = getTextSize().getWidth() - 14;
if (textRenderer.prepareLayout(objText, Fonts::getCurrentFont().withHeight(15), colour, textWidth, getValue<int>(sizeProperty), false)) {
repaint();
}
}
void setPdBounds(Rectangle<int> b) override
{
if (auto gobj = ptr.get<t_gobj>()) {
auto* patch = cnv->patch.getRawPointer();
pd::Interface::moveObject(patch, gobj.get(), b.getX(), b.getY());
if (TextObjectHelper::getWidthInChars(gobj.get())) {
TextObjectHelper::setWidthInChars(gobj.get(), b.getWidth() / glist_fontwidth(patch));
}
}
}
void updateSizeProperty() override
{
setPdBounds(object->getObjectBounds());
if (auto text = ptr.get<t_text>()) {
setParameterExcludingListener(sizeProperty, TextObjectHelper::getWidthInChars(text.get()));
}
}
void lock(bool const locked) override
{
isLocked = locked;
}
void render(NVGcontext* nvg) override
{
auto const bounds = getLocalBounds();
auto const b = bounds.toFloat();
auto const sb = b.reduced(0.5f); // reduce size of background to stop AA edges from showing through
auto const bgCol = isDown ? cnv->guiObjectInternalOutlineCol : cnv->guiObjectBackgroundCol;
// Draw background
nvgDrawObjectWithFlag(nvg, sb.getX(), sb.getY(), sb.getWidth(), sb.getHeight(),
bgCol, bgCol, bgCol,
Corners::objectCornerRadius, ObjectFlagType::FlagMessage, static_cast<PlugDataLook&>(cnv->getLookAndFeel()).getUseFlagOutline());
auto const flagCol = isDown && ::getValue<bool>(object->locked) ? cnv->selectedOutlineCol : cnv->guiObjectInternalOutlineCol;
auto const outlineCol = object->isSelected() ? cnv->selectedOutlineCol : cnv->objectOutlineCol;
// Draw highlight around inner area when box is clicked
// We do this by drawing an inner area that is bright, while changing the background colour darker
if (isDown) {
auto const dB = bounds.reduced(5);
nvgDrawRoundedRect(nvg, dB.getX(), dB.getY(), dB.getWidth(), dB.getHeight(), cnv->guiObjectBackgroundCol, cnv->guiObjectBackgroundCol, 0);
}
// Draw outline & flag with shader
nvgDrawObjectWithFlag(nvg, b.getX(), b.getY(), b.getWidth(), b.getHeight(),
nvgRGBA(0, 0, 0, 0), outlineCol, flagCol,
Corners::objectCornerRadius, ObjectFlagType::FlagMessage, static_cast<PlugDataLook&>(cnv->getLookAndFeel()).getUseFlagOutline());
if (editor) {
imageRenderer.renderJUCEComponent(nvg, *editor, getImageScale());
} else {
textRenderer.renderText(nvg, border.subtractedFrom(getLocalBounds()).toFloat(), getImageScale());
}
}
void receiveObjectMessage(hash32 symbol, SmallArray<pd::Atom> const& atoms) override
{
if (symbol == hash("float"))
return;
String const v = getSymbol();
if (objectText != v) {
objectText = v;
repaint();
object->updateBounds();
}
}
void resized() override
{
updateTextLayout();
if (editor) {
editor->setBounds(getLocalBounds().withTrimmedRight(5));
}
}
void lookAndFeelChanged() override
{
updateTextLayout();
}
bool isEditorShown() override
{
return editor != nullptr;
}
void showEditor() override
{
if (editor == nullptr) {
editor.reset(TextObjectHelper::createTextEditor(object, 15));
editor->setLookAndFeel(&object->getLookAndFeel());
editor->setBorder(border.addedTo(BorderSize<int>(0, 0, 1, 0)));
editor->setBounds(getLocalBounds().withTrimmedRight(5));
editor->setText(objectText, false);
editor->addListener(this);
editor->addKeyListener(this);
editor->selectAll();
editor->setReturnKeyStartsNewLine(false);
addAndMakeVisible(editor.get());
editor->grabKeyboardFocus();
cnv->showSuggestions(object, editor.get());
editor->onFocusLost = [this] {
if (cnv->suggestor->hasKeyboardFocus(true) || Component::getCurrentlyFocusedComponent() == editor.get()) {
editor->grabKeyboardFocus();
return;
}
hideEditor();
};
object->updateBounds();
repaint();
}
}
void hideEditor() override
{
if (editor != nullptr) {
std::unique_ptr<TextEditor> outgoingEditor;
std::swap(outgoingEditor, editor);
cnv->hideSuggestions();
auto newText = outgoingEditor->getText();
newText = TextObjectHelper::fixNewlines(newText);
if (objectText != newText) {
objectText = newText;
object->updateBounds(); // Recalculate bounds
setPdBounds(object->getObjectBounds());
setSymbol(objectText);
cnv->synchronise();
}
outgoingEditor.reset();
repaint();
}
}
void mouseDown(MouseEvent const& e) override
{
if (!e.mods.isLeftButtonDown())
return;
if (isLocked) {
isDown = true;
repaint();
// startEdition();
click();
// stopEdition();
}
}
void click()
{
cnv->pd->enqueueFunctionAsync<t_pd>(ptr, [](t_pd* obj) {
pd_float(obj, 0);
});
}
void mouseUp(MouseEvent const& e) override
{
isDown = false;
repaint();
}
void textEditorReturnKeyPressed(TextEditor& ed) override
{
cnv->grabKeyboardFocus();
}
// For resize-while-typing behaviour
void textEditorTextChanged(TextEditor& ed) override
{
object->updateBounds();
}
String getSymbol() const
{
char* text;
int size;
if (auto messObj = ptr.get<t_text>()) {
binbuf_gettext(messObj->te_binbuf, &text, &size);
} else {
return {};
}
auto const result = String::fromUTF8(text, size);
freebytes(text, size);
return result.trimEnd();
}
void propertyChanged(Value& v) override
{
if (v.refersToSameSourceAs(sizeProperty)) {
auto const width = getValue<int>(sizeProperty);
setParameterExcludingListener(sizeProperty, width);
if (auto text = ptr.get<t_text>()) {
TextObjectHelper::setWidthInChars(text.get(), width);
}
object->updateBounds();
}
}
void setSymbol(String const& value)
{
auto* cstr = value.toRawUTF8();
if (auto messobj = ptr.get<t_text>()) {
auto* canvas = cnv->patch.getRawPointer();
pd::Interface::renameObject(canvas, messobj.cast<t_gobj>(), cstr, value.getNumBytesAsUTF8());
}
}
bool keyPressed(KeyPress const& key, Component* component) override
{
if (key.getKeyCode() == KeyPress::returnKey && editor && key.getModifiers().isShiftDown()) {
int caretPosition = editor->getCaretPosition();
auto text = editor->getText();
if (!editor->getHighlightedRegion().isEmpty())
return false;
if (text[caretPosition - 1] == ';') {
text = text.substring(0, caretPosition) + "\n" + text.substring(caretPosition);
caretPosition += 1;
} else {
text = text.substring(0, caretPosition) + ";\n" + text.substring(caretPosition);
caretPosition += 2;
}
editor->setText(text);
editor->setCaretPosition(caretPosition);
return true;
}
return false;
}
bool hideInGraph() override
{
return true;
}
std::unique_ptr<ComponentBoundsConstrainer> createConstrainer() override
{
return TextObjectHelper::createConstrainer(object);
}
};