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