Text.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 effect.
  29. enum TextEffect
  30. {
  31. TE_NONE = 0
  32. TE_SHADOW,
  33. TE_STROKE,
  34. };
  35. /// %Text %UI element.
  36. class URHO3D_API Text : public UIElement
  37. {
  38. OBJECT(Text);
  39. friend class Text3D;
  40. public:
  41. /// Construct.
  42. Text(Context* context);
  43. /// Destruct.
  44. virtual ~Text();
  45. /// Register object factory.
  46. static void RegisterObject(Context* context);
  47. /// Apply attribute changes that can not be applied immediately.
  48. virtual void ApplyAttributes();
  49. /// Return UI rendering batches.
  50. virtual void GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor);
  51. /// React to resize.
  52. virtual void OnResize();
  53. /// Set font and font size.
  54. bool SetFont(const String& fontName, int size = DEFAULT_FONT_SIZE);
  55. /// Set font and font size.
  56. bool SetFont(Font* font, int size = DEFAULT_FONT_SIZE);
  57. /// Set text. Text is assumed to be either ASCII or UTF8-encoded.
  58. void SetText(const String& text);
  59. /// Set row alignment.
  60. void SetTextAlignment(HorizontalAlignment align);
  61. /// Set row spacing, 1.0 for original font spacing.
  62. void SetRowSpacing(float spacing);
  63. /// Set wordwrap. In wordwrap mode the text element will respect its current width. Otherwise it resizes itself freely.
  64. void SetWordwrap(bool enable);
  65. /// Set selection. When length is not provided, select until the text ends.
  66. void SetSelection(unsigned start, unsigned length = M_MAX_UNSIGNED);
  67. /// Clear selection.
  68. void ClearSelection();
  69. /// Set selection background color. Color with 0 alpha (default) disables.
  70. void SetSelectionColor(const Color& color);
  71. /// Set hover background color. Color with 0 alpha (default) disables.
  72. void SetHoverColor(const Color& color);
  73. /// Return font.
  74. Font* GetFont() const { return font_; }
  75. /// Return font size.
  76. int GetFontSize() const { return fontSize_; }
  77. /// Return text.
  78. const String& GetText() const { return text_; }
  79. /// Return row alignment.
  80. HorizontalAlignment GetTextAlignment() const { return textAlignment_; }
  81. /// Return row spacing.
  82. float GetRowSpacing() const { return rowSpacing_; }
  83. /// Return wordwrap mode.
  84. bool GetWordwrap() const { return wordWrap_; }
  85. /// Return selection start.
  86. unsigned GetSelectionStart() const { return selectionStart_; }
  87. /// Return selection length.
  88. unsigned GetSelectionLength() const { return selectionLength_; }
  89. /// Return selection background color.
  90. const Color& GetSelectionColor() const { return selectionColor_; }
  91. /// Return hover background color.
  92. const Color& GetHoverColor() const { return hoverColor_; }
  93. /// Return row height.
  94. int GetRowHeight() const { return rowHeight_; }
  95. /// Return number of rows.
  96. unsigned GetNumRows() const { return rowWidths_.Size(); }
  97. /// Return width of each row.
  98. const PODVector<int>& GetRowWidths() const { return rowWidths_; }
  99. /// Return position of each character.
  100. const PODVector<IntVector2>& GetCharPositions() const { return charPositions_; }
  101. /// Return size of each character.
  102. const PODVector<IntVector2>& GetCharSizes() const { return charSizes_; }
  103. /// Set font attribute.
  104. void SetFontAttr(ResourceRef value);
  105. /// Return font attribute.
  106. ResourceRef GetFontAttr() const;
  107. protected:
  108. /// Filter implicit attributes in serialization process.
  109. virtual bool FilterImplicitAttributes(XMLElement& dest) const;
  110. /// Update text when text, font or spacing changed.
  111. void UpdateText();
  112. /// Validate text selection to be within the text.
  113. void ValidateSelection();
  114. /// Return row start X position.
  115. int GetRowStartPosition(unsigned rowIndex) const;
  116. /// Font.
  117. SharedPtr<Font> font_;
  118. /// Font size.
  119. int fontSize_;
  120. /// UTF-8 encoded text.
  121. String text_;
  122. /// Text as Unicode characters.
  123. PODVector<unsigned> unicodeText_;
  124. /// Text modified into printed form.
  125. PODVector<unsigned> printText_;
  126. /// Row alignment.
  127. HorizontalAlignment textAlignment_;
  128. /// Row spacing.
  129. float rowSpacing_;
  130. /// Wordwrap mode.
  131. bool wordWrap_;
  132. /// Selection start.
  133. unsigned selectionStart_;
  134. /// Selection length.
  135. unsigned selectionLength_;
  136. /// Selection background color.
  137. Color selectionColor_;
  138. /// Hover background color.
  139. Color hoverColor_;
  140. /// Row height.
  141. int rowHeight_;
  142. /// Row widths.
  143. PODVector<int> rowWidths_;
  144. /// Positions of each character.
  145. PODVector<IntVector2> charPositions_;
  146. /// Sizes of each character.
  147. PODVector<IntVector2> charSizes_;
  148. };
  149. }