Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve tooltip looks #729

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/graphics/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,9 @@ int text_draw_multiline(const uint8_t *str, int x_offset, int y_offset, int box_
return y - y_offset;
}

int text_measure_multiline(const uint8_t *str, int box_width, font_t font)
int text_measure_multiline(const uint8_t *str, int box_width, font_t font, int *largest_width)
{
*largest_width = 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking largest_width is an optional parameter so it can be null. I know we only use this in one place (the tooltips), but let's be safe and add a null-check around accessing it.

int has_more_characters = 1;
int guard = 0;
int num_lines = 0;
Expand All @@ -429,14 +430,15 @@ int text_measure_multiline(const uint8_t *str, int box_width, font_t font)
break;
}
int current_width = 0;
while (has_more_characters && current_width < box_width) {
while (has_more_characters) {
int word_num_chars;
int word_width = get_word_width(str, font, &word_num_chars);
current_width += word_width;
if (current_width >= box_width) {
if (current_width == 0) {
has_more_characters = 0;
}
break;
} else {
str += word_num_chars;
if (!*str) {
Expand All @@ -447,6 +449,9 @@ int text_measure_multiline(const uint8_t *str, int box_width, font_t font)
}
}
}
if (current_width > *largest_width) {
*largest_width = current_width;
}
num_lines += 1;
}
return num_lines;
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ int text_draw_multiline(const uint8_t *str, int x_offset, int y_offset, int box_
/**
* @return Number of lines required to draw the text
*/
int text_measure_multiline(const uint8_t *str, int box_width, font_t font);
int text_measure_multiline(const uint8_t *str, int box_width, font_t font, int *largest_width);

#endif // GRAPHICS_TEXT_H
16 changes: 10 additions & 6 deletions src/graphics/tooltip.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,14 @@ static void draw_button_tooltip(tooltip_context *c)
const uint8_t *text = get_tooltip_text(c);

int width = 200;
int lines = text_measure_multiline(text, width - 5, FONT_SMALL_PLAIN);
int largest_width;
int lines = text_measure_multiline(text, width - 16, FONT_SMALL_PLAIN, &largest_width);
if (lines > 2) {
width = 300;
lines = text_measure_multiline(text, width - 5, FONT_SMALL_PLAIN);
lines = text_measure_multiline(text, width - 16, FONT_SMALL_PLAIN, &largest_width);
}
int height = 16 * lines + 10;
width = largest_width + 16;

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

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

static void draw_overlay_tooltip(tooltip_context *c)
{
const uint8_t *text = get_tooltip_text(c);
int width = 200;
int lines = text_measure_multiline(text, width - 5, FONT_SMALL_PLAIN);
int largest_width;
int lines = text_measure_multiline(text, width - 16, FONT_SMALL_PLAIN, &largest_width);
if (lines > 2) {
width = 300;
lines = text_measure_multiline(text, width - 5, FONT_SMALL_PLAIN);
lines = text_measure_multiline(text, width - 16, FONT_SMALL_PLAIN, &largest_width);
}
int height = 16 * lines + 10;
width = largest_width + 16;

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

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

static void draw_senate_tooltip(tooltip_context *c)
Expand Down
Loading