Skip to content

Commit b26bbc2

Browse files
author
s.kushnirenko
committed
ui: simplify draw label_num/label_percent
1 parent 4573a7b commit b26bbc2

11 files changed

+71
-47
lines changed

src/building/building_tax_collector.cpp

+12-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "city/object_info.h"
55
#include "figure/figure.h"
66
#include "game/resource.h"
7+
#include "city/finance.h"
78
#include "graphics/elements/panel.h"
89
#include "graphics/view/view.h"
910
#include "graphics/elements/lang_text.h"
@@ -15,27 +16,27 @@
1516
#include "game/game.h"
1617

1718
void building_tax_collector::window_info_background(object_info &c) {
18-
ui::begin_window(c.offset);
19+
ui::begin_widget(c.offset);
1920

2021
c.help_id = e_text_building_tax_collector;
2122
c.go_to_advisor.left_a = ADVISOR_RATINGS;
2223
c.go_to_advisor.left_b = ADVISOR_FINANCIAL;
2324

2425
window_building_play_sound(&c, snd::get_building_info_sound("tax_collector"));
2526

26-
{
27+
{ // header
2728
ui::panel(vec2i{0, 0}, {c.width_blocks, c.height_blocks}, UiFlags_PanelOuter);
28-
ui::label(106, 0, vec2i{0, 10}, 16 * c.width_blocks, FONT_LARGE_BLACK_ON_LIGHT, UiFlags_LabelCentered);
29+
ui::label(106, 0, vec2i{0, 10}, FONT_LARGE_BLACK_ON_LIGHT, UiFlags_LabelCentered, 16 * c.width_blocks);
2930
ui::icon(vec2i{16, 36}, RESOURCE_DEBEN);
3031
}
3132

3233
building* b = building_get(c.building_id);
33-
int width = lang_text_draw(106, 2, c.offset.x + 44, c.offset.y + 43, FONT_NORMAL_BLACK_ON_LIGHT);
34-
if (config_get(CONFIG_GP_CH_NEW_TAX_COLLECTION_SYSTEM)) {
35-
lang_text_draw_amount(8, 0, b->deben_storage, c.offset.x + 44 + width, c.offset.y + 43, FONT_NORMAL_BLACK_ON_LIGHT);
36-
} else {
37-
lang_text_draw_amount(8, 0, b->tax_income_or_storage, c.offset.x + 44 + width, c.offset.y + 43, FONT_NORMAL_BLACK_ON_LIGHT);
38-
}
34+
int width = ui::label(106, 2, {44, 43});
35+
int amount = config_get(CONFIG_GP_CH_NEW_TAX_COLLECTION_SYSTEM) ? b->deben_storage : b->tax_income_or_storage;
36+
ui::label_num(8, 0, amount, {44 + width, 43});
37+
38+
ui::label(60, 1, {c.width_blocks * 16 / 2 + 50, 40});
39+
ui::label_percent(city_finance_tax_percentage(), {c.width_blocks * 16 / 2 + 150, 40});
3940

4041
if (!c.has_road_access) {
4142
window_building_draw_description(c, 69, 25);
@@ -55,6 +56,8 @@ void building_tax_collector::window_info_background(object_info &c) {
5556

5657
inner_panel_draw(c.offset.x + 16, c.offset.y + 136, c.width_blocks - 2, 4);
5758
window_building_draw_employment(&c, 142);
59+
60+
ui::end_widget();
5861
}
5962

6063
void building_tax_collector::update_month() {

src/graphics/elements/arrow_button.h

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#ifndef GRAPHICS_ARROW_BUTTON_H
2-
#define GRAPHICS_ARROW_BUTTON_H
1+
#pragma once
32

43
#include "input/mouse.h"
54

@@ -18,11 +17,4 @@ typedef struct {
1817

1918
void arrow_buttons_draw(int x, int y, arrow_button* buttons, int num_buttons, bool tiny = false);
2019

21-
int arrow_buttons_handle_mouse(const mouse* m,
22-
int x,
23-
int y,
24-
arrow_button* buttons,
25-
int num_buttons,
26-
int* focus_button_id);
27-
28-
#endif // GRAPHICS_ARROW_BUTTON_H
20+
int arrow_buttons_handle_mouse(const mouse* m, int x, int y, arrow_button* buttons, int num_buttons, int* focus_button_id);

src/graphics/elements/lang_text.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ int lang_text_draw_amount(int group, int number, int amount, int x_offset, int y
6262
}
6363
return desc_offset_x + lang_text_draw(group, number + amount_offset, x_offset + desc_offset_x, y_offset, font);
6464
}
65+
6566
int lang_text_draw_year(int year, int x_offset, int y_offset, font_t font) {
6667
int width = 0;
6768
if (year >= 0) {

src/graphics/elements/lang_text.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@ void lang_text_draw_month_year_max_width(int month, int year, int x_offset, int
3232
int lang_text_draw_multiline(int group, int number, vec2i offset, int box_width, font_t font);
3333

3434
namespace ui {
35-
void label(int group, int number, vec2i pos, int box_width, font_t font, UiFlags_ flags);
35+
int label(int group, int number, vec2i pos, font_t font = FONT_NORMAL_BLACK_ON_LIGHT, UiFlags_ flags = UiFlags_None, int box_width = 0);
36+
int label_num(int group, int number, int amount, vec2i pos, font_t font = FONT_NORMAL_BLACK_ON_LIGHT, pcstr postfix = "");
37+
int label_percent(int amount, vec2i pos, font_t font = FONT_NORMAL_BLACK_ON_LIGHT);
3638
}

src/graphics/elements/ui.cpp

+35-12
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,45 @@
77
#include "game/game.h"
88
#include "graphics/boilerplate.h"
99

10+
#include <stack>
11+
1012
namespace ui {
1113
struct state {
12-
vec2i offset;
14+
std::stack<vec2i> _offset;
1315
std::vector<generic_button> buttons;
1416
std::vector<image_button> img_buttons;
17+
18+
inline const vec2i offset() { return _offset.empty() ? vec2i{0, 0} : _offset.top(); }
1519
};
1620

1721
state g_state;
1822
generic_button dummy;
1923
}
2024

21-
void ui::begin_window(vec2i offset) {
22-
g_state.offset = offset;
25+
void ui::begin_widget(vec2i offset) {
26+
g_state._offset.push(offset);
2327
g_state.buttons.clear();
2428
g_state.img_buttons.clear();
2529
}
2630

31+
void ui::end_widget() {
32+
if (!g_state._offset.empty()) {
33+
g_state._offset.pop();
34+
}
35+
}
36+
2737
bool ui::handle_mouse(const mouse *m) {
2838
bool handle = false;
2939
int tmp_btn = 0;
30-
handle |= !!generic_buttons_handle_mouse(m, g_state.offset, g_state.buttons, tmp_btn);
31-
handle |= image_buttons_handle_mouse(m, g_state.offset, g_state.img_buttons, tmp_btn);
40+
handle |= !!generic_buttons_handle_mouse(m, g_state.offset(), g_state.buttons, tmp_btn);
41+
handle |= image_buttons_handle_mouse(m, g_state.offset(), g_state.img_buttons, tmp_btn);
3242

3343
return handle;
3444
}
3545

3646
int ui::button_hover(const mouse *m) {
3747
for (auto &btn : g_state.buttons) {
38-
if (is_button_hover(btn, g_state.offset)) {
48+
if (is_button_hover(btn, g_state.offset())) {
3949
return (std::distance(&g_state.buttons.front(), &btn) + 1);
4050
}
4151
}
@@ -44,7 +54,7 @@ int ui::button_hover(const mouse *m) {
4454
}
4555

4656
generic_button &ui::button(pcstr label, vec2i pos, vec2i size) {
47-
const vec2i offset = g_state.offset;
57+
const vec2i offset = g_state.offset();
4858

4959
g_state.buttons.push_back({pos.x, pos.y, size.x + 4, size.y + 4, button_none, button_none, 0, 0});
5060
int focused = is_button_hover(g_state.buttons.back(), offset);
@@ -60,7 +70,7 @@ generic_button &ui::button(uint32_t id) {
6070
}
6171

6272
image_button &ui::img_button(uint32_t group, uint32_t id, vec2i pos, vec2i size, int offset) {
63-
const vec2i img_offset = g_state.offset;
73+
const vec2i img_offset = g_state.offset();
6474
const mouse *m = mouse_get();
6575

6676
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});
@@ -72,22 +82,35 @@ image_button &ui::img_button(uint32_t group, uint32_t id, vec2i pos, vec2i size,
7282
return g_state.img_buttons.back();
7383
}
7484

75-
void ui::label(int group, int number, vec2i pos, int box_width, font_t font, UiFlags_ flags) {
76-
const vec2i offset = g_state.offset;
85+
int ui::label(int group, int number, vec2i pos, font_t font, UiFlags_ flags, int box_width) {
86+
const vec2i offset = g_state.offset();
7787
if (!!(flags & UiFlags_LabelCentered)) {
7888
lang_text_draw_centered(group, number, offset.x + pos.x, offset.y + pos.y, box_width, font);
89+
return box_width;
90+
} else {
91+
return lang_text_draw(group, number, offset + pos, font);
7992
}
8093
}
8194

95+
int ui::label_num(int group, int number, int amount, vec2i pos, font_t font, pcstr postfix) {
96+
const vec2i offset = g_state.offset();
97+
return lang_text_draw_amount(group, number, amount, offset.x + pos.x, offset.y + pos.y, font, postfix);
98+
}
99+
100+
int ui::label_percent(int amount, vec2i pos, font_t font) {
101+
const vec2i offset = g_state.offset();
102+
return text_draw_percentage(amount, offset.x + pos.x, offset.y + pos.y, font);
103+
}
104+
82105
void ui::panel(vec2i pos, vec2i size, UiFlags_ flags) {
83-
const vec2i offset = g_state.offset;
106+
const vec2i offset = g_state.offset();
84107
if (!!(flags & UiFlags_PanelOuter)) {
85108
outer_panel_draw(offset + pos, size.x, size.y);
86109
}
87110
}
88111

89112
void ui::icon(vec2i pos, e_resource img) {
90-
const vec2i offset = g_state.offset;
113+
const vec2i offset = g_state.offset();
91114
painter ctx = game.painter();
92115
ImageDraw::img_generic(ctx, image_id_resource_icon(RESOURCE_DEBEN), offset.x + pos.x, offset.y + pos.y);
93116
}

src/graphics/elements/ui.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
struct mouse;
1212

1313
enum UiFlags_ {
14+
UiFlags_None = 0,
1415
UiFlags_LabelCentered = 1 << 1,
1516
UiFlags_PanelOuter = 1 << 2,
1617
};
1718

1819
namespace ui {
1920

20-
void begin_window(vec2i offset);
21+
void begin_widget(vec2i offset);
22+
void end_widget();
2123
bool handle_mouse(const mouse *m);
2224

2325
}

src/graphics/text.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,13 @@ int text_draw_money(int value, int x_offset, int y_offset, font_t font) {
352352

353353
return text_draw(str, x_offset, y_offset, font, 0);
354354
}
355+
355356
int text_draw_percentage(int value, int x_offset, int y_offset, font_t font) {
356357
uint8_t str[NUMBER_BUFFER_LENGTH];
357358
number_to_string(str, value, '@', "%");
358359
return text_draw(str, x_offset, y_offset, font, 0);
359360
}
361+
360362
int text_draw_label_and_number(const char* label,
361363
int value,
362364
const char* postfix,

src/window/advisor/financial.cpp src/window/advisor/advisor_financial.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "financial.h"
1+
#include "advisor_financial.h"
22

33
#include "city/finance.h"
44
#include "core/calc.h"
@@ -14,8 +14,9 @@
1414

1515
static void button_change_taxes(int is_down, int param2);
1616

17-
static arrow_button arrow_buttons_taxes[]
18-
= {{180, 75, 17, 24, button_change_taxes, 1, 0}, {204, 75, 15, 24, button_change_taxes, 0, 0}};
17+
static arrow_button arrow_buttons_taxes[] = {
18+
{180, 75, 17, 24, button_change_taxes, 1, 0}, {204, 75, 15, 24, button_change_taxes, 0, 0}
19+
};
1920

2021
static int arrow_button_focus;
2122

@@ -25,6 +26,7 @@ static void draw_row(int group, int number, int* y, int value_last_year, int val
2526
text_draw_number(value_this_year, '@', " ", 430, *y, FONT_NORMAL_BLACK_ON_LIGHT);
2627
*y += 15;
2728
}
29+
2830
static int draw_background() {
2931
painter ctx = game.painter();
3032
outer_panel_draw(vec2i{0, 0}, 40, ADVISOR_HEIGHT);
@@ -55,8 +57,7 @@ static int draw_background() {
5557
// percentage taxpayers
5658
width = text_draw_percentage(city_finance_percentage_taxed_people(), 70, 103, FONT_NORMAL_WHITE_ON_DARK);
5759
width += lang_text_draw(60, 5, 70 + width, 103, FONT_NORMAL_WHITE_ON_DARK);
58-
lang_text_draw_amount(
59-
60, 22, city_finance_estimated_tax_uncollected(), 70 + width, 103, FONT_NORMAL_WHITE_ON_DARK, "Db");
60+
lang_text_draw_amount(60, 22, city_finance_estimated_tax_uncollected(), 70 + width, 103, FONT_NORMAL_WHITE_ON_DARK, "Db");
6061

6162
// table headers
6263
lang_text_draw(60, 6, 270, 133, FONT_NORMAL_BLACK_ON_LIGHT);
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
3+
#include "window/advisors.h"
4+
5+
const advisor_window_type* window_advisor_financial(void);

src/window/advisor/financial.h

-8
This file was deleted.

src/window/window_building_info.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ static void draw_background() {
687687
}
688688

689689
static void draw_foreground() {
690-
ui::begin_window(g_building_info_context.offset);
690+
ui::begin_widget(g_building_info_context.offset);
691691
auto &context = g_building_info_context;
692692

693693
// building-specific buttons
@@ -823,6 +823,7 @@ static void draw_foreground() {
823823
window_invalidate();
824824
});
825825
}
826+
ui::end_widget();
826827
}
827828

828829
static int handle_specific_building_info_mouse(const mouse *m) {

0 commit comments

Comments
 (0)