5 #include "system/stacktrace.h"
6 #include "layer_picker.h"
8 #include "game/camera.h"
10 #include "math/extrema.h"
11 #include "game/sprite_font.h"
13 #define LAYER_TITLE_PADDING 15.0f
14 #define LAYER_TITLE_SIZE 3.0f
15 #define LAYER_SELECTED_OFFSET 15.0f
17 static const Color LAYER_CELL_BACKGROUND_COLORS[LAYER_PICKER_N] = {
18 {1.0f, 0.0f, 0.0f, 1.0f}, // LAYER_PICKER_BOXES = 0,
19 {0.0f, 1.0f, 0.0f, 1.0f}, // LAYER_PICKER_PLATFORMS,
20 {0.0f, 0.0f, 1.0f, 1.0f}, // LAYER_PICKER_BACK_PLATFORMS,
21 {1.0f, 1.0f, 1.0f, 1.0f}, // LAYER_PICKER_GOALS
22 {1.0f, 0.2f, 0.6f, 1.0f}, // LAYER_PICKER_PLAYER
23 {0.6f, 0.6f, 1.0f, 1.0f}, // LAYER_PICKER_LAVA
24 {0.2f, 1.0f, 0.6f, 1.0f}, // LAYER_PICKER_REGIONS
25 {0.2f, 0.6f, 1.0f, 1.0f}, // LAYER_PICKER_BACKGROUND
26 {0.2f, 1.0f, 0.6f, 1.0f}, // LAYER_PICKER_LABELS
29 static const char *LAYER_CELL_TITLES[LAYER_PICKER_N] = {
30 "Boxes", // LAYER_PICKER_BOXES = 0,
31 "Platforms", // LAYER_PICKER_PLATFORMS,
32 "Back Platforms", // LAYER_PICKER_BACK_PLATFORMS,
33 "Goals", // LAYER_PICKER_GOALS
34 "Player", // LAYER_PICKER_PLAYER
35 "Lava", // LAYER_PICKER_LAVA
36 "Regions", // LAYER_PICKER_REGIONS
37 "Background", // LAYER_PICKER_BACKGROUND
38 "Labels", // LAYER_PICKER_LABELS
41 inline static float layer_picker_max_width(void)
45 for (size_t i = 0; i < LAYER_PICKER_N; ++i) {
46 max = max_size_t(max, strlen(LAYER_CELL_TITLES[i]));
49 return (float) max * FONT_CHAR_WIDTH * LAYER_TITLE_SIZE + LAYER_TITLE_PADDING * 2.0f;
52 #define LAYER_CELL_WIDTH layer_picker_max_width()
53 #define LAYER_CELL_HEIGHT (LAYER_TITLE_SIZE * FONT_CHAR_HEIGHT + LAYER_TITLE_PADDING * 2.0f)
55 inline static Vec layer_picker_position(const Camera *camera)
59 const Rect viewport = camera_view_port_screen(camera);
63 .y = viewport.h * 0.5f - LAYER_CELL_HEIGHT * LAYER_PICKER_N * 0.5f
69 int layer_picker_render(const LayerPicker *layer_picker,
72 trace_assert(layer_picker);
75 for (size_t i = 0; i < LAYER_PICKER_N; ++i) {
76 Vec position = layer_picker_position(camera);
77 Color color = LAYER_CELL_BACKGROUND_COLORS[i];
79 if (*layer_picker == i) {
80 position.x += LAYER_SELECTED_OFFSET;
85 if (camera_fill_rect_screen(
89 LAYER_CELL_HEIGHT * (float) i + position.y,
96 if (camera_render_text_screen(
99 vec(LAYER_TITLE_SIZE, LAYER_TITLE_SIZE),
101 vec(position.x + LAYER_TITLE_PADDING,
102 LAYER_CELL_HEIGHT * (float) i + position.y + LAYER_TITLE_PADDING)) < 0) {
110 int layer_picker_event(LayerPicker *layer_picker,
111 const SDL_Event *event,
112 const Camera *camera,
115 trace_assert(layer_picker);
117 trace_assert(camera);
119 const Vec position = layer_picker_position(camera);
121 switch (event->type) {
122 case SDL_MOUSEBUTTONDOWN: {
123 for (size_t i = 0; i < LAYER_PICKER_N; ++i) {
124 const Rect cell = rect(position.x,
125 LAYER_CELL_HEIGHT * (float) i + position.y,
128 if (rect_contains_point(cell, vec((float) event->button.x, (float) event->button.y))) {