DrawCentredParagraph: fix line centring breaking at unequal lengths

This commit is contained in:
Hyper 2025-01-16 15:04:33 +00:00
parent 39b0aeb2dc
commit adb9bedfa1

View file

@ -369,29 +369,18 @@ inline void DrawCentredParagraph(const ImFont* font, float fontSize, float maxWi
auto paragraphSize = MeasureCentredParagraph(font, fontSize, lineMargin, lines);
auto offsetY = 0.0f;
auto hasList = std::strstr(text, "- ");
auto isList = false;
auto listOffsetX = 0.0f;
for (int i = 0; i < lines.size(); i++)
{
auto& str = lines[i];
auto textSize = font->CalcTextSizeA(fontSize, FLT_MAX, 0, str.c_str());
if (hasList)
{
if (!isList && str.starts_with("- ") && lines.size() > i + 1 && lines[i + 1].starts_with("- "))
{
isList = true;
listOffsetX = centre.x - textSize.x / 2;
}
else if (isList && !str.starts_with("- "))
{
isList = false;
}
}
auto textX = str.starts_with("- ")
? centre.x - paragraphSize.x / 2
: centre.x - textSize.x / 2;
drawMethod(str.c_str(), ImVec2(/* X */ isList ? listOffsetX : centre.x - textSize.x / 2, /* Y */ centre.y - paragraphSize.y / 2 + offsetY));
auto textY = centre.y - paragraphSize.y / 2 + offsetY;
drawMethod(str.c_str(), { textX, textY });
offsetY += textSize.y + Scale(lineMargin);
}