]> git.lizzy.rs Git - nothing.git/blob - src/game/level/level_editor/background_layer.c
Implement chop_background_layer
[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 BackgroundLayer chop_background_layer(String *input)
35 {
36     String line = trim(chop_by_delim(input, '\n'));
37     return create_background_layer(hexs(line));
38 }
39
40 int background_layer_render(BackgroundLayer *layer,
41                             const Camera *camera,
42                             int active)
43 {
44     trace_assert(layer);
45     trace_assert(camera);
46
47     if (active) {
48         return color_picker_render(
49             &layer->color_picker,
50             camera);
51     }
52
53     return 0;
54 }
55
56 typedef struct {
57     BackgroundLayer *layer;
58     Color color;
59 } BackgroundUndoContext;
60
61 static
62 void background_undo_color(void *context, size_t context_size)
63 {
64     trace_assert(context);
65     trace_assert(sizeof(BackgroundUndoContext) == context_size);
66
67     BackgroundUndoContext *undo_context = context;
68     BackgroundLayer *background_layer = undo_context->layer;
69
70     background_layer->color_picker = create_color_picker_from_rgba(undo_context->color);
71 }
72
73 int background_layer_event(BackgroundLayer *layer,
74                            const SDL_Event *event,
75                            const Camera *camera,
76                            UndoHistory *undo_history)
77 {
78     trace_assert(layer);
79     trace_assert(event);
80     trace_assert(camera);
81     trace_assert(undo_history);
82
83     int selected = 0;
84
85     if (color_picker_event(
86             &layer->color_picker,
87             event,
88             camera,
89             &selected) < 0) {
90         return -1;
91     }
92
93     if (selected && !color_picker_drag(&layer->color_picker)) {
94         BackgroundUndoContext context = {
95             .layer = layer,
96             .color = layer->prev_color
97         };
98
99         undo_history_push(
100             undo_history,
101             background_undo_color,
102             &context, sizeof(context));
103         layer->prev_color = color_picker_rgba(&layer->color_picker);
104     }
105
106     return 0;
107 }
108
109 int background_layer_dump_stream(BackgroundLayer *layer,
110                                  FILE *stream)
111 {
112     trace_assert(layer);
113     trace_assert(stream);
114
115     color_hex_to_stream(
116         color_picker_rgba(&layer->color_picker),
117         stream);
118
119     return fprintf(stream, "\n");
120 }