From de2b5a92af22612e74243428b78ac8238d53a328 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Sun, 6 Jan 2019 20:26:38 -0500 Subject: [PATCH] Clean up event documentation, change event::PathDrop::path type to const reference, make event::Zoom recurse regardless of being consumed --- include/app/ModuleWidget.hpp | 3 ++- include/event.hpp | 25 ++++++++++++++++--------- include/widgets/Widget.hpp | 5 ++--- src/app/ModuleBrowser.cpp | 2 +- src/event.cpp | 5 ++--- 5 files changed, 23 insertions(+), 17 deletions(-) diff --git a/include/app/ModuleWidget.hpp b/include/app/ModuleWidget.hpp index 2886d4a9..216494ff 100644 --- a/include/app/ModuleWidget.hpp +++ b/include/app/ModuleWidget.hpp @@ -20,6 +20,8 @@ struct ModuleWidget : OpaqueWidget { std::vector params; std::vector inputs; std::vector outputs; + /** For RackWidget dragging */ + math::Vec dragPos; ModuleWidget(Module *module); ~ModuleWidget(); @@ -64,7 +66,6 @@ struct ModuleWidget : OpaqueWidget { void draw(NVGcontext *vg) override; void drawShadow(NVGcontext *vg); - math::Vec dragPos; void onHover(const event::Hover &e) override; void onButton(const event::Button &e) override; void onHoverKey(const event::HoverKey &e) override; diff --git a/include/event.hpp b/include/event.hpp index 966b078f..aad1b68e 100644 --- a/include/event.hpp +++ b/include/event.hpp @@ -83,7 +83,7 @@ struct Button : Event, Position { }; -/** Occurs when a key is pressed while the mouse is hovering a Widget. +/** Occurs when a key is pressed, released, or repeated while the mouse is hovering a Widget. Recurses until consumed. */ struct HoverKey : Event, Position, Key { @@ -130,7 +130,7 @@ struct Deselect : Event { }; -/** Occurs when a key is pressed while a Widget is selected. +/** Occurs when a key is pressed, released, or repeated while a Widget is selected. If consumed, a HoverKey event will not be triggered. */ struct SelectKey : Event, Key { @@ -144,7 +144,6 @@ struct SelectText : Event, Text { /** Occurs when a Widget begins being dragged. -Must consume to allow the drag to occur. */ struct DragStart : Event { }; @@ -157,16 +156,18 @@ struct DragEnd : Event { /** Occurs every frame on the dragged Widget. -`mouseDelta` may be zero. */ struct DragMove : Event { + /** Change in mouse position since the last frame. Can be zero. */ math::Vec mouseDelta; }; -/** Occurs every frame when the mouse is hovering over a Widget while dragging. +/** Occurs every frame when the mouse is hovering over a Widget while another Widget (possibly the same one) is being dragged. +Recurses until consumed. */ struct DragHover : Event, Position { + /** The dragged widget */ Widget *origin = NULL; /** Change in mouse position since the last frame. Can be zero. */ math::Vec mouseDelta; @@ -175,6 +176,7 @@ struct DragHover : Event, Position { /** Occurs when the mouse enters a Widget while dragging. */ struct DragEnter : Event { + /** The dragged widget */ Widget *origin = NULL; }; @@ -182,6 +184,7 @@ struct DragEnter : Event { /** Occurs when the mouse leaves a Widget while dragging. */ struct DragLeave : Event { + /** The dragged widget */ Widget *origin = NULL; }; @@ -189,15 +192,19 @@ struct DragLeave : Event { /** Occurs when the mouse button is released over a Widget while dragging. */ struct DragDrop : Event { + /** The dragged widget */ Widget *origin = NULL; }; -/** Occurs when a selection of files from the operating system are dropped onto a Widget. +/** Occurs when a selection of files from the operating system is dropped onto a Widget. +Recurses until consumed. */ struct PathDrop : Event, Position { + PathDrop(const std::vector &paths) : paths(paths) {} + /** List of file paths in the dropped selection */ - std::vector paths; + const std::vector &paths; }; @@ -214,7 +221,7 @@ struct Change : Event { /** Occurs when the zoom level of a Widget is changed. -Recurses. +Recurses until consumed. */ struct Zoom : Event { }; @@ -245,7 +252,7 @@ struct State { void handleScroll(math::Vec pos, math::Vec scrollDelta); void handleText(math::Vec pos, int codepoint); void handleKey(math::Vec pos, int key, int scancode, int action, int mods); - void handleDrop(math::Vec pos, std::vector paths); + void handleDrop(math::Vec pos, const std::vector &paths); void handleZoom(); }; diff --git a/include/widgets/Widget.hpp b/include/widgets/Widget.hpp index 50c3dfd2..58be4548 100644 --- a/include/widgets/Widget.hpp +++ b/include/widgets/Widget.hpp @@ -74,6 +74,7 @@ struct Widget { // Events + /** Recurses an event to all visible Widgets */ template void recurseEvent(TMethod f, const TEvent &e) { for (auto it = children.rbegin(); it != children.rend(); it++) { @@ -84,12 +85,10 @@ struct Widget { // Call child event handler (child->*f)(e); - // Stop iterating if consumed - if (e.getConsumed()) - break; } } + /** Recurses an event to all visible Widgets until it is consumed. */ template void recursePositionEvent(TMethod f, const TEvent &e) { for (auto it = children.rbegin(); it != children.rend(); it++) { diff --git a/src/app/ModuleBrowser.cpp b/src/app/ModuleBrowser.cpp index c8ec7d42..2f66910f 100644 --- a/src/app/ModuleBrowser.cpp +++ b/src/app/ModuleBrowser.cpp @@ -72,7 +72,7 @@ struct ModuleBox : OpaqueWidget { // Create module ModuleWidget *moduleWidget = model->createModuleWidget(); assert(moduleWidget); - context()->scene->rackWidget->addModuleAtMouse(moduleWidget); + context()->scene->rackWidget->addModule(moduleWidget); // This is a bit nonstandard/unsupported usage, but pretend the moduleWidget was clicked so it can be dragged in the RackWidget e.consume(moduleWidget); // Close Module Browser diff --git a/src/event.cpp b/src/event.cpp index 6f0601f5..7de01112 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -192,13 +192,12 @@ void State::handleScroll(math::Vec pos, math::Vec scrollDelta) { rootWidget->onHoverScroll(eHoverScroll); } -void State::handleDrop(math::Vec pos, std::vector paths) { +void State::handleDrop(math::Vec pos, const std::vector &paths) { // event::PathDrop event::Context ePathDropContext; - event::PathDrop ePathDrop; + event::PathDrop ePathDrop(paths); ePathDrop.context = &ePathDropContext; ePathDrop.pos = pos; - ePathDrop.paths = paths; rootWidget->onPathDrop(ePathDrop); }