Text.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // Copyright (c) 2008-2015 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 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. /// The text will be automatically translated. The text value used as string identifier.
  95. void SetAutoLocalizable(bool enable);
  96. /// Set selection. When length is not provided, select until the text ends.
  97. void SetSelection(unsigned start, unsigned length = M_MAX_UNSIGNED);
  98. /// Clear selection.
  99. void ClearSelection();
  100. /// Set selection background color. Color with 0 alpha (default) disables.
  101. void SetSelectionColor(const Color& color);
  102. /// Set hover background color. Color with 0 alpha (default) disables.
  103. void SetHoverColor(const Color& color);
  104. /// Set text effect.
  105. void SetTextEffect(TextEffect textEffect);
  106. /// Set effect color.
  107. void SetEffectColor(const Color& effectColor);
  108. /// Return font.
  109. Font* GetFont() const { return font_; }
  110. /// Return font size.
  111. int GetFontSize() const { return fontSize_; }
  112. /// Return text.
  113. const String& GetText() const { return text_; }
  114. /// Return row alignment.
  115. HorizontalAlignment GetTextAlignment() const { return textAlignment_; }
  116. /// Return row spacing.
  117. float GetRowSpacing() const { return rowSpacing_; }
  118. /// Return wordwrap mode.
  119. bool GetWordwrap() const { return wordWrap_; }
  120. /// Return auto localizable mode.
  121. bool GetAutoLocalizable() const { return autoLocalizable_; }
  122. /// Return selection start.
  123. unsigned GetSelectionStart() const { return selectionStart_; }
  124. /// Return selection length.
  125. unsigned GetSelectionLength() const { return selectionLength_; }
  126. /// Return selection background color.
  127. const Color& GetSelectionColor() const { return selectionColor_; }
  128. /// Return hover background color.
  129. const Color& GetHoverColor() const { return hoverColor_; }
  130. /// Return text effect.
  131. TextEffect GetTextEffect() const { return textEffect_; }
  132. /// Return effect color.
  133. const Color& GetEffectColor() const { return effectColor_; }
  134. /// Return row height.
  135. int GetRowHeight() const { return rowHeight_; }
  136. /// Return number of rows.
  137. unsigned GetNumRows() const { return rowWidths_.Size(); }
  138. /// Return number of characters.
  139. unsigned GetNumChars() const { return unicodeText_.Size(); }
  140. /// Return width of row by index.
  141. int GetRowWidth(unsigned index) const;
  142. /// Return position of character by index relative to the text element origin.
  143. IntVector2 GetCharPosition(unsigned index);
  144. /// Return size of character by index.
  145. IntVector2 GetCharSize(unsigned index);
  146. /// Set used in Text3D.
  147. void SetUsedInText3D(bool usedInText3D);
  148. /// Set text effect Z bias. Zero by default, adjusted only in 3D mode.
  149. void SetEffectDepthBias(float bias);
  150. /// Return effect Z bias.
  151. float GetEffectDepthBias() const { return effectDepthBias_; }
  152. /// Set font attribute.
  153. void SetFontAttr(const ResourceRef& value);
  154. /// Return font attribute.
  155. ResourceRef GetFontAttr() const;
  156. protected:
  157. /// Filter implicit attributes in serialization process.
  158. virtual bool FilterImplicitAttributes(XMLElement& dest) const;
  159. /// Update text when text, font or spacing changed.
  160. void UpdateText(bool onResize = false);
  161. /// Update cached character locations after text update, or when text alignment or indent has changed.
  162. void UpdateCharLocations();
  163. /// Validate text selection to be within the text.
  164. void ValidateSelection();
  165. /// Return row start X position.
  166. int GetRowStartPosition(unsigned rowIndex) const;
  167. /// Contruct batch.
  168. void ConstructBatch
  169. (UIBatch& pageBatch, const PODVector<GlyphLocation>& pageGlyphLocation, int dx = 0, int dy = 0, Color* color = 0,
  170. float depthBias = 0.0f);
  171. /// Used in Text3D.
  172. bool usedInText3D_;
  173. /// Font.
  174. SharedPtr<Font> font_;
  175. /// Current face.
  176. WeakPtr<FontFace> fontFace_;
  177. /// Font size.
  178. int fontSize_;
  179. /// UTF-8 encoded text.
  180. String text_;
  181. /// Row alignment.
  182. HorizontalAlignment textAlignment_;
  183. /// Row spacing.
  184. float rowSpacing_;
  185. /// Wordwrap mode.
  186. bool wordWrap_;
  187. /// Char positions dirty flag.
  188. bool charLocationsDirty_;
  189. /// Selection start.
  190. unsigned selectionStart_;
  191. /// Selection length.
  192. unsigned selectionLength_;
  193. /// Selection background color.
  194. Color selectionColor_;
  195. /// Hover background color.
  196. Color hoverColor_;
  197. /// Text effect.
  198. TextEffect textEffect_;
  199. /// Effect color.
  200. Color effectColor_;
  201. /// Text effect Z bias.
  202. float effectDepthBias_;
  203. /// Row height.
  204. int rowHeight_;
  205. /// Text as Unicode characters.
  206. PODVector<unsigned> unicodeText_;
  207. /// Text modified into printed form.
  208. PODVector<unsigned> printText_;
  209. /// Mapping of printed form back to original char indices.
  210. PODVector<unsigned> printToText_;
  211. /// Row widths.
  212. PODVector<int> rowWidths_;
  213. /// Glyph locations per each texture in the font.
  214. Vector<PODVector<GlyphLocation> > pageGlyphLocations_;
  215. /// Cached locations of each character in the text.
  216. PODVector<CharLocation> charLocations_;
  217. /// The text will be automatically translated.
  218. bool autoLocalizable_;
  219. /// Storage string id. Used when enabled autoLocalizable.
  220. String stringId_;
  221. /// Handle change Language.
  222. void HandleChangeLanguage(StringHash eventType, VariantMap& eventData);
  223. /// UTF8 to Unicode.
  224. void DecodeToUnicode();
  225. };
  226. }