Skip to content

Commit 1717002

Browse files
author
s.kushnirenko
committed
feat: add logic for handle image_button in window
what: fix: clear img_buttons vector in begin_window function fix: update image_buttons_handle_mouse function to use button x and y coordinates
1 parent 554a256 commit 1717002

8 files changed

+41
-34
lines changed

src/building/building_mansion.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ void building_mansion::window_info_background(object_info &c) {
4545
}
4646

4747
int building_mansion::window_info_handle_mouse(const mouse *m, object_info &c) {
48-
return generic_buttons_handle_mouse(m, 0, 0, imperial_buttons, 8, &g_window_info_mansion.focus_button_id);
48+
return 0;
4949
}

src/graphics/elements/button.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ namespace ui {
6868
void ui::begin_window(vec2i offset) {
6969
g_state.offset = offset;
7070
g_state.buttons.clear();
71+
g_state.img_buttons.clear();
7172
}
7273

7374
bool ui::handle_mouse(const mouse *m) {
@@ -107,9 +108,13 @@ generic_button &ui::button(uint32_t id) {
107108

108109
image_button &ui::img_button(uint32_t group, uint32_t id, vec2i pos, vec2i size, int offset) {
109110
const vec2i img_offset = g_state.offset;
111+
const mouse *m = mouse_get();
110112

111113
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());
114+
auto &button = g_state.img_buttons.back();
115+
button.focused = is_button_hover(g_state.img_buttons.back(), img_offset);
116+
button.pressed = button.focused && m->left.is_down;
117+
image_buttons_draw(img_offset, button);
113118

114119
return g_state.img_buttons.back();
115120
}

src/graphics/elements/button.h

+9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
#pragma once
22

3+
#include "input/mouse.h"
34
#include "core/string.h"
45
#include "core/vec2i.h"
56

67
void button_none(int param1, int param2);
78

89
void button_border_draw(int x, int y, int width_pixels, int height_pixels, bool has_focus);
910

11+
template<class T>
12+
bool is_button_hover(const T &button, vec2i context) {
13+
const mouse *m = mouse_get();
14+
vec2i bpos = context + vec2i{button.x, button.y};
15+
return ( bpos.x <= m->x && bpos.x + button.width > m->x
16+
&& bpos.y <= m->y && bpos.y + button.height > m->y);
17+
}
18+
1019
struct generic_button;
1120
struct image_button;
1221
struct mouse;

src/graphics/elements/generic_button.cpp

-7
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,4 @@ int generic_buttons_min_handle_mouse(const mouse* m, int x, int y, const generic
6565
return 0;
6666

6767
return button_id;
68-
}
69-
70-
bool is_button_hover(generic_button &button, vec2i context) {
71-
const mouse *m = mouse_get();
72-
vec2i bpos = context + vec2i{button.x, button.y};
73-
return ( bpos.x <= m->x && bpos.x + button.width > m->x
74-
&& bpos.y <= m->y && bpos.y + button.height > m->y);
7568
}

src/graphics/elements/generic_button.h

-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ struct generic_button {
3030
int generic_buttons_handle_mouse(const mouse* m, int x, int y, const generic_button* buttons, int num_buttons, int* focus_button_id);
3131
int generic_buttons_min_handle_mouse(const mouse* m, int x, int y, const generic_button* buttons, int num_buttons, int* focus_button_id, int minimum_button);
3232

33-
bool is_button_hover(generic_button &button, vec2i context);
34-
3533
template<uint32_t N>
3634
inline int generic_buttons_handle_mouse(const mouse *m, vec2i pos, const generic_button (&buttons)[N], int &focus_button_id) {
3735
return generic_buttons_handle_mouse(m, pos.x, pos.y, buttons, N, &focus_button_id);

src/graphics/elements/image_button.cpp

+15-7
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void image_buttons_draw(int x, int y, image_button* buttons, int num_buttons, in
4848
} else {
4949
image_id += 3;
5050
}
51-
ImageDraw::img_generic(ctx, image_id, vec2i{x + btn->x_offset, y + btn->y_offset});
51+
ImageDraw::img_generic(ctx, image_id, vec2i{x + btn->x, y + btn->y});
5252
}
5353
}
5454

@@ -64,8 +64,8 @@ bool image_buttons_handle_mouse(const mouse* m, int x, int y, image_button* butt
6464
if (btn->focused)
6565
btn->focused--;
6666

67-
if (x + btn->x_offset <= m->x && x + btn->x_offset + btn->width > m->x && y + btn->y_offset <= m->y
68-
&& y + btn->y_offset + btn->height > m->y) {
67+
if (x + btn->x <= m->x && x + btn->x + btn->width > m->x && y + btn->y <= m->y
68+
&& y + btn->y + btn->height > m->y) {
6969
if (focus_button_id)
7070
*focus_button_id = i + 1;
7171

@@ -82,11 +82,15 @@ bool image_buttons_handle_mouse(const mouse* m, int x, int y, image_button* butt
8282
}
8383
}
8484
}
85-
if (!hit_button)
85+
if (!hit_button) {
8686
return false;
87-
if (hit_button->button_type == IB_SCROLL)
88-
if (!m->left.went_down && !m->left.is_down)
87+
}
88+
89+
if (hit_button->button_type == IB_SCROLL) {
90+
if (!m->left.went_down && !m->left.is_down) {
8991
return false;
92+
}
93+
}
9094

9195
// on click, press button and set reminder to this button (floating = true)
9296
if (m->left.went_down && hit_button->button_type != IB_SCROLL) {
@@ -99,11 +103,15 @@ bool image_buttons_handle_mouse(const mouse* m, int x, int y, image_button* butt
99103
if (m->left.went_up) {
100104
sound_effect_play(SOUND_EFFECT_ICON);
101105
remove_pressed_effect_build(buttons, num_buttons);
102-
if (hit_button->button_type == IB_BUILD || hit_button->button_type == IB_OVERSEER)
106+
if (hit_button->button_type == IB_BUILD || hit_button->button_type == IB_OVERSEER) {
103107
hit_button->pressed = 1;
108+
}
104109
hit_button->floating = 0;
105110
hit_button->pressed_since = time_get_millis();
106111
hit_button->left_click_handler(hit_button->parameter1, hit_button->parameter2);
112+
if (hit_button->_onclick) {
113+
hit_button->_onclick(hit_button->parameter1, hit_button->parameter2);
114+
}
107115
} else if (m->right.went_up) {
108116
if (hit_button->button_type == IB_BUILD || hit_button->button_type == IB_OVERSEER)
109117
hit_button->pressed = 1;

src/graphics/elements/image_button.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ enum {
1616
};
1717

1818
struct image_button {
19-
int x_offset;
20-
int y_offset;
19+
int x;
20+
int y;
2121
int width;
2222
int height;
2323
short button_type;
@@ -32,7 +32,7 @@ struct image_button {
3232
// state
3333
char pressed;
3434
char floating;
35-
char focused;
35+
uint8_t focused;
3636
time_millis pressed_since;
3737

3838
std::function<void(int,int)> _onclick;

src/window/window_building_info.cpp

+7-13
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 l_advisor_a = {40, 0, 28, 28, IB_NORMAL, GROUP_MESSAGE_ADVISOR_BUTTONS, 9, button_advisor, button_none, ADVISOR_RATINGS, 0, 1};
8281
image_button l_advisor_b = {65, 0, 28, 28, IB_NORMAL, GROUP_MESSAGE_ADVISOR_BUTTONS, 9, button_advisor, button_none, ADVISOR_RATINGS, 0, 1};
8382
} buttons;
8483

@@ -782,16 +781,19 @@ static void draw_foreground() {
782781
}
783782

784783
if (context.go_to_advisor.first) {
785-
ui::img_button(GROUP_MESSAGE_ADVISOR_BUTTONS, vec2i(0, 16 * context.height_blocks - 40), {28, 28}, (context.go_to_advisor.first - 1) * 3)
784+
int img_offset = (context.go_to_advisor.left_a - 1) * 3;
785+
ui::img_button(GROUP_MESSAGE_ADVISOR_BUTTONS, vec2i(40, 16 * context.height_blocks - 40), {28, 28}, img_offset)
786786
.onclick([&context] (int, int) {
787787
window_advisors_show_advisor(context.go_to_advisor.first);
788788
});
789789
}
790790

791791
if (context.go_to_advisor.left_a) {
792-
g_building_info.buttons.l_advisor_a.parameter1 = context.go_to_advisor.left_a;
793-
g_building_info.buttons.l_advisor_a.image_offset = (context.go_to_advisor.left_a - 1) * 3;
794-
image_buttons_draw(context.offset + vec2i(0, 16 * context.height_blocks - 40), g_building_info.buttons.l_advisor_a);
792+
int img_offset = (context.go_to_advisor.left_a - 1) * 3;
793+
ui::img_button(GROUP_MESSAGE_ADVISOR_BUTTONS, vec2i(40, 16 * context.height_blocks - 40), {28, 28}, img_offset)
794+
.onclick([&context] (int, int) {
795+
window_advisors_show_advisor(context.go_to_advisor.left_a);
796+
});
795797
}
796798

797799
if (context.go_to_advisor.left_b) {
@@ -915,14 +917,6 @@ static void handle_input(const mouse* m, const hotkeys* h) {
915917
g_building_info.image_buttons_help_close, g_building_info.image_button_id);
916918
}
917919

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-
//}
921-
922-
if (context.go_to_advisor.left_a) {
923-
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);
924-
}
925-
926920
if (context.go_to_advisor.left_b) {
927921
button_id |= image_buttons_handle_mouse(m, context.offset + vec2i(0, 16 * context.height_blocks - 40), g_building_info.buttons.l_advisor_b, tmp_btn_id);
928922
}

0 commit comments

Comments
 (0)