Text.h 7.2 KB

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