4 #include "game/level/boxes.h"
5 #include "system/stacktrace.h"
6 #include "system/line_stream.h"
7 #include "system/log.h"
8 #include "game/camera.h"
9 #include "color_picker.h"
11 #include "undo_history.h"
13 // TODO(#1021): ColorPicker doesn't have any padding between the sliders
14 #define COLOR_SLIDER_HEIGHT 60.0f
15 #define COLOR_PICKER_WIDTH 300.0f
16 #define COLOR_PICKER_HEIGHT (COLOR_SLIDER_HEIGHT * COLOR_SLIDER_N)
17 #define COLOR_PICKER_REFERENCE 1920.0f
18 #define COLOR_PICKER_HW_RATIO (COLOR_PICKER_HEIGHT/ COLOR_PICKER_WIDTH)
19 #define COLOR_PICKER_WR_RATIO (COLOR_PICKER_WIDTH / COLOR_PICKER_REFERENCE)
21 const char *slider_labels[COLOR_SLIDER_N] = {
27 ColorPicker create_color_picker_from_rgba(Color color)
29 Color color_hsla = rgba_to_hsla(color);
30 ColorPicker color_picker = {
32 {0, color_hsla.r, 360.0f},
33 {0, color_hsla.g, 1.0f},
34 {0, color_hsla.b, 1.0f}
40 int color_picker_read_from_line_stream(ColorPicker *color_picker,
41 LineStream *line_stream)
44 const char *line = line_stream_next(line_stream);
49 if (sscanf(line, "%6s", color) == EOF) {
50 log_fail("Could not read color\n");
53 *color_picker = create_color_picker_from_rgba(hexstr(color));
58 int color_picker_render(const ColorPicker *color_picker,
61 trace_assert(color_picker);
64 const Rect viewport = camera_view_port_screen(camera);
65 const Rect boundary = rect(
67 viewport.w * COLOR_PICKER_WR_RATIO,
68 viewport.w * COLOR_PICKER_WR_RATIO * COLOR_PICKER_HW_RATIO);
70 const float color_slider_height =
71 boundary.h / (COLOR_SLIDER_N + 1.0f);
73 if (camera_fill_rect_screen(
75 rect(boundary.x, boundary.y,
76 boundary.w, color_slider_height),
77 color_picker_rgba(color_picker)) < 0) {
81 for (ColorPickerSlider index = 0; index < COLOR_SLIDER_N; ++index) {
82 const Rect slider_rect =
84 boundary.y + color_slider_height * (float) (index + 1),
85 boundary.w, color_slider_height);
86 const float font_scale = boundary.w / COLOR_PICKER_WIDTH;
87 const Point label_size = vec(2.5f * font_scale, 2.5f * font_scale);
90 &color_picker->sliders[index],
96 if (camera_render_text_screen(
101 vec(slider_rect.x + boundary.w,
102 slider_rect.y + color_slider_height * 0.5f - label_size.y * (float) FONT_CHAR_HEIGHT * 0.5f)) < 0) {
110 // TODO(#932): the `selected` event propagation control is cumbersome
111 int color_picker_event(ColorPicker *color_picker,
112 const SDL_Event *event,
113 const Camera *camera,
116 trace_assert(color_picker);
118 trace_assert(camera);
122 const Rect viewport = camera_view_port_screen(camera);
123 const Rect boundary = rect(
125 viewport.w * COLOR_PICKER_WR_RATIO,
126 viewport.w * COLOR_PICKER_WR_RATIO * COLOR_PICKER_HW_RATIO);
128 const float color_slider_height =
129 boundary.h / (COLOR_SLIDER_N + 1.0f);
131 for (ColorPickerSlider index = 0;
132 !selected && index < COLOR_SLIDER_N;
135 &color_picker->sliders[index],
138 boundary.y + color_slider_height * (float) (index + 1),
139 boundary.w, color_slider_height),
146 *selected_out = selected;
152 Color color_picker_rgba(const ColorPicker *color_picker)
155 color_picker->sliders[COLOR_SLIDER_HUE].value,
156 color_picker->sliders[COLOR_SLIDER_SAT].value,
157 color_picker->sliders[COLOR_SLIDER_LIT].value,