forked from plugdata-team/plugdata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDropzoneObject.h
More file actions
204 lines (170 loc) · 7.46 KB
/
DropzoneObject.h
File metadata and controls
204 lines (170 loc) · 7.46 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
/*
// Copyright (c) 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 DropzoneObject final : public ObjectBase, public FileDragAndDropTarget, public TextDragAndDropTarget {
bool isDraggingOver = false;
Point<int> lastPosition;
Value sizeProperty = SynchronousValue();
public:
DropzoneObject(pd::WeakReference ptr, Object* object)
: ObjectBase(ptr, object)
{
objectParameters.addParamSize(&sizeProperty);
}
~DropzoneObject() override = default;
void render(NVGcontext* nvg) override
{
auto const b = getLocalBounds().toFloat();
auto fillColour = Colours::transparentBlack;
auto outlineColour = cnv->editor->getLookAndFeel().findColour(object->isSelected() && !cnv->isGraph ? PlugDataColour::objectSelectedOutlineColourId : PlugDataColour::outlineColourId);
nvgDrawRoundedRect(nvg, b.getX(), b.getY(), b.getWidth(), b.getHeight(), convertColour(fillColour), convertColour(outlineColour), Corners::objectCornerRadius);
if(isDraggingOver)
{
auto const hoverBounds = getLocalBounds().reduced(1.5f).toFloat();
nvgBeginPath(nvg);
nvgRoundedRect(nvg, hoverBounds.getX(), hoverBounds.getY(), hoverBounds.getWidth(), hoverBounds.getHeight(), Corners::objectCornerRadius);
nvgStrokeWidth(nvg, 3.0f);
nvgStrokeColor(nvg, convertColour(outlineColour));
nvgStroke(nvg);
}
}
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());
t_atom atoms[2];
SETFLOAT(atoms, b.getWidth() - 1);
SETFLOAT(atoms + 1, b.getHeight() - 1);
pd_typedmess(gobj.cast<t_pd>(), pd->generateSymbol("dim"), 2, atoms);
}
}
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 { x, y, w + 1, h + 1 };
}
return {};
}
void update() 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);
sizeProperty = VarArray { var(w), var(h) };
}
}
void updateSizeProperty() override
{
setPdBounds(object->getObjectBounds());
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);
sizeProperty = VarArray { var(w), var(h) };
}
}
void propertyChanged(Value& value) override
{
if (value.refersToSameSourceAs(sizeProperty)) {
auto const& arr = *sizeProperty.getValue().getArray();
auto const* constrainer = getConstrainer();
auto const width = std::max(static_cast<int>(arr[0]), constrainer->getMinimumWidth());
auto const height = std::max(static_cast<int>(arr[1]), constrainer->getMinimumHeight());
setParameterExcludingListener(sizeProperty, VarArray { var(width), var(height) });
t_atom atoms[2];
SETFLOAT(atoms, width);
SETFLOAT(atoms + 1, height);
if (auto gobj = ptr.get<t_gobj>()) {
pd_typedmess(gobj.cast<t_pd>(), pd->generateSymbol("dim"), 2, atoms);
}
object->updateBounds();
}
}
// Check if top-level canvas is locked to determine if we should respond to mouse events
bool isLocked() const
{
// Find top-level canvas
auto const* topLevel = cnv;
while (auto const* nextCanvas = topLevel->findParentComponentOfClass<Canvas>()) {
topLevel = nextCanvas;
}
return getValue<bool>(topLevel->locked) || getValue<bool>(topLevel->commandLocked) || topLevel->isGraph;
}
bool isInterestedInFileDrag(const StringArray& files) override {
return true;
};
void fileDragEnter(const StringArray& files, int x, int y) override {
isDraggingOver = true;
repaint();
};
void fileDragMove(const StringArray& files, int x, int y) override {
auto bounds = getPdBounds().toFloat();
auto* patch = cnv->patch.getRawPointer();
char cnvName[32];
snprintf(cnvName, 32, ".x%lx", reinterpret_cast<unsigned long>(glist_getcanvas(patch)));
if (auto gobj = ptr.get<t_gobj>()) {
pd->sendMessage("__else_dnd_rcv", "_drag_over", { pd->generateSymbol(cnvName), x + bounds.getX(), y + bounds.getY() });
}
};
void fileDragExit(const StringArray& files) override {
if (auto gobj = ptr.get<t_gobj>()) {
pd->sendMessage("__else_dnd_rcv", "_drag_leave", {});
}
isDraggingOver = false;
repaint();
};
void filesDropped(const StringArray& files, int x, int y) override {
for(auto& file : files)
{
auto* patch = cnv->patch.getRawPointer();
char cnvName[32];
snprintf(cnvName, 32, ".x%lx", reinterpret_cast<unsigned long>(glist_getcanvas(patch)));
if (auto gobj = ptr.get<t_gobj>()) {
pd->sendMessage("__else_dnd_rcv", "_drag_drop", { pd->generateSymbol(cnvName), 0.0f, pd->generateSymbol(file.replace("\\", "/")) });
}
}
isDraggingOver = false;
repaint();
};
bool isInterestedInTextDrag(const String& text) override {
return true;
}
void textDragEnter(const String& text, int x, int y) override {
isDraggingOver = true;
repaint();
}
void textDragMove(const String& text, int x, int y) override {
auto bounds = getPdBounds().toFloat();
auto* patch = cnv->patch.getRawPointer();
char cnvName[32];
snprintf(cnvName, 32, ".x%lx", reinterpret_cast<unsigned long>(glist_getcanvas(patch)));
if (auto gobj = ptr.get<t_gobj>()) {
pd->sendMessage("__else_dnd_rcv", "_drag_over", { pd->generateSymbol(cnvName), x + bounds.getX(), y + bounds.getY() });
}
}
void textDragExit(const String& text) override {
if (auto gobj = ptr.get<t_gobj>()) {
pd->sendMessage("__else_dnd_rcv", "_drag_leave", {});
}
isDraggingOver = false;
repaint();
}
void textDropped (const String& text, int x, int y) override {
auto* patch = cnv->patch.getRawPointer();
char cnvName[32];
snprintf(cnvName, 32, ".x%lx", reinterpret_cast<unsigned long>(glist_getcanvas(patch)));
if (auto gobj = ptr.get<t_gobj>()) {
pd->sendMessage("__else_dnd_rcv", "_drag_drop", { pd->generateSymbol(cnvName), 1.0f, pd->generateSymbol(text) });
}
isDraggingOver = false;
repaint();
}
};