From d7cc71129167ab7ab5467897a11c129d6ce17009 Mon Sep 17 00:00:00 2001 From: Shaun Inman Date: Sun, 15 Jan 2023 22:43:04 -0500 Subject: [PATCH] added name truncation api --- src/common/api.c | 19 +++++++++++++++++++ src/common/api.h | 1 + src/minui/main.c | 8 +++++--- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/common/api.c b/src/common/api.c index 1fed824..ab09573 100644 --- a/src/common/api.c +++ b/src/common/api.c @@ -300,6 +300,25 @@ SDL_Surface* GFX_getBufferCopy(void) { // must be freed by caller memcpy(copy->pixels, (gfx.map + gfx.buffer_size * buffer), (SCREEN_HEIGHT * SCREEN_PITCH)); return copy; } +int GFX_truncateDisplayName(const char* in_name, char* out_name, int max_width) { + int text_width; + strcpy(out_name, in_name); + TTF_SizeUTF8(font.large, out_name, &text_width, NULL); + text_width += SCALE1(12*2); + + while (text_width>max_width) { + int len = strlen(out_name); + out_name[len-1] = '\0'; + out_name[len-2] = '.'; + out_name[len-3] = '.'; + out_name[len-4] = '.'; + TTF_SizeUTF8(font.large, out_name, &text_width, NULL); + text_width += SCALE1(12*2); + } + + return text_width; +} + void GFX_blitAsset(int asset, SDL_Rect* src_rect, SDL_Surface* dst, SDL_Rect* dst_rect) { SDL_Rect* rect = &asset_rects[asset]; SDL_Rect adj_rect = { diff --git a/src/common/api.h b/src/common/api.h index 3dd2a02..d152e36 100644 --- a/src/common/api.h +++ b/src/common/api.h @@ -73,6 +73,7 @@ void GFX_flip(SDL_Surface* screen); void GFX_quit(void); SDL_Surface* GFX_getBufferCopy(void); // must be freed by caller +int GFX_truncateDisplayName(const char* in_name, char* out_name, int max_width); // returns final width (including pill padding) // NOTE: all dimensions should be pre-scaled void GFX_blitAsset(int asset, SDL_Rect* src_rect, SDL_Surface* dst, SDL_Rect* dst_rect); diff --git a/src/minui/main.c b/src/minui/main.c index ce964d5..68ea95f 100644 --- a/src/minui/main.c +++ b/src/minui/main.c @@ -1377,12 +1377,15 @@ int main (int argc, char *argv[]) { int selected_row = top->selected - top->start; for (int i=top->start,j=0; iend; i++,j++) { Entry* entry = top->entries->items[i]; - char* display_name = entry->unique ? entry->unique : entry->name; + char* entry_name = entry->unique ? entry->unique : entry->name; int max_width = SCREEN_WIDTH - SCALE1(PADDING * 2); - if (i==top->start) max_width -= ow; // + SCALE1(PADDING); + if (i==top->start) max_width -= ow; SDL_Color text_color = COLOR_WHITE; + char display_name[256]; + int text_width = GFX_truncateDisplayName(entry_name, display_name, max_width); + max_width = MIN(max_width, text_width); if (j==selected_row) { GFX_blitPill(ASSET_WHITE_PILL, screen, &(SDL_Rect){ SCALE1(PADDING), @@ -1393,7 +1396,6 @@ int main (int argc, char *argv[]) { text_color = COLOR_BLACK; } SDL_Surface* text = TTF_RenderUTF8_Blended(font.large, display_name, text_color); - // TODO: handle auto-scrolling SDL_BlitSurface(text, &(SDL_Rect){ 0, 0,