Skip to content

Commit 8f2f10d

Browse files
author
s.kushnirenko
committed
ui: more options for rating window in js config
1 parent b5f0202 commit 8f2f10d

File tree

13 files changed

+70
-28
lines changed

13 files changed

+70
-28
lines changed

src/building/building_tax_collector.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void building_tax_collector::window_info_background(object_info &c) {
3535
building* b = building_get(c.building_id);
3636
int width = ui::label(106, 2, {44, 43});
3737
int amount = config_get(CONFIG_GP_CH_NEW_TAX_COLLECTION_SYSTEM) ? b->deben_storage : b->tax_income_or_storage;
38-
ui::label_num(8, 0, amount, {44 + width, 43});
38+
ui::label_amount(8, 0, amount, {44 + width, 43});
3939

4040
int tax_block = c.width_blocks * 16 / 2;
4141
ui::label(60, 1, {tax_block + 50, 44});

src/core/bstring.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,17 @@ class bstring {
6565

6666
inline ref ncat(const char* s, size_t cnt) { ::strncat(_data, s, cnt); return *this; }
6767

68-
inline ref append(const char* s) {
68+
template <typename... Args>
69+
inline ref append(const char* fmt, Args&&... args) {
6970
size_t size = this->len();
7071
char* dest = _data + size;
7172
size_t remain = _size - size;
72-
snprintf(dest, remain, "%s", s);
73+
if (remain > 0) {
74+
snprintf(dest, remain, fmt, std::forward<Args>(args)...);
75+
}
7376
return *this;
7477
}
78+
7579
inline ref append(char s) {
7680
size_t size = this->len();
7781
char* dest = _data + size;
@@ -83,6 +87,7 @@ class bstring {
8387

8488
return *this;
8589
}
90+
8691
inline ref itoa(int n) { ::snprintf(_data, _size, "%d", n); return *this; }
8792
inline int atoi() { return ::atoi(_data); }
8893
inline float atof() { return ::atof(_data); }

src/graphics/elements/lang_text.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ int lang_text_draw(int group, int number, int x_offset, int y_offset, font_t fon
1818
return text_draw(str, x_offset, y_offset, font, 0);
1919
}
2020

21-
int lang_text_draw(const char* str, int x_offset, int y_offset, font_t font) {
22-
return text_draw((const uint8_t*)str, x_offset, y_offset, font, 0);
21+
int lang_text_draw(pcstr str, vec2i pos, font_t font) {
22+
return text_draw((const uint8_t*)str, pos.x, pos.y, font, 0);
2323
}
24+
2425
int lang_text_draw_colored(int group, int number, int x_offset, int y_offset, font_t font, color color) {
2526
const uint8_t* str = lang_get_string(group, number);
2627
return text_draw(str, x_offset, y_offset, font, color);

src/graphics/elements/lang_text.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
#include "graphics/color.h"
44
#include "core/vec2i.h"
55
#include "graphics/font.h"
6+
#include "core/string.h"
67
#include "ui.h"
78

89
int lang_text_get_width(int group, int number, font_t font);
910
int lang_text_get_width(const char* str, font_t font);
1011

1112
int lang_text_draw(int group, int number, int x_offset, int y_offset, font_t font);
1213
inline int lang_text_draw(int group, int number, vec2i offset, font_t font) { return lang_text_draw(group, number, offset.x, offset.y, font); }
13-
int lang_text_draw(const char* str, int x_offset, int y_offset, font_t font);
14+
int lang_text_draw(pcstr str, vec2i pos, font_t font);
1415
int lang_text_draw_colored(int group, int number, int x_offset, int y_offset, font_t font, color color);
1516

1617
int lang_text_draw_left(int group, int number, int x_offset, int y_offset, font_t font);
@@ -33,6 +34,8 @@ int lang_text_draw_multiline(int group, int number, vec2i offset, int box_width,
3334

3435
namespace ui {
3536
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(pcstr, vec2i pos, font_t font = FONT_NORMAL_BLACK_ON_LIGHT, UiFlags_ flags = UiFlags_None, int box_width = 0);
38+
int label_amount(int group, int number, int amount, vec2i pos, font_t font = FONT_NORMAL_BLACK_ON_LIGHT, pcstr postfix = "");
3739
int label_percent(int amount, vec2i pos, font_t font = FONT_NORMAL_BLACK_ON_LIGHT);
40+
void image(e_image_id img, vec2i pos);
3841
}

src/graphics/elements/ui.cpp

+18-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "panel.h"
99
#include "game/game.h"
1010
#include "graphics/boilerplate.h"
11+
#include "io/gamefiles/lang.h"
1112

1213
#include <stack>
1314

@@ -56,6 +57,10 @@ bool ui::handle_mouse(const mouse *m) {
5657
return handle;
5758
}
5859

60+
pcstr ui::str(int group, int id) {
61+
return (pcstr)lang_get_string(group, id);
62+
}
63+
5964
int ui::button_hover(const mouse *m) {
6065
for (auto &btn : g_state.buttons) {
6166
if (is_button_hover(btn, g_state.offset())) {
@@ -96,16 +101,21 @@ image_button &ui::img_button(uint32_t group, uint32_t id, vec2i pos, vec2i size,
96101
}
97102

98103
int ui::label(int group, int number, vec2i pos, font_t font, UiFlags_ flags, int box_width) {
104+
pcstr str = (pcstr)lang_get_string(group, number);
105+
return label(str, pos, font, flags, box_width);
106+
}
107+
108+
int ui::label(pcstr label, vec2i pos, font_t font, UiFlags_ flags, int box_width) {
99109
const vec2i offset = g_state.offset();
100110
if (!!(flags & UiFlags_LabelCentered)) {
101-
lang_text_draw_centered(group, number, offset.x + pos.x, offset.y + pos.y, box_width, font);
111+
lang_text_draw_centered(label, offset.x + pos.x, offset.y + pos.y, box_width, font);
102112
return box_width;
103113
} else {
104-
return lang_text_draw(group, number, offset + pos, font);
114+
return lang_text_draw(label, offset + pos, font);
105115
}
106116
}
107117

108-
int ui::label_num(int group, int number, int amount, vec2i pos, font_t font, pcstr postfix) {
118+
int ui::label_amount(int group, int number, int amount, vec2i pos, font_t font, pcstr postfix) {
109119
const vec2i offset = g_state.offset();
110120
return lang_text_draw_amount(group, number, amount, offset.x + pos.x, offset.y + pos.y, font, postfix);
111121
}
@@ -115,6 +125,11 @@ int ui::label_percent(int amount, vec2i pos, font_t font) {
115125
return text_draw_percentage(amount, offset.x + pos.x, offset.y + pos.y, font);
116126
}
117127

128+
void ui::image(e_image_id img, vec2i pos) {
129+
painter ctx = game.painter();
130+
ImageDraw::img_generic(ctx, image_group(img), pos);
131+
}
132+
118133
void ui::panel(vec2i pos, vec2i size, UiFlags_ flags) {
119134
const vec2i offset = g_state.offset();
120135
if (!!(flags & UiFlags_PanelOuter)) {

src/graphics/elements/ui.h

+1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ void begin_frame();
2222
void begin_widget(vec2i offset, bool relative = false);
2323
void end_widget();
2424
bool handle_mouse(const mouse *m);
25+
pcstr str(int group, int id);
2526

2627
}

src/graphics/image_groups.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ enum e_pack {
291291

292292
#define GROUP_SYSTEM_GRAPHICS PACK_UNLOADED, 0
293293
#define GROUP_FORT_FORMATIONS PACK_UNLOADED, 1 // 197
294-
#define GROUP_ADVISOR_RATINGS_BACKGROUND PACK_UNLOADED, 2 // 195
294+
//#define GROUP_ADVISOR_RATINGS_BACKGROUND PACK_UNLOADED, 2 // 195
295295
#define GROUP_BIGPEOPLE PACK_UNLOADED, 3
296296
#define GROUP_PROMO_3 PACK_UNLOADED, 4 // 188
297297
#define GROUP_PROMO_2 PACK_UNLOADED, 5 // 187

src/graphics/indexes.h

+1
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,4 @@ REGISTER_IMG(IMG_BUILDING_APOTHECARY, 128)
130130
REGISTER_IMG(IMG_PERSONAL_MANSION, 129)
131131
REGISTER_IMG(IMG_ADVISOR_RATING_ICON, 130)
132132
REGISTER_IMG(IMG_ADVISOR_ICONS, 131)
133+
REGISTER_IMG(IMG_ADVISOR_BACKGROUND, 132)

src/scripts/images.js

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ images = [
151151
{img: IMG_PERSONAL_MANSION, pack:PACK_GENERAL, id:85},
152152
{img: IMG_ADVISOR_RATING_ICON, pack:PACK_GENERAL, id:128, offset:3},
153153
{img: IMG_ADVISOR_ICONS, pack:PACK_GENERAL, id:128, offset:0},
154+
{img: IMG_ADVISOR_BACKGROUND, pack:PACK_UNLOADED, id:2, offset:0},
154155
]
155156

156157
cart_offsets = [

src/scripts/ui.js

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ advisor_rating_window = {
3131
outer_panel_size : {w:40, h:27},
3232
advisor_icon_image : IMG_ADVISOR_RATING_ICON,
3333
advisor_icon_pos : {x: 10, y: 10},
34+
header_pos : {x:60, y:17},
35+
header_population_pos : {x: 160, y:20},
36+
background_img : IMG_ADVISOR_BACKGROUND,
37+
background_img_pos: {x:60, y:38},
38+
column_offset : {x: 30, y:-11}
3439
}
3540

3641
empire_window = {

src/widget/top_menu.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ void menu_bar_draw(const std::span<menu_bar_item>& items) {
337337
int current_id = std::distance(items.begin(), &item) + 1;
338338
font_t font = (current_id == data.focus_menu_id) ? hightlight_font : FONT_NORMAL_BLACK_ON_LIGHT;
339339
int text_length = item.text_raw
340-
? lang_text_draw(item.text_raw, x_offset, data.y_offset, font)
341-
: lang_text_draw(item.text_group, 0, x_offset, data.y_offset, font);
340+
? lang_text_draw(item.text_raw, vec2i{x_offset, data.y_offset}, font)
341+
: lang_text_draw(item.text_group, 0, vec2i{x_offset, data.y_offset}, font);
342342
x_offset += text_length;
343343
item.x_end = x_offset;
344344
x_offset += data.spacing;
@@ -405,12 +405,12 @@ void menu_draw(menu_bar_item& menu, int focus_item_id) {
405405
// Set color/font on the menu item mouse hover
406406
if (i == focus_item_id - 1) {
407407
sub->text_raw
408-
? lang_text_draw(sub->text_raw, menu.x_start + 8, y_offset, FONT_NORMAL_YELLOW)
409-
: lang_text_draw(sub->text_group, sub->text_number, menu.x_start + 8, y_offset, FONT_NORMAL_YELLOW);
408+
? lang_text_draw(sub->text_raw, vec2i{menu.x_start + 8, y_offset}, FONT_NORMAL_YELLOW)
409+
: lang_text_draw(sub->text_group, sub->text_number, vec2i{menu.x_start + 8, y_offset}, FONT_NORMAL_YELLOW);
410410
} else {
411411
sub->text_raw
412-
? lang_text_draw(sub->text_raw, menu.x_start + 8, y_offset, FONT_NORMAL_BLACK_ON_LIGHT)
413-
: lang_text_draw(sub->text_group, sub->text_number, menu.x_start + 8, y_offset, FONT_NORMAL_BLACK_ON_LIGHT);
412+
? lang_text_draw(sub->text_raw, vec2i{menu.x_start + 8, y_offset}, FONT_NORMAL_BLACK_ON_LIGHT)
413+
: lang_text_draw(sub->text_group, sub->text_number, vec2i{menu.x_start + 8, y_offset}, FONT_NORMAL_BLACK_ON_LIGHT);
414414
}
415415
y_offset += data.item_height;
416416
}

src/window/advisor/advisor_ratings.cpp

+20-10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ struct advisor_rating_window {
2424
vec2i outer_panel_size;
2525
e_image_id advisor_icon_image;
2626
vec2i advisor_icon_pos;
27+
vec2i header_pos;
28+
vec2i header_population_pos;
29+
vec2i background_img_pos;
30+
e_image_id background_img;
31+
vec2i column_offset;
2732
};
2833

2934
advisor_rating_window g_advisor_rating_window;
@@ -35,6 +40,11 @@ void config_load_advisor_rating() {
3540
w.outer_panel_size = arch.r_size2i("outer_panel_size");
3641
w.advisor_icon_image = arch.r_image("advisor_icon_image");
3742
w.advisor_icon_pos = arch.r_vec2i("advisor_icon_pos");
43+
w.header_pos = arch.r_vec2i("header_pos");
44+
w.header_population_pos = arch.r_vec2i("header_population_pos");
45+
w.background_img = arch.r_image("background_img");
46+
w.background_img_pos = arch.r_vec2i("background_img_pos");
47+
w.column_offset = arch.r_vec2i("column_offset");
3848
});
3949
}
4050

@@ -69,6 +79,7 @@ static void draw_rating_column(int x_offset, int y_offset, int value, int has_re
6979
}
7080

7181
static void draw_rating(int id, int value, int open_play, int goal) {
82+
auto &w = g_advisor_rating_window;
7283
// int value = city_rating_culture();
7384
int enabled = !open_play && goal;
7485
button_border_draw(rating_buttons[id].x, rating_buttons[id].y, rating_buttons[id].width, rating_buttons[id].height, focus_button_id == SELECTED_RATING_CULTURE);
@@ -77,28 +88,27 @@ static void draw_rating(int id, int value, int open_play, int goal) {
7788
int width = text_draw_number(enabled ? goal : 0, '@', " ", rating_buttons[id].x + 5, rating_buttons[id].y + 45, FONT_NORMAL_BLACK_ON_LIGHT);
7889
lang_text_draw(53, 5, rating_buttons[id].x + 5 + width, rating_buttons[id].y + 45, FONT_NORMAL_BLACK_ON_LIGHT);
7990
int has_reached = !enabled || value >= goal;
80-
draw_rating_column(rating_buttons[id].x + 30, rating_buttons[id].y, value, has_reached);
91+
draw_rating_column(rating_buttons[id].x + w.column_offset.x, rating_buttons[id].y + w.column_offset.y, value, has_reached);
8192
}
8293

8394
static int draw_background() {
8495
return g_advisor_rating_window.outer_panel_size.y;
8596
}
8697

8798
static void draw_foreground() {
88-
painter ctx = game.painter();
89-
9099
auto &w = g_advisor_rating_window;
91100
ui::panel(w.outer_panel_pos, w.outer_panel_size, UiFlags_PanelOuter);
92101
ui::icon(w.advisor_icon_pos, ADVISOR_RATINGS);
93102

94-
int width = lang_text_draw(53, 0, 60, 12, FONT_LARGE_BLACK_ON_LIGHT);
95-
if (!winning_population() || scenario_is_open_play()) {
96-
lang_text_draw(53, 7, 80 + width, 17, FONT_NORMAL_BLACK_ON_LIGHT);
97-
} else {
98-
width += lang_text_draw(53, 6, 80 + width, 17, FONT_NORMAL_BLACK_ON_LIGHT);
99-
text_draw_number(winning_population(), '@', ")", 80 + width, 17, FONT_NORMAL_BLACK_ON_LIGHT);
103+
ui::label(53, 0, w.header_pos, FONT_LARGE_BLACK_ON_LIGHT);
104+
bstring128 caption = (pcstr)ui::str(53, 7);
105+
if (!(!winning_population() || scenario_is_open_play())) {
106+
caption = (pcstr)ui::str(53, 6);
107+
caption.append("%u", winning_population());
100108
}
101-
ImageDraw::img_generic(ctx, image_id_from_group(GROUP_ADVISOR_RATINGS_BACKGROUND), 60, 48 - 10);
109+
110+
ui::label(caption, w.header_population_pos, FONT_NORMAL_BLACK_ON_LIGHT);
111+
ui::image(w.background_img, w.background_img_pos);
102112

103113
int open_play = scenario_is_open_play();
104114

src/window/main_menu.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ static void draw_foreground() {
106106
graphics_set_to_dialog();
107107

108108
for (int i = 0; i < std::size(data.buttons); i++) {
109-
auto text = data.buttons_text[i];
109+
auto text = (i >= data.buttons_text.size()) ? std::pair{0, 0} : data.buttons_text[i];
110110
large_label_draw(data.buttons[i].x, data.buttons[i].y, data.buttons[i].width / 16, data.focus_button_id == i + 1 ? 1 : 0);
111111
lang_text_draw_centered(text.first, text.second, data.button_pos.x, data.button_pos.y + 40 * i + 6, data.button_size.x, FONT_NORMAL_BLACK_ON_LIGHT);
112112
}

0 commit comments

Comments
 (0)