added GFX_wrapText and refactored GFX_truncateText
This commit is contained in:
parent
0edeaf06eb
commit
efa1e9bd0c
4 changed files with 71 additions and 13 deletions
|
|
@ -316,24 +316,78 @@ 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 GFX_truncateText(TTF_Font* font, const char* in_name, char* out_name, int max_width) {
|
||||||
int text_width;
|
int text_width;
|
||||||
strcpy(out_name, in_name);
|
strcpy(out_name, in_name);
|
||||||
TTF_SizeUTF8(font.large, out_name, &text_width, NULL);
|
TTF_SizeUTF8(font, out_name, &text_width, NULL);
|
||||||
text_width += SCALE1(12*2);
|
text_width += SCALE1(12*2);
|
||||||
|
|
||||||
while (text_width>max_width) {
|
while (text_width>max_width) {
|
||||||
int len = strlen(out_name);
|
int len = strlen(out_name);
|
||||||
out_name[len-1] = '\0';
|
strcpy(&out_name[len-4], "...\0");
|
||||||
out_name[len-2] = '.';
|
TTF_SizeUTF8(font, out_name, &text_width, NULL);
|
||||||
out_name[len-3] = '.';
|
|
||||||
out_name[len-4] = '.';
|
|
||||||
TTF_SizeUTF8(font.large, out_name, &text_width, NULL);
|
|
||||||
text_width += SCALE1(12*2);
|
text_width += SCALE1(12*2);
|
||||||
}
|
}
|
||||||
|
|
||||||
return text_width;
|
return text_width;
|
||||||
}
|
}
|
||||||
|
int GFX_wrapText(TTF_Font* font, char* str, int max_width, int max_lines) {
|
||||||
|
// TODO: this is kinda buggy mess
|
||||||
|
|
||||||
|
if (!str) return 0;
|
||||||
|
|
||||||
|
int line_width;
|
||||||
|
int max_line_width;
|
||||||
|
char* line = str;
|
||||||
|
char buffer[MAX_PATH];
|
||||||
|
|
||||||
|
TTF_SizeUTF8(font, line, &line_width, NULL);
|
||||||
|
if (line_width<=max_width) {
|
||||||
|
line_width = GFX_truncateText(font,line,buffer,max_width);
|
||||||
|
strcpy(line,buffer);
|
||||||
|
return line_width;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* prev;
|
||||||
|
char* tmp = line;
|
||||||
|
int lines = 1;
|
||||||
|
int i = 0;
|
||||||
|
while (!max_lines || lines<max_lines) {
|
||||||
|
tmp = strchr(tmp, ' ');
|
||||||
|
if (!tmp) {
|
||||||
|
if (prev) {
|
||||||
|
prev[0] = '\n';
|
||||||
|
line = prev + 1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
tmp[0] = '\0';
|
||||||
|
|
||||||
|
TTF_SizeUTF8(font, line, &line_width, NULL);
|
||||||
|
|
||||||
|
if (line_width>=max_width) { // wrap
|
||||||
|
if (line_width>max_line_width) max_line_width = line_width;
|
||||||
|
tmp[0] = ' ';
|
||||||
|
tmp += 1;
|
||||||
|
prev[0] = '\n';
|
||||||
|
prev += 1;
|
||||||
|
line = prev;
|
||||||
|
lines += 1;
|
||||||
|
}
|
||||||
|
else { // continue
|
||||||
|
tmp[0] = ' ';
|
||||||
|
prev = tmp;
|
||||||
|
tmp += 1;
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
line_width = GFX_truncateText(font,line,buffer,max_width);
|
||||||
|
strcpy(line,buffer);
|
||||||
|
|
||||||
|
if (line_width>max_line_width) max_line_width = line_width;
|
||||||
|
return max_line_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];
|
||||||
|
|
@ -630,7 +684,7 @@ int GFX_blitButtonGroup(char** pairs, SDL_Surface* dst, int align_right) {
|
||||||
return ow;
|
return ow;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define MAX_TEXT_LINES 3
|
#define MAX_TEXT_LINES 16
|
||||||
void GFX_sizeText(TTF_Font* font, char* str, int leading, int* w, int* h) {
|
void GFX_sizeText(TTF_Font* font, char* str, int leading, int* w, int* h) {
|
||||||
char* lines[MAX_TEXT_LINES];
|
char* lines[MAX_TEXT_LINES];
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
@ -638,7 +692,7 @@ void GFX_sizeText(TTF_Font* font, char* str, int leading, int* w, int* h) {
|
||||||
char* tmp;
|
char* tmp;
|
||||||
lines[count++] = str;
|
lines[count++] = str;
|
||||||
while ((tmp=strchr(lines[count-1], '\n'))!=NULL) {
|
while ((tmp=strchr(lines[count-1], '\n'))!=NULL) {
|
||||||
if (count+1>MAX_TEXT_LINES) break; // TODO: bail
|
if (count+1>MAX_TEXT_LINES) break; // TODO: bail?
|
||||||
lines[count++] = tmp+1;
|
lines[count++] = tmp+1;
|
||||||
}
|
}
|
||||||
*h = count * leading;
|
*h = count * leading;
|
||||||
|
|
@ -674,7 +728,7 @@ void GFX_blitText(TTF_Font* font, char* str, int leading, SDL_Color color, SDL_S
|
||||||
char* tmp;
|
char* tmp;
|
||||||
lines[count++] = str;
|
lines[count++] = str;
|
||||||
while ((tmp=strchr(lines[count-1], '\n'))!=NULL) {
|
while ((tmp=strchr(lines[count-1], '\n'))!=NULL) {
|
||||||
if (count+1>MAX_TEXT_LINES) break; // TODO: bail
|
if (count+1>MAX_TEXT_LINES) break; // TODO: bail?
|
||||||
lines[count++] = tmp+1;
|
lines[count++] = tmp+1;
|
||||||
}
|
}
|
||||||
int x = dst_rect->x;
|
int x = dst_rect->x;
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,8 @@ int GFX_getVsync(void);
|
||||||
void GFX_setVsync(int vsync);
|
void GFX_setVsync(int vsync);
|
||||||
|
|
||||||
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)
|
int GFX_truncateText(TTF_Font* font, const char* in_name, char* out_name, int max_width); // returns final width (including pill padding)
|
||||||
|
int GFX_wrapText(TTF_Font* font, char* str, int max_width, int max_lines);
|
||||||
|
|
||||||
// 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);
|
||||||
|
|
|
||||||
|
|
@ -783,7 +783,10 @@ static void OptionList_init(const struct retro_core_option_definition *defs) {
|
||||||
if (def->info) {
|
if (def->info) {
|
||||||
len = strlen(def->info) + 1;
|
len = strlen(def->info) + 1;
|
||||||
item->desc = calloc(len, sizeof(char));
|
item->desc = calloc(len, sizeof(char));
|
||||||
strcpy(item->desc, def->info);
|
strncpy(item->desc, def->info, len);
|
||||||
|
item->desc[len-1] = '\0';
|
||||||
|
|
||||||
|
GFX_wrapText(font.tiny, item->desc, SCALE1(240), 2); // TODO magic number! (this is more about chars per line than pixel width so it's not going to be relative to the screen size, only the scale)
|
||||||
}
|
}
|
||||||
|
|
||||||
item->visible = 1;
|
item->visible = 1;
|
||||||
|
|
|
||||||
|
|
@ -1384,7 +1384,7 @@ int main (int argc, char *argv[]) {
|
||||||
SDL_Color text_color = COLOR_WHITE;
|
SDL_Color text_color = COLOR_WHITE;
|
||||||
|
|
||||||
char display_name[256];
|
char display_name[256];
|
||||||
int text_width = GFX_truncateDisplayName(entry_name, display_name, max_width);
|
int text_width = GFX_truncateText(font.large, entry_name, display_name, max_width);
|
||||||
max_width = MIN(max_width, text_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){
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue