Browse Source

When dragging cable back to its original port, don't push history which does nothing.

tags/v2.6.0
Andrew Belt 5 months ago
parent
commit
26ef3b574e
3 changed files with 34 additions and 10 deletions
  1. +1
    -0
      include/history.hpp
  2. +26
    -10
      src/app/PortWidget.cpp
  3. +7
    -0
      src/history.cpp

+ 1
- 0
include/history.hpp View File

@@ -142,6 +142,7 @@ struct CableAdd : Action {
int outputId;
NVGcolor color;
void setCable(app::CableWidget* cw);
bool isCable(app::CableWidget* cw) const;
void undo() override;
void redo() override;
CableAdd() {


+ 26
- 10
src/app/PortWidget.cpp View File

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

// Should never happen since it's created in onDragStart().
if (!internal->history)
return;

// Remove all incomplete cables
for (CableWidget* cw : APP->scene->rack->getIncompleteCables()) {
APP->scene->rack->removeCable(cw);
@@ -487,7 +483,10 @@ void PortWidget::onDragEnd(const DragEndEvent& e) {
}

// Push history
if (internal->history->isEmpty()) {
if (!internal->history) {
// This shouldn't happen since it's created in onDragStart()
}
else if (internal->history->isEmpty()) {
// No history actions, don't push anything
delete internal->history;
}
@@ -541,11 +540,28 @@ void PortWidget::onDragDrop(const DragDropEvent& e) {
}
cw->updateCable();

history::CableAdd* h = new history::CableAdd;
h->setCable(cw);
pwOrigin->internal->history->push(h);

// TODO Reject history if plugging into same port
// This should always be true since the ComplexAction is created in onDragStart()
history::ComplexAction* history = pwOrigin->internal->history;
if (history) {
// Reject history if plugging into same port
auto& actions = history->actions;
auto it = std::find_if(actions.begin(), actions.end(), [&](history::Action* h) {
history::CableAdd* hca = dynamic_cast<history::CableAdd*>(h);
if (!hca)
return false;
return hca->isCable(cw);
});

if (it != actions.end()) {
actions.erase(it);
}
else {
// Push CableAdd action
history::CableAdd* h = new history::CableAdd;
h->setCable(cw);
history->push(h);
}
}
}
}



+ 7
- 0
src/history.cpp View File

@@ -160,6 +160,13 @@ void CableAdd::setCable(app::CableWidget* cw) {
color = cw->color;
}

bool CableAdd::isCable(app::CableWidget* cw) const {
if (!(cw && cw->cable && cw->cable->id >= 0 && cw->cable->outputModule && cw->cable->inputModule))
return false;
engine::Cable* c = cw->cable;
return c->inputModule->id == inputModuleId && c->inputId == inputId && c->outputModule->id == outputModuleId && c->outputId == outputId;
}

void CableAdd::undo() {
app::CableWidget* cw = APP->scene->rack->getCable(cableId);
if (!cw)


Loading…
Cancel
Save