summaryrefslogtreecommitdiff
path: root/undo.cpp
blob: e288784727132ee7486c47388c8154f8a12d9c0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// get ready for some MLG...

// The undo system currently works in two layers: a lower-level structless
// stack that records "actions" and a higher-level array of "entries" that
// bundle actions together. An action can either record a single change to a
// specific memory address or a shift of pointers. Entries are what the user
// sees and can contain multiple actions (i.e. adding a source changes the
// value of the string and the increment of how many sources there are).
// Entries are allowed to call functions in case there's something that can't
// be incorporated into this memory model (i.e. deallocating libav contexts
// when a layer's creation is undone), though they should be used only
// when necessary.

// These get four things pushed together: the address of what's being
// changed, what type the data is, the original data, and the type again.
// The type is encoded twice so we always know how big the data is whether
// we're undoing or redoing.
// We need to encode data that's able to go from A -> B as well as B -> A. Thus
// for the ints I encode the difference instead of just whatever the last value
// was. Strings currently just stores both.
void Action_Change_Commit(memory *Memory, void *DataAddress, void *OriginalData, void *NewData, action_change_type ActionChange)
{
    Memory->Action.Entry[Memory->Action.Index].NumberOfActions++;
    void *UndoEntry = AllocateMemory(Memory, sizeof(void *), P_UndoBuffer);
    *(ptrsize *)UndoEntry = (ptrsize)DataAddress;
    void *Data = AllocateMemory(Memory, sizeof(action_change_type), P_UndoBuffer);
    *(action_change_type *)Data = ActionChange;
    switch (ActionChange)
    {
        case action_change_u16:
        {
            uint16 OriginalValue = *(uint16 *)OriginalData;
            uint16 NewValue = *(uint16 *)NewData;
            uint16 Difference = NewValue - OriginalValue;
            void *Data = AllocateMemory(Memory, sizeof(uint16), P_UndoBuffer);
            *(uint16 *)Data = Difference;
            *(uint16 *)DataAddress = *(uint16 *)NewData;
        } break;
        case action_change_i16:
        {
            int16 OriginalValue = *(int16 *)OriginalData;
            int16 NewValue = *(int16 *)NewData;
            int16 Difference = NewValue - OriginalValue;
            void *Data = AllocateMemory(Memory, sizeof(int16), P_UndoBuffer);
            *(int16 *)Data = Difference;
            *(int16 *)DataAddress = *(int16 *)NewData;
        } break;
        case action_change_u32:
        {
            uint32 OriginalValue = *(uint32 *)OriginalData;
            uint32 NewValue = *(uint32 *)NewData;
            uint32 Difference = NewValue - OriginalValue;
            void *Data = AllocateMemory(Memory, sizeof(uint32), P_UndoBuffer);
            *(uint32 *)Data = Difference;
            *(uint32 *)DataAddress = *(uint32 *)NewData;
        } break;
        case action_change_i32:
        {
            int32 OriginalValue = *(int32 *)OriginalData;
            int32 NewValue = *(int32 *)NewData;
            int32 Difference = NewValue - OriginalValue;
            void *Data = AllocateMemory(Memory, sizeof(int32), P_UndoBuffer);
            *(int32 *)Data = Difference;
            *(int32 *)DataAddress = *(int32 *)NewData;
        } break;
        case action_change_r32:
        {
            real32 OriginalValue = *(real32 *)OriginalData;
            real32 NewValue = *(real32 *)NewData;
            real32 Difference = NewValue - OriginalValue;
            void *Data = AllocateMemory(Memory, sizeof(real32), P_UndoBuffer);
            *(real32 *)Data = Difference;
            *(real32 *)DataAddress = *(real32 *)NewData;
        } break;
        case action_change_u64:
        {
            uint64 OriginalValue = *(uint64 *)OriginalData;
            uint64 NewValue = *(uint64 *)NewData;
            uint64 Difference = NewValue - OriginalValue;
            void *Data = AllocateMemory(Memory, sizeof(uint64), P_UndoBuffer);
            *(uint64 *)Data = Difference;
            *(uint64 *)DataAddress = *(uint64 *)NewData;
        } break;
        case action_change_ptr:
        {
            ptrsize OriginalValue = *(ptrsize *)OriginalData;
            ptrsize NewValue = *(ptrsize *)NewData;
            ptrsize Difference = NewValue - OriginalValue;
            void *Data = AllocateMemory(Memory, sizeof(ptrsize), P_UndoBuffer);
            *(ptrsize *)Data = Difference;
            *(ptrsize *)DataAddress = *(ptrsize *)NewData;
        } break;
        case action_change_string:
        {
            void *Data = AllocateMemory(Memory, STRING_SIZE, P_UndoBuffer);
            CopyStrings(Data, OriginalData);
            Data = AllocateMemory(Memory, STRING_SIZE, P_UndoBuffer);
            CopyStrings(Data, NewData);
            CopyStrings(DataAddress, NewData);
        } break;
    }
    Data = AllocateMemory(Memory, sizeof(action_change_type), P_UndoBuffer);
    *(action_change_type *)Data = ActionChange;
}

