diff --git a/include/app/Knob.hpp b/include/app/Knob.hpp index 73a3b1fc..1010c596 100644 --- a/include/app/Knob.hpp +++ b/include/app/Knob.hpp @@ -25,6 +25,8 @@ struct Knob : ParamWidget { void onDragStart(const widget::DragStartEvent &e) override; void onDragEnd(const widget::DragEndEvent &e) override; void onDragMove(const widget::DragMoveEvent &e) override; + void reset() override; + void randomize() override; }; diff --git a/include/app/ParamWidget.hpp b/include/app/ParamWidget.hpp index ae3b0aa6..0e28d0c7 100644 --- a/include/app/ParamWidget.hpp +++ b/include/app/ParamWidget.hpp @@ -28,6 +28,8 @@ struct ParamWidget : widget::OpaqueWidget { void fromJson(json_t *rootJ); void createContextMenu(); void resetAction(); + virtual void reset() {} + virtual void randomize() {} }; diff --git a/include/app/Switch.hpp b/include/app/Switch.hpp index 35c4a4b2..1ae21fe9 100644 --- a/include/app/Switch.hpp +++ b/include/app/Switch.hpp @@ -19,6 +19,8 @@ struct Switch : ParamWidget { void onDoubleClick(const widget::DoubleClickEvent &e) override; void onDragStart(const widget::DragStartEvent &e) override; void onDragEnd(const widget::DragEndEvent &e) override; + void reset() override; + void randomize() override; }; diff --git a/include/engine/Module.hpp b/include/engine/Module.hpp index 21405c5d..131f7d08 100644 --- a/include/engine/Module.hpp +++ b/include/engine/Module.hpp @@ -54,8 +54,6 @@ struct Module { void config(int numParams, int numInputs, int numOutputs, int numLights = 0); json_t *toJson(); void fromJson(json_t *rootJ); - void reset(); - void randomize(); struct ProcessArgs { float sampleRate; diff --git a/include/engine/Param.hpp b/include/engine/Param.hpp index ecc14f0f..0af8a4ee 100644 --- a/include/engine/Param.hpp +++ b/include/engine/Param.hpp @@ -48,8 +48,6 @@ struct Param { /** An optional one-sentence description of the parameter. */ std::string description; ParamQuantityFactory *paramQuantityFactory = NULL; - /** Determines whether this Param will be randomized when the user requests to randomize the Module. */ - bool randomizable = true; ~Param() { if (paramQuantityFactory) @@ -93,8 +91,6 @@ struct Param { json_t *toJson(); void fromJson(json_t *rootJ); - void reset(); - void randomize(); }; diff --git a/src/app/Knob.cpp b/src/app/Knob.cpp index bbbbb11c..1cf1aeb8 100644 --- a/src/app/Knob.cpp +++ b/src/app/Knob.cpp @@ -1,6 +1,7 @@ #include "app/Knob.hpp" #include "app.hpp" #include "app/Scene.hpp" +#include "random.hpp" #include "history.hpp" @@ -98,6 +99,21 @@ void Knob::onDragMove(const widget::DragMoveEvent &e) { ParamWidget::onDragMove(e); } +void Knob::reset() { + if (paramQuantity && paramQuantity->isBounded()) { + paramQuantity->reset(); + } +} + +void Knob::randomize() { + if (paramQuantity && paramQuantity->isBounded()) { + float value = math::rescale(random::uniform(), 0.f, 1.f, paramQuantity->getMinValue(), paramQuantity->getMaxValue()); + if (snap) + value = std::round(value); + paramQuantity->setValue(value); + } +} + } // namespace app } // namespace rack diff --git a/src/app/ModuleWidget.cpp b/src/app/ModuleWidget.cpp index 91873cb4..da94e349 100644 --- a/src/app/ModuleWidget.cpp +++ b/src/app/ModuleWidget.cpp @@ -651,6 +651,9 @@ void ModuleWidget::resetAction() { h->moduleId = module->id; h->oldModuleJ = toJson(); + for (ParamWidget *param : params) { + param->reset(); + } APP->engine->resetModule(module); h->newModuleJ = toJson(); @@ -666,6 +669,9 @@ void ModuleWidget::randomizeAction() { h->moduleId = module->id; h->oldModuleJ = toJson(); + for (ParamWidget *param : params) { + param->randomize(); + } APP->engine->randomizeModule(module); h->newModuleJ = toJson(); diff --git a/src/app/ParamWidget.cpp b/src/app/ParamWidget.cpp index 6202680e..5a5d5757 100644 --- a/src/app/ParamWidget.cpp +++ b/src/app/ParamWidget.cpp @@ -6,7 +6,6 @@ #include "app.hpp" #include "engine/Engine.hpp" #include "settings.hpp" -#include "random.hpp" #include "history.hpp" #include "helpers.hpp" @@ -230,7 +229,7 @@ void ParamWidget::createContextMenu() { void ParamWidget::resetAction() { if (paramQuantity && paramQuantity->isBounded()) { float oldValue = paramQuantity->getValue(); - paramQuantity->reset(); + reset(); float newValue = paramQuantity->getValue(); if (oldValue != newValue) { diff --git a/src/app/Switch.cpp b/src/app/Switch.cpp index 1a2d48d8..6f4154ac 100644 --- a/src/app/Switch.cpp +++ b/src/app/Switch.cpp @@ -1,6 +1,7 @@ #include "app/Switch.hpp" #include "app.hpp" #include "app/Scene.hpp" +#include "random.hpp" #include "history.hpp" @@ -69,6 +70,19 @@ void Switch::onDragEnd(const widget::DragEndEvent &e) { } } +void Switch::reset() { + if (paramQuantity && !momentary) { + paramQuantity->reset(); + } +} + +void Switch::randomize() { + if (paramQuantity && !momentary) { + float value = paramQuantity->getMinValue() + std::floor(random::uniform() * (paramQuantity->getRange() + 1)); + paramQuantity->setValue(value); + } +} + } // namespace app } // namespace rack diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp index 6de88eda..91630e37 100644 --- a/src/engine/Engine.cpp +++ b/src/engine/Engine.cpp @@ -507,7 +507,7 @@ void Engine::resetModule(Module *module) { VIPLock vipLock(internal->vipMutex); std::lock_guard lock(internal->mutex); - module->reset(); + module->onReset(); } void Engine::randomizeModule(Module *module) { @@ -515,7 +515,7 @@ void Engine::randomizeModule(Module *module) { VIPLock vipLock(internal->vipMutex); std::lock_guard lock(internal->mutex); - module->randomize(); + module->onRandomize(); } void Engine::bypassModule(Module *module, bool bypass) { diff --git a/src/engine/Module.cpp b/src/engine/Module.cpp index 64088708..38d6e565 100644 --- a/src/engine/Module.cpp +++ b/src/engine/Module.cpp @@ -89,21 +89,6 @@ void Module::fromJson(json_t *rootJ) { rightModuleId = json_integer_value(rightModuleIdJ); } -void Module::reset() { - for (Param ¶m : params) { - param.reset(); - } - onReset(); -} - -void Module::randomize() { - for (Param ¶m : params) { - if (param.randomizable) - param.randomize(); - } - onRandomize(); -} - } // namespace engine } // namespace rack diff --git a/src/engine/Param.cpp b/src/engine/Param.cpp index a4d5dcee..a13277e9 100644 --- a/src/engine/Param.cpp +++ b/src/engine/Param.cpp @@ -26,18 +26,6 @@ void Param::fromJson(json_t *rootJ) { } } -void Param::reset() { - if (isBounded()) { - value = defaultValue; - } -} - -void Param::randomize() { - if (isBounded()) { - value = math::rescale(random::uniform(), 0.f, 1.f, minValue, maxValue); - } -} - } // namespace engine } // namespace rack