3
3
#include " graphics/boilerplate.h"
4
4
#include " graphics/view/view.h"
5
5
#include " game/game.h"
6
+ #include " generic_button.h"
7
+ #include " image_button.h"
6
8
7
9
void button_none (int param1, int param2) {
8
10
}
@@ -51,3 +53,63 @@ void button_border_draw(int x, int y, int width_pixels, int height_pixels, bool
51
53
}
52
54
}
53
55
}
56
+
57
+ namespace ui {
58
+ struct state {
59
+ vec2i offset;
60
+ std::vector<generic_button> buttons;
61
+ std::vector<image_button> img_buttons;
62
+ };
63
+
64
+ state g_state;
65
+ generic_button dummy;
66
+ }
67
+
68
+ void ui::begin_window (vec2i offset) {
69
+ g_state.offset = offset;
70
+ g_state.buttons .clear ();
71
+ }
72
+
73
+ bool ui::handle_mouse (const mouse *m) {
74
+ bool handle = false ;
75
+ int tmp_btn = 0 ;
76
+ handle |= !!generic_buttons_handle_mouse (m, g_state.offset , g_state.buttons , tmp_btn);
77
+ handle |= image_buttons_handle_mouse (m, g_state.offset , g_state.img_buttons , tmp_btn);
78
+
79
+ return handle;
80
+ }
81
+
82
+ int ui::button_hover (const mouse *m) {
83
+ for (auto &btn : g_state.buttons ) {
84
+ if (is_button_hover (btn, g_state.offset )) {
85
+ return (std::distance (&g_state.buttons .front (), &btn) + 1 );
86
+ }
87
+ }
88
+
89
+ return 0 ;
90
+ }
91
+
92
+ generic_button &ui::button (pcstr label, vec2i pos, vec2i size) {
93
+ const vec2i offset = g_state.offset ;
94
+
95
+ g_state.buttons .push_back ({pos.x , pos.y , size.x + 4 , size.y + 4 , button_none, button_none, 0 , 0 });
96
+ int focused = is_button_hover (g_state.buttons .back (), offset);
97
+
98
+ button_border_draw (offset.x + pos.x , offset.y + pos.y , size.x , size.y , focused ? 1 : 0 );
99
+ text_draw_centered ((uint8_t *)label, offset.x + pos.x + 1 , offset.y + pos.y + 4 , 20 , FONT_NORMAL_BLACK_ON_LIGHT, 0 );
100
+
101
+ return g_state.buttons .back ();
102
+ }
103
+
104
+ generic_button &ui::button (uint32_t id) {
105
+ return (id < g_state.buttons .size ()) ? g_state.buttons [id] : dummy;
106
+ }
107
+
108
+ image_button &ui::img_button (uint32_t group, uint32_t id, vec2i pos, vec2i size, int offset) {
109
+ const vec2i img_offset = g_state.offset ;
110
+
111
+ g_state.img_buttons .push_back ({pos.x , pos.y , size.x + 4 , size.y + 4 , IB_NORMAL, group, id, offset, button_none, button_none, 0 , 0 , true });
112
+ image_buttons_draw (img_offset + pos, g_state.img_buttons .back ());
113
+
114
+ return g_state.img_buttons .back ();
115
+ }
0 commit comments