Text.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. /// \file
  4. #pragma once
  5. #include "../UI/UISelectable.h"
  6. namespace Urho3D
  7. {
  8. inline constexpr float DEFAULT_FONT_SIZE = 12;
  9. class Font;
  10. class FontFace;
  11. struct FontGlyph;
  12. /// Text effect.
  13. enum TextEffect
  14. {
  15. TE_NONE = 0,
  16. TE_SHADOW,
  17. TE_STROKE
  18. };
  19. /// Cached character location and size within text. Used for queries related to text editing.
  20. struct CharLocation
  21. {
  22. /// Position.
  23. Vector2 position_;
  24. /// Size.
  25. Vector2 size_;
  26. };
  27. /// Glyph and its location within the text. Used when preparing text rendering.
  28. /// @nobind
  29. struct GlyphLocation
  30. {
  31. /// Fields are not initialized.
  32. GlyphLocation() = default;
  33. /// Construct.
  34. GlyphLocation(float x, float y, const FontGlyph* glyph) :
  35. x_(x),
  36. y_(y),
  37. glyph_(glyph)
  38. {
  39. }
  40. /// X coordinate.
  41. float x_;
  42. /// Y coordinate.
  43. float y_;
  44. /// Glyph.
  45. const FontGlyph* glyph_;
  46. };
  47. /// %Text %UI element.
  48. class URHO3D_API Text : public UISelectable
  49. {
  50. URHO3D_OBJECT(Text, UISelectable);
  51. friend class Text3D;
  52. public:
  53. /// Construct.
  54. explicit Text(Context* context);
  55. /// Destruct.
  56. ~Text() override;
  57. /// Register object factory.
  58. /// @nobind
  59. static void RegisterObject(Context* context);
  60. /// Apply attribute changes that can not be applied immediately.
  61. void ApplyAttributes() override;
  62. /// Return UI rendering batches.
  63. void GetBatches(Vector<UIBatch>& batches, Vector<float>& vertexData, const IntRect& currentScissor) override;
  64. /// React to resize.
  65. void OnResize(const IntVector2& newSize, const IntVector2& delta) override;
  66. /// React to indent change.
  67. void OnIndentSet() override;
  68. /// Set font by looking from resource cache by name and font size. Return true if successful.
  69. bool SetFont(const String& fontName, float size = DEFAULT_FONT_SIZE);
  70. /// Set font and font size. Return true if successful.
  71. bool SetFont(Font* font, float size = DEFAULT_FONT_SIZE);
  72. /// Set font size only while retaining the existing font. Return true if successful.
  73. /// @property
  74. bool SetFontSize(float size);
  75. /// Set text. Text is assumed to be either ASCII or UTF8-encoded.
  76. /// @property
  77. void SetText(const String& text);
  78. /// Set row alignment.
  79. /// @property
  80. void SetTextAlignment(HorizontalAlignment align);
  81. /// Set row spacing, 1.0 for original font spacing.
  82. /// @property
  83. void SetRowSpacing(float spacing);
  84. /// Set wordwrap. In wordwrap mode the text element will respect its current width. Otherwise it resizes itself freely.
  85. /// @property
  86. void SetWordwrap(bool enable);
  87. /// The text will be automatically translated. The text value used as string identifier.
  88. /// @property
  89. void SetAutoLocalizable(bool enable);
  90. /// Set selection. When length is not provided, select until the text ends.
  91. void SetSelection(i32 start, i32 length = M_MAX_INT);
  92. /// Clear selection.
  93. void ClearSelection();
  94. /// Set text effect.
  95. /// @property
  96. void SetTextEffect(TextEffect textEffect);
  97. /// Set shadow offset.
  98. /// @property
  99. void SetEffectShadowOffset(const IntVector2& offset);
  100. /// Set stroke thickness.
  101. /// @property
  102. void SetEffectStrokeThickness(int thickness);
  103. /// Set stroke rounding. Corners of the font will be rounded off in the stroke so the stroke won't have corners.
  104. /// @property
  105. void SetEffectRoundStroke(bool roundStroke);
  106. /// Set effect color.
  107. /// @property
  108. void SetEffectColor(const Color& effectColor);
  109. /// Return font.
  110. /// @property
  111. Font* GetFont() const { return font_; }
  112. /// Return font size.
  113. /// @property
  114. float GetFontSize() const { return fontSize_; }
  115. /// Return text.
  116. /// @property
  117. const String& GetText() const { return text_; }
  118. /// Return row alignment.
  119. /// @property
  120. HorizontalAlignment GetTextAlignment() const { return textAlignment_; }
  121. /// Return row spacing.
  122. /// @property
  123. float GetRowSpacing() const { return rowSpacing_; }
  124. /// Return wordwrap mode.
  125. /// @property
  126. bool GetWordwrap() const { return wordWrap_; }
  127. /// Return auto localizable mode.
  128. /// @property
  129. bool GetAutoLocalizable() const { return autoLocalizable_; }
  130. /// Return selection start.
  131. /// @property
  132. i32 GetSelectionStart() const { return selectionStart_; }
  133. /// Return selection length.
  134. /// @property
  135. i32 GetSelectionLength() const { return selectionLength_; }
  136. /// Return text effect.
  137. /// @property
  138. TextEffect GetTextEffect() const { return textEffect_; }
  139. /// Return effect shadow offset.
  140. /// @property
  141. const IntVector2& GetEffectShadowOffset() const { return shadowOffset_; }
  142. /// Return effect stroke thickness.
  143. /// @property
  144. int GetEffectStrokeThickness() const { return strokeThickness_; }
  145. /// Return effect round stroke.
  146. /// @property
  147. bool GetEffectRoundStroke() const { return roundStroke_; }
  148. /// Return effect color.
  149. /// @property
  150. const Color& GetEffectColor() const { return effectColor_; }
  151. /// Return row height.
  152. /// @property
  153. float GetRowHeight() const { return rowHeight_; }
  154. /// Return number of rows.
  155. /// @property
  156. i32 GetNumRows() const { return rowWidths_.Size(); }
  157. /// Return number of characters.
  158. /// @property
  159. i32 GetNumChars() const { return unicodeText_.Size(); }
  160. /// Return width of row by index.
  161. /// @property{get_rowWidths}
  162. float GetRowWidth(i32 index) const;
  163. /// Return position of character by index relative to the text element origin.
  164. /// @property{get_charPositions}
  165. Vector2 GetCharPosition(i32 index);
  166. /// Return size of character by index.
  167. /// @property{get_charSizes}
  168. Vector2 GetCharSize(i32 index);
  169. /// Set text effect Z bias. Zero by default, adjusted only in 3D mode.
  170. void SetEffectDepthBias(float bias);
  171. /// Return effect Z bias.
  172. float GetEffectDepthBias() const { return effectDepthBias_; }
  173. /// Set font attribute.
  174. void SetFontAttr(const ResourceRef& value);
  175. /// Return font attribute.
  176. ResourceRef GetFontAttr() const;
  177. /// Set text attribute.
  178. void SetTextAttr(const String& value);
  179. /// Return text attribute.
  180. String GetTextAttr() const;
  181. protected:
  182. /// Filter implicit attributes in serialization process.
  183. bool FilterImplicitAttributes(XMLElement& dest) const override;
  184. /// Update text when text, font or spacing changed.
  185. void UpdateText(bool onResize = false);
  186. /// Update cached character locations after text update, or when text alignment or indent has changed.
  187. void UpdateCharLocations();
  188. /// Validate text selection to be within the text.
  189. void ValidateSelection();
  190. /// Return row start X position.
  191. int GetRowStartPosition(i32 rowIndex) const;
  192. /// Construct batch.
  193. void ConstructBatch
  194. (UIBatch& pageBatch, const Vector<GlyphLocation>& pageGlyphLocation, float dx = 0, float dy = 0, Color* color = nullptr,
  195. float depthBias = 0.0f);
  196. /// Font.
  197. SharedPtr<Font> font_;
  198. /// Current face.
  199. WeakPtr<FontFace> fontFace_;
  200. /// Font size.
  201. float fontSize_;
  202. /// UTF-8 encoded text.
  203. String text_;
  204. /// Row alignment.
  205. HorizontalAlignment textAlignment_;
  206. /// Row spacing.
  207. float rowSpacing_;
  208. /// Wordwrap mode.
  209. bool wordWrap_;
  210. /// Char positions dirty flag.
  211. bool charLocationsDirty_;
  212. /// Selection start.
  213. i32 selectionStart_;
  214. /// Selection length.
  215. i32 selectionLength_;
  216. /// Text effect.
  217. TextEffect textEffect_;
  218. /// Text effect shadow offset.
  219. IntVector2 shadowOffset_;
  220. /// Text effect stroke thickness.
  221. int strokeThickness_;
  222. /// Text effect stroke rounding flag.
  223. bool roundStroke_;
  224. /// Effect color.
  225. Color effectColor_;
  226. /// Text effect Z bias.
  227. float effectDepthBias_;
  228. /// Row height.
  229. float rowHeight_;
  230. /// Text as Unicode characters.
  231. Vector<c32> unicodeText_;
  232. /// Text modified into printed form.
  233. Vector<c32> printText_;
  234. /// Mapping of printed form back to original char indices.
  235. Vector<i32> printToText_;
  236. /// Row widths.
  237. Vector<float> rowWidths_;
  238. /// Glyph locations per each texture in the font.
  239. Vector<Vector<GlyphLocation>> pageGlyphLocations_;
  240. /// Cached locations of each character in the text.
  241. Vector<CharLocation> charLocations_;
  242. /// The text will be automatically translated.
  243. bool autoLocalizable_;
  244. /// Localization string id storage. Used when autoLocalizable flag is set.
  245. String stringId_;
  246. /// Handle change Language.
  247. void HandleChangeLanguage(StringHash eventType, VariantMap& eventData);
  248. /// UTF8 to Unicode.
  249. void DecodeToUnicode();
  250. };
  251. }