Text.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "UIElement.h"
  25. static const int DEFAULT_FONT_SIZE = 12;
  26. class Font;
  27. /// %Text %UI element.
  28. class Text : public UIElement
  29. {
  30. OBJECT(Text);
  31. using UIElement::SetStyle;
  32. public:
  33. /// Construct.
  34. Text(Context* context);
  35. /// Destruct.
  36. virtual ~Text();
  37. /// Register object factory.
  38. static void RegisterObject(Context* context);
  39. /// %Set UI element style from XML data.
  40. virtual void SetStyle(const XMLElement& element);
  41. /// Return UI rendering batches.
  42. virtual void GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor);
  43. /// React to resize.
  44. virtual void OnResize();
  45. /// %Set font and font size.
  46. bool SetFont(const String& fontName, int size = DEFAULT_FONT_SIZE);
  47. /// %Set font and font size.
  48. bool SetFont(Font* font, int size = DEFAULT_FONT_SIZE);
  49. /// %Set text.
  50. void SetText(const String& text);
  51. /// %Set row alignment.
  52. void SetTextAlignment(HorizontalAlignment align);
  53. /// %Set row spacing, 1.0 for original font spacing.
  54. void SetRowSpacing(float spacing);
  55. /// %Set wordwrap. In wordwrap mode the text element will respect its current width. Otherwise it resizes itself freely.
  56. void SetWordwrap(bool enable);
  57. /// %Set selection.
  58. void SetSelection(unsigned start, unsigned length);
  59. /// Clear selection.
  60. void ClearSelection();
  61. /// %Set selection background color. Color with 0 alpha (default) disables.
  62. void SetSelectionColor(const Color& color);
  63. /// %Set hover background color. Color with 0 alpha (default) disables.
  64. void SetHoverColor(const Color& color);
  65. /// Return font.
  66. Font* GetFont() const { return font_; }
  67. /// Return font size.
  68. int GetFontSize() const { return fontSize_; }
  69. /// Return text.
  70. const String& GetText() const { return text_; }
  71. /// Return row alignment.
  72. HorizontalAlignment GetTextAlignment() const { return textAlignment_; }
  73. /// Return row spacing.
  74. float GetRowSpacing() const { return rowSpacing_; }
  75. /// Return wordwrap mode.
  76. bool GetWordwrap() const { return wordWrap_; }
  77. /// Return selection start.
  78. unsigned GetSelectionStart() const { return selectionStart_; }
  79. /// Return selection length.
  80. unsigned GetSelectionLength() const { return selectionLength_; }
  81. /// Return selection background color.
  82. const Color& GetSelectionColor() const { return selectionColor_; }
  83. /// Return hover background color.
  84. const Color& GetHoverColor() const { return hoverColor_; }
  85. /// Return row height.
  86. int GetRowHeight() const { return rowHeight_; }
  87. /// Return number of rows.
  88. unsigned GetNumRows() const { return rowWidths_.Size(); }
  89. /// Return width of each row.
  90. const PODVector<int>& GetRowWidths() const { return rowWidths_; }
  91. /// Return position of each character.
  92. const PODVector<IntVector2>& GetCharPositions() const { return charPositions_; }
  93. /// Return size of each character.
  94. const PODVector<IntVector2>& GetCharSizes() const { return charSizes_; }
  95. protected:
  96. /// Update text when text, font or spacing changed.
  97. void UpdateText(bool inResize = false);
  98. /// Validate text selection to be within the text.
  99. void ValidateSelection();
  100. /// Return row start X position.
  101. int GetRowStartPosition(unsigned rowIndex) const;
  102. /// Font.
  103. SharedPtr<Font> font_;
  104. /// Font size.
  105. int fontSize_;
  106. /// Text.
  107. String text_;
  108. /// Text modified into printed form.
  109. String printText_;
  110. /// Row alignment.
  111. HorizontalAlignment textAlignment_;
  112. /// Row spacing.
  113. float rowSpacing_;
  114. /// Wordwrap mode.
  115. bool wordWrap_;
  116. /// Selection start.
  117. unsigned selectionStart_;
  118. /// Selection length.
  119. unsigned selectionLength_;
  120. /// Selection background color.
  121. Color selectionColor_;
  122. /// Hover background color.
  123. Color hoverColor_;
  124. /// Row height.
  125. int rowHeight_;
  126. /// Row widths.
  127. PODVector<int> rowWidths_;
  128. /// Positions of each character.
  129. PODVector<IntVector2> charPositions_;
  130. /// Sizes of each character.
  131. PODVector<IntVector2> charSizes_;
  132. };