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