Skip to content

Commit 8e257a1

Browse files
author
s.kushnirenko
committed
ui: use e_font instead font_t, add label for simplified widgets
1 parent 70edd59 commit 8e257a1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+321
-260
lines changed

src/content/imagepak.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ static void create_special_fonts(std::vector<image_t>* images, int start_index)
243243
rect->input.height = img->height + 2;
244244
}
245245
}
246-
static bool is_font_glyph_in_range(const image_t &img, font_t font_start, font_t font_end) {
246+
static bool is_font_glyph_in_range(const image_t &img, e_font font_start, e_font font_end) {
247247
int i = img.sgx_index - 201;
248248
int starting_offset = font_definition_for(font_start)->image_offset;
249249
int ending_offset = -1;

src/dev/debug.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static const uint8_t* font_test_str = (uint8_t*)(char*)"abcdefghijklmnopqrstuvwx
6565
static const uint8_t* font_test_str_ascii = (uint8_t*)(char*)"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!\"%*()-+=:;'?\\/,._";
6666
static const uint8_t* font_test_str_extended = (uint8_t*)(char*)"äáàâëéèêïíìîöóòôüúùûçñæßÄÉÜÑÆŒœÁÂÀÊÈÍÎÌÓÔÒÖÚÛÙ¡¿^°ÅØåø";
6767

68-
static void debug_font_line(int* y, font_t font) {
68+
static void debug_font_line(int* y, e_font font) {
6969
int line_height = font_definition_for(font)->line_height;
7070
if (line_height < 11)
7171
line_height = 11;
@@ -90,13 +90,13 @@ void debug_font_test() {
9090
debug_font_line(&y, FONT_SMALL_SHADED);
9191
}
9292

93-
void debug_text(painter &ctx, uint8_t* str, int x, int y, int indent, const char* text, int value, color color, font_t font) {
93+
void debug_text(painter &ctx, uint8_t* str, int x, int y, int indent, const char* text, int value, color color, e_font font) {
9494
text_draw(ctx, string_from_ascii(text), x, y, font, color);
9595
string_from_int(str, value, 0);
9696
text_draw(ctx, str, x + indent, y, font, color);
9797
}
9898

99-
void debug_text_a(painter &ctx, uint8_t* str, int x, int y, int indent, const char* text, color color, font_t font) {
99+
void debug_text_a(painter &ctx, uint8_t* str, int x, int y, int indent, const char* text, color color, e_font font) {
100100
text_draw(ctx, string_from_ascii(text), x, y, font, color);
101101
}
102102

src/dev/debug.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ extern int g_debug_render;
6464

6565
void handle_debug_hotkeys(const hotkeys* h);
6666

67-
void debug_font_test();
67+
void debug_e_fontest();
6868

69-
void debug_text(painter &ctx, uint8_t* str, int x, int y, int indent, const char* text, int value, color color = COLOR_WHITE, font_t font = FONT_SMALL_OUTLINED);
70-
void debug_text_a(painter &ctx, uint8_t* str, int x, int y, int indent, const char* text, color color = COLOR_WHITE, font_t font = FONT_SMALL_OUTLINED);
69+
void debug_text(painter &ctx, uint8_t* str, int x, int y, int indent, const char* text, int value, color color = COLOR_WHITE, e_font font = FONT_SMALL_OUTLINED);
70+
void debug_text_a(painter &ctx, uint8_t* str, int x, int y, int indent, const char* text, color color = COLOR_WHITE, e_font font = FONT_SMALL_OUTLINED);
7171
void debug_text_float(uint8_t* str, int x, int y, int indent, const char* text, double value, color color = COLOR_WHITE);
7272
void debug_text_dual_left(uint8_t* str, int x, int y, int indent, int indent2, const char* text, int value1, int value2, color color = COLOR_WHITE);
7373

src/graphics/boilerplate.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -652,12 +652,12 @@ static void draw_footprint_size_any(int image_id, int x, int y, int size, color
652652
}
653653
}
654654
}
655-
static color base_color_for_font(font_t font) {
655+
static color base_color_for_font(e_font font) {
656656
if (font == FONT_SMALL_PLAIN || font == FONT_SMALL_OUTLINED || font == FONT_SMALL_SHADED)
657657
return COLOR_FONT_PLAIN;
658658
return COLOR_MASK_NONE;
659659
}
660-
static void draw_multibyte_letter(font_t font, const image_t* img, int x, int y, color color_mask, float scale) {
660+
static void draw_multibyte_letter(e_font font, const image_t* img, int x, int y, color color_mask, float scale) {
661661
// switch (font) {
662662
// case FONT_NORMAL_WHITE_ON_DARK:
663663
//// graphics_renderer()->draw_image(img, x + 1, y + 1, 0xff311c10, scale, false);
@@ -733,7 +733,7 @@ void ImageDraw::img_from_below(painter &ctx, int image_id, int x, int y, color c
733733
graphics_renderer()->draw_image(ctx, img, x, y - img->height, color_mask, scale);
734734
}
735735

736-
void ImageDraw::img_letter(painter &ctx,font_t font, int letter_id, int x, int y, color color_mask, float scale) {
736+
void ImageDraw::img_letter(painter &ctx, e_font font, int letter_id, int x, int y, color color_mask, float scale) {
737737
const image_t* img = image_letter(letter_id);
738738
if (letter_id >= IMAGE_FONT_MULTIBYTE_OFFSET) {
739739
// draw_multibyte_letter(font, img, x, y, color_mask, scale);

src/graphics/boilerplate.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ inline void img_sprite(painter &ctx, int image_id, vec2i p, color color_mask = C
3535

3636
void img_ornament(painter &ctx, int image_id, int base_id, int x, int y, color color_mask = COLOR_MASK_NONE, float scale = 1.0f);
3737
void img_from_below(painter &ctx, int image_id, int x, int y, color color_mask = COLOR_MASK_NONE, float scale = 1.0f);
38-
void img_letter(painter &ctx, font_t font, int letter_id, int x, int y, color color_mask = COLOR_MASK_NONE, float scale = 1.0f);
38+
void img_letter(painter &ctx, e_font font, int letter_id, int x, int y, color color_mask = COLOR_MASK_NONE, float scale = 1.0f);
3939
void img_background(painter &ctx, int image_id, float scale = 1.0f);
4040
void isometric(painter &ctx, int image_id, vec2i pos, color color_mask = COLOR_MASK_NONE, float scale = 1.0f);
4141
const image_t* isometric_from_drawtile(painter &ctx, int image_id, vec2i pos, color color_mask = COLOR_MASK_NONE, bool alpha = false);

src/graphics/elements/lang_text.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,53 @@
44
#include "graphics/text.h"
55
#include "io/gamefiles/lang.h"
66

7-
int lang_text_get_width(int group, int number, font_t font) {
7+
int lang_text_get_width(int group, int number, e_font font) {
88
const uint8_t* str = lang_get_string(group, number);
99
return text_get_width(str, font) + font_definition_for(font)->space_width;
1010
}
1111

12-
int lang_text_get_width(const char* str, font_t font) {
12+
int lang_text_get_width(const char* str, e_font font) {
1313
return text_get_width((const uint8_t*)str, font) + font_definition_for(font)->space_width;
1414
}
1515

16-
int lang_text_draw(int group, int number, int x_offset, int y_offset, font_t font) {
16+
int lang_text_draw(int group, int number, int x_offset, int y_offset, e_font font) {
1717
const uint8_t* str = lang_get_string(group, number);
1818
return text_draw(str, x_offset, y_offset, font, 0);
1919
}
2020

21-
int lang_text_draw(pcstr str, vec2i pos, font_t font) {
21+
int lang_text_draw(pcstr str, vec2i pos, e_font font) {
2222
return text_draw((const uint8_t*)str, pos.x, pos.y, font, 0);
2323
}
2424

25-
int lang_text_draw_colored(int group, int number, int x_offset, int y_offset, font_t font, color color) {
25+
int lang_text_draw_colored(int group, int number, int x_offset, int y_offset, e_font font, color color) {
2626
const uint8_t* str = lang_get_string(group, number);
2727
return text_draw(str, x_offset, y_offset, font, color);
2828
}
2929

30-
int lang_text_draw_left(int group, int number, int x_offset, int y_offset, font_t font) {
30+
int lang_text_draw_left(int group, int number, int x_offset, int y_offset, e_font font) {
3131
const uint8_t* str = lang_get_string(group, number);
3232
return text_draw(str, x_offset - text_get_width(str, font), y_offset, font, 0);
3333
}
34-
int lang_text_draw_left_colored(int group, int number, int x_offset, int y_offset, font_t font, color color) {
34+
int lang_text_draw_left_colored(int group, int number, int x_offset, int y_offset, e_font font, color color) {
3535
const uint8_t* str = lang_get_string(group, number);
3636
return text_draw(str, x_offset - text_get_width(str, font), y_offset, font, color);
3737
}
3838

39-
void lang_text_draw_centered(int group, int number, int x_offset, int y_offset, int box_width, font_t font) {
39+
void lang_text_draw_centered(int group, int number, int x_offset, int y_offset, int box_width, e_font font) {
4040
const uint8_t* str = lang_get_string(group, number);
4141
text_draw_centered(str, x_offset, y_offset, box_width, font, 0);
4242
}
4343

44-
void lang_text_draw_centered(const char* text, int x_offset, int y_offset, int box_width, font_t font) {
44+
void lang_text_draw_centered(const char* text, int x_offset, int y_offset, int box_width, e_font font) {
4545
text_draw_centered((const uint8_t*)text, x_offset, y_offset, box_width, font, 0);
4646
}
4747

48-
void lang_text_draw_centered_colored(int group, int number, int x_offset, int y_offset, int box_width, font_t font, color color) {
48+
void lang_text_draw_centered_colored(int group, int number, int x_offset, int y_offset, int box_width, e_font font, color color) {
4949
const uint8_t* str = lang_get_string(group, number);
5050
text_draw_centered(str, x_offset, y_offset, box_width, font, color);
5151
}
5252

53-
int lang_text_draw_amount(int group, int number, int amount, int x_offset, int y_offset, font_t font, const char* postfix) {
53+
int lang_text_draw_amount(int group, int number, int amount, int x_offset, int y_offset, e_font font, const char* postfix) {
5454
int amount_offset = 1;
5555
if (amount == 1 || amount == -1)
5656
amount_offset = 0;
@@ -64,7 +64,7 @@ int lang_text_draw_amount(int group, int number, int amount, int x_offset, int y
6464
return desc_offset_x + lang_text_draw(group, number + amount_offset, x_offset + desc_offset_x, y_offset, font);
6565
}
6666

67-
int lang_text_draw_year(int year, int x_offset, int y_offset, font_t font) {
67+
int lang_text_draw_year(int year, int x_offset, int y_offset, e_font font) {
6868
int width = 0;
6969
if (year >= 0) {
7070
int use_year_ad = locale_year_before_ad();
@@ -81,7 +81,7 @@ int lang_text_draw_year(int year, int x_offset, int y_offset, font_t font) {
8181
}
8282
return width;
8383
}
84-
void lang_text_draw_month_year_max_width(int month, int year, int x_offset, int y_offset, int box_width, font_t font, color color) {
84+
void lang_text_draw_month_year_max_width(int month, int year, int x_offset, int y_offset, int box_width, e_font font, color color) {
8585
int month_width = lang_text_get_width(25, month, font);
8686
int ad_bc_width = lang_text_get_width(20, year >= 0 ? 1 : 0, font);
8787
int space_width = font_definition_for(font)->space_width;
@@ -113,7 +113,7 @@ void lang_text_draw_month_year_max_width(int month, int year, int x_offset, int
113113
}
114114
}
115115

116-
int lang_text_draw_multiline(int group, int number, vec2i offset, int box_width, font_t font) {
116+
int lang_text_draw_multiline(int group, int number, vec2i offset, int box_width, e_font font) {
117117
const uint8_t* str = lang_get_string(group, number);
118118
return text_draw_multiline(str, offset.x, offset.y, box_width, font, 0);
119119
}

src/graphics/elements/lang_text.h

+16-16
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,28 @@
66
#include "core/string.h"
77
#include "ui.h"
88

9-
int lang_text_get_width(int group, int number, font_t font);
10-
int lang_text_get_width(const char* str, font_t font);
9+
int lang_text_get_width(int group, int number, e_font font);
10+
int lang_text_get_width(const char* str, e_font font);
1111

12-
int lang_text_draw(int group, int number, int x_offset, int y_offset, font_t font);
13-
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); }
14-
int lang_text_draw(pcstr str, vec2i pos, font_t font);
15-
int lang_text_draw_colored(int group, int number, int x_offset, int y_offset, font_t font, color color);
12+
int lang_text_draw(int group, int number, int x_offset, int y_offset, e_font font);
13+
inline int lang_text_draw(int group, int number, vec2i offset, e_font font) { return lang_text_draw(group, number, offset.x, offset.y, font); }
14+
int lang_text_draw(pcstr str, vec2i pos, e_font font);
15+
int lang_text_draw_colored(int group, int number, int x_offset, int y_offset, e_font font, color color);
1616

17-
int lang_text_draw_left(int group, int number, int x_offset, int y_offset, font_t font);
18-
int lang_text_draw_left_colored(int group, int number, int x_offset, int y_offset, font_t font, color color);
17+
int lang_text_draw_left(int group, int number, int x_offset, int y_offset, e_font font);
18+
int lang_text_draw_left_colored(int group, int number, int x_offset, int y_offset, e_font font, color color);
1919

20-
void lang_text_draw_centered(int group, int number, int x_offset, int y_offset, int box_width, font_t font);
21-
void lang_text_draw_centered(const char* text, int x_offset, int y_offset, int box_width, font_t font);
20+
void lang_text_draw_centered(int group, int number, int x_offset, int y_offset, int box_width, e_font font);
21+
void lang_text_draw_centered(const char* text, int x_offset, int y_offset, int box_width, e_font font);
2222

23-
void lang_text_draw_centered_colored(int group, int number, int x_offset, int y_offset, int box_width, font_t font, color color);
23+
void lang_text_draw_centered_colored(int group, int number, int x_offset, int y_offset, int box_width, e_font font, color color);
2424

25-
int lang_text_draw_amount(int group, int number, int amount, int x_offset, int y_offset, font_t font, const char* postfix = " ");
26-
inline int lang_text_draw_amount(int group, int number, int amount, vec2i offset, font_t font, const char *postfix = " ") {
25+
int lang_text_draw_amount(int group, int number, int amount, int x_offset, int y_offset, e_font font, const char* postfix = " ");
26+
inline int lang_text_draw_amount(int group, int number, int amount, vec2i offset, e_font font, const char *postfix = " ") {
2727
return lang_text_draw_amount(group, number, amount, offset.x, offset.y, font, postfix);
2828
}
2929

30-
int lang_text_draw_year(int year, int x_offset, int y_offset, font_t font);
31-
void lang_text_draw_month_year_max_width(int month, int year, int x_offset, int y_offset, int box_width, font_t font, color color);
30+
int lang_text_draw_year(int year, int x_offset, int y_offset, e_font font);
31+
void lang_text_draw_month_year_max_width(int month, int year, int x_offset, int y_offset, int box_width, e_font font, color color);
3232

33-
int lang_text_draw_multiline(int group, int number, vec2i offset, int box_width, font_t font);
33+
int lang_text_draw_multiline(int group, int number, vec2i offset, int box_width, e_font font);

src/graphics/elements/rich_text.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ int rich_text_init(const uint8_t* text,
7373
return data.text_width_blocks;
7474
}
7575

76-
void rich_text_set_fonts(font_t normal_font, font_t link_font) {
76+
void rich_text_set_fonts(e_font normal_font, e_font link_font) {
7777
normal_font_def = font_definition_for(normal_font);
7878
link_font_def = font_definition_for(link_font);
7979
}

src/graphics/elements/rich_text.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ int rich_text_init(const uint8_t* text,
2727
* @param normal_font Normal text
2828
* @param link_font Link text
2929
*/
30-
void rich_text_set_fonts(font_t normal_font, font_t link_font);
30+
void rich_text_set_fonts(e_font normal_font, e_font link_font);
3131

3232
/**
3333
* Resets the text to the specified scroll position and forces recalculation of lines

src/graphics/elements/scroll_list_panel.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ void scroll_list_panel::draw() {
197197
bstring256 text_utf8;
198198
bstring256 text;
199199
for (int i = 0; i < num_buttons; ++i) {
200-
font_t font = ui_params.font_asleep;
200+
e_font font = ui_params.font_asleep;
201201
if (selected_entry_idx == i + scrollbar.scroll_position)
202202
font = ui_params.font_selected;
203203
else if (focus_button_id == i + 1)
@@ -221,9 +221,9 @@ void scroll_list_panel::draw() {
221221
encoding_from_utf8(text_utf8, text, text.capacity);
222222
}
223223

224-
if (using_custom_text_render)
224+
if (using_custom_text_render) {
225225
custom_text_render(i, text, text_pos_x, text_pos_y, font);
226-
else {
226+
} else {
227227
if (ui_params.text_max_width != -1)
228228
text_ellipsize(text, font, ui_params.text_max_width);
229229
text_draw(text, text_pos_x, text_pos_y, font, 0);

src/graphics/elements/scroll_list_panel.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ struct scrollable_list_ui_params {
2929
bool thin_scrollbar = false;
3030
bool draw_scrollbar_always = false;
3131
bool draw_paneling = true;
32-
font_t font_asleep = FONT_NORMAL_BLACK_ON_DARK;
33-
font_t font_focus = FONT_NORMAL_YELLOW;
34-
font_t font_selected = FONT_NORMAL_WHITE_ON_DARK;
32+
e_font font_asleep = FONT_NORMAL_BLACK_ON_DARK;
33+
e_font font_focus = FONT_NORMAL_YELLOW;
34+
e_font font_selected = FONT_NORMAL_WHITE_ON_DARK;
3535
};
3636

3737
enum scroll_list_file_param {
@@ -61,7 +61,7 @@ class scroll_list_panel {
6161

6262
bstring256 manual_entry_list[MAX_MANUAL_ENTRIES];
6363

64-
void (*custom_text_render)(int button_index, const uint8_t* text, int x, int y, font_t font) = nullptr;
64+
void (*custom_text_render)(int button_index, const uint8_t* text, int x, int y, e_font font) = nullptr;
6565
bool using_custom_text_render = false;
6666

6767
bool WAS_DRAWN = false; // for frame-ordered caching logic purposes

src/graphics/elements/ui.cpp

+43-11
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace ui {
2424

2525
state g_state;
2626
generic_button dummy;
27+
element dummy_element;
2728
}
2829

2930
void ui::begin_widget(vec2i offset, bool relative) {
@@ -100,12 +101,12 @@ image_button &ui::img_button(uint32_t group, uint32_t id, vec2i pos, vec2i size,
100101
return g_state.img_buttons.back();
101102
}
102103

103-
int ui::label(int group, int number, vec2i pos, font_t font, UiFlags_ flags, int box_width) {
104+
int ui::label(int group, int number, vec2i pos, e_font font, UiFlags_ flags, int box_width) {
104105
pcstr str = (pcstr)lang_get_string(group, number);
105106
return label(str, pos, font, flags, box_width);
106107
}
107108

108-
int ui::label(pcstr label, vec2i pos, font_t font, UiFlags_ flags, int box_width) {
109+
int ui::label(pcstr label, vec2i pos, e_font font, UiFlags_ flags, int box_width) {
109110
const vec2i offset = g_state.offset();
110111
if (!!(flags & UiFlags_LabelCentered)) {
111112
lang_text_draw_centered(label, offset.x + pos.x, offset.y + pos.y, box_width, font);
@@ -115,12 +116,12 @@ int ui::label(pcstr label, vec2i pos, font_t font, UiFlags_ flags, int box_width
115116
}
116117
}
117118

118-
int ui::label_amount(int group, int number, int amount, vec2i pos, font_t font, pcstr postfix) {
119+
int ui::label_amount(int group, int number, int amount, vec2i pos, e_font font, pcstr postfix) {
119120
const vec2i offset = g_state.offset();
120121
return lang_text_draw_amount(group, number, amount, offset.x + pos.x, offset.y + pos.y, font, postfix);
121122
}
122123

123-
int ui::label_percent(int amount, vec2i pos, font_t font) {
124+
int ui::label_percent(int amount, vec2i pos, e_font font) {
124125
const vec2i offset = g_state.offset();
125126
return text_draw_percentage(amount, offset.x + pos.x, offset.y + pos.y, font);
126127
}
@@ -168,10 +169,10 @@ void ui::outer_panel::draw() {
168169
}
169170

170171
void ui::outer_panel::load(archive arch) {
172+
element::load(arch);
173+
171174
pcstr type = arch.r_string("type");
172175
assert(!strcmp(type, "outer_panel"));
173-
174-
element::load(arch);
175176
}
176177

177178
void ui::widget::draw() {
@@ -184,23 +185,54 @@ void ui::widget::load(archive arch) {
184185
elements.clear();
185186
arch.r_objects("ui", [this] (pcstr key, archive elem) {
186187
pcstr type = elem.r_string("type");
188+
element::ptr elm;
187189
if (!strcmp(type, "outer_panel")) {
188-
elements.push_back(std::make_shared<outer_panel>());
189-
elements.back()->load(elem);
190+
elm = std::make_shared<outer_panel>();
190191
} else if (!strcmp(type, "image")) {
191-
elements.push_back(std::make_shared<image>());
192-
elements.back()->load(elem);
192+
elm = std::make_shared<image>();
193+
} else if (!strcmp(type, "label")) {
194+
elm = std::make_shared<elabel>();
195+
}
196+
197+
if (elm) {
198+
elm->id = key;
199+
elements.push_back(elm);
200+
elm->load(elem);
193201
}
194202
});
195203
}
196204

205+
ui::element& ui::widget::operator[](pcstr id) {
206+
auto it = std::find_if(elements.begin(), elements.end(), [id] (const auto &e) { return e->id == id; });
207+
return (it != elements.end() ? **it : ui::dummy_element);
208+
}
209+
197210
void ui::image::draw() {
198-
ui::icon(pos, ADVISOR_RATINGS);
211+
ui::eimage(img, pos);
199212
}
200213

201214
void ui::image::load(archive arch) {
215+
element::load(arch);
216+
202217
pcstr type = arch.r_string("type");
203218
assert(!strcmp(type, "image"));
204219
img = arch.r_image("image");
220+
}
221+
222+
void ui::elabel::draw() {
223+
ui::label(_text.c_str(), pos, _font);
224+
}
225+
226+
void ui::elabel::load(archive arch) {
205227
element::load(arch);
228+
229+
pcstr type = arch.r_string("type");
230+
assert(!strcmp(type, "label"));
231+
232+
_text = arch.r_string("text");
233+
_font = (e_font)arch.r_int("font", FONT_NORMAL_BLACK_ON_LIGHT);
234+
}
235+
236+
void ui::elabel::text(pcstr v) {
237+
_text = v;
206238
}

0 commit comments

Comments
 (0)