Skip to content

Commit 0017996

Browse files
author
s.kushnirenko
committed
ui: simplify figure path handler in info window
1 parent 0c61bca commit 0017996

File tree

3 files changed

+51
-64
lines changed

3 files changed

+51
-64
lines changed

src/graphics/elements/generic_button.cpp

+29-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,36 @@
33
#include "input/mouse.h"
44

55
namespace ui {
6+
struct state {
7+
vec2i offset;
8+
std::vector<generic_button> buttons;
9+
};
10+
611
state g_state;
712
}
813

14+
void ui::begin_window(vec2i offset) {
15+
g_state.offset = offset;
16+
g_state.buttons.clear();
17+
}
18+
19+
int ui::handle_mouse(const mouse *m) {
20+
int tmp_btn;
21+
return generic_buttons_handle_mouse(m, g_state.offset, g_state.buttons, tmp_btn);
22+
}
23+
24+
generic_button &ui::button(pcstr label, vec2i pos, vec2i size) {
25+
const vec2i offset = g_state.offset;
26+
27+
g_state.buttons.push_back({pos.x, pos.y, size.x + 4, size.y + 4, button_none, button_none, 0, 0});
28+
int focused = is_button_hover(g_state.buttons.back(), offset);
29+
30+
button_border_draw(offset.x + pos.x, offset.y + pos.y, size.x, size.y, focused ? 1 : 0);
31+
text_draw_centered((uint8_t *)label, offset.x + pos.x + 1, offset.y + pos.y + 4, 20, FONT_NORMAL_BLACK_ON_LIGHT, 0);
32+
33+
return g_state.buttons.back();
34+
}
35+
936
static int get_button(const mouse* m, int x, int y, const generic_button* buttons, int num_buttons) {
1037
for (int i = 0; i < num_buttons; i++) {
1138
if (x + buttons[i].x <= m->x && x + buttons[i].x + buttons[i].width > m->x && y + buttons[i].y <= m->y
@@ -39,8 +66,8 @@ int generic_buttons_handle_mouse(const mouse* m, int x, int y, const generic_but
3966
const generic_button* button = &buttons[button_id - 1];
4067
if (m->left.went_up) {
4168
button->left_click_handler(button->parameter1, button->parameter2);
42-
if (button->onclick) {
43-
button->onclick(button->parameter1, button->parameter2);
69+
if (button->_onclick) {
70+
button->_onclick(button->parameter1, button->parameter2);
4471
}
4572
} else if (m->right.went_up) {
4673
button->right_click_handler(button->parameter1, button->parameter2);

src/graphics/elements/generic_button.h

+6-28
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ struct generic_button {
1919
int parameter1;
2020
int parameter2;
2121

22-
std::function<void(int,int)> onclick;
22+
std::function<void(int,int)> _onclick;
23+
24+
template<class Func> void onclick(Func f) { _onclick = f; }
2325
};
2426

2527
int generic_buttons_handle_mouse(const mouse* m, int x, int y, const generic_button* buttons, int num_buttons, int* focus_button_id);
@@ -39,32 +41,8 @@ inline int generic_buttons_handle_mouse(const mouse *m, vec2i pos, const T &butt
3941

4042
namespace ui {
4143

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-
}
44+
void begin_window(vec2i offset);
45+
int handle_mouse(const mouse *m);
46+
generic_button &button(pcstr label, vec2i pos, vec2i size);
6947

7048
}

src/window/window_building_info.cpp

+16-34
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ static void button_help(int param1, int param2);
7272
static void button_close(int param1, int param2);
7373
static void button_advisor(int advisor, int param2);
7474
static void button_mothball(int mothball, int param2);
75-
static void button_debugpath(int debug, int param2);
76-
static void button_overlay(int overlay, int param2);
7775

7876
object_info g_building_info_context;
7977

@@ -82,10 +80,6 @@ struct building_info_data {
8280
int generic_button_id = 0;
8381
int debug_path_button_id = 0;
8482

85-
generic_button generic_button_figures[1] = {
86-
{400, 3, 24, 24, button_debugpath, button_none, 0, 0}
87-
};
88-
8983
generic_button generic_button_mothball[1] = {
9084
{400, 3, 24, 24, button_mothball, button_none, 0, 0}
9185
};
@@ -574,13 +568,6 @@ static void draw_mothball_button(int x, int y, int focused) {
574568
}
575569
}
576570

577-
static void draw_debugpath_button(int x, int y, int focused) {
578-
auto &context = g_building_info_context;
579-
button_border_draw(x, y, 20, 20, focused ? 1 : 0);
580-
figure* f = figure_get(context.figure.figure_ids[0]);
581-
text_draw_centered((uint8_t *)(f->draw_debug_mode ? "P" : "p"), x + 1, y + 4, 20, FONT_NORMAL_BLACK_ON_LIGHT, 0);
582-
}
583-
584571
static void draw_refresh_background() {
585572
auto &context = g_building_info_context;
586573
if (context.type == BUILDING_INFO_NONE) {
@@ -837,19 +824,26 @@ static void draw_foreground() {
837824
}
838825

839826
if (context.figure.draw_debug_path) {
840-
draw_debugpath_button(context.offset.x + 400, context.offset.y + 3 + 16 * context.height_blocks - 40, g_building_info.debug_path_button_id);
827+
figure* f = figure_get(context.figure.figure_ids[0]);
828+
pcstr label = (f->draw_debug_mode ? "P" : "p");
829+
ui::button(label, {400, 3 + 16 * context.height_blocks - 40}, {24, 24})
830+
.onclick([&context, f] (int, int) {
831+
f->draw_debug_mode = f->draw_debug_mode ? 0 :FIGURE_DRAW_DEBUG_ROUTING;
832+
window_invalidate();
833+
});
841834
}
842835

843836
if (context.show_overlay != OVERLAY_NONE) {
844837
pcstr label = (game_state_overlay() != context.show_overlay ? "v" : "V");
845-
ui::button(label, {375, 3 + 16 * context.height_blocks - 40}, {20, 20}, [&context] (int, int) {
846-
if (game_state_overlay() != context.show_overlay) {
847-
game_state_set_overlay((e_overlay)context.show_overlay);
848-
} else {
849-
game_state_reset_overlay();
850-
}
851-
window_invalidate();
852-
});
838+
ui::button(label, {375, 3 + 16 * context.height_blocks - 40}, {20, 20})
839+
.onclick([&context] (int, int) {
840+
if (game_state_overlay() != context.show_overlay) {
841+
game_state_set_overlay((e_overlay)context.show_overlay);
842+
} else {
843+
game_state_reset_overlay();
844+
}
845+
window_invalidate();
846+
});
853847
}
854848

855849
}
@@ -946,11 +940,6 @@ static void handle_input(const mouse* m, const hotkeys* h) {
946940
button_id |= !!handle_specific_building_info_mouse(m);
947941
}
948942

949-
if (context.figure.draw_debug_path) {
950-
button_id |= generic_buttons_handle_mouse(m, context.offset + vec2i(0, 16 * context.height_blocks - 40),
951-
g_building_info.generic_button_figures, g_building_info.debug_path_button_id);
952-
}
953-
954943
button_id |= ui::handle_mouse(m);
955944

956945
if (!button_id && input_go_back_requested(m, h)) {
@@ -997,13 +986,6 @@ static void button_mothball(int mothball, int param2) {
997986
}
998987
}
999988

1000-
static void button_debugpath(int debug, int param2) {
1001-
auto &context = g_building_info_context;
1002-
figure* f = figure_get(context.figure.figure_ids[0]);
1003-
f->draw_debug_mode = f->draw_debug_mode ? 0 :FIGURE_DRAW_DEBUG_ROUTING;
1004-
window_invalidate();
1005-
}
1006-
1007989
void window_building_info_show(const map_point& point) {
1008990
window_type window = {
1009991
WINDOW_BUILDING_INFO,

0 commit comments

Comments
 (0)