2
0

UIStaticText.cpp 3.9 KB

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