]> git.lizzy.rs Git - nothing.git/blob - src/game/level/level_editor/background_layer.c
158eff00a673ecf2c7ac854427e48a51cef40063
[nothing.git] / src / game / level / level_editor / background_layer.c
1 #include <string.h>
2 #include <SDL.h>
3
4 #include "system/stacktrace.h"
5 #include "game/camera.h"
6 #include "math/rect.h"
7 #include "color.h"
8 #include "background_layer.h"
9 #include "undo_history.h"
10
11 BackgroundLayer create_background_layer(Color color)
12 {
13     BackgroundLayer layer = {
14         .color_picker = create_color_picker_from_rgba(color),
15         .prev_color = color
16     };
17     return layer;
18 }
19
20 int background_layer_read_from_line_stream(BackgroundLayer *layer,
21                                            LineStream *line_stream)
22 {
23     if (color_picker_read_from_line_stream(
24             &layer->color_picker,
25             line_stream) < 0) {
26         return -1;
27     }
28
29     layer->prev_color = color_picker_rgba(&layer->color_picker);
30
31     return 0;
32 }
33
34 int background_layer_render(BackgroundLayer *layer,
35                             const Camera *camera,
36                             int active)
37 {
38     trace_assert(layer);
39     trace_assert(camera);
40
41     if (active) {
42         return color_picker_render(
43             &layer->color_picker,
44             camera);
45     }
46
47     return 0;
48 }
49
50 typedef struct {
51     BackgroundLayer *layer;
52     Color color;
53 } BackgroundUndoContext;
54
55 static
56 void background_undo_color(void *context, size_t context_size)
57 {
58     trace_assert(context);
59     trace_assert(sizeof(BackgroundUndoContext) == context_size);
60
61     BackgroundUndoContext *undo_context = context;
62     BackgroundLayer *background_layer = undo_context->layer;
63
64     background_layer->color_picker = create_color_picker_from_rgba(undo_context->color);
65 }
66
67 int background_layer_event(BackgroundLayer *layer,
68                            const SDL_Event *event,
69                            const Camera *camera,
70                            UndoHistory *undo_history)
71 {
72     trace_assert(layer);
73     trace_assert(event);
74     trace_assert(camera);
75     trace_assert(undo_history);
76
77     int selected = 0;
78
79     if (color_picker_event(
80             &layer->color_picker,
81             event,
82             camera,
83             &selected) < 0) {
84         return -1;
85     }
86
87     if (selected && !color_picker_drag(&layer->color_picker)) {
88         BackgroundUndoContext context = {
89             .layer = layer,
90             .color = layer->prev_color
91         };
92
93         undo_history_push(
94             undo_history,
95             background_undo_color,
96             &context, sizeof(context));
97         layer->prev_color = color_picker_rgba(&layer->color_picker);
98     }
99
100     return 0;
101 }
102
103 int background_layer_dump_stream(BackgroundLayer *layer,
104                                  FILE *stream)
105 {
106     trace_assert(layer);
107     trace_assert(stream);
108
109     color_hex_to_stream(
110         color_picker_rgba(&layer->color_picker),
111         stream);
112
113     return fprintf(stream, "\n");
114 }