Skip to content

Commit 3370aeb

Browse files
author
s.kushnirenko
committed
ui: feat: add tooltip functionality to generic_button struct
what: fix: add button_hover function to handle button hover events
1 parent 0017996 commit 3370aeb

File tree

4 files changed

+56
-55
lines changed

4 files changed

+56
-55
lines changed

src/graphics/elements/generic_button.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace ui {
99
};
1010

1111
state g_state;
12+
generic_button dummy;
1213
}
1314

1415
void ui::begin_window(vec2i offset) {
@@ -21,6 +22,16 @@ int ui::handle_mouse(const mouse *m) {
2122
return generic_buttons_handle_mouse(m, g_state.offset, g_state.buttons, tmp_btn);
2223
}
2324

25+
int ui::button_hover(const mouse *m) {
26+
for (auto &btn : g_state.buttons) {
27+
if (is_button_hover(btn, g_state.offset)) {
28+
return (std::distance(&g_state.buttons.front(), &btn) + 1);
29+
}
30+
}
31+
32+
return 0;
33+
}
34+
2435
generic_button &ui::button(pcstr label, vec2i pos, vec2i size) {
2536
const vec2i offset = g_state.offset;
2637

@@ -33,6 +44,11 @@ generic_button &ui::button(pcstr label, vec2i pos, vec2i size) {
3344
return g_state.buttons.back();
3445
}
3546

47+
48+
generic_button &ui::button(uint32_t id) {
49+
return (id < g_state.buttons.size()) ? g_state.buttons[id] : dummy;
50+
}
51+
3652
static int get_button(const mouse* m, int x, int y, const generic_button* buttons, int num_buttons) {
3753
for (int i = 0; i < num_buttons; i++) {
3854
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

+6-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ struct generic_button {
2020
int parameter2;
2121

2222
std::function<void(int,int)> _onclick;
23+
std::pair<int, int> _tooltip;
2324

24-
template<class Func> void onclick(Func f) { _onclick = f; }
25+
template<class Func> generic_button &onclick(Func f) { _onclick = f; return *this; }
26+
void tooltip(std::pair<int, int> t) { _tooltip = t; }
27+
void tooltip(const std::initializer_list<int> &t) { _tooltip.first = *t.begin(); _tooltip.second = *(t.begin() + 1); }
2528
};
2629

2730
int generic_buttons_handle_mouse(const mouse* m, int x, int y, const generic_button* buttons, int num_buttons, int* focus_button_id);
@@ -40,9 +43,10 @@ inline int generic_buttons_handle_mouse(const mouse *m, vec2i pos, const T &butt
4043
}
4144

4245
namespace ui {
43-
4446
void begin_window(vec2i offset);
4547
int handle_mouse(const mouse *m);
48+
int button_hover(const mouse *m);
4649
generic_button &button(pcstr label, vec2i pos, vec2i size);
50+
generic_button &button(uint32_t id);
4751

4852
}

src/window/window_building_info.cpp

+32-51
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,11 @@
7171
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);
74-
static void button_mothball(int mothball, int param2);
7574

7675
object_info g_building_info_context;
7776

7877
struct building_info_data {
7978
int image_button_id = 0;
80-
int generic_button_id = 0;
81-
int debug_path_button_id = 0;
82-
83-
generic_button generic_button_mothball[1] = {
84-
{400, 3, 24, 24, button_mothball, button_none, 0, 0}
85-
};
8679

8780
struct {
8881
image_button advisor = {350, -38, 28, 28, IB_NORMAL, GROUP_MESSAGE_ADVISOR_BUTTONS, 9, button_advisor, button_none, ADVISOR_RATINGS, 0, 1};
@@ -202,42 +195,41 @@ static int get_height_id() {
202195

203196
static void get_tooltip(tooltip_context* c) {
204197
auto &context = g_building_info_context;
205-
int text_id = 0, group_id = 0;
198+
std::pair<int, int> tooltip;
206199
if (g_building_info.image_button_id) {
207-
text_id = g_building_info.image_button_id;
200+
tooltip.second = g_building_info.image_button_id;
208201

209-
} else if (g_building_info.generic_button_id) {
210-
if (building_get(context.building_id)->state == BUILDING_STATE_VALID) {
211-
text_id = 8;
212-
group_id = 54;
213-
} else {
214-
text_id = 10;
215-
group_id = 54;
216-
}
202+
//} else if (g_building_info.generic_button_id) {
203+
;
217204

218-
} else if (g_building_info.debug_path_button_id) {
205+
//} else if (g_building_info.debug_path_button_id) {
219206
;
220207

221208
} else if (context.type == BUILDING_INFO_LEGION) {
222-
text_id = window_building_get_legion_info_tooltip_text(&context);
209+
tooltip.second = window_building_get_legion_info_tooltip_text(&context);
223210

224211
} else if (context.type == BUILDING_INFO_BUILDING && context.storage_show_special_orders) {
225212
switch (building_get(context.building_id)->type) {
226213
case BUILDING_GRANARY:
227-
window_building_get_tooltip_granary_orders(&group_id, &text_id);
214+
window_building_get_tooltip_granary_orders(&tooltip.first, &tooltip.second);
228215
break;
229216

230217
case BUILDING_STORAGE_YARD:
231-
window_building_get_tooltip_warehouse_orders(&group_id, &text_id);
218+
window_building_get_tooltip_warehouse_orders(&tooltip.first, &tooltip.second);
232219
break;
233220
}
234221
}
235222

236-
if (text_id || group_id) {
223+
int button_id = ui::button_hover(mouse_get());
224+
if (button_id > 0) {
225+
tooltip = ui::button(button_id - 1)._tooltip;
226+
}
227+
228+
if (tooltip.first || tooltip.second) {
237229
c->type = TOOLTIP_BUTTON;
238-
c->text_id = text_id;
239-
if (group_id) {
240-
c->text_group = group_id;
230+
c->text_id = tooltip.second;
231+
if (tooltip.first) {
232+
c->text_group = tooltip.first;
241233
}
242234
window_request_refresh_background();
243235
}
@@ -559,15 +551,6 @@ static void init(map_point tile) {
559551
}
560552
}
561553

562-
static void draw_mothball_button(int x, int y, int focused) {
563-
auto &context = g_building_info_context;
564-
button_border_draw(x, y, 20, 20, focused ? 1 : 0);
565-
building* b = building_get(context.building_id);
566-
if (b->state == BUILDING_STATE_VALID) {
567-
text_draw_centered((uint8_t*)"x", x + 1, y + 4, 20, FONT_NORMAL_BLACK_ON_LIGHT, 0);
568-
}
569-
}
570-
571554
static void draw_refresh_background() {
572555
auto &context = g_building_info_context;
573556
if (context.type == BUILDING_INFO_NONE) {
@@ -747,8 +730,9 @@ static void draw_foreground() {
747730
auto &data = g_building_info;
748731

749732
// building-specific buttons
733+
building *b = nullptr;
750734
if (context.type == BUILDING_INFO_BUILDING) {
751-
building *b = building_get(context.building_id);
735+
b = building_get(context.building_id);
752736
switch (b->type) {
753737
case BUILDING_GRANARY:
754738
if (context.storage_show_special_orders) {
@@ -816,17 +800,26 @@ static void draw_foreground() {
816800
image_buttons_draw(context.offset + vec2i(0, 16 * context.height_blocks - 40), g_building_info.buttons.l_advisor_b);
817801
}
818802

819-
if (!context.storage_show_special_orders) {
820-
int workers_needed = model_get_building(building_get(context.building_id)->type)->laborers;
803+
if (b) {
804+
int workers_needed = model_get_building(b->type)->laborers;
821805
if (workers_needed) {
822-
draw_mothball_button(context.offset.x + 400, context.offset.y + 3 + 16 * context.height_blocks - 40, g_building_info.generic_button_id);
806+
pcstr label = (b->state == BUILDING_STATE_VALID ? "x" : "");
807+
auto tooltip = (b->state == BUILDING_STATE_VALID) ? std::pair{54, 8} : std::pair{53, 10};
808+
ui::button(label, {400, 3 + 16 * context.height_blocks - 40}, {20, 20})
809+
.onclick([&context, b, workers_needed] (int, int) {
810+
if (workers_needed) {
811+
building_mothball_toggle(b);
812+
window_invalidate();
813+
}
814+
})
815+
.tooltip(tooltip);
823816
}
824817
}
825818

826819
if (context.figure.draw_debug_path) {
827820
figure* f = figure_get(context.figure.figure_ids[0]);
828821
pcstr label = (f->draw_debug_mode ? "P" : "p");
829-
ui::button(label, {400, 3 + 16 * context.height_blocks - 40}, {24, 24})
822+
ui::button(label, {400, 3 + 16 * context.height_blocks - 40}, {20, 20})
830823
.onclick([&context, f] (int, int) {
831824
f->draw_debug_mode = f->draw_debug_mode ? 0 :FIGURE_DRAW_DEBUG_ROUTING;
832825
window_invalidate();
@@ -920,8 +913,6 @@ static void handle_input(const mouse* m, const hotkeys* h) {
920913
} else {
921914
button_id |= image_buttons_handle_mouse(m, context.offset + vec2i(0, 16 * context.height_blocks - 40),
922915
g_building_info.image_buttons_help_close, g_building_info.image_button_id);
923-
button_id |= generic_buttons_handle_mouse(m, context.offset + vec2i(0, 16 * context.height_blocks - 40),
924-
g_building_info.generic_button_mothball, g_building_info.generic_button_id);
925916
}
926917

927918
if (context.go_to_advisor.first) {
@@ -976,16 +967,6 @@ static void button_advisor(int advisor, int param2) {
976967
window_advisors_show_advisor((e_advisor)advisor);
977968
}
978969

979-
static void button_mothball(int mothball, int param2) {
980-
auto &context = g_building_info_context;
981-
building* b = building_get(context.building_id);
982-
int workers_needed = model_get_building(b->type)->laborers;
983-
if (workers_needed) {
984-
building_mothball_toggle(b);
985-
window_invalidate();
986-
}
987-
}
988-
989970
void window_building_info_show(const map_point& point) {
990971
window_type window = {
991972
WINDOW_BUILDING_INFO,

src/window/window_building_info.h

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

3-
class map_point;
3+
#include "grid/point.h"
44

5-
void window_building_info_show(const map_point& point);
5+
void window_building_info_show(const tile2i& point);
66
int window_building_info_get_int();
77
void window_building_info_show_storage_orders();

0 commit comments

Comments
 (0)