Text.h 5.7 KB

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