]> git.lizzy.rs Git - nothing.git/blob - src/game/level/level_editor/undo_history.h
Merge pull request #1036 from NilsIrl/master
[nothing.git] / src / game / level / level_editor / undo_history.h
1 #ifndef UNDO_HISTORY_H_
2 #define UNDO_HISTORY_H_
3
4 #include "layer.h"
5
6 // TODO(#1030): UndoHistory context size is fixed
7 //   If we treated it as a stack we could store
8 //   the elements with variable size.
9 #define CONTEXT_SIZE 256
10
11 #define ASSERT_CONTEXT_SIZE(context)               \
12     trace_assert(sizeof(context) <= CONTEXT_SIZE)
13
14 typedef enum {
15     UNDO_ADD,
16     UNDO_DELETE,
17     UNDO_UPDATE
18 } UndoType;
19
20 typedef struct {
21     char data[CONTEXT_SIZE];
22 } Context;
23
24 typedef void (*RevertAction)(void *layer, Context context);
25
26 typedef struct {
27     void *layer;
28     Context context;
29     RevertAction revert;
30 } Action;
31
32 static inline
33 Action create_action(void *layer, RevertAction revert,
34                      void *context_data,
35                      size_t context_data_size)
36 {
37     trace_assert(layer);
38     trace_assert(revert);
39     trace_assert(context_data_size < CONTEXT_SIZE);
40
41     Action action = {
42         .layer = layer,
43         .revert = revert
44     };
45
46     if (context_data) {
47         memcpy(action.context.data, context_data, context_data_size);
48     }
49
50     return action;
51 }
52
53 typedef struct UndoHistory UndoHistory;
54
55 UndoHistory *create_undo_history(void);
56 void destroy_undo_history(UndoHistory *undo_history);
57
58 void undo_history_push(UndoHistory *undo_history, Action action);
59 void undo_history_pop(UndoHistory *undo_history);
60
61 #endif  // UNDO_HISTORY_H_