]> git.lizzy.rs Git - nothing.git/blob - src/game/level/level_editor/layer_picker.c
Remove TODO(#964)
[nothing.git] / src / game / level / level_editor / layer_picker.c
1 #include <stdbool.h>
2 #include <string.h>
3
4 #include <SDL.h>
5
6 #include "system/stacktrace.h"
7 #include "layer_picker.h"
8 #include "color.h"
9 #include "game/camera.h"
10 #include "math/rect.h"
11 #include "math/extrema.h"
12 #include "game/sprite_font.h"
13
14 #define LAYER_TITLE_PADDING 15.0f
15 #define LAYER_TITLE_SIZE 3.0f
16 #define LAYER_SELECTED_OFFSET 15.0f
17
18 static const Color LAYER_CELL_BACKGROUND_COLORS[LAYER_PICKER_N] = {
19     {1.0f, 0.0f, 0.0f, 1.0f},
20     {0.0f, 1.0f, 0.0f, 1.0f},
21     {0.0f, 0.0f, 1.0f, 1.0f},
22     {1.0f, 1.0f, 1.0f, 1.0f},
23     {1.0f, 0.2f, 0.6f, 1.0f},
24     {0.6f, 0.6f, 1.0f, 1.0f},
25     {0.2f, 1.0f, 0.6f, 1.0f},
26     {0.2f, 0.6f, 1.0f, 1.0f},
27     {0.2f, 1.0f, 0.6f, 1.0f},
28 };
29
30 static const char *LAYER_CELL_TITLES[LAYER_PICKER_N] = {
31     "Background",              // LAYER_PICKER_BACKGROUND
32     "Player",                  // LAYER_PICKER_PLAYER
33     "Platforms",               // LAYER_PICKER_PLATFORMS,
34     "Goals",                   // LAYER_PICKER_GOALS
35     "Lava",                    // LAYER_PICKER_LAVA
36     "Back Platforms",          // LAYER_PICKER_BACK_PLATFORMS,
37     "Boxes",                   // LAYER_PICKER_BOXES = 0,
38     "Labels",                  // LAYER_PICKER_LABELS
39     "Regions",                 // LAYER_PICKER_REGIONS
40 };
41
42 inline static float layer_picker_max_width(void)
43 {
44     size_t max = 0;
45
46     for (size_t i = 0; i < LAYER_PICKER_N; ++i) {
47         max = max_size_t(max, strlen(LAYER_CELL_TITLES[i]));
48     }
49
50     return (float) max * FONT_CHAR_WIDTH * LAYER_TITLE_SIZE + LAYER_TITLE_PADDING * 2.0f;
51 }
52
53 #define LAYER_CELL_WIDTH layer_picker_max_width()
54 #define LAYER_CELL_HEIGHT (LAYER_TITLE_SIZE * FONT_CHAR_HEIGHT + LAYER_TITLE_PADDING * 2.0f)
55
56 inline static Vec layer_picker_position(const Camera *camera)
57 {
58     trace_assert(camera);
59
60     const Rect viewport = camera_view_port_screen(camera);
61
62     Vec position = {
63         .x = 0.0f,
64         .y = viewport.h * 0.5f - LAYER_CELL_HEIGHT * LAYER_PICKER_N * 0.5f
65     };
66
67     return position;
68 }
69
70 int layer_picker_render(const LayerPicker *layer_picker,
71                         Camera *camera)
72 {
73     trace_assert(layer_picker);
74     trace_assert(camera);
75
76     for (size_t i = 0; i < LAYER_PICKER_N; ++i) {
77         Vec position = layer_picker_position(camera);
78         Color color = LAYER_CELL_BACKGROUND_COLORS[i];
79
80         if (*layer_picker == i) {
81             position.x += LAYER_SELECTED_OFFSET;
82         } else {
83             color.a *= 0.70f;
84         }
85
86         if (camera_fill_rect_screen(
87                 camera,
88                 rect(
89                     position.x,
90                     LAYER_CELL_HEIGHT * (float) i + position.y,
91                     LAYER_CELL_WIDTH,
92                     LAYER_CELL_HEIGHT),
93                 color) < 0) {
94             return -1;
95         }
96
97         if (camera_render_text_screen(
98                 camera,
99                 LAYER_CELL_TITLES[i],
100                 vec(LAYER_TITLE_SIZE, LAYER_TITLE_SIZE),
101                 color_invert(color),
102                 vec(position.x + LAYER_TITLE_PADDING,
103                     LAYER_CELL_HEIGHT * (float) i + position.y + LAYER_TITLE_PADDING)) < 0) {
104             return -1;
105         }
106     }
107
108     return 0;
109 }
110
111 int layer_picker_event(LayerPicker *layer_picker,
112                        const SDL_Event *event,
113                        const Camera *camera,
114                        bool *selected)
115 {
116     trace_assert(layer_picker);
117     trace_assert(event);
118     trace_assert(camera);
119
120     const Vec position = layer_picker_position(camera);
121
122     switch (event->type) {
123     case SDL_MOUSEBUTTONDOWN: {
124         for (size_t i = 0; i < LAYER_PICKER_N; ++i) {
125             const Rect cell = rect(position.x,
126                                    LAYER_CELL_HEIGHT * (float) i + position.y,
127                                    LAYER_CELL_WIDTH,
128                                    LAYER_CELL_HEIGHT);
129             if (rect_contains_point(cell, vec((float) event->button.x, (float) event->button.y))) {
130                 *layer_picker = i;
131                 *selected = true;
132                 return 0;
133             }
134         }
135     } break;
136     }
137
138     return 0;
139 }