// This is only called when we're certain the action is going to be taken.
void Action_Entry_Commit(memory *Memory, action_entry_type Type, char *Name)
{
    // We need to at least clear NumberOfActions in case this index is being reused.
    Memory->Action.Entry[Memory->Action.Index] = {};
    Memory->Action.Entry[Memory->Action.Index].Name = Name;
    Memory->Action.Entry[Memory->Action.Index].Type = Type;
    // Effectively deletes entries in front if we're beginning out of an undo.
    // It wouldn't be all that much more difficult to support branched undoing
    // now that I think about it... (would anyone use it though?)
    if (Memory->Action.Index != Memory->Action.NumberOfEntries) {
        Memory->Action.NumberOfEntries = Memory->Action.Index;
    }
}

void Action_Entry_SetPointer(memory *Memory, void *Data)
{
    Memory->Action.Entry[Memory->Action.Index].ExtraPointer = Data;
}

void Action_Entry_End(memory *Memory)
{
    Memory->Action.Index++;
    Memory->Action.NumberOfEntries++;
}

// TODO(fox): Maybe take the extra lines to separate the incrementing of the
// address from the variable assignment to improve legibility.

// The pointer is unwinded.
void Action_Change_Undo(memory *Memory) {
    memory_table *Table = &Memory->Slot[P_UndoBuffer];
    uint8 *LastPos = (uint8 *)Table->Address + Table->CurrentPosition;
    action_change_type *ActionType = (action_change_type *)LastPos - 1;
    uint8 *TableAddress;
    switch (*ActionType)
    {
        case action_change_u16:
        {
            uint16 *Difference = (uint16 *)ActionType - 1;
            TableAddress = (uint8 *)Difference - sizeof(ptrsize) - sizeof(action_change_type);
            void *Address = (void *)*(ptrsize *)TableAddress;
            *(uint16 *)Address -= *Difference;
        } break;
        case action_change_i16:
        {
            int16 *Difference = (int16 *)ActionType - 1;
            TableAddress = (uint8 *)Difference - sizeof(ptrsize) - sizeof(action_change_type);
            void *Address = (void *)*(ptrsize *)TableAddress;
            *(int16 *)Address -= *Difference;
        } break;
        case action_change_u32:
        {
            uint32 *Difference = (uint32 *)ActionType - 1;
            TableAddress = (uint8 *)Difference - sizeof(ptrsize) - sizeof(action_change_type);
            void *Address = (void *)*(ptrsize *)TableAddress;
            *(uint32 *)Address -= *Difference;
        } break;
        case action_change_i32:
        {
            int32 *Difference = (int32 *)ActionType - 1;
            TableAddress = (uint8 *)Difference - sizeof(ptrsize) - sizeof(action_change_type);
            void *Address = (void *)*(ptrsize *)TableAddress;
            *(int32 *)Address -= *Difference;
        } break;
        case action_change_r32:
        {
            real32 *Difference = (real32 *)ActionType - 1;
            TableAddress = (uint8 *)Difference - sizeof(ptrsize) - sizeof(action_change_type);
            void *Address = (void *)*(ptrsize *)TableAddress;
            *(real32 *)Address -= *Difference;
        } break;
        case action_change_u64:
        {
            uint64 *Difference = (uint64 *)ActionType - 1;
            TableAddress = (uint8 *)Difference - sizeof(ptrsize) - sizeof(action_change_type);
            void *Address = (void *)*(ptrsize *)TableAddress;
            *(uint64 *)Address -= *Difference;
        } break;
        case action_change_ptr:
        {
            ptrsize *Difference = (ptrsize *)ActionType - 1;
            TableAddress = (uint8 *)Difference - sizeof(ptrsize) - sizeof(action_change_type);
            void *Address = (void *)*(ptrsize *)TableAddress;
            *(ptrsize *)Address -= *Difference;
        } break;
        case action_change_string:
        {
            TableAddress = 0x0;
            Assert(0);
        } break;
    }
    Table->CurrentPosition = (TableAddress - (uint8 *)Table->Address);
}

