Skip to content

Commit 554a256

Browse files
author
s.kushnirenko
committed
ui: add simplify using image_button logic
1 parent 3370aeb commit 554a256

File tree

6 files changed

+112
-77
lines changed

6 files changed

+112
-77
lines changed

src/graphics/elements/button.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "graphics/boilerplate.h"
44
#include "graphics/view/view.h"
55
#include "game/game.h"
6+
#include "generic_button.h"
7+
#include "image_button.h"
68

79
void button_none(int param1, int param2) {
810
}
@@ -51,3 +53,63 @@ void button_border_draw(int x, int y, int width_pixels, int height_pixels, bool
5153
}
5254
}
5355
}
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+
}

src/graphics/elements/button.h

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
1-
#ifndef GRAPHICS_BUTTON_H
2-
#define GRAPHICS_BUTTON_H
1+
#pragma once
2+
3+
#include "core/string.h"
4+
#include "core/vec2i.h"
35

46
void button_none(int param1, int param2);
57

68
void button_border_draw(int x, int y, int width_pixels, int height_pixels, bool has_focus);
79

8-
#endif // GRAPHICS_BUTTON_H
10+
struct generic_button;
11+
struct image_button;
12+
struct mouse;
13+
14+
namespace ui {
15+
16+
void begin_window(vec2i offset);
17+
bool handle_mouse(const mouse *m);
18+
int button_hover(const mouse *m);
19+
generic_button &button(pcstr label, vec2i pos, vec2i size);
20+
generic_button &button(uint32_t id);
21+
image_button &img_button(uint32_t group, uint32_t id, vec2i pos, vec2i size, int offset = 0);
22+
23+
}

src/graphics/elements/generic_button.cpp

-47
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,6 @@
22

33
#include "input/mouse.h"
44

