Skip to content

Commit d753eff

Browse files
authored
Merge pull request #729 from crudelios/prettier-tooltips
Improve tooltip looks
2 parents 0248205 + 9a5567b commit d753eff

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

src/graphics/text.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,11 @@ int text_draw_multiline(const uint8_t *str, int x_offset, int y_offset, int box_
419419
return y - y_offset;
420420
}
421421

422-
int text_measure_multiline(const uint8_t *str, int box_width, font_t font)
422+
int text_measure_multiline(const uint8_t *str, int box_width, font_t font, int *largest_width)
423423
{
424+
if (largest_width) {
425+
*largest_width = 0;
426+
}
424427
int has_more_characters = 1;
425428
int guard = 0;
426429
int num_lines = 0;
@@ -429,14 +432,15 @@ int text_measure_multiline(const uint8_t *str, int box_width, font_t font)
429432
break;
430433
}
431434
int current_width = 0;
432-
while (has_more_characters && current_width < box_width) {
435+
while (has_more_characters) {
433436
int word_num_chars;
434437
int word_width = get_word_width(str, font, &word_num_chars);
435438
current_width += word_width;
436439
if (current_width >= box_width) {
437440
if (current_width == 0) {
438441
has_more_characters = 0;
439442
}
443+
break;
440444
} else {
441445
str += word_num_chars;
442446
if (!*str) {
@@ -447,6 +451,9 @@ int text_measure_multiline(const uint8_t *str, int box_width, font_t font)
447451
}
448452
}
449453
}
454+
if (largest_width && current_width > *largest_width) {
455+
*largest_width = current_width;
456+
}
450457
num_lines += 1;
451458
}
452459
return num_lines;

src/graphics/text.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ int text_draw_multiline(const uint8_t *str, int x_offset, int y_offset, int box_
3636
/**
3737
* @return Number of lines required to draw the text
3838
*/
39-
int text_measure_multiline(const uint8_t *str, int box_width, font_t font);
39+
int text_measure_multiline(const uint8_t *str, int box_width, font_t font, int *largest_width);
4040

4141
#endif // GRAPHICS_TEXT_H

src/graphics/tooltip.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,14 @@ static void draw_button_tooltip(tooltip_context *c)
132132
const uint8_t *text = get_tooltip_text(c);
133133

134134
int width = 200;
135-
int lines = text_measure_multiline(text, width - 5, FONT_SMALL_PLAIN);
135+
int largest_width;
136+
int lines = text_measure_multiline(text, width - 16, FONT_SMALL_PLAIN, &largest_width);
136137
if (lines > 2) {
137138
width = 300;
138-
lines = text_measure_multiline(text, width - 5, FONT_SMALL_PLAIN);
139+
lines = text_measure_multiline(text, width - 16, FONT_SMALL_PLAIN, &largest_width);
139140
}
140141
int height = 16 * lines + 10;
142+
width = largest_width + 16;
141143

142144
int x, y;
143145
if (c->mouse_x < screen_dialog_offset_x() + width + 100) {
@@ -191,19 +193,21 @@ static void draw_button_tooltip(tooltip_context *c)
191193

192194
graphics_draw_rect(x, y, width, height, COLOR_BLACK);
193195
graphics_fill_rect(x + 1, y + 1, width - 2, height - 2, COLOR_WHITE);
194-
text_draw_multiline(text, x + 5, y + 7, width - 5, FONT_SMALL_PLAIN, COLOR_TOOLTIP);
196+
text_draw_multiline(text, x + 8, y + 8, width - 15, FONT_SMALL_PLAIN, COLOR_TOOLTIP);
195197
}
196198

197199
static void draw_overlay_tooltip(tooltip_context *c)
198200
{
199201
const uint8_t *text = get_tooltip_text(c);
200202
int width = 200;
201-
int lines = text_measure_multiline(text, width - 5, FONT_SMALL_PLAIN);
203+
int largest_width;
204+
int lines = text_measure_multiline(text, width - 16, FONT_SMALL_PLAIN, &largest_width);
202205
if (lines > 2) {
203206
width = 300;
204-
lines = text_measure_multiline(text, width - 5, FONT_SMALL_PLAIN);
207+
lines = text_measure_multiline(text, width - 16, FONT_SMALL_PLAIN, &largest_width);
205208
}
206209
int height = 16 * lines + 10;
210+
width = largest_width + 16;
207211

208212
int x, y;
209213
if (c->mouse_x < width + 20) {
@@ -223,7 +227,7 @@ static void draw_overlay_tooltip(tooltip_context *c)
223227

224228
graphics_draw_rect(x, y, width, height, COLOR_BLACK);
225229
graphics_fill_rect(x + 1, y + 1, width - 2, height - 2, COLOR_WHITE);
226-
text_draw_multiline(text, x + 5, y + 7, width - 5, FONT_SMALL_PLAIN, COLOR_TOOLTIP);
230+
text_draw_multiline(text, x + 8, y + 8, width - 15, FONT_SMALL_PLAIN, COLOR_TOOLTIP);
227231
}
228232

229233
static void draw_senate_tooltip(tooltip_context *c)

0 commit comments

Comments
 (0)