added name truncation api

This commit is contained in:
Shaun Inman 2023-01-15 22:43:04 -05:00
parent 5c8f231162
commit d7cc711291
3 changed files with 25 additions and 3 deletions

View file

@ -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)); memcpy(copy->pixels, (gfx.map + gfx.buffer_size * buffer), (SCREEN_HEIGHT * SCREEN_PITCH));
return copy; 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) { void GFX_blitAsset(int asset, SDL_Rect* src_rect, SDL_Surface* dst, SDL_Rect* dst_rect) {
SDL_Rect* rect = &asset_rects[asset]; SDL_Rect* rect = &asset_rects[asset];
SDL_Rect adj_rect = { SDL_Rect adj_rect = {

View file

@ -73,6 +73,7 @@ void GFX_flip(SDL_Surface* screen);
void GFX_quit(void); void GFX_quit(void);
SDL_Surface* GFX_getBufferCopy(void); // must be freed by caller 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 // NOTE: all dimensions should be pre-scaled
void GFX_blitAsset(int asset, SDL_Rect* src_rect, SDL_Surface* dst, SDL_Rect* dst_rect); void GFX_blitAsset(int asset, SDL_Rect* src_rect, SDL_Surface* dst, SDL_Rect* dst_rect);

View file

@ -1377,12 +1377,15 @@ int main (int argc, char *argv[]) {
int selected_row = top->selected - top->start; int selected_row = top->selected - top->start;
for (int i=top->start,j=0; i<top->end; i++,j++) { for (int i=top->start,j=0; i<top->end; i++,j++) {
Entry* entry = top->entries->items[i]; 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); 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; 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) { if (j==selected_row) {
GFX_blitPill(ASSET_WHITE_PILL, screen, &(SDL_Rect){ GFX_blitPill(ASSET_WHITE_PILL, screen, &(SDL_Rect){
SCALE1(PADDING), SCALE1(PADDING),
@ -1393,7 +1396,6 @@ int main (int argc, char *argv[]) {
text_color = COLOR_BLACK; text_color = COLOR_BLACK;
} }
SDL_Surface* text = TTF_RenderUTF8_Blended(font.large, display_name, text_color); SDL_Surface* text = TTF_RenderUTF8_Blended(font.large, display_name, text_color);
// TODO: handle auto-scrolling
SDL_BlitSurface(text, &(SDL_Rect){ SDL_BlitSurface(text, &(SDL_Rect){
0, 0,
0, 0,