Text.h 9.9 KB

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