VCV Rack API v2
Loading...
Searching...
No Matches
Widget.hpp
Go to the documentation of this file.
1#pragma once
2#include <list>
3
4#include <common.hpp>
5#include <math.hpp>
6#include <window/Window.hpp>
7#include <color.hpp>
8#include <widget/event.hpp>
9#include <weakptr.hpp>
10
11
12namespace rack {
14namespace widget {
15
16
21struct Widget : WeakBase {
23 math::Rect box = math::Rect(math::Vec(), math::Vec(INFINITY, INFINITY));
25 Widget* parent = NULL;
26 std::list<Widget*> children;
30 bool visible = true;
34 bool requestedDelete = false;
35
36 virtual ~Widget();
37
46 void setSize(math::Vec size);
48 bool isVisible();
50 void setVisible(bool visible);
52 void show() {
53 setVisible(true);
54 }
56 void hide() {
57 setVisible(false);
58 }
59
62
70 bool isDescendantOf(Widget* ancestor);
77 return getRelativeOffset(v, NULL);
78 }
82 virtual float getRelativeZoom(Widget* ancestor);
84 return getRelativeZoom(NULL);
85 }
89
90 template <class T>
92 if (!parent)
93 return NULL;
94 T* p = dynamic_cast<T*>(parent);
95 if (p)
96 return p;
97 return parent->getAncestorOfType<T>();
98 }
99
100 template <class T>
102 for (Widget* child : children) {
103 T* c = dynamic_cast<T*>(child);
104 if (c)
105 return c;
106 c = child->getFirstDescendantOfType<T>();
107 if (c)
108 return c;
109 }
110 return NULL;
111 }
112
115 bool hasChild(Widget* child);
119 void addChild(Widget* child);
122 void addChildBottom(Widget* child);
126 void addChildBelow(Widget* child, Widget* sibling);
127 void addChildAbove(Widget* child, Widget* sibling);
132 void removeChild(Widget* child);
137
139 virtual void step();
140
141 struct DrawArgs {
142 NVGcontext* vg = NULL;
145 NVGLUframebuffer* fb = NULL;
146 };
147
152 virtual void draw(const DrawArgs& args);
154 DEPRECATED virtual void draw(NVGcontext* vg) {}
155
163 virtual void drawLayer(const DrawArgs& args, int layer);
164
168 void drawChild(Widget* child, const DrawArgs& args, int layer = 0);
169
170 // Events
171
173 template <typename TMethod, class TEvent>
174 void recurseEvent(TMethod f, const TEvent& e) {
175 for (auto it = children.rbegin(); it != children.rend(); it++) {
176 // Stop propagation if requested
177 if (!e.isPropagating())
178 break;
179 Widget* child = *it;
180 // Don't filter child by visibility. Typically only position events need to be filtered by visibility.
181 // if (!child->visible)
182 // continue;
183
184 // Clone event for (currently) no reason
185 TEvent e2 = e;
186 // Call child event handler
187 (child->*f)(e2);
188 }
189 }
190
192 template <typename TMethod, class TEvent>
193 void recursePositionEvent(TMethod f, const TEvent& e) {
194 for (auto it = children.rbegin(); it != children.rend(); it++) {
195 // Stop propagation if requested
196 if (!e.isPropagating())
197 break;
198 Widget* child = *it;
199 // Filter child by visibility and position
200 if (!child->visible)
201 continue;
202 if (!child->box.contains(e.pos))
203 continue;
204
205 // Clone event and adjust its position
206 TEvent e2 = e;
207 e2.pos = e.pos.minus(child->box.pos);
208 // Call child event handler
209 (child->*f)(e2);
210 }
211 }
212
214
219 };
220
228 };
229 virtual void onHover(const HoverEvent& e) {
231 }
232
243 int mods;
244 };
245 virtual void onButton(const ButtonEvent& e) {
247 }
248
253 virtual void onDoubleClick(const DoubleClickEvent& e) {}
254
264 int key;
278 std::string keyName;
288 int mods;
289 };
290
295 virtual void onHoverKey(const HoverKeyEvent& e) {
297 }
298
303 };
308 virtual void onHoverText(const HoverTextEvent& e) {
310 }
311
318 };
319 virtual void onHoverScroll(const HoverScrollEvent& e) {
321 }
322
328 virtual void onEnter(const EnterEvent& e) {}
329
334 virtual void onLeave(const LeaveEvent& e) {}
335
341 virtual void onSelect(const SelectEvent& e) {}
342
347 virtual void onDeselect(const DeselectEvent& e) {}
348
353 virtual void onSelectKey(const SelectKeyEvent& e) {}
354
359 virtual void onSelectText(const SelectTextEvent& e) {}
360
364 };
370 virtual void onDragStart(const DragStartEvent& e) {}
371
376 virtual void onDragEnd(const DragEndEvent& e) {}
377
384 };
385 virtual void onDragMove(const DragMoveEvent& e) {}
386
393 Widget* origin = NULL;
396 };
397 virtual void onDragHover(const DragHoverEvent& e) {
399 }
400
407 Widget* origin = NULL;
408 };
409 virtual void onDragEnter(const DragEnterEvent& e) {}
410
416 Widget* origin = NULL;
417 };
418 virtual void onDragLeave(const DragLeaveEvent& e) {}
419
425 Widget* origin = NULL;
426 };
427 virtual void onDragDrop(const DragDropEvent& e) {}
428
433 PathDropEvent(const std::vector<std::string>& paths) : paths(paths) {}
434
436 const std::vector<std::string>& paths;
437 };
438 virtual void onPathDrop(const PathDropEvent& e) {
440 }
441
446 virtual void onAction(const ActionEvent& e) {}
447
452 virtual void onChange(const ChangeEvent& e) {}
453
458 virtual void onDirty(const DirtyEvent& e) {
460 }
461
465 virtual void onReposition(const RepositionEvent& e) {}
466
470 virtual void onResize(const ResizeEvent& e) {}
471
474 struct AddEvent : BaseEvent {};
475 virtual void onAdd(const AddEvent& e) {}
476
480 virtual void onRemove(const RemoveEvent& e) {}
481
485 struct ShowEvent : BaseEvent {};
486 virtual void onShow(const ShowEvent& e) {
488 }
489
493 struct HideEvent : BaseEvent {};
494 virtual void onHide(const HideEvent& e) {
496 }
497
502 NVGcontext* vg;
503 };
504 virtual void onContextCreate(const ContextCreateEvent& e) {
506 }
507
512 NVGcontext* vg;
513 };
514 virtual void onContextDestroy(const ContextDestroyEvent& e) {
516 }
517};
518
519
520} // namespace widget
521
525namespace event {
560}
561
562
563} // namespace rack
#define DEPRECATED
Attribute for deprecated functions and symbols.
Definition common.hpp:24
Root namespace for the Rack API.
Definition AudioDisplay.hpp:9
Base class for classes that allow WeakPtrs to be used.
Definition weakptr.hpp:16
2-dimensional rectangle for graphics.
Definition math.hpp:301
Vec pos
Definition math.hpp:302
bool contains(Vec v) const
Returns whether this Rect contains a point, inclusive on the left/top, exclusive on the right/bottom.
Definition math.hpp:326
static Rect inf()
Returns the infinite Rect.
Definition math.hpp:319
2-dimensional vector of floats, representing a point on the plane for graphics.
Definition math.hpp:189
Base class for all events.
Definition event.hpp:54
Occurs after a certain action is triggered on a Widget.
Definition Widget.hpp:445
Occurs after a Widget is added to a parent.
Definition Widget.hpp:474
Occurs each mouse button press or release.
Definition Widget.hpp:237
int button
GLFW_MOUSE_BUTTON_LEFT, GLFW_MOUSE_BUTTON_RIGHT, GLFW_MOUSE_BUTTON_MIDDLE, etc.
Definition Widget.hpp:239
int action
GLFW_PRESS or GLFW_RELEASE.
Definition Widget.hpp:241
int mods
GLFW_MOD_*.
Definition Widget.hpp:243
Occurs after the value of a Widget changes.
Definition Widget.hpp:451
Occurs after the Window (including OpenGL and NanoVG contexts) are created.
Definition Widget.hpp:501
NVGcontext * vg
Definition Widget.hpp:502
Occurs before the Window (including OpenGL and NanoVG contexts) are destroyed.
Definition Widget.hpp:511
NVGcontext * vg
Definition Widget.hpp:512
Occurs when a different Widget is selected.
Definition Widget.hpp:346
Occurs when the pixel buffer of this module must be refreshed.
Definition Widget.hpp:457
Occurs when the left mouse button is pressed a second time on the same Widget within a time duration.
Definition Widget.hpp:252
Definition Widget.hpp:361
int button
The mouse button held while dragging.
Definition Widget.hpp:363
Occurs when the mouse button is released over a Widget while dragging.
Definition Widget.hpp:423
Widget * origin
The dragged widget.
Definition Widget.hpp:425
Occurs when a Widget stops being dragged by releasing the mouse button.
Definition Widget.hpp:375
Occurs when the mouse enters a Widget while dragging.
Definition Widget.hpp:405
Widget * origin
The dragged widget.
Definition Widget.hpp:407
Occurs every frame when the mouse is hovering over a Widget while another Widget (possibly the same o...
Definition Widget.hpp:391
Widget * origin
The dragged widget.
Definition Widget.hpp:393
math::Vec mouseDelta
Change in mouse position since the last frame.
Definition Widget.hpp:395
Occurs when the mouse leaves a Widget while dragging.
Definition Widget.hpp:414
Widget * origin
The dragged widget.
Definition Widget.hpp:416
Occurs every frame on the dragged Widget.
Definition Widget.hpp:381
math::Vec mouseDelta
Change in mouse position since the last frame.
Definition Widget.hpp:383
Occurs when a Widget begins being dragged.
Definition Widget.hpp:369
Definition Widget.hpp:141
NVGLUframebuffer * fb
Definition Widget.hpp:145
math::Rect clipBox
Local box representing the visible viewport.
Definition Widget.hpp:144
NVGcontext * vg
Definition Widget.hpp:142
Occurs when a Widget begins consuming the Hover event.
Definition Widget.hpp:327
Occurs after a Widget is hidden with Widget::hide().
Definition Widget.hpp:493
Occurs every frame when the mouse is hovering over a Widget.
Definition Widget.hpp:225
math::Vec mouseDelta
Change in mouse position since the last frame.
Definition Widget.hpp:227
Occurs when a key is pressed, released, or repeated while the mouse is hovering a Widget.
Definition Widget.hpp:294
Occurs when the mouse scroll wheel is moved while the mouse is hovering a Widget.
Definition Widget.hpp:315
math::Vec scrollDelta
Change of scroll wheel position.
Definition Widget.hpp:317
Occurs when a character is typed while the mouse is hovering a Widget.
Definition Widget.hpp:307
An event prototype with a GLFW key.
Definition Widget.hpp:256
int action
The type of event occurring with the key.
Definition Widget.hpp:283
int scancode
Platform-dependent "software" key code.
Definition Widget.hpp:270
int mods
Bitwise OR of key modifiers, such as Ctrl or Shift.
Definition Widget.hpp:288
std::string keyName
String containing the lowercase key name, if it produces a printable character.
Definition Widget.hpp:278
int key
The key corresponding to what it would be called in its position on a QWERTY US keyboard.
Definition Widget.hpp:264
Occurs when a different Widget is entered.
Definition Widget.hpp:333
Occurs when a selection of files from the operating system is dropped onto a Widget.
Definition Widget.hpp:432
const std::vector< std::string > & paths
List of file paths in the dropped selection.
Definition Widget.hpp:436
PathDropEvent(const std::vector< std::string > &paths)
Definition Widget.hpp:433
An event prototype with a vector position.
Definition Widget.hpp:216
math::Vec pos
The pixel coordinate where the event occurred, relative to the Widget it is called on.
Definition Widget.hpp:218
Occurs before a Widget is removed from its parent.
Definition Widget.hpp:479
Occurs after a Widget's position is set by Widget::setPosition().
Definition Widget.hpp:464
Occurs after a Widget's size is set by Widget::setSize().
Definition Widget.hpp:469
Occurs when a Widget begins consuming the Button press event for the left mouse button.
Definition Widget.hpp:340
Occurs when a key is pressed, released, or repeated while a Widget is selected.
Definition Widget.hpp:352
Occurs when text is typed while a Widget is selected.
Definition Widget.hpp:358
Occurs after a Widget is shown with Widget::show().
Definition Widget.hpp:485
An event prototype with a Unicode character.
Definition Widget.hpp:300
int codepoint
Unicode code point of the character.
Definition Widget.hpp:302
A node in the 2D scene graph.
Definition Widget.hpp:21
virtual void onContextCreate(const ContextCreateEvent &e)
Definition Widget.hpp:504
void addChildAbove(Widget *child, Widget *sibling)
void show()
Makes Widget visible and triggers ShowEvent if changed.
Definition Widget.hpp:52
virtual DEPRECATED void draw(NVGcontext *vg)
Override draw(const DrawArgs &args) instead.
Definition Widget.hpp:154
void setSize(math::Vec size)
Sets size and triggers ResizeEvent if size changed.
virtual void drawLayer(const DrawArgs &args, int layer)
Draw additional layers.
std::list< Widget * > children
Definition Widget.hpp:26
math::Vec getAbsoluteOffset(math::Vec v)
Returns v transformed into world/root/global/absolute coordinates.
Definition Widget.hpp:76
void clearChildren()
Removes and deletes all child Widgets.
math::Rect getBox()
virtual void onAction(const ActionEvent &e)
Definition Widget.hpp:446
void setBox(math::Rect box)
Calls setPosition() and then setSize().
virtual void onEnter(const EnterEvent &e)
Definition Widget.hpp:328
widget::Widget * getParent()
bool isDescendantOf(Widget *ancestor)
Returns whether ancestor is a parent or distant parent of this widget.
math::Vec getPosition()
void recursePositionEvent(TMethod f, const TEvent &e)
Recurses an event to all visible Widgets until it is consumed.
Definition Widget.hpp:193
virtual void onDragEnter(const DragEnterEvent &e)
Definition Widget.hpp:409
virtual math::Vec getRelativeOffset(math::Vec v, Widget *ancestor)
Returns v (given in local coordinates) transformed into the coordinate system of ancestor.
void hide()
Makes Widget not visible and triggers HideEvent if changed.
Definition Widget.hpp:56
virtual float getRelativeZoom(Widget *ancestor)
Returns the zoom level in the coordinate system of ancestor.
virtual void onContextDestroy(const ContextDestroyEvent &e)
Definition Widget.hpp:514
void addChildBottom(Widget *child)
Adds widget to the bottom of the children.
virtual void onDirty(const DirtyEvent &e)
Definition Widget.hpp:458
virtual math::Rect getChildrenBoundingBox()
Returns the smallest rectangle containing this widget's children (visible and invisible) in its local...
virtual void onRemove(const RemoveEvent &e)
Definition Widget.hpp:480
virtual void onHide(const HideEvent &e)
Definition Widget.hpp:494
virtual void onHoverKey(const HoverKeyEvent &e)
Definition Widget.hpp:295
virtual void onHover(const HoverEvent &e)
Definition Widget.hpp:229
virtual void onAdd(const AddEvent &e)
Definition Widget.hpp:475
virtual void onDragEnd(const DragEndEvent &e)
Definition Widget.hpp:376
bool requestedDelete
If set to true, parent will delete Widget in the next step().
Definition Widget.hpp:34
virtual void onShow(const ShowEvent &e)
Definition Widget.hpp:486
virtual math::Rect getViewport(math::Rect r=math::Rect::inf())
Returns a subset of the given Rect bounded by the box of this widget and all ancestors.
virtual void onLeave(const LeaveEvent &e)
Definition Widget.hpp:334
virtual void onHoverText(const HoverTextEvent &e)
Definition Widget.hpp:308
virtual void onPathDrop(const PathDropEvent &e)
Definition Widget.hpp:438
virtual void onDragLeave(const DragLeaveEvent &e)
Definition Widget.hpp:418
float getAbsoluteZoom()
Definition Widget.hpp:83
virtual void onDoubleClick(const DoubleClickEvent &e)
Definition Widget.hpp:253
bool hasChild(Widget *child)
Checks if the given widget is a child of this widget.
virtual void onHoverScroll(const HoverScrollEvent &e)
Definition Widget.hpp:319
void requestDelete()
Requests this Widget's parent to delete it in the next step().
virtual void onSelect(const SelectEvent &e)
Definition Widget.hpp:341
virtual void onDeselect(const DeselectEvent &e)
Definition Widget.hpp:347
virtual void step()
Advances the module by one frame.
Widget * parent
Automatically set when Widget is added as a child to another Widget.
Definition Widget.hpp:25
void drawChild(Widget *child, const DrawArgs &args, int layer=0)
Draws a particular child.
math::Rect box
Position relative to parent and size of widget.
Definition Widget.hpp:23
virtual void onDragDrop(const DragDropEvent &e)
Definition Widget.hpp:427
virtual math::Rect getVisibleChildrenBoundingBox()
virtual void onButton(const ButtonEvent &e)
Definition Widget.hpp:245
T * getFirstDescendantOfType()
Definition Widget.hpp:101
void addChild(Widget *child)
Adds widget to the top of the children.
T * getAncestorOfType()
Definition Widget.hpp:91
void addChildBelow(Widget *child, Widget *sibling)
Adds widget directly below another widget.
virtual void onDragStart(const DragStartEvent &e)
Definition Widget.hpp:370
virtual void onChange(const ChangeEvent &e)
Definition Widget.hpp:452
virtual void onReposition(const RepositionEvent &e)
Definition Widget.hpp:465
virtual void onDragMove(const DragMoveEvent &e)
Definition Widget.hpp:385
virtual void onDragHover(const DragHoverEvent &e)
Definition Widget.hpp:397
virtual void draw(const DrawArgs &args)
Draws the widget to the NanoVG context.
void setVisible(bool visible)
Sets visible and triggers ShowEvent or HideEvent if changed.
virtual void onSelectKey(const SelectKeyEvent &e)
Definition Widget.hpp:353
void recurseEvent(TMethod f, const TEvent &e)
Recurses an event to all visible Widgets.
Definition Widget.hpp:174
bool visible
Disables rendering but allow stepping.
Definition Widget.hpp:30
virtual void onSelectText(const SelectTextEvent &e)
Definition Widget.hpp:359
virtual void onResize(const ResizeEvent &e)
Definition Widget.hpp:470
void removeChild(Widget *child)
Removes widget from list of children if it exists.
void setPosition(math::Vec pos)
Sets position and triggers RepositionEvent if position changed.