]> git.lizzy.rs Git - nothing.git/blob - src/game/level/level_editor/color_picker.c
(#788) Additional work
[nothing.git] / src / game / level / level_editor / color_picker.c
1 #include <stdbool.h>
2
3 #include "game/level/boxes.h"
4 #include "system/stacktrace.h"
5 #include "system/line_stream.h"
6 #include "system/log.h"
7 #include "game/camera.h"
8 #include "proto_rect.h"
9 #include "color_picker.h"
10 #include "color.h"
11
12 #define COLOR_SLIDER_HEIGHT 50.0f
13 #define COLOR_SLIDER_WIDTH 300.0f
14
15 LayerPtr color_picker_as_layer(ColorPicker *color_picker)
16 {
17     LayerPtr layer = {
18         .ptr = color_picker,
19         .type = LAYER_COLOR_PICKER
20     };
21     return layer;
22 }
23
24 ColorPicker create_color_picker_from_rgba(Color color)
25 {
26     Color color_hsla = rgba_to_hsla(color);
27     ColorPicker color_picker = {
28         .hue = {0, color_hsla.r, 360.0f},
29         .saturation = {0, color_hsla.g, 1.0f},
30         .lightness = {0, color_hsla.b, 1.0f}
31     };
32     return color_picker;
33 }
34
35 int color_picker_read_from_line_stream(ColorPicker *color_picker,
36                                        LineStream *line_stream)
37 {
38     char color[7];
39     const char *line = line_stream_next(line_stream);
40     if (line == NULL) {
41         return -1;
42     }
43
44     if (sscanf(line, "%6s", color) == EOF) {
45         log_fail("Could not read color\n");
46     }
47
48     *color_picker = create_color_picker_from_rgba(hexstr(color));
49
50     return 0;
51 }
52
53 // TODO: Color Picker doesn't have any visual indication about the current color
54 int color_picker_render(const ColorPicker *color_picker,
55                         Camera *camera)
56 {
57     trace_assert(color_picker);
58     trace_assert(camera);
59
60     /* TODO: Color Picker sliders don't have any labels */
61
62     if (slider_render(
63             &color_picker->hue,
64             camera,
65             rect(0.0f, COLOR_SLIDER_HEIGHT,
66                  COLOR_SLIDER_WIDTH, COLOR_SLIDER_HEIGHT)) < 0) {
67         return -1;
68     }
69
70     if (slider_render(
71             &color_picker->saturation,
72             camera,
73             rect(0.0f, COLOR_SLIDER_HEIGHT * 2.0f,
74                  COLOR_SLIDER_WIDTH, COLOR_SLIDER_HEIGHT)) < 0) {
75         return -1;
76     }
77
78     if (slider_render(
79             &color_picker->lightness,
80             camera,
81             rect(0.0f, COLOR_SLIDER_HEIGHT * 3.0f,
82                  COLOR_SLIDER_WIDTH, COLOR_SLIDER_HEIGHT)) < 0) {
83         return -1;
84     }
85
86
87     return 0;
88 }
89
90 // TODO: the `selected` event propagation control is cumbersome
91 int color_picker_event(ColorPicker *color_picker, const SDL_Event *event, int *selected_out)
92 {
93     trace_assert(color_picker);
94     trace_assert(event);
95
96     int selected = 0;
97
98     if (slider_event(&color_picker->hue,
99                      event,
100                      rect(0.0f, COLOR_SLIDER_HEIGHT,
101                           COLOR_SLIDER_WIDTH, COLOR_SLIDER_HEIGHT),
102                      &selected) < 0) {
103         return -1;
104     }
105
106     if (!selected) {
107         if (slider_event(&color_picker->saturation,
108                          event,
109                          rect(0.0f, COLOR_SLIDER_HEIGHT * 2.0f,
110                               COLOR_SLIDER_WIDTH, COLOR_SLIDER_HEIGHT),
111                          &selected) < 0) {
112             return -1;
113         }
114     }
115
116     if (!selected) {
117         if (slider_event(&color_picker->lightness,
118                          event,
119                          rect(0.0f, COLOR_SLIDER_HEIGHT * 3.0f,
120                               COLOR_SLIDER_WIDTH, COLOR_SLIDER_HEIGHT),
121                          &selected) < 0) {
122             return -1;
123         }
124     }
125
126     if (selected_out) {
127         *selected_out = selected;
128     }
129
130     return 0;
131 }
132
133 Color color_picker_rgba(const ColorPicker *color_picker)
134 {
135     return hsla(
136         color_picker->hue.value,
137         color_picker->saturation.value,
138         color_picker->lightness.value,
139         1.0f);
140 }