-
-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathCanvas.h
More file actions
306 lines (224 loc) · 8.73 KB
/
Canvas.h
File metadata and controls
306 lines (224 loc) · 8.73 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
/*
// 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
#include <nanovg.h>
#ifdef NANOVG_GL_IMPLEMENTATION
# include <juce_opengl/juce_opengl.h>
using namespace juce::gl;
# undef NANOVG_GL_IMPLEMENTATION
# include <nanovg_gl_utils.h>
# define NANOVG_GL_IMPLEMENTATION 1
#endif
#include "ObjectGrid.h" // move to impl
#include "Utility/RateReducer.h" // move to impl
#include "Utility/ModifierKeyListener.h"
#include "Components/CheckedTooltip.h"
#include "Pd/MessageListener.h"
#include "Pd/Patch.h"
#include "Constants.h"
#include "Objects/ObjectParameters.h"
#include "NVGSurface.h"
#include "Utility/NVGUtils.h"
#include "Utility/GlobalMouseListener.h"
namespace pd {
class Patch;
}
class SuggestionComponent;
class GraphArea;
class GraphOnParent;
class Iolet;
class Object;
class Connection;
class PluginEditor;
class PluginProcessor;
class ConnectionPathUpdater;
class ConnectionBeingCreated;
class TabComponent;
class BorderResizer;
class CanvasSearchHighlight;
class ObjectsResizer;
class CanvasViewport;
struct ObjectDragState {
bool wasDragDuplicated : 1 = false;
bool didStartDragging : 1 = false;
bool wasSelectedOnMouseDown : 1 = false;
bool wasResized : 1 = false;
bool wasDuplicated : 1 = false;
Point<int> canvasDragStartPosition = { 0, 0 };
Component::SafePointer<Object> componentBeingDragged;
Component::SafePointer<Object> objectSnappingInbetween;
Component::SafePointer<Connection> connectionToSnapInbetween;
Point<int> duplicateOffset = { 0, 0 };
Point<int> lastDuplicateOffset = { 0, 0 };
};
class Canvas final : public Component
, public Value::Listener
, public SettingsFileListener
, public LassoSource<WeakReference<Component>>
, public ModifierKeyListener
, public pd::MessageListener
, public AsyncUpdater
, public NVGComponent
, public ChangeListener {
public:
Canvas(PluginEditor* parent, pd::Patch::Ptr patch, Component* parentGraph = nullptr);
~Canvas() override;
PluginEditor* editor;
PluginProcessor* pd;
void mouseDown(MouseEvent const& e) override;
void mouseDrag(MouseEvent const& e) override;
void mouseUp(MouseEvent const& e) override;
bool hitTest(int x, int y) override;
void commandKeyChanged(bool isHeld) override;
void shiftKeyChanged(bool isHeld) override;
void middleMouseChanged(bool isHeld) override;
void altKeyChanged(bool isHeld) override;
void settingsChanged(String const& name, var const& value) override;
void focusGained(FocusChangeType cause) override;
void focusLost(FocusChangeType cause) override;
void updateFramebuffers(NVGcontext* nvg) override;
void performRender(NVGcontext* nvg, Rectangle<int> invalidRegion);
void resized() override;
void renderAllObjects(NVGcontext* nvg, Rectangle<int> area);
void renderAllConnections(NVGcontext* nvg, Rectangle<int> area);
int getOverlays() const;
void updateOverlays();
bool shouldShowObjectActivity() const;
bool shouldShowIndex() const;
bool shouldShowConnectionDirection() const;
bool shouldShowConnectionActivity() const;
void save(std::function<void()> const& nestedCallback = [] { });
void saveAs(std::function<void()> const& nestedCallback = [] { });
void synchroniseAllCanvases();
void synchroniseSplitCanvas();
void synchronise();
void performSynchronise();
void handleAsyncUpdate() override;
void updateDrawables();
bool keyPressed(KeyPress const& key) override;
void valueChanged(Value& v) override;
void tabChanged();
void hideAllActiveEditors();
void copySelection();
void removeSelection();
void removeSelectedConnections();
void dragAndDropPaste(String const& patchString, Point<int> mousePos, int patchWidth, int patchHeight);
void pasteSelection();
void duplicateSelection();
void encapsulateSelection();
void triggerizeSelection();
void cycleSelection();
void connectSelection();
void tidySelection();
void cancelConnectionCreation();
void alignObjects(Align alignment);
void undo();
void redo();
void jumpToOrigin();
void restoreViewportState();
void saveViewportState();
void zoomToFitAll();
bool autoscroll(MouseEvent const& e);
// Multi-dragger functions
void deselectAll(bool broadcastChange = true);
void setSelected(Component* component, bool shouldNowBeSelected, bool updateCommandStatus = true, bool broadcastChange = true);
SelectedItemSet<WeakReference<Component>>& getLassoSelection() override;
bool checkPanDragMode();
bool setPanDragMode(bool shouldPan);
bool isPointOutsidePluginArea(Point<int> point) const;
void findLassoItemsInArea(Array<WeakReference<Component>>& itemsFound, Rectangle<int> const& area) override;
void updateSidebarSelection();
void orderConnections();
void showSuggestions(Object* object, TextEditor* textEditor);
void hideSuggestions();
bool panningModifierDown() const;
ObjectParameters& getInspectorParameters();
void receiveMessage(t_symbol* symbol, SmallArray<pd::Atom> const& atoms) override;
void activateCanvasSearchHighlight(Point<float> viewPos, Object* obj);
void removeCanvasSearchHighlight();
template<typename T>
SmallArray<T*> getSelectionOfType()
{
SmallArray<T*> result;
for (auto const& obj : selectedComponents) {
if (auto* objOfType = dynamic_cast<T*>(obj.get())) {
result.add(objOfType);
}
}
return result;
}
std::unique_ptr<CanvasViewport> viewport;
bool connectingWithDrag : 1 = false;
bool connectionCancelled : 1 = false;
SafePointer<Iolet> nearestIolet;
std::unique_ptr<SuggestionComponent> suggestor;
pd::Patch::Ptr refCountedPatch;
pd::Patch& patch;
Value locked = SynchronousValue();
Value commandLocked;
Value presentationMode;
SmallArray<juce::WeakReference<NVGComponent>> drawables;
// Needs to be allocated before object and connection so they can deselect themselves in the destructor
SelectedItemSet<WeakReference<Component>> selectedComponents;
PooledPtrArray<Object> objects;
PooledPtrArray<Connection> connections;
PooledPtrArray<ConnectionBeingCreated> connectionsBeingCreated;
bool showOrigin : 1 = false;
bool showBorder : 1 = false;
bool showConnectionOrder : 1 = false;
bool connectionsBehind : 1 = true;
bool showObjectActivity : 1 = false;
bool showIndex : 1 = false;
bool showConnectionDirection : 1 = false;
bool showConnectionActivity : 1 = false;
bool isZooming : 1 = false;
bool isGraph : 1 = false;
bool isDraggingLasso : 1 = false;
bool needsSearchUpdate : 1 = false;
bool altDown : 1 = false;
bool shiftDown : 1 = false;
Rectangle<int> currentRenderArea;
Value isGraphChild = SynchronousValue(var(false));
Value hideNameAndArgs = SynchronousValue(var(false));
Value xRange = SynchronousValue();
Value yRange = SynchronousValue();
Value patchWidth = SynchronousValue();
Value patchHeight = SynchronousValue();
Value zoomScale;
ObjectGrid objectGrid = ObjectGrid(this);
int lastObjectGridSize = -1;
NVGImage dotsLargeImage;
Point<int> const canvasOrigin;
std::unique_ptr<GraphArea> graphArea;
SafePointer<Object> lastSelectedObject; // For auto patching
SafePointer<Connection> lastSelectedConnection; // For auto patching
Point<int> pastedPosition;
Point<int> pastedPadding;
std::unique_ptr<ConnectionPathUpdater> pathUpdater;
RateReducer objectRateReducer = RateReducer(90);
ObjectDragState dragState;
static constexpr int infiniteCanvasSize = 128000;
Component objectLayer;
Component connectionLayer;
NVGImage resizeHandleImage;
NVGImage presentationShadowImage;
private:
void changeListenerCallback(ChangeBroadcaster* c) override;
SelectedItemSet<WeakReference<Component>> previousSelectedComponents;
void lookAndFeelChanged() override;
void parentHierarchyChanged() override;
GlobalMouseListener globalMouseListener;
bool dimensionsAreBeingEdited = false;
int lastMouseX, lastMouseY;
LassoComponent<WeakReference<Component>> lasso;
RateReducer canvasRateReducer = RateReducer(90);
// Properties that can be shown in the inspector by right-clicking on canvas
ObjectParameters parameters;
std::unique_ptr<BorderResizer> canvasBorderResizer;
std::unique_ptr<ObjectsResizer> objectsDistributeResizer;
std::unique_ptr<CanvasSearchHighlight> canvasSearchHighlight;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Canvas)
};