diff --git a/src/Drawer2D.c b/src/Drawer2D.c index fb4822df0..d5b0a2f97 100644 --- a/src/Drawer2D.c +++ b/src/Drawer2D.c @@ -542,6 +542,10 @@ int Drawer2D_TextWidth(struct DrawTextArgs* args) { return width; } +int Drawer2D_TextHeight(struct DrawTextArgs* args) { + return Drawer2D_FontHeight(&args->font, args->useShadow); +} + int Drawer2D_FontHeight(const FontDesc* font, bool useShadow) { int height, point; if (Drawer2D_BitmappedText) { @@ -560,7 +564,7 @@ int Drawer2D_FontHeight(const FontDesc* font, bool useShadow) { Size2D Drawer2D_MeasureText(struct DrawTextArgs* args) { Size2D size; size.Width = Drawer2D_TextWidth(args); - size.Height = Drawer2D_FontHeight(&args->font, args->useShadow); + size.Height = Drawer2D_TextHeight(args); if (!size.Width) size.Height = 0; return size; diff --git a/src/Drawer2D.h b/src/Drawer2D.h index 0a5f36d34..d4efb6546 100644 --- a/src/Drawer2D.h +++ b/src/Drawer2D.h @@ -63,8 +63,11 @@ void Drawer2D_Underline(Bitmap* bmp, int x, int y, int width, int height, Bitmap CC_API void Drawer2D_DrawText(Bitmap* bmp, struct DrawTextArgs* args, int x, int y); /* Returns how wide the given text would be when drawn. */ int Drawer2D_TextWidth(struct DrawTextArgs* args); +/* Returns how tall the given text would be when drawn. */ +/* NOTE: Height returned only depends on the font. (see Drawer2D_FontHeight). */ +int Drawer2D_TextHeight(struct DrawTextArgs* args); /* Returns size the given text would be when drawn. */ -/* NOTE: Height returned only depends on the font. (see Drawer2D_FontHeight).*/ +/* NOTE: Height returned only depends on the font. (see Drawer2D_FontHeight). */ CC_API Size2D Drawer2D_MeasureText(struct DrawTextArgs* args); /* Similar to Drawer2D_DrawText, but trims the text with trailing ".." if wider than maxWidth. */ void Drawer2D_DrawClippedText(Bitmap* bmp, struct DrawTextArgs* args, int x, int y, int maxWidth); diff --git a/src/LWidgets.c b/src/LWidgets.c index 2390ea93b..41185f14a 100644 --- a/src/LWidgets.c +++ b/src/LWidgets.c @@ -235,7 +235,7 @@ static void LInput_BlendBoxTop(struct LInput* w) { } static void LInput_DrawText(struct LInput* w, struct DrawTextArgs* args) { - int hintHeight, y; + int y, hintHeight; if (w->Text.length || !w->HintText) { y = w->Y + (w->Height - w->_TextHeight) / 2; @@ -244,7 +244,7 @@ static void LInput_DrawText(struct LInput* w, struct DrawTextArgs* args) { args->text = String_FromReadonly(w->HintText); args->font = Launcher_HintFont; - hintHeight = Drawer2D_MeasureText(args).Height; + hintHeight = Drawer2D_TextHeight(args); y = w->Y + (w->Height - hintHeight) / 2; Drawer2D_DrawText(&Launcher_Framebuffer, args, w->X + 5, y); } diff --git a/src/Widgets.c b/src/Widgets.c index 98cde52a8..4bf2be0c1 100644 --- a/src/Widgets.c +++ b/src/Widgets.c @@ -1728,11 +1728,12 @@ void ChatInputWidget_Create(struct ChatInputWidget* w) { } void ChatInputWidget_SetFont(struct ChatInputWidget* w, FontDesc* font) { - struct DrawTextArgs args; - w->base.font = font; - w->base.lineHeight = Drawer2D_FontHeight(font, true); + struct DrawTextArgs args; DrawTextArgs_Make(&args, &chatInputPrefix, font, true); + + w->base.font = font; w->base.prefixWidth = Drawer2D_TextWidth(&args); + w->base.lineHeight = Drawer2D_TextHeight(&args); } @@ -2541,16 +2542,15 @@ static void SpecialInputWidget_UpdateColString(struct SpecialInputWidget* w) { } static bool SpecialInputWidget_IntersectsTitle(struct SpecialInputWidget* w, int x, int y) { - Size2D size; - int i, titleX = 0; + int i, width, titleX = 0; for (i = 0; i < Array_Elems(w->tabs); i++) { - size = w->tabs[i].titleSize; - if (Gui_Contains(titleX, 0, size.Width, size.Height, x, y)) { + width = w->tabs[i].titleWidth; + if (Gui_Contains(titleX, 0, width, w->titleHeight, x, y)) { w->selectedIndex = i; return true; } - titleX += size.Width; + titleX += width; } return false; } @@ -2560,7 +2560,7 @@ static void SpecialInputWidget_IntersectsBody(struct SpecialInputWidget* w, int String str; int i; - y -= w->tabs[0].titleSize.Height; + y -= w->titleHeight; x /= w->elementSize.Width; y /= w->elementSize.Height; i = (x + y * e.itemsPerRow) * e.charsPerItem; @@ -2575,10 +2575,9 @@ static void SpecialInputWidget_IntersectsBody(struct SpecialInputWidget* w, int } static void SpecialInputTab_Init(struct SpecialInputTab* tab, STRING_REF String* title, int itemsPerRow, int charsPerItem, STRING_REF String* contents) { - tab->title = *title; - tab->titleSize.Width = 0; - tab->titleSize.Height = 0; - tab->contents = *contents; + tab->title = *title; + tab->titleWidth = 0; + tab->contents = *contents; tab->itemsPerRow = itemsPerRow; tab->charsPerItem = charsPerItem; } @@ -2613,12 +2612,12 @@ static Size2D SpecialInputWidget_MeasureTitles(struct SpecialInputWidget* w) { for (i = 0; i < Array_Elems(w->tabs); i++) { args.text = w->tabs[i].title; - w->tabs[i].titleSize = Drawer2D_MeasureText(&args); - w->tabs[i].titleSize.Width += SPECIAL_TITLE_SPACING; - size.Width += w->tabs[i].titleSize.Width; + w->tabs[i].titleWidth = Drawer2D_TextWidth(&args) + SPECIAL_TITLE_SPACING; + size.Width += w->tabs[i].titleWidth; } - size.Height = w->tabs[0].titleSize.Height; + w->titleHeight = Drawer2D_TextHeight(&args); + size.Height = w->titleHeight; return size; } @@ -2626,20 +2625,18 @@ static void SpecialInputWidget_DrawTitles(struct SpecialInputWidget* w, Bitmap* BitmapCol col_selected = BITMAPCOL_CONST(30, 30, 30, 200); BitmapCol col_inactive = BITMAPCOL_CONST( 0, 0, 0, 127); BitmapCol col; - struct DrawTextArgs args; - Size2D size; - int i, x = 0; + int i, width, x = 0; DrawTextArgs_MakeEmpty(&args, w->font, false); for (i = 0; i < Array_Elems(w->tabs); i++) { args.text = w->tabs[i].title; - col = i == w->selectedIndex ? col_selected : col_inactive; - size = w->tabs[i].titleSize; + col = i == w->selectedIndex ? col_selected : col_inactive; + width = w->tabs[i].titleWidth; - Drawer2D_Clear(bmp, col, x, 0, size.Width, size.Height); + Drawer2D_Clear(bmp, col, x, 0, width, w->titleHeight); Drawer2D_DrawText(bmp, &args, x + SPECIAL_TITLE_SPACING / 2, 0); - x += size.Width; + x += width; } } diff --git a/src/Widgets.h b/src/Widgets.h index 453a57faf..55ef53191 100644 --- a/src/Widgets.h +++ b/src/Widgets.h @@ -251,8 +251,7 @@ CC_NOINLINE void PlayerListWidget_GetNameUnder(struct PlayerListWidget* w, int m typedef void (*SpecialInputAppendFunc)(void* userData, char c); struct SpecialInputTab { - int itemsPerRow, charsPerItem; - Size2D titleSize; + int itemsPerRow, charsPerItem, titleWidth; String title, contents; }; @@ -264,6 +263,7 @@ struct SpecialInputWidget { struct InputWidget* target; struct Texture tex; FontDesc* font; + int titleHeight; struct SpecialInputTab tabs[5]; String colString; char _colBuffer[DRAWER2D_MAX_COLS * 4];