Implement getMaxStringWidth (#1060)

This commit is contained in:
Duncan 2021-07-21 13:02:40 +01:00 committed by GitHub
parent 92b781cf48
commit 82adf56d87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 99 additions and 11 deletions

View File

@ -439,6 +439,103 @@ namespace OpenLoco::Gfx
return width;
}
/**
* 0x004955BC
*
* @param buffer @<esi>
* @return width @<cx>
*/
uint16_t getMaxStringWidth(const char* buffer)
{
uint16_t width = 0;
uint16_t maxWidth = 0;
const uint8_t* str = reinterpret_cast<const uint8_t*>(buffer);
int16_t fontSpriteBase = _currentFontSpriteBase;
while (*str != (uint8_t)0)
{
const uint8_t chr = *str;
str++;
if (chr >= 32)
{
width += _characterWidths[chr - 32 + fontSpriteBase];
continue;
}
switch (chr)
{
case ControlCodes::move_x:
maxWidth = std::max(width, maxWidth);
width = *str;
str++;
break;
case ControlCodes::adjust_palette:
case 3:
case 4:
str++;
break;
case ControlCodes::newline:
case ControlCodes::newline_smaller:
continue;
case ControlCodes::font_small:
fontSpriteBase = Font::small;
break;
case ControlCodes::font_large:
fontSpriteBase = Font::large;
break;
case ControlCodes::font_bold:
fontSpriteBase = Font::medium_bold;
break;
case ControlCodes::font_regular:
fontSpriteBase = Font::medium_normal;
break;
case ControlCodes::outline:
case ControlCodes::outline_off:
case ControlCodes::window_colour_1:
case ControlCodes::window_colour_2:
case ControlCodes::window_colour_3:
case ControlCodes::window_colour_4:
break;
case ControlCodes::newline_x_y:
maxWidth = std::max(width, maxWidth);
width = *str;
str += 2;
break;
case ControlCodes::inline_sprite_str:
{
const uint32_t image = reinterpret_cast<const uint32_t*>(str)[0];
const uint32_t imageId = image & 0x7FFFF;
str += 4;
width += _g1Elements[imageId].width;
break;
}
default:
if (chr <= 0x16)
{
str += 2;
}
else
{
str += 4;
}
break;
}
}
maxWidth = std::max(width, maxWidth);
return maxWidth;
}
static void setTextColours(PaletteIndex_t pal1, PaletteIndex_t pal2, PaletteIndex_t pal3)
{
if ((_currentFontFlags & text_draw_flags::inset) != 0)

View File

@ -135,6 +135,7 @@ namespace OpenLoco::Gfx
int16_t clipString(int16_t width, char* string);
uint16_t getStringWidth(const char* buffer);
uint16_t getMaxStringWidth(const char* buffer);
Gfx::point_t drawString(Context* context, int16_t x, int16_t y, uint8_t colour, void* str);

View File

@ -336,16 +336,6 @@ namespace OpenLoco::Ui::Dropdown
flags &= ~(1 << 7);
}
// 0x004955BC
static uint16_t getStringWidth(char* buffer)
{
registers regs;
regs.esi = (int32_t)buffer;
call(0x004955BC, regs);
return regs.cx;
}
// 0x004CCAB2
static void showText(int16_t x, int16_t y, int16_t width, int16_t height, uint8_t itemHeight, Colour_t colour, size_t count, uint8_t flags)
{
@ -371,7 +361,7 @@ namespace OpenLoco::Ui::Dropdown
_currentFontSpriteBase = Font::medium_bold;
auto stringWidth = getStringWidth(_byte_112CC04);
auto stringWidth = Gfx::getMaxStringWidth(_byte_112CC04);
maxStringWidth = std::max(maxStringWidth, stringWidth);
}