Skip to content

Commit 5f864ce

Browse files
committed
ui: fixed draw border when noborder button hovered
1 parent 866cccf commit 5f864ce

File tree

9 files changed

+53
-77
lines changed

9 files changed

+53
-77
lines changed

src/graphics/elements/button.cpp

+17-25
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,38 @@
88
void button_none(int param1, int param2) {
99
}
1010

11-
void button_border_draw(int x, int y, int width_pixels, int height_pixels, bool has_focus) {
11+
void button_border_draw(vec2i pos, vec2i size, bool has_focus) {
1212
painter ctx = game.painter();
13-
int width_blocks = width_pixels / 16;
14-
if (width_pixels % 16)
13+
int width_blocks = size.x / 16;
14+
if (size.x % 16)
1515
width_blocks++;
1616

17-
int height_blocks = height_pixels / 16;
18-
if (height_pixels % 16)
17+
int height_blocks = size.y / 16;
18+
if (size.x % 16)
1919
height_blocks++;
2020

21-
int last_block_offset_x = 16 * width_blocks - width_pixels;
22-
int last_block_offset_y = 16 * height_blocks - height_pixels;
21+
int last_block_offset_x = 16 * width_blocks - size.x;
22+
int last_block_offset_y = 16 * height_blocks - size.y;
2323

2424
int image_base = image_id_from_group(GROUP_BORDERED_BUTTON);
2525
if (has_focus)
2626
image_base += 8;
2727

2828
for (int yy = 0; yy < height_blocks; yy++) {
29-
vec2i draw_offset = {0, y + 16 * yy};
29+
vec2i draw_offset = {0, pos.y + 16 * yy};
3030
for (int xx = 0; xx < width_blocks; xx++) {
31-
draw_offset.x = x + 16 * xx;
31+
draw_offset.x = pos.x + 16 * xx;
3232
if (yy == 0) {
33-
if (xx == 0)
34-
ImageDraw::img_generic(ctx, image_base, draw_offset);
35-
else if (xx < width_blocks - 1)
36-
ImageDraw::img_generic(ctx, image_base + 1, draw_offset);
37-
else
38-
ImageDraw::img_generic(ctx, image_base + 2, draw_offset - vec2i{last_block_offset_x, 0});
33+
if (xx == 0) ImageDraw::img_generic(ctx, image_base, draw_offset);
34+
else if (xx < width_blocks - 1) ImageDraw::img_generic(ctx, image_base + 1, draw_offset);
35+
else ImageDraw::img_generic(ctx, image_base + 2, draw_offset - vec2i{last_block_offset_x, 0});
3936
} else if (yy < height_blocks - 1) {
40-
if (xx == 0)
41-
ImageDraw::img_generic(ctx, image_base + 7, draw_offset);
42-
else if (xx >= width_blocks - 1)
43-
ImageDraw::img_generic(ctx, image_base + 3, draw_offset - vec2i{last_block_offset_x, 0});
37+
if (xx == 0) ImageDraw::img_generic(ctx, image_base + 7, draw_offset);
38+
else if (xx >= width_blocks - 1) ImageDraw::img_generic(ctx, image_base + 3, draw_offset - vec2i{last_block_offset_x, 0});
4439
} else {
45-
if (xx == 0)
46-
ImageDraw::img_generic(ctx, image_base + 6, draw_offset - vec2i{0, last_block_offset_y});
47-
else if (xx < width_blocks - 1)
48-
ImageDraw::img_generic(ctx, image_base + 5, draw_offset - vec2i{0, last_block_offset_y});
49-
else
50-
ImageDraw::img_generic(ctx, image_base + 4, draw_offset - vec2i{last_block_offset_x, last_block_offset_y});
40+
if (xx == 0) ImageDraw::img_generic(ctx, image_base + 6, draw_offset - vec2i{0, last_block_offset_y});
41+
else if (xx < width_blocks - 1) ImageDraw::img_generic(ctx, image_base + 5, draw_offset - vec2i{0, last_block_offset_y});
42+
else ImageDraw::img_generic(ctx, image_base + 4, draw_offset - vec2i{last_block_offset_x, last_block_offset_y});
5143
}
5244
}
5345
}

src/graphics/elements/button.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
void button_none(int param1, int param2);
88

9-
void button_border_draw(int x, int y, int width_pixels, int height_pixels, bool has_focus);
10-
inline void button_border_draw(vec2i pos, vec2i size, bool has_focus) { button_border_draw(pos.x, pos.y, size.x, size.y, has_focus); }
9+
void button_border_draw(vec2i pos, vec2i size, bool has_focus);
10+
inline void button_border_draw(int x, int y, int width_pixels, int height_pixels, bool has_focus) { button_border_draw({ x, y }, { width_pixels, height_pixels }, has_focus); }
1111
bool button_inside_clip(vec2i);
1212

1313
template<class T>

src/graphics/elements/ui.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,11 @@ generic_button &ui::button(pcstr label, vec2i pos, vec2i size, fonts_vec fonts,
270270
const bool noborder = !!(flags & UiFlags_NoBorder);
271271

272272
if (!noborder) {
273-
button_border_draw(offset.x + pos.x, offset.y + pos.y, size.x, size.y, gbutton.hovered && !grayed);
273+
button_border_draw(offset + pos, size, gbutton.hovered && !grayed);
274+
} else {
275+
if (gbutton.hovered && !grayed) {
276+
button_border_draw(offset + pos, size, true);
277+
}
274278
}
275279

276280
e_font font = fonts[gbutton.hovered ? 1 : 0];
@@ -323,7 +327,7 @@ generic_button &ui::button(const svector<pcstr,4> &labels, vec2i pos, vec2i size
323327
font = fonts[0];
324328
}
325329

326-
button_border_draw(offset.x + pos.x, offset.y + pos.y, size.x, size.y, gbutton.hovered ? 1 : 0);
330+
button_border_draw(offset + pos, size, gbutton.hovered ? 1 : 0);
327331
int symbolh = get_letter_height((uint8_t *)"H", font);
328332
int labels_num = labels.size();
329333
int starty = offset.y + pos.y + (size.y - (symbolh + 2) * labels_num) / 2 + 4;
@@ -685,7 +689,7 @@ void ui::eborder::draw() {
685689
switch (border) {
686690
default: //fallthrought
687691
case 0:
688-
button_border_draw(offset.x + pos.x, offset.y + pos.y, size.x, size.y, false);
692+
button_border_draw(offset + pos, size, false);
689693
break;
690694
}
691695
}
@@ -836,7 +840,7 @@ void ui::eimage_button::draw() {
836840
}
837841

838842
if (border && selected) {
839-
button_border_draw(doffset.x + pos.x - 4, doffset.y + pos.y - 4, tsize.x + 8, tsize.y + 8, true);
843+
button_border_draw(doffset + pos - vec2i{ 4, 4 }, tsize + vec2i{ 8, 8 }, true);
840844
}
841845

842846
btn->onclick(_func);

src/platform/renderer.cpp

+4-24
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
#include "game/game.h"
1414
#include "input/cursor.h"
1515

16-
#include <gpupixel.h>
17-
#undef min
18-
#undef max
19-
2016
#include <SDL.h>
2117
#include <SDL_video.h>
2218

@@ -44,16 +40,6 @@
4440
#define HAS_TEXTURE_SCALE_MODE 0
4541
#endif
4642

47-
// TODO: fix the render geometry functions for newer SDL version
48-
// Even though geometry rendering is supported since SDL 2.0.18, that version still has some drawing bugs, so we only
49-
// enable geometry rendering with SDL 2.0.20. Also, the software renderer also has drawing bugs, so it's also disabled.
50-
// #if SDL_VERSION_ATLEAST(2, 0, 20)
51-
// #define USE_RENDER_GEOMETRY
52-
// #define HAS_RENDER_GEOMETRY (platform_sdl_version_at_least(2, 0, 20) && !data.is_software_renderer)
53-
// #else
54-
#define HAS_RENDER_GEOMETRY 0
55-
// #endif
56-
5743
#define MAX_UNPACKED_IMAGES 10
5844

5945
#define MAX_PACKED_IMAGE_SIZE 64000
@@ -63,6 +49,10 @@
6349
// some images from the atlas with an off-by-one pixel, making things look terrible. Defining a smaller atlas texture
6450
// prevents the problem, at the cost of performance due to the extra texture context switching.
6551
#define MAX_TEXTURE_SIZE 2048
52+
#else
53+
#include <gpupixel.h>
54+
#undef min
55+
#undef max
6656
#endif
6757

6858
#ifdef __vita__
@@ -724,16 +714,6 @@ void load_unpacked_image(const image_t* img, const color* pixels) {
724714
SDL_FreeSurface(surface);
725715
}
726716

727-
// int graphics_renderer_interface::should_pack_image(int width, int height) {
728-
// return width * height < MAX_PACKED_IMAGE_SIZE;
729-
// }
730-
// int graphics_renderer_interface::isometric_images_are_joined(void) {
731-
// return HAS_RENDER_GEOMETRY;
732-
// }
733-
// int graphics_renderer_interface::supports_yuv_texture(void) {
734-
// return data.supports_yuv_textures;
735-
// }
736-
737717
SDL_Texture* graphics_renderer_interface::create_texture_from_buffer(color* p_data, int width, int height) {
738718
auto &data = g_renderer_data;
739719
if (p_data == nullptr)

src/window/advisor/housing.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ int ui::advisor_housing_window::draw_background() {
106106

107107
void ui::advisor_housing_window::draw_foreground() {
108108
if (focus_button_id == 0)
109-
button_border_draw(545, 260, 60, 51, 0);
109+
button_border_draw({ 545, 260 }, { 60, 51 }, 0);
110110
else if (focus_button_id == 1)
111-
button_border_draw(545, 260, 60, 51, 1);
111+
button_border_draw({ 545, 260 }, { 60, 51 }, 1);
112112
}
113113

114114
int ui::advisor_housing_window::handle_mouse(const mouse* m) {

src/window/editor/attributes.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ static void draw_foreground() {
9999

100100
input_box_draw(&scenario_description_input);
101101

102-
button_border_draw(212, 76, 250, 30, data.focus_button_id == 1);
102+
button_border_draw({ 212, 76 }, { 250, 30 }, data.focus_button_id == 1);
103103
lang_text_draw_centered(44, 88, 212, 85, 250, FONT_NORMAL_BLACK_ON_LIGHT);
104104

105105
lang_text_draw(44, 76, 32, 125, FONT_NORMAL_BLACK_ON_LIGHT);
106-
button_border_draw(212, 116, 250, 30, data.focus_button_id == 2);
106+
button_border_draw({ 212, 116 }, { 250, 30 }, data.focus_button_id == 2);
107107
lang_text_draw_centered(44, 77 + scenario_property_climate(), 212, 125, 250, FONT_NORMAL_BLACK_ON_LIGHT);
108108

109109
lang_text_draw(44, 40, 32, 165, FONT_NORMAL_BLACK_ON_LIGHT);
110-
button_border_draw(212, 156, 250, 30, data.focus_button_id == 3);
110+
button_border_draw({ 212, 156 }, { 250, 30 }, data.focus_button_id == 3);
111111

112112
editor_request request;
113113
scenario_editor_request_get(0, &request);
@@ -121,11 +121,11 @@ static void draw_foreground() {
121121
}
122122

123123
lang_text_draw(44, 41, 32, 205, FONT_NORMAL_BLACK_ON_LIGHT);
124-
button_border_draw(212, 196, 250, 30, data.focus_button_id == 4);
124+
button_border_draw({ 212, 196 }, { 250, 30 }, data.focus_button_id == 4);
125125
lang_text_draw_centered(37, scenario_property_enemy(), 212, 205, 250, FONT_NORMAL_BLACK_ON_LIGHT);
126126

127127
lang_text_draw(44, 42, 32, 245, FONT_NORMAL_BLACK_ON_LIGHT);
128-
button_border_draw(212, 236, 250, 30, data.focus_button_id == 5);
128+
button_border_draw({ 212, 236 }, { 250, 30 }, data.focus_button_id == 5);
129129

130130
editor_invasion invasion;
131131
scenario_editor_invasion_get(0, &invasion);
@@ -137,22 +137,22 @@ static void draw_foreground() {
137137
lang_text_draw_centered(44, 20, 212, 245, 250, FONT_NORMAL_BLACK_ON_LIGHT);
138138
}
139139

140-
button_border_draw(212, 276, 250, 30, data.focus_button_id == 6);
140+
button_border_draw({ 212, 276 }, { 250, 30 }, data.focus_button_id == 6);
141141
lang_text_draw_centered(44, 44, 212, 285, 250, FONT_NORMAL_BLACK_ON_LIGHT);
142142

143-
button_border_draw(212, 316, 250, 30, data.focus_button_id == 7);
143+
button_border_draw({ 212, 316 }, { 250, 30 }, data.focus_button_id == 7);
144144
lang_text_draw_centered(44, 45, 212, 325, 250, FONT_NORMAL_BLACK_ON_LIGHT);
145145

146-
button_border_draw(212, 356, 250, 30, data.focus_button_id == 8);
146+
button_border_draw({ 212, 356 }, { 250, 30 }, data.focus_button_id == 8);
147147
lang_text_draw_centered(44, 49, 212, 365, 250, FONT_NORMAL_BLACK_ON_LIGHT);
148148

149-
button_border_draw(212, 396, 250, 30, data.focus_button_id == 9);
149+
button_border_draw({ 212, 396 }, { 250, 30 }, data.focus_button_id == 9);
150150
lang_text_draw_centered(44, 95, 212, 405, 250, FONT_NORMAL_BLACK_ON_LIGHT);
151151

152-
button_border_draw(212, 436, 250, 30, data.focus_button_id == 10);
152+
button_border_draw({ 212, 436 }, { 250, 30 }, data.focus_button_id == 10);
153153
lang_text_draw_centered(44, 94, 212, 445, 250, FONT_NORMAL_BLACK_ON_LIGHT);
154154

155-
button_border_draw(18, 278, 184, 144, 0);
155+
button_border_draw({ 18, 278 }, { 184, 144 }, 0);
156156
ImageDraw::img_generic(ctx, image_id_from_group(GROUP_EDITOR_SCENARIO_IMAGE) + scenario_image_id(), 20, 280);
157157

158158
arrow_buttons_draw({0, 0}, image_arrows, 2);

src/window/editor/edit_request.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,29 @@ static void draw_foreground(void) {
5454
outer_panel_draw(vec2i{0, 100}, 38, 11);
5555
lang_text_draw(44, 21, 14, 114, FONT_LARGE_BLACK_ON_LIGHT);
5656

57-
button_border_draw(30, 152, 60, 25, data.focus_button_id == 1);
57+
button_border_draw({ 30, 152 }, { 60, 25 }, data.focus_button_id == 1);
5858
text_draw_number_centered_prefix(data.request.year, '+', 30, 158, 60, FONT_NORMAL_BLACK_ON_LIGHT);
5959
lang_text_draw_year(scenario_property_start_year() + data.request.year, 110, 158, FONT_NORMAL_BLACK_ON_LIGHT);
6060

6161
lang_text_draw(44, 72, 250, 158, FONT_NORMAL_BLACK_ON_LIGHT);
62-
button_border_draw(330, 152, 80, 25, data.focus_button_id == 2);
62+
button_border_draw({ 330, 152 }, { 80, 25 }, data.focus_button_id == 2);
6363
text_draw_number_centered(data.request.amount, 330, 158, 80, FONT_NORMAL_BLACK_ON_LIGHT);
6464

65-
button_border_draw(430, 152, 100, 25, data.focus_button_id == 3);
65+
button_border_draw({ 430, 152 }, { 100, 25 }, data.focus_button_id == 3);
6666
lang_text_draw_centered(23, data.request.resource, 430, 158, 100, FONT_NORMAL_BLACK_ON_LIGHT);
6767

6868
lang_text_draw(44, 24, 40, 196, FONT_NORMAL_BLACK_ON_LIGHT);
69-
button_border_draw(70, 190, 140, 25, data.focus_button_id == 4);
69+
button_border_draw({ 70, 190 }, { 140, 25 }, data.focus_button_id == 4);
7070
lang_text_draw_amount(8, 8, data.request.deadline_years, 80, 196, FONT_NORMAL_BLACK_ON_LIGHT);
7171

7272
lang_text_draw(44, 73, 300, 196, FONT_NORMAL_BLACK_ON_LIGHT);
73-
button_border_draw(400, 190, 80, 25, data.focus_button_id == 5);
73+
button_border_draw({ 400, 190 }, { 80, 25 }, data.focus_button_id == 5);
7474
text_draw_number_centered_prefix(data.request.kingdom, '+', 400, 196, 80, FONT_NORMAL_BLACK_ON_LIGHT);
7575

76-
button_border_draw(300, 234, 100, 25, data.focus_button_id == 7);
76+
button_border_draw({ 300, 234 }, { 100, 25 }, data.focus_button_id == 7);
7777
lang_text_draw_centered(18, 3, 300, 240, 100, FONT_NORMAL_BLACK_ON_LIGHT);
7878

79-
button_border_draw(10, 234, 250, 25, data.focus_button_id == 6);
79+
button_border_draw({ 10, 234 }, { 250, 25 }, data.focus_button_id == 6);
8080
lang_text_draw_centered(44, 25, 10, 240, 250, FONT_NORMAL_BLACK_ON_LIGHT);
8181

8282
graphics_reset_dialog();

src/window/hotkey_editor.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static void draw_foreground(void) {
6262

6363
for (int i = 0; i < NUM_BOTTOM_BUTTONS; i++) {
6464
generic_button* btn = &bottom_buttons[i];
65-
button_border_draw(btn->x, btn->y, btn->width, btn->height, data.focus_button == i + 1);
65+
button_border_draw({ btn->x, btn->y }, { btn->width, btn->height }, data.focus_button == i + 1);
6666
}
6767
graphics_reset_dialog();
6868
}

src/window/window_roadblock_info.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ void info_window_roadblock::window_info_foreground(object_info &c) {
112112
// building *b = building_get(c->building_id);
113113
// }
114114
} else {
115-
button_border_draw(c.offset.x + 80, c.offset.y + 16 * c.bgsize.y - 34, 16 * (c.bgsize.x - 10), 20, data.focus_button_id == 1 ? 1 : 0);
115+
button_border_draw(c.offset + vec2i{ 80, 16 * c.bgsize.y - 34 }, { 16 * (c.bgsize.x - 10), 20 }, data.focus_button_id == 1 ? 1 : 0);
116116
lang_text_draw_centered(98, 5, c.offset.x + 80, c.offset.y + 16 * c.bgsize.y - 30, 16 * (c.bgsize.x - 10), FONT_NORMAL_BLACK_ON_LIGHT);
117117

118118
}

0 commit comments

Comments
 (0)