Skip to content

Commit d659974

Browse files
committed
ui: top menu items show tooltips
1 parent e59c2fe commit d659974

File tree

8 files changed

+31
-15
lines changed

8 files changed

+31
-15
lines changed

src/graphics/elements/generic_button.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct generic_button {
3737
generic_button &onrclick(onclick_void f) { return onrclick([f] (int, int) { f(); }); }
3838

3939
generic_button &tooltip(textid t);
40-
generic_button &tooltip(pcstr t) { _tooltip = t; return *this; }
40+
generic_button &tooltip(const xstring &t) { _tooltip = t; return *this; }
4141
generic_button &tooltip(const std::initializer_list<int> &t);
4242
};
4343

src/graphics/elements/menu.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct menu_item {
1313
void (*left_click_handler)(int) = nullptr;
1414
int parameter;
1515
int hidden = false;
16-
bstring64 text;
16+
xstring text;
1717
xstring id;
1818
};
1919

src/graphics/elements/tooltip.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ static void draw_button_tooltip(tooltip_context* c) {
7676
}
7777

7878
int width = 200;
79-
int lines = text_measure_multiline(c->text, width - 5, FONT_SMALL_SHADED);
79+
int lines = text_measure_multiline((const uint8_t *)c->text.c_str(), width - 5, FONT_SMALL_SHADED);
8080
if (lines > 2) {
8181
width = 300;
82-
lines = text_measure_multiline(c->text, width - 5, FONT_SMALL_SHADED);
82+
lines = text_measure_multiline((const uint8_t*)c->text.c_str(), width - 5, FONT_SMALL_SHADED);
8383
}
8484

8585
int height = 16 * lines + 10;
@@ -137,7 +137,7 @@ static void draw_button_tooltip(tooltip_context* c) {
137137

138138
//save_window_under_tooltip_to_buffer(x, y, width, height);
139139
draw_tooltip_box(x, y, width, height);
140-
text_draw_multiline(c->text, x + 5, y + 7, width - 5, FONT_SMALL_SHADED, COLOR_TOOLTIP_TEXT);
140+
text_draw_multiline((const uint8_t *)c->text.c_str(), x + 5, y + 7, width - 5, FONT_SMALL_SHADED, COLOR_TOOLTIP_TEXT);
141141
}
142142

