Text.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 "UIElement.h"
  24. namespace Atomic
  25. {
  26. namespace SystemUI
  27. {
  28. static const int DEFAULT_FONT_SIZE = 12;
  29. class Font;
  30. class FontFace;
  31. struct FontGlyph;
  32. /// Text effect.
  33. enum TextEffect
  34. {
  35. TE_NONE = 0,
  36. TE_SHADOW,
  37. TE_STROKE
  38. };
  39. /// Cached character location and size within text. Used for queries related to text editing.
  40. struct CharLocation
  41. {
  42. /// Position.
  43. IntVector2 position_;
  44. /// Size.
  45. IntVector2 size_;
  46. };
  47. /// Glyph and its location within the text. Used when preparing text rendering.
  48. struct GlyphLocation
  49. {
  50. // Construct.
  51. GlyphLocation(int x, int y, const FontGlyph* glyph) :
  52. x_(x),
  53. y_(y),
  54. glyph_(glyph)
  55. {
  56. }
  57. /// X coordinate.
  58. int x_;
  59. /// Y coordinate.
  60. int y_;
  61. /// Glyph.
  62. const FontGlyph* glyph_;
  63. };
  64. /// %Text %UI element.
  65. class ATOMIC_API Text : public UIElement
  66. {
  67. OBJECT(Text);
  68. friend class Text3D;
  69. public:
  70. /// Construct.
  71. Text(Context* context);
  72. /// Destruct.
  73. virtual ~Text();
  74. /// Register object factory.
  75. static void RegisterObject(Context* context);
  76. /// Apply attribute changes that can not be applied immediately.
  77. virtual void ApplyAttributes();
  78. /// Return UI rendering batches.
  79. virtual void GetBatches(PODVector<SystemUIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor);
  80. /// React to resize.
  81. virtual void OnResize();
  82. /// React to indent change.
  83. virtual void OnIndentSet();
  84. /// Set font and font size and use signed distance field.
  85. bool SetFont(const String& fontName, int size = DEFAULT_FONT_SIZE);
  86. /// Set font and font size and use signed distance field.
  87. bool SetFont(Font* font, int size = DEFAULT_FONT_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 effect color.
  109. void SetEffectColor(const Color& effectColor);
  110. /// Return font.
  111. Font* GetFont() const { return font_; }
  112. /// Return font size.
  113. int GetFontSize() const { return fontSize_; }
  114. /// Return text.
  115. const String& GetText() const { return text_; }
  116. /// Return row alignment.
  117. HorizontalAlignment GetTextAlignment() const { return textAlignment_; }
  118. /// Return row spacing.
  119. float GetRowSpacing() const { return rowSpacing_; }
  120. /// Return wordwrap mode.
  121. bool GetWordwrap() const { return wordWrap_; }
  122. /// Return auto localizable mode.
  123. bool GetAutoLocalizable() const { return autoLocalizable_; }
  124. /// Return selection start.
  125. unsigned GetSelectionStart() const { return selectionStart_; }
  126. /// Return selection length.
  127. unsigned GetSelectionLength() const { return selectionLength_; }
  128. /// Return selection background color.
  129. const Color& GetSelectionColor() const { return selectionColor_; }
  130. /// Return hover background color.
  131. const Color& GetHoverColor() const { return hoverColor_; }
  132. /// Return text effect.
  133. TextEffect GetTextEffect() const { return textEffect_; }
  134. /// Return effect color.
  135. const Color& GetEffectColor() const { return effectColor_; }
  136. /// Return row height.
  137. int GetRowHeight() const { return rowHeight_; }
  138. /// Return number of rows.
  139. unsigned GetNumRows() const { return rowWidths_.Size(); }
  140. /// Return number of characters.
  141. unsigned GetNumChars() const { return unicodeText_.Size(); }
  142. /// Return width of row by index.
  143. int GetRowWidth(unsigned index) const;
  144. /// Return position of character by index relative to the text element origin.
  145. IntVector2 GetCharPosition(unsigned index);
  146. /// Return size of character by index.
  147. IntVector2 GetCharSize(unsigned index);
  148. /// Set used in Text3D.
  149. void SetUsedInText3D(bool usedInText3D);
  150. /// Set text effect Z bias. Zero by default, adjusted only in 3D mode.
  151. void SetEffectDepthBias(float bias);
  152. /// Return effect Z bias.
  153. float GetEffectDepthBias() const { return effectDepthBias_; }
  154. /// Set font attribute.
  155. void SetFontAttr(const ResourceRef& value);
  156. /// Return font attribute.
  157. ResourceRef GetFontAttr() const;
  158. protected:
  159. /// Filter implicit attributes in serialization process.
  160. virtual bool FilterImplicitAttributes(XMLElement& dest) const;
  161. /// Update text when text, font or spacing changed.
  162. void UpdateText(bool onResize = false);
  163. /// Update cached character locations after text update, or when text alignment or indent has changed.
  164. void UpdateCharLocations();
  165. /// Validate text selection to be within the text.
  166. void ValidateSelection();
  167. /// Return row start X position.
  168. int GetRowStartPosition(unsigned rowIndex) const;
  169. /// Contruct batch.
  170. void ConstructBatch
  171. (SystemUIBatch& pageBatch, const PODVector<GlyphLocation>& pageGlyphLocation, int dx = 0, int dy = 0, Color* color = 0,
  172. float depthBias = 0.0f);
  173. /// Used in Text3D.
  174. bool usedInText3D_;
  175. /// Font.
  176. SharedPtr<Font> font_;
  177. /// Current face.
  178. WeakPtr<FontFace> fontFace_;
  179. /// Font size.
  180. int fontSize_;
  181. /// UTF-8 encoded text.
  182. String text_;
  183. /// Row alignment.
  184. HorizontalAlignment textAlignment_;
  185. /// Row spacing.
  186. float rowSpacing_;
  187. /// Wordwrap mode.
  188. bool wordWrap_;
  189. /// Char positions dirty flag.
  190. bool charLocationsDirty_;
  191. /// Selection start.
  192. unsigned selectionStart_;
  193. /// Selection length.
  194. unsigned selectionLength_;
  195. /// Selection background color.
  196. Color selectionColor_;
  197. /// Hover background color.
  198. Color hoverColor_;
  199. /// Text effect.
  200. TextEffect textEffect_;
  201. /// Effect color.
  202. Color effectColor_;
  203. /// Text effect Z bias.
  204. float effectDepthBias_;
  205. /// Row height.
  206. int rowHeight_;
  207. /// Text as Unicode characters.
  208. PODVector<unsigned> unicodeText_;
  209. /// Text modified into printed form.
  210. PODVector<unsigned> printText_;
  211. /// Mapping of printed form back to original char indices.
  212. PODVector<unsigned> printToText_;
  213. /// Row widths.
  214. PODVector<int> rowWidths_;
  215. /// Glyph locations per each texture in the font.
  216. Vector<PODVector<GlyphLocation> > pageGlyphLocations_;
  217. /// Cached locations of each character in the text.
  218. PODVector<CharLocation> charLocations_;
  219. /// The text will be automatically translated.
  220. bool autoLocalizable_;
  221. /// Storage string id. Used when enabled autoLocalizable.
  222. String stringId_;
  223. /// Handle change Language.
  224. void HandleChangeLanguage(StringHash eventType, VariantMap& eventData);
  225. /// UTF8 to Unicode.
  226. void DecodeToUnicode();
  227. };
  228. }
  229. }