From 19d9fdd041cf3148bb64fad7ae020ee558dab51d Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Thu, 10 Jan 2019 01:48:13 -0500 Subject: [PATCH] Add ModuleBypass. Serialize Module::bypass. --- include/app/ModuleWidget.hpp | 1 - include/history.hpp | 7 +++++++ src/app/ModuleWidget.cpp | 15 +++++++++------ src/engine/Module.cpp | 12 ++++++++++-- src/history.cpp | 13 +++++++++++++ 5 files changed, 39 insertions(+), 9 deletions(-) diff --git a/include/app/ModuleWidget.hpp b/include/app/ModuleWidget.hpp index cbbe4020..e858ec3b 100644 --- a/include/app/ModuleWidget.hpp +++ b/include/app/ModuleWidget.hpp @@ -41,7 +41,6 @@ struct ModuleWidget : OpaqueWidget { void save(std::string filename); void loadDialog(); void saveDialog(); - void toggleBypass(); /** Disconnects cables from all ports Called when the user clicks Disconnect Cables in the context menu. diff --git a/include/history.hpp b/include/history.hpp index caac2842..9420d62a 100644 --- a/include/history.hpp +++ b/include/history.hpp @@ -62,6 +62,13 @@ struct ModuleMove : ModuleAction { }; +struct ModuleBypass : ModuleAction { + bool bypass; + void undo() override; + void redo() override; +}; + + struct ParamChange : ModuleAction { int paramId; float oldValue; diff --git a/src/app/ModuleWidget.cpp b/src/app/ModuleWidget.cpp index 2f1a0df1..6465a4bb 100644 --- a/src/app/ModuleWidget.cpp +++ b/src/app/ModuleWidget.cpp @@ -243,10 +243,13 @@ void ModuleWidget::saveDialog() { save(pathStr); } -void ModuleWidget::toggleBypass() { - if (!module) - return; - module->bypass ^= true; +void ModuleWidget_bypassAction(ModuleWidget *moduleWidget) { + // Push ModuleBypass history action + history::ModuleBypass *h = new history::ModuleBypass; + h->bypass = !moduleWidget->module->bypass; + h->moduleId = moduleWidget->module->id; + app()->history->push(h); + h->redo(); } void ModuleWidget::disconnect() { @@ -392,7 +395,7 @@ void ModuleWidget::onHoverKey(const event::HoverKey &e) { } break; case GLFW_KEY_E: { if (app()->window->isModPressed() && !app()->window->isShiftPressed()) { - toggleBypass(); + ModuleWidget_bypassAction(this); e.consume(this); } } break; @@ -527,7 +530,7 @@ struct ModuleBypassItem : MenuItem { rightText = CHECKMARK_STRING " " + rightText; } void onAction(const event::Action &e) override { - moduleWidget->toggleBypass(); + ModuleWidget_bypassAction(moduleWidget); } }; diff --git a/src/engine/Module.cpp b/src/engine/Module.cpp index 9bdcc385..ba48294f 100644 --- a/src/engine/Module.cpp +++ b/src/engine/Module.cpp @@ -30,6 +30,10 @@ json_t *Module::toJson() { } json_object_set_new(rootJ, "params", paramsJ); + // bypass + if (bypass) + json_object_set_new(rootJ, "bypass", json_boolean(bypass)); + // data json_t *dataJ = dataToJson(); if (dataJ) { @@ -58,11 +62,15 @@ void Module::fromJson(json_t *rootJ) { } } + // bypass + json_t *bypassJ = json_object_get(rootJ, "bypass"); + if (bypassJ) + bypass = json_boolean_value(bypassJ); + // data json_t *dataJ = json_object_get(rootJ, "data"); - if (dataJ) { + if (dataJ) dataFromJson(dataJ); - } } void Module::reset() { diff --git a/src/history.cpp b/src/history.cpp index 84ac4f53..54e6eab4 100644 --- a/src/history.cpp +++ b/src/history.cpp @@ -83,6 +83,19 @@ void ModuleMove::redo() { } +void ModuleBypass::undo() { + ModuleWidget *moduleWidget = app()->scene->rackWidget->getModule(moduleId); + assert(moduleWidget); + moduleWidget->module->bypass = !bypass; +} + +void ModuleBypass::redo() { + ModuleWidget *moduleWidget = app()->scene->rackWidget->getModule(moduleId); + assert(moduleWidget); + moduleWidget->module->bypass = bypass; +} + + void ParamChange::undo() { ModuleWidget *moduleWidget = app()->scene->rackWidget->getModule(moduleId); assert(moduleWidget);