// The pointer is rewinded.
void Action_Change_Redo(memory *Memory) {
    memory_table *Table = &Memory->Slot[P_UndoBuffer];
    uint8 *LastPos = (uint8 *)Table->Address + Table->CurrentPosition;
    void *Address = (void *)*(ptrsize *)LastPos;
    action_change_type *ActionType = (action_change_type *)(LastPos + sizeof(void *));
    uint8 *TableAddress;
    switch (*ActionType)
    {
        case action_change_u16:
        {
            uint16 *Difference = (uint16 *)(ActionType + 1);
            TableAddress = (uint8 *)Difference + sizeof(uint16) + sizeof(action_change_type);
            *(uint16 *)Address += *Difference;
        } break;
        case action_change_i16:
        {
            int16 *Difference = (int16 *)(ActionType + 1);
            TableAddress = (uint8 *)Difference + sizeof(int16) + sizeof(action_change_type);
            *(int16 *)Address += *Difference;
        } break;
        case action_change_u32:
        {
            uint32 *Difference = (uint32 *)(ActionType + 1);
            TableAddress = (uint8 *)Difference + sizeof(uint32) + sizeof(action_change_type);
            *(uint32 *)Address += *Difference;
        } break;
        case action_change_i32:
        {
            int32 *Difference = (int32 *)(ActionType + 1);
            TableAddress = (uint8 *)Difference + sizeof(int32) + sizeof(action_change_type);
            *(int32 *)Address += *Difference;
        } break;
        case action_change_r32:
        {
            real32 *Difference = (real32 *)(ActionType + 1);
            TableAddress = (uint8 *)Difference + sizeof(real32) + sizeof(action_change_type);
            *(real32 *)Address += *Difference;
        } break;
        case action_change_u64:
        {
            uint64 *Difference = (uint64 *)(ActionType + 1);
            TableAddress = (uint8 *)Difference + sizeof(uint64) + sizeof(action_change_type);
            *(uint64 *)Address += *Difference;
        } break;
        case action_change_ptr:
        {
            ptrsize *Difference = (ptrsize *)(ActionType + 1);
            TableAddress = (uint8 *)Difference + sizeof(ptrsize) + sizeof(action_change_type);
            *(ptrsize *)Address += *Difference;
        } break;
        case action_change_string:
        {
            TableAddress = 0x0;
            Assert(0);
        } break;
    }
    Table->CurrentPosition = (TableAddress - (uint8 *)Table->Address);
}

void Action_Undo(memory *Memory) {
    if (Memory->Action.Index != 0) {
        Memory->Action.Index--;
        action_entry Entry = Memory->Action.Entry[Memory->Action.Index];
        switch (Entry.Type)
        {
            case action_entry_layerinit:
            {
                AV_Dealloc((av_info *)*(ptrsize *)Entry.ExtraPointer);
                *(ptrsize *)Entry.ExtraPointer = 0x0;   // what actually dereferences the pointer
            } break;
            case action_entry_default:
            {
            } break;
        }
        for (int i = 0; i < Entry.NumberOfActions; i++)
            Action_Change_Undo(Memory);
    }
}

void Action_Redo(memory *Memory) {
    if (Memory->Action.Index != Memory->Action.NumberOfEntries) {
        action_entry Entry = Memory->Action.Entry[Memory->Action.Index];
        switch (Entry.Type)
        {
            case action_entry_layerinit:
            {
            } break;
            case action_entry_default:
            {
            } break;
        }
        for (int i = 0; i < Entry.NumberOfActions; i++)
            Action_Change_Redo(Memory);
        Memory->Action.Index++;
    }
}