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 "color_picker.h"
11 #define COLOR_SLIDER_HEIGHT 50.0f
12 #define COLOR_SLIDER_WIDTH 300.0f
13 #define COLOR_SLIDER_PADDING_BOTTOM 10.0f
15 const char *slider_labels[COLOR_SLIDER_N] = {
21 LayerPtr color_picker_as_layer(ColorPicker *color_picker)
25 .type = LAYER_COLOR_PICKER
30 ColorPicker create_color_picker_from_rgba(Color color)
32 Color color_hsla = rgba_to_hsla(color);
33 ColorPicker color_picker = {
35 {0, color_hsla.r, 360.0f},
36 {0, color_hsla.g, 1.0f},
37 {0, color_hsla.b, 1.0f}
43 int color_picker_read_from_line_stream(ColorPicker *color_picker,
44 LineStream *line_stream)
47 const char *line = line_stream_next(line_stream);
52 if (sscanf(line, "%6s", color) == EOF) {
53 log_fail("Could not read color\n");
56 *color_picker = create_color_picker_from_rgba(hexstr(color));
61 int color_picker_render(const ColorPicker *color_picker,
64 trace_assert(color_picker);
67 if (camera_fill_rect_screen(
69 rect(0.0f, 0.0f, COLOR_SLIDER_WIDTH, COLOR_SLIDER_HEIGHT),
70 color_picker_rgba(color_picker)) < 0) {
74 for (ColorPickerSlider index = 0; index < COLOR_SLIDER_N; ++index) {
75 const Rect slider_rect =
76 rect(0.0f, (COLOR_SLIDER_HEIGHT + COLOR_SLIDER_PADDING_BOTTOM) * (float) (index + 1),
77 COLOR_SLIDER_WIDTH, COLOR_SLIDER_HEIGHT);
78 const Point label_size = vec(2.5f, 2.5f);
81 &color_picker->sliders[index],
87 if (camera_render_text_screen(
92 vec(slider_rect.x + COLOR_SLIDER_WIDTH,
93 slider_rect.y + COLOR_SLIDER_HEIGHT * 0.5f - label_size.y * (float) FONT_CHAR_HEIGHT * 0.5f)) < 0) {
101 // TODO(#932): the `selected` event propagation control is cumbersome
102 int color_picker_event(ColorPicker *color_picker, const SDL_Event *event, int *selected_out)
104 trace_assert(color_picker);
109 for (ColorPickerSlider index = 0;
110 !selected && index < COLOR_SLIDER_N;
113 &color_picker->sliders[index],
115 rect(0.0f, (COLOR_SLIDER_HEIGHT + COLOR_SLIDER_PADDING_BOTTOM) * (float) (index + 1),
116 COLOR_SLIDER_WIDTH, COLOR_SLIDER_HEIGHT),
123 *selected_out = selected;
129 Color color_picker_rgba(const ColorPicker *color_picker)
132 color_picker->sliders[COLOR_SLIDER_HUE].value,
133 color_picker->sliders[COLOR_SLIDER_SAT].value,
134 color_picker->sliders[COLOR_SLIDER_LIT].value,