Text.h 9.3 KB

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