143143
static void draw_overlay_tooltip(tooltip_context* c) {
@@ -149,7 +149,7 @@ static void draw_overlay_tooltip(tooltip_context* c) {
149149
text = overlay_string;
150150
} else if (c->num_extra_values > 0) {
151151
text = overlay_string;
152-
int offset = text.len();
152+
size_t offset = text.len();
153153
overlay_string[offset++] = ':';
154154
overlay_string[offset++] = '\n';
155155
for (int i = 0; i < c->num_extra_values; i++) {

src/graphics/elements/tooltip.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "core/vec2i.h"
44
#include "core/string.h"
5+
#include "core/xstring.h"
56

67
#include <functional>
78

@@ -11,7 +12,7 @@ struct mouse;
1112
struct tooltip_context {
1213
vec2i mpos;
1314
int high_priority = 0;
14-
bstring256 text;
15+
xstring text;
1516
int has_numeric_prefix = 0;
1617
int numeric_prefix = 0;
1718
int num_extra_values = 0;

src/graphics/elements/ui.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ namespace ui {
2929
return tooltipctx;
3030
}
3131

32+
void set_tooltip(const xstring &text) {
33+
tooltipctx.text = text;
34+
}
35+
3236
struct universal_button {
3337
enum e_btn_type {
3438
unknown = -1,
@@ -937,6 +941,8 @@ void ui::emenu_header::load(archive arch, element *parent, items &elems) {
937941
assert(!strcmp(type, "menu_header"));
938942

939943
_font = (e_font)arch.r_int("font", FONT_NORMAL_BLACK_ON_LIGHT);
944+
_tooltip = arch.r_string("tooltip");
945+
940946
impl.text = arch.r_string("text");
941947
if (!!impl.text && impl.text[0u] == '#') {
942948
impl.text = lang_text_from_key(impl.text.c_str());

src/graphics/elements/ui.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ namespace ui {
4949
struct img_button_offsets { int data[4] = {0, 1, 2, 3}; };
5050

5151
const tooltip_context &get_tooltip();
52+
void set_tooltip(const xstring &text);
5253
void begin_frame();
5354
void end_frame();
5455
void begin_widget(vec2i offset, bool relative = false);
@@ -126,7 +127,7 @@ struct element {
126127
virtual int value() const { return 0; }
127128
virtual void select(bool v) {}
128129
virtual void max_value(int v) {}
129-
virtual pcstr tooltip() const { return ""; }
130+
virtual const xstring &tooltip() const { return xstring(); }
130131
virtual element &onclick(std::function<void(int, int)>) { return *this; }
131132
element &onclick(std::function<void()> f) { onclick([f] (int, int) { f(); }); return *this; }
132133
virtual void onevent(std::function<void()>) { }
@@ -256,7 +257,7 @@ struct elabel : public element {
256257
virtual void text_color(color) override;
257258
virtual void font(int) override;
258259
virtual e_font font() const override { return _font; }
259-
virtual pcstr tooltip() const override { return _tooltip.c_str(); }
260+
virtual const xstring &tooltip() const override { return _tooltip; }
260261
virtual void width(int) override;
261262
};
262263

@@ -277,13 +278,15 @@ struct escrollbar : public element {
277278

278279
struct emenu_header : public element {
279280
menu_header impl;
281+
xstring _tooltip;
280282
e_font _font;
281283

282284
virtual void load(archive elem, element *parent, items &elems) override;
283285
void load_items(archive elem, pcstr section);
284286
virtual void draw() override;
285287
virtual void font(int v) override { _font = (e_font)v; }
286288
virtual void text(pcstr text) override { impl.text = text; }
289+
virtual const xstring &tooltip() const override { return _tooltip; }
287290
virtual int text_width() override;
288291
menu_item &item(int i) { static menu_item dummy; return i < impl.items.size() ? impl.items[i] : dummy; }
289292
menu_item &item(pcstr key);

src/scripts/ui.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ top_menu_widget = {
5959
offset_rotate_basic : 200,
6060

6161
headers : {
62-
file : { type: "menu_header", text: {group:7, id:0} },
63-
options : { type: "menu_header", text: {group:2, id:0} },
64-
help : { type: "menu_header", text: {group:3, id:0} },
62+
file : { type: "menu_header", text: {group:7, id:0}, tooltip:[68, 51] },
63+
options : { type: "menu_header", text: {group:2, id:0}, tooltip:[68, 52] },
64+
help : { type: "menu_header", text: {group:3, id:0}, tooltip:[68, 53] },
6565
advisors : { type: "menu_header", text: {group:4, id:0} },
6666
debug : { type: "menu_header", text: "Debug" },
6767
debug_render : { type: "menu_header", text: "Render" },

src/widget/widget_top_menu_game.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,17 @@ void widget_top_menu_draw_elements() {
279279
continue;
280280
}
281281

282+
const bool is_hovered = (it->id == data.focus_menu_id);
283+
282284
header->impl.x_start = offset.x;
283-
header->font((it->id == data.focus_menu_id) ? hightlight_font : FONT_NORMAL_BLACK_ON_LIGHT);
285+
header->font(is_hovered ? hightlight_font : FONT_NORMAL_BLACK_ON_LIGHT);
284286
header->pos = vec2i{offset.x, data.offset.y};
285287
header->draw();
286288

289+
if (is_hovered) {
290+
ui::set_tooltip(header->tooltip());
291+
}
292+
287293
offset.x += header->text_width();
288294
header->impl.x_end = offset.x;
289295
offset.x += data.spacing;
@@ -321,7 +327,7 @@ static void top_menu_calculate_menu_dimensions(menu_header& menu) {
321327
continue;
322328
}
323329

324-
int width_pixels = lang_text_get_width(item.text, FONT_NORMAL_BLACK_ON_LIGHT);
330+
int width_pixels = lang_text_get_width(item.text.c_str(), FONT_NORMAL_BLACK_ON_LIGHT);
325331
max_width = std::max(max_width, width_pixels);
326332

327333
height_pixels += data.item_height;
@@ -346,7 +352,7 @@ void top_menu_menu_draw(const xstring header, const xstring focus_item_id) {
346352
continue;
347353
}
348354
// Set color/font on the menu item mouse hover
349-
lang_text_draw(item.text, vec2i{impl.x_start + 8, y_offset}, item.id == focus_item_id ? FONT_NORMAL_YELLOW : FONT_NORMAL_BLACK_ON_LIGHT);
355+
lang_text_draw(item.text.c_str(), vec2i{impl.x_start + 8, y_offset}, item.id == focus_item_id ? FONT_NORMAL_YELLOW : FONT_NORMAL_BLACK_ON_LIGHT);
350356
y_offset += menu.item_height;
351357
}
352358
}

0 commit comments

Comments
 (0)