Skip to content

Commit 0c61bca

Browse files
author
s.kushnirenko
committed
ui: unified using buttons in user interface
1 parent 1c4549b commit 0c61bca

File tree

4 files changed

+51
-31
lines changed

4 files changed

+51
-31
lines changed

src/graphics/elements/generic_button.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
#include "input/mouse.h"
44

5+
namespace ui {
6+
state g_state;
7+
}
8+
59
static int get_button(const mouse* m, int x, int y, const generic_button* buttons, int num_buttons) {
610
for (int i = 0; i < num_buttons; i++) {
711
if (x + buttons[i].x <= m->x && x + buttons[i].x + buttons[i].width > m->x && y + buttons[i].y <= m->y

src/graphics/elements/generic_button.h

+36-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#pragma once
22

3-
#include "button.h"
3+
#include "core/string.h"
44
#include "input/mouse.h"
5+
#include "graphics/text.h"
6+
#include "graphics/elements/button.h"
57

68
#include <vector>
79
#include <cstdint>
@@ -32,5 +34,37 @@ inline int generic_buttons_handle_mouse(const mouse *m, vec2i pos, const T (&but
3234

3335
template<class T>
3436
inline int generic_buttons_handle_mouse(const mouse *m, vec2i pos, const T &buttons, int &focus_button_id) {
35-
return generic_buttons_handle_mouse(m, pos.x, pos.y, &buttons.front(), buttons.size(), &focus_button_id);
37+
return generic_buttons_handle_mouse(m, pos.x, pos.y, &buttons.front(), (int)buttons.size(), &focus_button_id);
38+
}
39+
40+
namespace ui {
41+
42+
struct state {
43+
vec2i offset;
44+
std::vector<generic_button> buttons;
45+
};
46+
47+
extern state g_state;
48+
49+
inline void begin_window(vec2i offset) {
50+
g_state.offset = offset;
51+
g_state.buttons.clear();
52+
}
53+
54+
inline int handle_mouse(const mouse *m) {
55+
int tmp_btn;
56+
return generic_buttons_handle_mouse(m, g_state.offset, g_state.buttons, tmp_btn);
57+
}
58+
59+
template<class Func>
60+
void button(pcstr label, vec2i pos, vec2i size, Func func) {
61+
const vec2i offset = g_state.offset;
62+
63+
g_state.buttons.push_back({pos.x, pos.y, size.x + 4, size.y + 4, button_none, button_none, 0, 0, func});
64+
int focused = is_button_hover(g_state.buttons.back(), offset);
65+
66+
button_border_draw(offset.x + pos.x, offset.y + pos.y, size.x, size.y, focused ? 1 : 0);
67+
text_draw_centered((uint8_t *)label, offset.x + pos.x + 1, offset.y + pos.y + 4, 20, FONT_NORMAL_BLACK_ON_LIGHT, 0);
68+
}
69+
3670
}

src/graphics/window.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ static void noop(void) {
2525
}
2626
static void noop_input(const mouse* m, const hotkeys* h) {
2727
}
28-
static void reset_input(void) {
28+
static void reset_input() {
2929
mouse_reset_button_state();
3030
reset_touches(1);
3131
scroll_stop();
3232
}
33-
static void increase_queue_index(void) {
33+
static void increase_queue_index() {
3434
auto& data = g_window;
3535
data.queue_index++;
3636
if (data.queue_index >= MAX_QUEUE)
@@ -59,7 +59,7 @@ int window_is_invalid(void) {
5959
return data.refresh_immediate;
6060
}
6161

62-
void window_request_refresh(void) {
62+
void window_request_refresh() {
6363
auto& data = g_window;
6464
data.refresh_on_draw = true;
6565
}
@@ -69,7 +69,7 @@ int window_is(e_window_id id) {
6969
return data.current_window->id == id;
7070
}
7171

72-
e_window_id window_get_id(void) {
72+
e_window_id window_get_id() {
7373
auto& data = g_window;
7474
return data.current_window->id;
7575
}
@@ -89,14 +89,15 @@ void window_show(const window_type* window) {
8989
data.current_window->handle_input = noop_input;
9090
window_invalidate();
9191
}
92-
void window_go_back(void) {
92+
93+
void window_go_back() {
9394
auto& data = g_window;
9495
reset_input();
9596
decrease_queue_index();
9697
data.current_window = &data.window_queue[data.queue_index];
9798
window_invalidate();
9899
}
99-
static void update_input_before(void) {
100+
static void update_input_before() {
100101
if (!touch_to_mouse()) {
101102
mouse_determine_button_state(); // touch overrides mouse
102103
}
@@ -146,7 +147,7 @@ window_type *window_current() {
146147
return g_window.current_window;
147148
}
148149

149-
void window_draw_underlying_window(void) {
150+
void window_draw_underlying_window() {
150151
auto& data = g_window;
151152
if (data.underlying_windows_redrawing < MAX_QUEUE) {
152153
++data.underlying_windows_redrawing;

src/window/window_building_info.cpp

+3-22
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ struct building_info_data {
8282
int generic_button_id = 0;
8383
int debug_path_button_id = 0;
8484

85-
std::vector<generic_button> btns;
86-
8785
generic_button generic_button_figures[1] = {
8886
{400, 3, 24, 24, button_debugpath, button_none, 0, 0}
8987
};
@@ -576,21 +574,6 @@ static void draw_mothball_button(int x, int y, int focused) {
576574
}
577575
}
578576

579-
template<class Func>
580-
static void make_button(pcstr label, vec2i pos, vec2i size, Func func) {
581-
auto &context = g_building_info_context;
582-
auto &data = g_building_info;
583-
584-
data.btns.push_back({pos.x, pos.y, size.x + 4, size.y + 4, button_none, button_none, 0, 0, func});
585-
int focused = is_button_hover(data.btns.back(), context.offset);
586-
587-
button_border_draw(context.offset.x + pos.x, context.offset.y + pos.y, size.x, size.y, focused ? 1 : 0);
588-
589-
if (context.show_overlay != OVERLAY_NONE) {
590-
text_draw_centered((uint8_t *)label, context.offset.x + pos.x + 1, context.offset.y + pos.y + 4, 20, FONT_NORMAL_BLACK_ON_LIGHT, 0);
591-
}
592-
}
593-
594577
static void draw_debugpath_button(int x, int y, int focused) {
595578
auto &context = g_building_info_context;
596579
button_border_draw(x, y, 20, 20, focused ? 1 : 0);
@@ -772,11 +755,10 @@ static void draw_background() {
772755
}
773756

774757
static void draw_foreground() {
758+
ui::begin_window(g_building_info_context.offset);
775759
auto &context = g_building_info_context;
776760
auto &data = g_building_info;
777761

778-
data.btns.clear();
779-
780762
// building-specific buttons
781763
if (context.type == BUILDING_INFO_BUILDING) {
782764
building *b = building_get(context.building_id);
@@ -860,7 +842,7 @@ static void draw_foreground() {
860842

861843
if (context.show_overlay != OVERLAY_NONE) {
862844
pcstr label = (game_state_overlay() != context.show_overlay ? "v" : "V");
863-
make_button(label, {375, 3 + 16 * context.height_blocks - 40}, {20, 20}, [&context] (int, int) {
845+
ui::button(label, {375, 3 + 16 * context.height_blocks - 40}, {20, 20}, [&context] (int, int) {
864846
if (game_state_overlay() != context.show_overlay) {
865847
game_state_set_overlay((e_overlay)context.show_overlay);
866848
} else {
@@ -969,8 +951,7 @@ static void handle_input(const mouse* m, const hotkeys* h) {
969951
g_building_info.generic_button_figures, g_building_info.debug_path_button_id);
970952
}
971953

972-
int tmp_btn;
973-
button_id |= generic_buttons_handle_mouse(m, context.offset, data.btns, tmp_btn);
954+
button_id |= ui::handle_mouse(m);
974955

975956
if (!button_id && input_go_back_requested(m, h)) {
976957
if (context.storage_show_special_orders) {

0 commit comments

Comments
 (0)