Text.h 8.1 KB

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