5-
namespace ui {
6-
struct state {
7-
vec2i offset;
8-
std::vector<generic_button> buttons;
9-
};
10-
11-
state g_state;
12-
generic_button dummy;
13-
}
14-
15-
void ui::begin_window(vec2i offset) {
16-
g_state.offset = offset;
17-
g_state.buttons.clear();
18-
}
19-
20-
int ui::handle_mouse(const mouse *m) {
21-
int tmp_btn;
22-
return generic_buttons_handle_mouse(m, g_state.offset, g_state.buttons, tmp_btn);
23-
}
24-
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-
35-
generic_button &ui::button(pcstr label, vec2i pos, vec2i size) {
36-
const vec2i offset = g_state.offset;
37-
38-
g_state.buttons.push_back({pos.x, pos.y, size.x + 4, size.y + 4, button_none, button_none, 0, 0});
39-
int focused = is_button_hover(g_state.buttons.back(), offset);
40-
41-
button_border_draw(offset.x + pos.x, offset.y + pos.y, size.x, size.y, focused ? 1 : 0);
42-
text_draw_centered((uint8_t *)label, offset.x + pos.x + 1, offset.y + pos.y + 4, 20, FONT_NORMAL_BLACK_ON_LIGHT, 0);
43-
44-
return g_state.buttons.back();
45-
}
46-
47-
48-
generic_button &ui::button(uint32_t id) {
49-
return (id < g_state.buttons.size()) ? g_state.buttons[id] : dummy;
50-
}
51-
525
static int get_button(const mouse* m, int x, int y, const generic_button* buttons, int num_buttons) {
536
for (int i = 0; i < num_buttons; i++) {
547
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

+5-12
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,14 @@ int generic_buttons_min_handle_mouse(const mouse* m, int x, int y, const generic
3232

3333
bool is_button_hover(generic_button &button, vec2i context);
3434

35-
template<class T, uint32_t N>
36-
inline int generic_buttons_handle_mouse(const mouse *m, vec2i pos, const T (&buttons)[N], int &focus_button_id) {
35+
template<uint32_t N>
36+
inline int generic_buttons_handle_mouse(const mouse *m, vec2i pos, const generic_button (&buttons)[N], int &focus_button_id) {
3737
return generic_buttons_handle_mouse(m, pos.x, pos.y, buttons, N, &focus_button_id);
3838
}
3939

4040
template<class T>
4141
inline int generic_buttons_handle_mouse(const mouse *m, vec2i pos, const T &buttons, int &focus_button_id) {
42-
return generic_buttons_handle_mouse(m, pos.x, pos.y, &buttons.front(), (int)buttons.size(), &focus_button_id);
43-
}
44-
45-
namespace ui {
46-
void begin_window(vec2i offset);
47-
int handle_mouse(const mouse *m);
48-
int button_hover(const mouse *m);
49-
generic_button &button(pcstr label, vec2i pos, vec2i size);
50-
generic_button &button(uint32_t id);
51-
42+
return buttons.size() > 0
43+
? generic_buttons_handle_mouse(m, pos.x, pos.y, &buttons.front(), (int)buttons.size(), &focus_button_id)
44+
: 0;
5245
}

src/graphics/elements/image_button.h

+20-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "core/time.h"
77

88
#include <vector>
9+
#include <functional>
910

1011
enum {
1112
IB_BUILD = 2,
@@ -15,14 +16,14 @@ enum {
1516
};
1617

1718
struct image_button {
18-
short x_offset;
19-
short y_offset;
20-
short width;
21-
short height;
19+
int x_offset;
20+
int y_offset;
21+
int width;
22+
int height;
2223
short button_type;
23-
unsigned int image_collection;
24-
unsigned int image_group;
25-
short image_offset;
24+
uint32_t image_collection;
25+
uint32_t image_group;
26+
int image_offset;
2627
void (*left_click_handler)(int param1, int param2);
2728
void (*right_click_handler)(int param1, int param2);
2829
int parameter1;
@@ -33,11 +34,22 @@ struct image_button {
3334
char floating;
3435
char focused;
3536
time_millis pressed_since;
37+
38+
std::function<void(int,int)> _onclick;
39+
40+
template<class Func> image_button &onclick(Func f) { _onclick = f; return *this; }
3641
};
3742

43+
template<size_t N>
44+
bool image_buttons_handle_mouse(const mouse *m, vec2i pos, image_button (&buttons)[N], int &focus_button_id) {
45+
return image_buttons_handle_mouse(m, pos.x, pos.y, std::begin(buttons), (int)N, &focus_button_id);
46+
}
47+
3848
template<class T>
3949
bool image_buttons_handle_mouse(const mouse *m, vec2i pos, T &buttons, int &focus_button_id) {
40-
return image_buttons_handle_mouse(m, pos.x, pos.y, std::begin(buttons), (int)std::size(buttons), &focus_button_id);
50+
return buttons.size() > 0
51+
? image_buttons_handle_mouse(m, pos.x, pos.y, &buttons.front(), (int)buttons.size(), &focus_button_id)
52+
: 0;
4153
}
4254

4355
template<class T>

src/window/window_building_info.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ struct building_info_data {
7878
int image_button_id = 0;
7979

8080
struct {
81-
image_button advisor = {350, -38, 28, 28, IB_NORMAL, GROUP_MESSAGE_ADVISOR_BUTTONS, 9, button_advisor, button_none, ADVISOR_RATINGS, 0, 1};
8281
image_button l_advisor_a = {40, 0, 28, 28, IB_NORMAL, GROUP_MESSAGE_ADVISOR_BUTTONS, 9, button_advisor, button_none, ADVISOR_RATINGS, 0, 1};
8382
image_button l_advisor_b = {65, 0, 28, 28, IB_NORMAL, GROUP_MESSAGE_ADVISOR_BUTTONS, 9, button_advisor, button_none, ADVISOR_RATINGS, 0, 1};
8483
} buttons;
@@ -783,9 +782,10 @@ static void draw_foreground() {
783782
}
784783

785784
if (context.go_to_advisor.first) {
786-
g_building_info.buttons.advisor.parameter1 = context.go_to_advisor.first;
787-
g_building_info.buttons.advisor.image_offset = (context.go_to_advisor.first - 1) * 3;
788-
image_buttons_draw(context.offset + vec2i(0, 16 * context.height_blocks - 40), g_building_info.buttons.advisor);
785+
ui::img_button(GROUP_MESSAGE_ADVISOR_BUTTONS, vec2i(0, 16 * context.height_blocks - 40), {28, 28}, (context.go_to_advisor.first - 1) * 3)
786+
.onclick([&context] (int, int) {
787+
window_advisors_show_advisor(context.go_to_advisor.first);
788+
});
789789
}
790790

791791
if (context.go_to_advisor.left_a) {
@@ -915,9 +915,9 @@ static void handle_input(const mouse* m, const hotkeys* h) {
915915
g_building_info.image_buttons_help_close, g_building_info.image_button_id);
916916
}
917917

918-
if (context.go_to_advisor.first) {
919-
button_id |= image_buttons_handle_mouse(m, context.offset + vec2i(0, 16 * context.height_blocks - 40), g_building_info.buttons.advisor, tmp_btn_id);
920-
}
918+
//if (context.go_to_advisor.first) {
919+
// button_id |= image_buttons_handle_mouse(m, context.offset + vec2i(0, 16 * context.height_blocks - 40), g_building_info.buttons.advisor, tmp_btn_id);
920+
//}
921921

922922
if (context.go_to_advisor.left_a) {
923923
button_id |= image_buttons_handle_mouse(m, context.offset + vec2i(0, 16 * context.height_blocks - 40), g_building_info.buttons.l_advisor_a, tmp_btn_id);

0 commit comments

Comments
 (0)