Browse Source

Add menu to PortWidget.

tags/v2.0.0
Andrew Belt 3 years ago
parent
commit
567c827092
2 changed files with 51 additions and 11 deletions
  1. +3
    -0
      include/app/PortWidget.hpp
  2. +48
    -11
      src/app/PortWidget.cpp

+ 3
- 0
include/app/PortWidget.hpp View File

@@ -25,6 +25,9 @@ struct PortWidget : widget::OpaqueWidget {
engine::PortInfo* getPortInfo(); engine::PortInfo* getPortInfo();
void createTooltip(); void createTooltip();
void destroyTooltip(); void destroyTooltip();
void createContextMenu();
virtual void appendContextMenu(ui::Menu* menu) {}
void deleteTopCableAction();


void step() override; void step() override;
void draw(const DrawArgs& args) override; void draw(const DrawArgs& args) override;


+ 48
- 11
src/app/PortWidget.cpp View File

@@ -5,6 +5,7 @@
#include <history.hpp> #include <history.hpp>
#include <engine/Engine.hpp> #include <engine/Engine.hpp>
#include <settings.hpp> #include <settings.hpp>
#include <helpers.hpp>




namespace rack { namespace rack {
@@ -71,6 +72,7 @@ PortWidget::PortWidget() {
internal = new Internal; internal = new Internal;
} }



PortWidget::~PortWidget() { PortWidget::~PortWidget() {
// The port shouldn't have any cables when destroyed, but just to make sure. // The port shouldn't have any cables when destroyed, but just to make sure.
if (module) if (module)
@@ -80,6 +82,7 @@ PortWidget::~PortWidget() {
delete internal; delete internal;
} }



engine::Port* PortWidget::getPort() { engine::Port* PortWidget::getPort() {
if (!module) if (!module)
return NULL; return NULL;
@@ -89,6 +92,7 @@ engine::Port* PortWidget::getPort() {
return &module->outputs[portId]; return &module->outputs[portId];
} }



engine::PortInfo* PortWidget::getPortInfo() { engine::PortInfo* PortWidget::getPortInfo() {
if (!module) if (!module)
return NULL; return NULL;
@@ -98,6 +102,7 @@ engine::PortInfo* PortWidget::getPortInfo() {
return module->outputInfos[portId]; return module->outputInfos[portId];
} }



void PortWidget::createTooltip() { void PortWidget::createTooltip() {
if (!settings::tooltips) if (!settings::tooltips)
return; return;
@@ -111,6 +116,7 @@ void PortWidget::createTooltip() {
internal->tooltip = tooltip; internal->tooltip = tooltip;
} }



void PortWidget::destroyTooltip() { void PortWidget::destroyTooltip() {
if (!internal->tooltip) if (!internal->tooltip)
return; return;
@@ -119,10 +125,43 @@ void PortWidget::destroyTooltip() {
internal->tooltip = NULL; internal->tooltip = NULL;
} }



void PortWidget::createContextMenu() {
ui::Menu* menu = createMenu();
WeakPtr<PortWidget> 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() { void PortWidget::step() {
Widget::step(); Widget::step();
} }



void PortWidget::draw(const DrawArgs& args) { void PortWidget::draw(const DrawArgs& args) {
CableWidget* cw = APP->scene->rack->incompleteCable; CableWidget* cw = APP->scene->rack->incompleteCable;
if (cw) { if (cw) {
@@ -134,33 +173,27 @@ void PortWidget::draw(const DrawArgs& args) {
Widget::draw(args); Widget::draw(args);
} }



void PortWidget::onButton(const ButtonEvent& e) { void PortWidget::onButton(const ButtonEvent& e) {
OpaqueWidget::onButton(e); OpaqueWidget::onButton(e);


if (e.action == GLFW_PRESS && e.button == GLFW_MOUSE_BUTTON_RIGHT) { 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); e.consume(this);
} }
} }



void PortWidget::onEnter(const EnterEvent& e) { void PortWidget::onEnter(const EnterEvent& e) {
createTooltip(); createTooltip();
} }



void PortWidget::onLeave(const LeaveEvent& e) { void PortWidget::onLeave(const LeaveEvent& e) {
destroyTooltip(); destroyTooltip();
} }



void PortWidget::onDragStart(const DragStartEvent& e) { void PortWidget::onDragStart(const DragStartEvent& e) {
if (e.button != GLFW_MOUSE_BUTTON_LEFT) if (e.button != GLFW_MOUSE_BUTTON_LEFT)
return; return;
@@ -216,6 +249,7 @@ void PortWidget::onDragStart(const DragStartEvent& e) {
APP->scene->rack->setIncompleteCable(cw); APP->scene->rack->setIncompleteCable(cw);
} }



void PortWidget::onDragEnd(const DragEndEvent& e) { void PortWidget::onDragEnd(const DragEndEvent& e) {
if (e.button != GLFW_MOUSE_BUTTON_LEFT) if (e.button != GLFW_MOUSE_BUTTON_LEFT)
return; return;
@@ -237,6 +271,7 @@ void PortWidget::onDragEnd(const DragEndEvent& e) {
} }
} }



void PortWidget::onDragDrop(const DragDropEvent& e) { void PortWidget::onDragDrop(const DragDropEvent& e) {
// HACK: Only delete tooltip if we're not (normal) dragging it. // HACK: Only delete tooltip if we're not (normal) dragging it.
if (e.origin == this) if (e.origin == this)
@@ -262,6 +297,7 @@ void PortWidget::onDragDrop(const DragDropEvent& e) {
} }
} }



void PortWidget::onDragEnter(const DragEnterEvent& e) { void PortWidget::onDragEnter(const DragEnterEvent& e) {
PortWidget* pw = dynamic_cast<PortWidget*>(e.origin); PortWidget* pw = dynamic_cast<PortWidget*>(e.origin);
if (pw) { if (pw) {
@@ -286,6 +322,7 @@ void PortWidget::onDragEnter(const DragEnterEvent& e) {
} }
} }



void PortWidget::onDragLeave(const DragLeaveEvent& e) { void PortWidget::onDragLeave(const DragLeaveEvent& e) {
destroyTooltip(); destroyTooltip();




Loading…
Cancel
Save