]> git.lizzy.rs Git - nothing.git/blob - src/game/level/level_editor/background_layer.c
Merge pull request #1040 from tsoding/1030
[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 static
52 void background_undo_color(void *layer, void *context, size_t context_size)
53 {
54     trace_assert(layer);
55     trace_assert(context);
56     trace_assert(sizeof(Color) == context_size);
57
58     BackgroundLayer *background_layer = layer;
59     Color *color = context;
60
61     background_layer->color_picker = create_color_picker_from_rgba(*color);
62 }
63
64 int background_layer_event(BackgroundLayer *layer,
65                            const SDL_Event *event,
66                            const Camera *camera,
67                            UndoHistory *undo_history)
68 {
69     trace_assert(layer);
70     trace_assert(event);
71     trace_assert(camera);
72     trace_assert(undo_history);
73
74     int selected = 0;
75
76     if (color_picker_event(
77             &layer->color_picker,
78             event,
79             camera,
80             &selected) < 0) {
81         return -1;
82     }
83
84     if (selected && !color_picker_drag(&layer->color_picker)) {
85         undo_history_push(
86             undo_history,
87             layer,
88             background_undo_color,
89             &layer->prev_color, sizeof(layer->prev_color));
90         layer->prev_color = color_picker_rgba(&layer->color_picker);
91     }
92
93     return 0;
94 }
95
96 int background_layer_dump_stream(BackgroundLayer *layer,
97                                  FILE *stream)
98 {
99     trace_assert(layer);
100     trace_assert(stream);
101
102     color_hex_to_stream(
103         color_picker_rgba(&layer->color_picker),
104         stream);
105
106     return fprintf(stream, "\n");;
107 }