Text.h 7.5 KB

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