diff --git a/include/app/PortWidget.hpp b/include/app/PortWidget.hpp index 207fe3cf..f89d353c 100644 --- a/include/app/PortWidget.hpp +++ b/include/app/PortWidget.hpp @@ -25,6 +25,9 @@ struct PortWidget : widget::OpaqueWidget { engine::PortInfo* getPortInfo(); void createTooltip(); void destroyTooltip(); + void createContextMenu(); + virtual void appendContextMenu(ui::Menu* menu) {} + void deleteTopCableAction(); void step() override; void draw(const DrawArgs& args) override; diff --git a/src/app/PortWidget.cpp b/src/app/PortWidget.cpp index 1d7844a0..1b8a450c 100644 --- a/src/app/PortWidget.cpp +++ b/src/app/PortWidget.cpp @@ -5,6 +5,7 @@ #include #include #include +#include namespace rack { @@ -71,6 +72,7 @@ PortWidget::PortWidget() { internal = new Internal; } + PortWidget::~PortWidget() { // The port shouldn't have any cables when destroyed, but just to make sure. if (module) @@ -80,6 +82,7 @@ PortWidget::~PortWidget() { delete internal; } + engine::Port* PortWidget::getPort() { if (!module) return NULL; @@ -89,6 +92,7 @@ engine::Port* PortWidget::getPort() { return &module->outputs[portId]; } + engine::PortInfo* PortWidget::getPortInfo() { if (!module) return NULL; @@ -98,6 +102,7 @@ engine::PortInfo* PortWidget::getPortInfo() { return module->outputInfos[portId]; } + void PortWidget::createTooltip() { if (!settings::tooltips) return; @@ -111,6 +116,7 @@ void PortWidget::createTooltip() { internal->tooltip = tooltip; } + void PortWidget::destroyTooltip() { if (!internal->tooltip) return; @@ -119,10 +125,43 @@ void PortWidget::destroyTooltip() { internal->tooltip = NULL; } + +void PortWidget::createContextMenu() { + ui::Menu* menu = createMenu(); + WeakPtr weakThis = this; + + menu->addChild(createMenuItem("Delete top cable", "", + [=]() { + if (!weakThis) + return; + weakThis->deleteTopCableAction(); + } + )); + + // TODO +} + + +void PortWidget::deleteTopCableAction() { + CableWidget* cw = APP->scene->rack->getTopCable(this); + if (!cw) + return; + + // history::CableRemove + history::CableRemove* h = new history::CableRemove; + h->setCable(cw); + APP->history->push(h); + + APP->scene->rack->removeCable(cw); + delete cw; +} + + void PortWidget::step() { Widget::step(); } + void PortWidget::draw(const DrawArgs& args) { CableWidget* cw = APP->scene->rack->incompleteCable; if (cw) { @@ -134,33 +173,27 @@ void PortWidget::draw(const DrawArgs& args) { Widget::draw(args); } + void PortWidget::onButton(const ButtonEvent& e) { OpaqueWidget::onButton(e); if (e.action == GLFW_PRESS && e.button == GLFW_MOUSE_BUTTON_RIGHT) { - CableWidget* cw = APP->scene->rack->getTopCable(this); - if (cw) { - // history::CableRemove - history::CableRemove* h = new history::CableRemove; - h->setCable(cw); - APP->history->push(h); - - APP->scene->rack->removeCable(cw); - delete cw; - } - + createContextMenu(); e.consume(this); } } + void PortWidget::onEnter(const EnterEvent& e) { createTooltip(); } + void PortWidget::onLeave(const LeaveEvent& e) { destroyTooltip(); } + void PortWidget::onDragStart(const DragStartEvent& e) { if (e.button != GLFW_MOUSE_BUTTON_LEFT) return; @@ -216,6 +249,7 @@ void PortWidget::onDragStart(const DragStartEvent& e) { APP->scene->rack->setIncompleteCable(cw); } + void PortWidget::onDragEnd(const DragEndEvent& e) { if (e.button != GLFW_MOUSE_BUTTON_LEFT) return; @@ -237,6 +271,7 @@ void PortWidget::onDragEnd(const DragEndEvent& e) { } } + void PortWidget::onDragDrop(const DragDropEvent& e) { // HACK: Only delete tooltip if we're not (normal) dragging it. if (e.origin == this) @@ -262,6 +297,7 @@ void PortWidget::onDragDrop(const DragDropEvent& e) { } } + void PortWidget::onDragEnter(const DragEnterEvent& e) { PortWidget* pw = dynamic_cast(e.origin); if (pw) { @@ -286,6 +322,7 @@ void PortWidget::onDragEnter(const DragEnterEvent& e) { } } + void PortWidget::onDragLeave(const DragLeaveEvent& e) { destroyTooltip();