add undo to instrument editor (check for diffs on the current DivInstrument in insEdit, record them in a stack)

This commit is contained in:
Adam Lederer 2024-08-04 21:07:08 -07:00 committed by tildearrow
parent a318508b40
commit 5c9fd69ac1
8 changed files with 307 additions and 55 deletions

View file

@ -23,8 +23,10 @@
#include "dataErrors.h"
#include "../ta-utils.h"
#include "../pch.h"
#include "../fixedQueue.h"
struct DivSong;
struct DivInstrument;
// NOTICE!
// before adding new instrument types to this struct, please ask me first.
@ -860,8 +862,7 @@ struct DivInstrumentSID2 {
noiseMode(0) {}
};
struct DivInstrument {
String name;
struct DivInstrumentPOD {
DivInstrumentType type;
DivInstrumentFM fm;
DivInstrumentSTD std;
@ -880,6 +881,63 @@ struct DivInstrument {
DivInstrumentPowerNoise powernoise;
DivInstrumentSID2 sid2;
DivInstrumentPOD() :
type(DIV_INS_FM) {
}
};
struct MemPatch {
MemPatch() :
data(nullptr)
, offset(0)
, size(0) {
}
~MemPatch() {
if (data) {
free(data);
}
}
void clear();
bool calcDiff(const void* pre, const void* post, size_t size);
void applyAndReverse(void* target, size_t inputSize);
bool isValid() const { return size > 0; }
uint8_t* data;
size_t offset;
size_t size;
};
struct DivInstrumentUndoStep {
DivInstrumentUndoStep() :
name(""),
nameValid(false),
processTime(0) {
}
MemPatch podPatch;
String name;
bool nameValid;
size_t processTime;
void clear();
void applyAndReverse(DivInstrument* target);
bool makeUndoPatch(size_t processTime_, const DivInstrument* pre, const DivInstrument* post);
};
struct DivInstrument : DivInstrumentPOD {
String name;
/**
* undo stuff
*/
FixedQueue<DivInstrumentUndoStep*, 128> undoHist;
FixedQueue<DivInstrumentUndoStep*, 128> redoHist;
void recordUndoStepIfChanged(size_t processTime, const DivInstrument* old);
int undo();
int redo();
/**
* these are internal functions.
*/
@ -964,9 +1022,11 @@ struct DivInstrument {
* @return whether it was successful.
*/
bool saveDMP(const char* path);
DivInstrument():
name(""),
type(DIV_INS_FM) {
DivInstrument() :
name("") {
// clear and construct DivInstrumentPOD so it doesn't have any garbage in the padding
memset((DivInstrumentPOD*)this, 0, sizeof(DivInstrumentPOD));
new ((DivInstrumentPOD*)this) DivInstrumentPOD;
}
};
#endif