]> git.lizzy.rs Git - nothing.git/blob - src/game/level/level_editor/undo_history.c
Merge pull request #998 from tsoding/824
[nothing.git] / src / game / level / level_editor / undo_history.c
1 #include <stdlib.h>
2 #include <stdio.h>
3
4 #include <SDL.h>
5
6 #include "system/nth_alloc.h"
7 #include "system/lt.h"
8 #include "dynarray.h"
9 #include "system/stacktrace.h"
10 #include "undo_history.h"
11
12 struct UndoHistory
13 {
14     Lt *lt;
15     Dynarray *actions;
16 };
17
18 UndoHistory *create_undo_history(void)
19 {
20     Lt *lt = create_lt();
21
22     UndoHistory *undo_history = PUSH_LT(
23         lt,
24         nth_calloc(1, sizeof(UndoHistory)),
25         free);
26     undo_history->lt = lt;
27
28     undo_history->actions = PUSH_LT(
29         lt,
30         create_dynarray(sizeof(Action)),
31         destroy_dynarray);
32
33     return undo_history;
34 }
35
36 void destroy_undo_history(UndoHistory *undo_history)
37 {
38     trace_assert(undo_history);
39     RETURN_LT0(undo_history->lt);
40 }
41
42 void undo_history_push(UndoHistory *undo_history, Action action)
43 {
44     trace_assert(undo_history);
45     dynarray_push(undo_history->actions, &action);
46 }
47
48 void undo_history_pop(UndoHistory *undo_history)
49 {
50     trace_assert(undo_history);
51
52     if (dynarray_count(undo_history->actions) > 0) {
53         Action action;
54         dynarray_pop(undo_history->actions, &action);
55         action.revert(action.layer, action.context);
56     }
57 }