]> git.lizzy.rs Git - nothing.git/blob - src/game/level/level_editor/undo_history.h
Add TODO(#1035)
[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 struct {
15     char data[CONTEXT_SIZE];
16 } Context;
17
18 typedef void (*RevertAction)(void *layer, Context context);
19
20 typedef struct {
21     void *layer;
22     Context context;
23     RevertAction revert;
24 } Action;
25
26 static inline
27 Action create_action(void *layer, RevertAction revert,
28                      void *context_data,
29                      size_t context_data_size)
30 {
31     trace_assert(layer);
32     trace_assert(revert);
33     trace_assert(context_data_size < CONTEXT_SIZE);
34
35     Action action = {
36         .layer = layer,
37         .revert = revert
38     };
39
40     if (context_data) {
41         memcpy(action.context.data, context_data, context_data_size);
42     }
43
44     return action;
45 }
46
47 typedef struct UndoHistory UndoHistory;
48
49 UndoHistory *create_undo_history(void);
50 void destroy_undo_history(UndoHistory *undo_history);
51
52 void undo_history_push(UndoHistory *undo_history, Action action);
53 void undo_history_pop(UndoHistory *undo_history);
54
55 #endif  // UNDO_HISTORY_H_