UIText.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // Copyright (c) 2008-2014 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 "Drawable2D.h"
  24. #include "Text.h"
  25. namespace Urho3D
  26. {
  27. class UIRect;
  28. /// UI Line glyphs.
  29. struct UITextLineInfo
  30. {
  31. /// Construct.
  32. UITextLineInfo() : lineWidth_(0.0f)
  33. {
  34. }
  35. /// Clear.
  36. void Clear();
  37. /// Add font flyph
  38. void Add(const FontGlyph* glyph, short kerning);
  39. /// Width.
  40. float lineWidth_;
  41. /// Font glyphs.
  42. PODVector<const FontGlyph*> fontGlyphs_;
  43. /// Advances;
  44. PODVector<float> glyphAdvances_;
  45. };
  46. /// %Text %UI element.
  47. class URHO3D_API UIText : public Drawable2D
  48. {
  49. OBJECT(UIText);
  50. public:
  51. /// Construct.
  52. UIText(Context* context);
  53. /// Destruct.
  54. virtual ~UIText();
  55. /// Register object factory.
  56. static void RegisterObject(Context* context);
  57. /// Set font and font size and use signed distance field.
  58. bool SetFont(const String& fontName, int size = DEFAULT_FONT_SIZE);
  59. /// Set font and font size and use signed distance field.
  60. bool SetFont(Font* font, int size = DEFAULT_FONT_SIZE);
  61. /// Set font size.
  62. void SetFontSize(int fontSize = DEFAULT_FONT_SIZE);
  63. /// Set text. Text is assumed to be either ASCII or UTF8-encoded.
  64. void SetText(const String& text);
  65. /// Set row alignment.
  66. void SetTextAlignment(HorizontalAlignment align);
  67. /// Set row spacing, 1.0 for original font spacing.
  68. void SetRowSpacing(float spacing);
  69. /// Set color.
  70. void SetColor(const Color& color);
  71. /// Set selection. When length is not provided, select until the text ends.
  72. void SetSelection(unsigned start, unsigned length = M_MAX_UNSIGNED);
  73. /// Clear selection.
  74. void ClearSelection();
  75. /// Set selection background color. Color with 0 alpha (default) disables.
  76. void SetSelectionColor(const Color& color);
  77. /// Set text effect.
  78. void SetTextEffect(TextEffect textEffect);
  79. /// Set effect color.
  80. void SetEffectColor(const Color& effectColor);
  81. /// Return font.
  82. Font* GetFont() const { return font_; }
  83. /// Return font size.
  84. int GetFontSize() const { return fontSize_; }
  85. /// Return text.
  86. const String& GetText() const { return text_; }
  87. /// Return row alignment.
  88. HorizontalAlignment GetTextAlignment() const { return textAlignment_; }
  89. /// Return row spacing.
  90. float GetRowSpacing() const { return rowSpacing_; }
  91. /// Return color.
  92. const Color& GetColor() const { return color_; }
  93. /// Return selection start.
  94. unsigned GetSelectionStart() const { return selectionStart_; }
  95. /// Return selection length.
  96. unsigned GetSelectionLength() const { return selectionLength_; }
  97. /// Return selection background color.
  98. const Color& GetSelectionColor() const { return selectionColor_; }
  99. /// Return text effect.
  100. TextEffect GetTextEffect() const { return textEffect_; }
  101. /// Return effect color.
  102. const Color& GetEffectColor() const { return effectColor_; }
  103. /// Return number of characters.
  104. unsigned GetNumChars() const { return numChars_; }
  105. /// Return number of rows.
  106. unsigned GetNumRows() const { return textLineInfo_.Size(); }
  107. /// Return row width.
  108. float GetRowWidth(unsigned row) const { return row < textLineInfo_.Size() ? textLineInfo_[row].lineWidth_ : 0.0f; }
  109. /// Return row height.
  110. float GetRowHeight() const { return rowHeight_; }
  111. /// Set font attribute.
  112. void SetFontAttr(const ResourceRef& value);
  113. /// Return font attribute.
  114. ResourceRef GetFontAttr() const;
  115. protected:
  116. /// Handle node being assigned.
  117. virtual void OnNodeSet(Node* node);
  118. /// Recalculate the world-space bounding box.
  119. virtual void OnWorldBoundingBoxUpdate();
  120. /// Update vertices.
  121. virtual void UpdateVertices();
  122. /// handle rect changed.
  123. void HandleRectDirtied(StringHash eventType, VariantMap& eventData);
  124. /// Update material.
  125. void UpdateMaterial(bool newFont = false);
  126. /// Update text when text, font or spacing changed.
  127. void UpdateText();
  128. /// Validate text selection to be within the text.
  129. void ValidateSelection();
  130. /// Contruct batch.
  131. void UpdateTextLineVertices(const UITextLineInfo& lineInfo, float x, float y, unsigned color);
  132. /// UIRect
  133. WeakPtr<UIRect> uiRect_;
  134. /// Used in Text3D.
  135. bool usedInText3D_;
  136. /// Font.
  137. SharedPtr<Font> font_;
  138. /// Current face.
  139. WeakPtr<FontFace> fontFace_;
  140. /// Font size.
  141. int fontSize_;
  142. /// UTF-8 encoded text.
  143. String text_;
  144. /// Row alignment.
  145. HorizontalAlignment textAlignment_;
  146. /// Row spacing.
  147. float rowSpacing_;
  148. /// Color.
  149. Color color_;
  150. /// Selection background color.
  151. Color selectionColor_;
  152. /// Selection start.
  153. unsigned selectionStart_;
  154. /// Selection length.
  155. unsigned selectionLength_;
  156. /// Text effect.
  157. TextEffect textEffect_;
  158. /// Effect color.
  159. Color effectColor_;
  160. /// Number of characters.
  161. unsigned numChars_;
  162. /// Text line info.
  163. Vector<UITextLineInfo> textLineInfo_;
  164. /// Row height.
  165. float rowHeight_;
  166. };
  167. }