Browse Source

Implement history::State. Add history::ModuleAdd declaration.

tags/v1.0.0
Andrew Belt 5 years ago
parent
commit
2910af755f
2 changed files with 38 additions and 5 deletions
  1. +18
    -2
      include/history.hpp
  2. +20
    -3
      src/history.cpp

+ 18
- 2
include/history.hpp View File

@@ -1,5 +1,8 @@
#pragma once
#include "common.hpp"
#include "math.hpp"
#include "plugin/Model.hpp"
#include <vector>


namespace rack {
@@ -8,13 +11,26 @@ namespace history {

struct Action {
virtual ~Action() {}
virtual void commit() {}
virtual void commitInverse() {}
virtual void undo() {}
virtual void redo() {}
};


struct ModuleAdd : Action {
Model *model;
int moduleId;
math::Vec pos;
void undo() override;
void redo() override;
};



struct State {
std::vector<Action*> actions;
int actionIndex = 0;

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


+ 20
- 3
src/history.cpp View File

@@ -5,16 +5,33 @@ namespace rack {
namespace history {


void State::push(Action *action) {
State::~State() {
for (Action *action : actions) {
delete action;
}
}

void State::push(Action *action) {
for (int i = actionIndex; i < (int) actions.size(); i++) {
delete actions[i];
}
actions.resize(actionIndex);
actions.push_back(action);
actionIndex++;
}

void State::undo() {
DEBUG("undo");
if (actionIndex > 0) {
actionIndex--;
actions[actionIndex]->undo();
}
}

void State::redo() {
DEBUG("redo");
if ((int) actions.size() > actionIndex) {
actions[actionIndex]->redo();
actionIndex++;
}
}




Loading…
Cancel
Save