UIStaticText.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <UI/UIStaticText.h>
  5. #include <Renderer/Renderer.h>
  6. #include <Renderer/Font.h>
  7. #include <UI/UIManager.h>
  8. JPH_IMPLEMENT_RTTI_VIRTUAL(UIStaticText)
  9. {
  10. JPH_ADD_BASE_CLASS(UIStaticText, UIElement)
  11. }
  12. void UIStaticText::CopyTo(UIElement *ioElement) const
  13. {
  14. UIElement::CopyTo(ioElement);
  15. UIStaticText *element = StaticCast<UIStaticText>(ioElement);
  16. element->mFont = mFont;
  17. element->mText = mText;
  18. element->mTextColor = mTextColor;
  19. element->mDisabledTextColor = mDisabledTextColor;
  20. element->mTextPadLeft = mTextPadLeft;
  21. element->mTextPadRight = mTextPadRight;
  22. element->mTextPadTop = mTextPadTop;
  23. element->mTextPadBottom = mTextPadBottom;
  24. element->mTextAlignment = mTextAlignment;
  25. element->mWrap = mWrap;
  26. }
  27. void UIStaticText::Draw() const
  28. {
  29. DrawCustom(IsDisabled()? mDisabledTextColor : mTextColor);
  30. UIElement::Draw();
  31. }
  32. void UIStaticText::AutoLayout()
  33. {
  34. UIElement::AutoLayout();
  35. // Update size
  36. if (mFont != nullptr)
  37. {
  38. Float2 size = mFont->MeasureText(GetWrappedText());
  39. int w = int(size.x * mFont->GetCharHeight()) + mTextPadLeft + mTextPadRight;
  40. int h = int(size.y * mFont->GetCharHeight()) + mTextPadTop + mTextPadBottom;
  41. if (GetWidth() <= 0)
  42. mWidth.Set(w, PIXELS);
  43. if (GetHeight() <= 0)
  44. mHeight.Set(h, PIXELS);
  45. }
  46. }
  47. String UIStaticText::GetWrappedText() const
  48. {
  49. String text;
  50. if (mWrap)
  51. {
  52. int width = GetWidth() - mTextPadLeft - mTextPadRight;
  53. size_t start_pos = 0, prev_end_pos = size_t(-1);
  54. for (;;)
  55. {
  56. // Find next space or end of text
  57. size_t end_pos = mText.find(' ', prev_end_pos + 1);
  58. if (end_pos == String::npos)
  59. end_pos = mText.length();
  60. // Get line to test for width
  61. String sub = mText.substr(start_pos, end_pos - start_pos);
  62. // Measure width
  63. Float2 size = mFont->MeasureText(sub);
  64. int w = int(size.x * mFont->GetCharHeight());
  65. // Check if still fits
  66. if (width < w)
  67. {
  68. // If nothing was found yet, add the last word anyhow so that we don't get into an infinite loop
  69. if (start_pos >= prev_end_pos
  70. || prev_end_pos == size_t(-1))
  71. prev_end_pos = end_pos;
  72. // Add current line
  73. text += mText.substr(start_pos, prev_end_pos - start_pos);
  74. text += "\n";
  75. // Start here for new line
  76. start_pos = prev_end_pos + 1;
  77. }
  78. else
  79. {
  80. // Store last fitting line
  81. prev_end_pos = end_pos;
  82. }
  83. // Check end
  84. if (end_pos >= mText.length())
  85. break;
  86. }
  87. // Add last line
  88. if (start_pos < mText.length())
  89. text += mText.substr(start_pos, mText.length() - start_pos);
  90. }
  91. else
  92. {
  93. // Don't autowrap
  94. text = mText;
  95. }
  96. return text;
  97. }
  98. void UIStaticText::DrawCustom(ColorArg inColor) const
  99. {
  100. if (mFont != nullptr && !mText.empty())
  101. {
  102. String text = GetWrappedText();
  103. int y = GetY() + mTextPadTop;
  104. if (mTextAlignment == LEFT)
  105. {
  106. GetManager()->DrawText(GetX() + mTextPadLeft, y, text, mFont, inColor);
  107. }
  108. else if (mTextAlignment == CENTER)
  109. {
  110. // Split lines
  111. Array<String> lines;
  112. StringToVector(text, lines, "\n");
  113. // Amount of space we have horizontally
  114. int width = GetWidth() - mTextPadLeft - mTextPadRight;
  115. // Center each line individually
  116. for (const String &l : lines)
  117. {
  118. Float2 size = mFont->MeasureText(l);
  119. int w = int(size.x * mFont->GetCharHeight());
  120. GetManager()->DrawText(GetX() + (width - w) / 2 + mTextPadLeft, y, l, mFont, inColor);
  121. y += mFont->GetCharHeight();
  122. }
  123. }
  124. else
  125. {
  126. JPH_ASSERT(mTextAlignment == RIGHT);
  127. // Split lines
  128. Array<String> lines;
  129. StringToVector(text, lines, "\n");
  130. // Center each line individually
  131. for (const String &l : lines)
  132. {
  133. Float2 size = mFont->MeasureText(l);
  134. int w = int(size.x * mFont->GetCharHeight());
  135. GetManager()->DrawText(GetX() + GetWidth() - mTextPadRight - w, y, l, mFont, inColor);
  136. y += mFont->GetCharHeight();
  137. }
  138. }
  139. }
  140. }