Text.h 10.0 KB

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