]> git.lizzy.rs Git - nothing.git/blob - src/game/level/level_editor/action_picker.c
46d115a025b02c4144ea73c53d34df5eaad7db4c
[nothing.git] / src / game / level / level_editor / action_picker.c
1 #include <string.h>
2
3 #include "system/stacktrace.h"
4 #include "action_picker.h"
5 #include "math/extrema.h"
6 #include "math/vec.h"
7
8 static const char *action_labels[ACTION_N] = {
9     [ACTION_NONE]        = "None",
10     [ACTION_HIDE_LABEL]  = "Hide Label",
11     [ACTION_TOGGLE_GOAL] = "Toggle Goal"
12 };
13
14 #define TEXT_SCALE vec(5.0f, 5.0f)
15 #define TEXT_COLOR COLOR_WHITE
16 #define SELECTION_COLOR COLOR_WHITE
17 #define BACKGROUND_COLOR COLOR_BLACK
18
19 void action_picker_render(const ActionPicker *picker,
20                           const Camera *camera)
21 {
22     trace_assert(picker);
23     trace_assert(camera);
24     (void) action_labels;
25
26     camera_fill_rect_screen(
27         camera,
28         picker->widget.boundary,
29         BACKGROUND_COLOR);
30
31     const float element_height = picker->widget.boundary.h / (float)ACTION_N;
32     for (size_t i = 0; i < ACTION_N; ++i) {
33         const Vec2f element_position =
34             vec_sum(
35                 vec(picker->widget.boundary.x, picker->widget.boundary.y),
36                 vec(0.0f, (float)i * element_height));
37         const Rect element_box =
38             rect_from_vecs(element_position,
39                            vec(picker->widget.boundary.w, element_height));
40
41
42         camera_render_text_screen(
43             camera,
44             action_labels[i],
45             TEXT_SCALE,
46             TEXT_COLOR,
47             element_position);
48
49         if (i == picker->action.type) {
50             camera_draw_thicc_rect_screen(
51                 camera,
52                 element_box,
53                 SELECTION_COLOR,
54                 5.0f);
55         }
56     }
57 }
58
59 void action_picker_event(ActionPicker *picker,
60                          const SDL_Event *event)
61 {
62     trace_assert(picker);
63     trace_assert(event);
64
65     switch (event->type) {
66     case SDL_MOUSEBUTTONDOWN: {
67         switch (event->button.button) {
68         case SDL_BUTTON_LEFT: {
69             const Vec2f mouse_position =
70                 vec((float)event->button.x,
71                     (float)event->button.y);
72
73             const float element_height = picker->widget.boundary.h / (float)ACTION_N;
74
75             for (ActionType i = 0; i < ACTION_N; ++i) {
76                 const Vec2f element_position =
77                     vec_sum(
78                         vec(picker->widget.boundary.x, picker->widget.boundary.y),
79                         vec(0.0f, (float)i * element_height));
80                 const Rect element_box =
81                     rect_from_vecs(element_position,
82                                    vec(picker->widget.boundary.w, element_height));
83
84                 if (rect_contains_point(element_box, mouse_position)) {
85                     picker->action.type = i;
86                     break;
87                 }
88             }
89         } break;
90         }
91     } break;
92
93     case SDL_KEYDOWN: {
94         switch (event->key.keysym.sym) {
95         case SDLK_UP: {
96             if (picker->action.type > 0) {
97                 picker->action.type--;
98             }
99         } break;
100
101         case SDLK_DOWN: {
102             if (picker->action.type < ACTION_N) {
103                 picker->action.type++;
104             }
105         } break;
106         }
107     } break;
108     }
109 }