From 04f53901b2612f79d72da19eb58b3c2a507f70f9 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Mon, 31 Dec 2018 14:49:42 -0500 Subject: [PATCH] Fix event and Wire bugs --- include/event.hpp | 1 + src/app/ModuleWidget.cpp | 1 - src/app/Port.cpp | 48 +++++++++++++++++++++------------------ src/app/WireContainer.cpp | 8 +++---- src/event.cpp | 3 +++ 5 files changed, 34 insertions(+), 27 deletions(-) diff --git a/include/event.hpp b/include/event.hpp index 06205795..a46e54d4 100644 --- a/include/event.hpp +++ b/include/event.hpp @@ -155,6 +155,7 @@ struct DragMove : Event { /** Occurs every frame when the mouse is hovering over a Widget while dragging. */ struct DragHover : Event, Position { + Widget *origin = NULL; /** Change in mouse position since the last frame. Can be zero. */ math::Vec mouseDelta; }; diff --git a/src/app/ModuleWidget.cpp b/src/app/ModuleWidget.cpp index be10d063..e069acd2 100644 --- a/src/app/ModuleWidget.cpp +++ b/src/app/ModuleWidget.cpp @@ -393,7 +393,6 @@ void ModuleWidget::onDragMove(event::DragMove &e) { if (!settings::lockModules) { math::Rect newBox = box; newBox.pos = context()->scene->rackWidget->lastMousePos.minus(dragPos); - DEBUG("%f %f", newBox.pos.x, newBox.pos.y); context()->scene->rackWidget->requestModuleBoxNearest(this, newBox); } } diff --git a/src/app/Port.cpp b/src/app/Port.cpp index 76c19739..89efbe6b 100644 --- a/src/app/Port.cpp +++ b/src/app/Port.cpp @@ -51,39 +51,33 @@ void Port::draw(NVGcontext *vg) { } void Port::onButton(event::Button &e) { - if (e.action == GLFW_PRESS && e.button == GLFW_MOUSE_BUTTON_LEFT) { + if (e.action == GLFW_PRESS && e.button == GLFW_MOUSE_BUTTON_RIGHT) { context()->scene->rackWidget->wireContainer->removeTopWire(this); // HACK // Update hovered*Port of active wire if applicable - event::DragEnter eDragEnter; - onDragEnter(eDragEnter); + // event::DragEnter eDragEnter; + // onDragEnter(eDragEnter); } e.target = this; } void Port::onDragStart(event::DragStart &e) { // Try to grab wire on top of stack - WireWidget *wire = context()->scene->rackWidget->wireContainer->getTopWire(this); - if (type == OUTPUT && context()->window->isModPressed()) { - wire = NULL; + WireWidget *wire = NULL; + if (type == INPUT || !context()->window->isModPressed()) { + wire = context()->scene->rackWidget->wireContainer->getTopWire(this); } if (wire) { // Disconnect existing wire - if (type == INPUT) - wire->inputPort = NULL; - else - wire->outputPort = NULL; + (type == INPUT ? wire->inputPort : wire->outputPort) = NULL; wire->updateWire(); } else { // Create a new wire wire = new WireWidget; - if (type == INPUT) - wire->inputPort = this; - else - wire->outputPort = this; + (type == INPUT ? wire->inputPort : wire->outputPort) = this; } context()->scene->rackWidget->wireContainer->setActiveWire(wire); } @@ -95,9 +89,21 @@ void Port::onDragEnd(event::DragEnd &e) { } void Port::onDragDrop(event::DragDrop &e) { + Port *originPort = dynamic_cast(e.origin); + if (!originPort) + return; + + // Fake onDragEnter because onDragLeave is triggered immediately before this one + event::DragEnter eDragEnter; + eDragEnter.origin = e.origin; + onDragEnter(eDragEnter); } void Port::onDragEnter(event::DragEnter &e) { + Port *originPort = dynamic_cast(e.origin); + if (!originPort) + return; + // Reject ports if this is an input port and something is already plugged into it if (type == INPUT) { WireWidget *topWire = context()->scene->rackWidget->wireContainer->getTopWire(this); @@ -107,20 +113,18 @@ void Port::onDragEnter(event::DragEnter &e) { WireWidget *activeWire = context()->scene->rackWidget->wireContainer->activeWire; if (activeWire) { - if (type == INPUT) - activeWire->hoveredInputPort = this; - else - activeWire->hoveredOutputPort = this; + (type == INPUT ? activeWire->hoveredInputPort : activeWire->hoveredOutputPort) = this; } } void Port::onDragLeave(event::DragLeave &e) { + Port *originPort = dynamic_cast(e.origin); + if (!originPort) + return; + WireWidget *activeWire = context()->scene->rackWidget->wireContainer->activeWire; if (activeWire) { - if (type == INPUT) - activeWire->hoveredInputPort = NULL; - else - activeWire->hoveredOutputPort = NULL; + (type == INPUT ? activeWire->hoveredInputPort : activeWire->hoveredOutputPort) = NULL; } } diff --git a/src/app/WireContainer.cpp b/src/app/WireContainer.cpp index ec05934c..42713ece 100644 --- a/src/app/WireContainer.cpp +++ b/src/app/WireContainer.cpp @@ -20,14 +20,14 @@ void WireContainer::commitActiveWire() { if (!activeWire) return; - if (activeWire->hoveredInputPort) { - activeWire->inputPort = activeWire->hoveredInputPort; - activeWire->hoveredInputPort = NULL; - } if (activeWire->hoveredOutputPort) { activeWire->outputPort = activeWire->hoveredOutputPort; activeWire->hoveredOutputPort = NULL; } + if (activeWire->hoveredInputPort) { + activeWire->inputPort = activeWire->hoveredInputPort; + activeWire->hoveredInputPort = NULL; + } activeWire->updateWire(); // Did it successfully connect? diff --git a/src/event.cpp b/src/event.cpp index f224b015..b393815a 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -51,6 +51,7 @@ void Context::setDragHovered(Widget *w) { if (dragHoveredWidget) { // event::DragLeave event::DragLeave eDragLeave; + eDragLeave.origin = draggedWidget; dragHoveredWidget->onDragLeave(eDragLeave); } @@ -59,6 +60,7 @@ void Context::setDragHovered(Widget *w) { if (dragHoveredWidget) { // event::DragEnter event::DragEnter eDragEnter; + eDragEnter.origin = draggedWidget; dragHoveredWidget->onDragEnter(eDragEnter); } } @@ -144,6 +146,7 @@ void Context::handleHover(math::Vec pos, math::Vec mouseDelta) { event::DragHover eDragHover; eDragHover.pos = pos; eDragHover.mouseDelta = mouseDelta; + eDragHover.origin = draggedWidget; rootWidget->onDragHover(eDragHover); setDragHovered(eDragHover.target);