Browse Source

Refactor ModuleAdd. Add history::State::clear()

tags/v1.0.0
Andrew Belt 5 years ago
parent
commit
42e47732f9
4 changed files with 57 additions and 67 deletions
  1. +6
    -8
      include/history.hpp
  2. +1
    -3
      src/app/ModuleBrowser.cpp
  3. +3
    -8
      src/app/ModuleWidget.cpp
  4. +47
    -48
      src/history.cpp

+ 6
- 8
include/history.hpp View File

@@ -10,6 +10,7 @@
namespace rack { namespace rack {




struct ModuleWidget;
struct CableWidget; struct CableWidget;




@@ -56,19 +57,15 @@ struct ModuleAction : Action {
struct ModuleAdd : ModuleAction { struct ModuleAdd : ModuleAction {
Model *model; Model *model;
math::Vec pos; math::Vec pos;
json_t *moduleJ;
~ModuleAdd();
void setModule(ModuleWidget *mw);
void undo() override; void undo() override;
void redo() override; void redo() override;
}; };




struct ModuleRemove : ModuleAction {
Model *model;
math::Vec pos;
json_t *moduleJ;
~ModuleRemove();
void undo() override;
void redo() override;
};
struct ModuleRemove : InverseAction<ModuleAdd> {};




struct ModuleMove : ModuleAction { struct ModuleMove : ModuleAction {
@@ -116,6 +113,7 @@ struct State {
int actionIndex = 0; int actionIndex = 0;


~State(); ~State();
void clear();
void push(Action *action); void push(Action *action);
void undo(); void undo();
void redo(); void redo();


+ 1
- 3
src/app/ModuleBrowser.cpp View File

@@ -88,9 +88,7 @@ struct ModuleBox : OpaqueWidget {


// Push ModuleAdd history action // Push ModuleAdd history action
history::ModuleAdd *h = new history::ModuleAdd; history::ModuleAdd *h = new history::ModuleAdd;
h->model = moduleWidget->model;
h->moduleId = moduleWidget->module->id;
h->pos = moduleWidget->box.pos;
h->setModule(moduleWidget);
app()->history->push(h); app()->history->push(h);
} }
OpaqueWidget::onButton(e); OpaqueWidget::onButton(e);


+ 3
- 8
src/app/ModuleWidget.cpp View File

@@ -553,10 +553,7 @@ void ModuleWidget::removeAction() {


// Push ModuleRemove history action // Push ModuleRemove history action
history::ModuleRemove *moduleRemove = new history::ModuleRemove; history::ModuleRemove *moduleRemove = new history::ModuleRemove;
moduleRemove->model = model;
moduleRemove->moduleId = module->id;
moduleRemove->pos = box.pos;
moduleRemove->moduleJ = toJson();
moduleRemove->setModule(this);
complexAction->push(moduleRemove); complexAction->push(moduleRemove);


app()->history->push(complexAction); app()->history->push(complexAction);
@@ -568,8 +565,8 @@ void ModuleWidget::removeAction() {
void ModuleWidget::bypassAction() { void ModuleWidget::bypassAction() {
// Push ModuleBypass history action // Push ModuleBypass history action
history::ModuleBypass *h = new history::ModuleBypass; history::ModuleBypass *h = new history::ModuleBypass;
h->bypass = !module->bypass;
h->moduleId = module->id; h->moduleId = module->id;
h->bypass = !module->bypass;
app()->history->push(h); app()->history->push(h);
h->redo(); h->redo();
} }
@@ -586,9 +583,7 @@ void ModuleWidget::cloneAction() {


// Push ModuleAdd history action // Push ModuleAdd history action
history::ModuleAdd *h = new history::ModuleAdd; history::ModuleAdd *h = new history::ModuleAdd;
h->model = clonedModuleWidget->model;
h->moduleId = clonedModuleWidget->module->id;
h->pos = clonedModuleWidget->box.pos;
h->setModule(clonedModuleWidget);
app()->history->push(h); app()->history->push(h);
} }




+ 47
- 48
src/history.cpp View File

@@ -32,81 +32,74 @@ void ComplexAction::push(Action *action) {
} }




void ModuleAdd::undo() {
ModuleWidget *moduleWidget = app()->scene->rackWidget->getModule(moduleId);
assert(moduleWidget);
app()->scene->rackWidget->removeModule(moduleWidget);
delete moduleWidget;
ModuleAdd::~ModuleAdd() {
json_decref(moduleJ);
} }


void ModuleAdd::redo() {
ModuleWidget *moduleWidget = model->createModuleWidget();
assert(moduleWidget);
assert(moduleWidget->module);
moduleWidget->module->id = moduleId;
moduleWidget->box.pos = pos;
app()->scene->rackWidget->addModule(moduleWidget);
void ModuleAdd::setModule(ModuleWidget *mw) {
model = mw->model;
assert(mw->module);
moduleId = mw->module->id;
pos = mw->box.pos;
// ModuleAdd doesn't *really* need the state to be serialized, although ModuleRemove certainly does.
// However, creating a module may give it a nondeterministic initial state for whatever reason, so serialize anyway.
moduleJ = mw->toJson();
} }



ModuleRemove::~ModuleRemove() {
json_decref(moduleJ);
}

void ModuleRemove::undo() {
ModuleWidget *moduleWidget = model->createModuleWidget();
assert(moduleWidget);
assert(moduleWidget->module);
moduleWidget->module->id = moduleId;
moduleWidget->box.pos = pos;
moduleWidget->fromJson(moduleJ);
app()->scene->rackWidget->addModule(moduleWidget);
void ModuleAdd::undo() {
ModuleWidget *mw = app()->scene->rackWidget->getModule(moduleId);
assert(mw);
app()->scene->rackWidget->removeModule(mw);
delete mw;
} }


void ModuleRemove::redo() {
ModuleWidget *moduleWidget = app()->scene->rackWidget->getModule(moduleId);
assert(moduleWidget);
app()->scene->rackWidget->removeModule(moduleWidget);
delete moduleWidget;
void ModuleAdd::redo() {
ModuleWidget *mw = model->createModuleWidget();
assert(mw);
assert(mw->module);
mw->module->id = moduleId;
mw->box.pos = pos;
mw->fromJson(moduleJ);
app()->scene->rackWidget->addModule(mw);
} }




void ModuleMove::undo() { void ModuleMove::undo() {
ModuleWidget *moduleWidget = app()->scene->rackWidget->getModule(moduleId);
assert(moduleWidget);
moduleWidget->box.pos = oldPos;
ModuleWidget *mw = app()->scene->rackWidget->getModule(moduleId);
assert(mw);
mw->box.pos = oldPos;
} }


void ModuleMove::redo() { void ModuleMove::redo() {
ModuleWidget *moduleWidget = app()->scene->rackWidget->getModule(moduleId);
assert(moduleWidget);
moduleWidget->box.pos = newPos;
ModuleWidget *mw = app()->scene->rackWidget->getModule(moduleId);
assert(mw);
mw->box.pos = newPos;
} }




void ModuleBypass::undo() { void ModuleBypass::undo() {
ModuleWidget *moduleWidget = app()->scene->rackWidget->getModule(moduleId);
assert(moduleWidget);
moduleWidget->module->bypass = !bypass;
ModuleWidget *mw = app()->scene->rackWidget->getModule(moduleId);
assert(mw);
mw->module->bypass = !bypass;
} }


void ModuleBypass::redo() { void ModuleBypass::redo() {
ModuleWidget *moduleWidget = app()->scene->rackWidget->getModule(moduleId);
assert(moduleWidget);
moduleWidget->module->bypass = bypass;
ModuleWidget *mw = app()->scene->rackWidget->getModule(moduleId);
assert(mw);
mw->module->bypass = bypass;
} }




void ParamChange::undo() { void ParamChange::undo() {
ModuleWidget *moduleWidget = app()->scene->rackWidget->getModule(moduleId);
assert(moduleWidget);
moduleWidget->module->params[paramId].value = oldValue;
ModuleWidget *mw = app()->scene->rackWidget->getModule(moduleId);
assert(mw);
mw->module->params[paramId].value = oldValue;
} }


void ParamChange::redo() { void ParamChange::redo() {
ModuleWidget *moduleWidget = app()->scene->rackWidget->getModule(moduleId);
assert(moduleWidget);
moduleWidget->module->params[paramId].value = newValue;
ModuleWidget *mw = app()->scene->rackWidget->getModule(moduleId);
assert(mw);
mw->module->params[paramId].value = newValue;
} }




@@ -152,9 +145,15 @@ void CableAdd::redo() {




State::~State() { State::~State() {
clear();
}

void State::clear() {
for (Action *action : actions) { for (Action *action : actions) {
delete action; delete action;
} }
actions.clear();
actionIndex = 0;
} }


void State::push(Action *action) { void State::push(Action *action) {


Loading…
Cancel
Save