Text.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // Copyright (c) 2008-2013 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 Urho3D
  25. {
  26. static const int DEFAULT_FONT_SIZE = 12;
  27. class Font;
  28. /// %Text %UI element.
  29. class Text : public UIElement
  30. {
  31. OBJECT(Text);
  32. public:
  33. /// Construct.
  34. Text(Context* context);
  35. /// Destruct.
  36. virtual ~Text();
  37. /// Register object factory.
  38. static void RegisterObject(Context* context);
  39. /// Apply attribute changes that can not be applied immediately.
  40. virtual void ApplyAttributes();
  41. /// Return UI rendering batches.
  42. virtual void GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor);
  43. /// React to resize.
  44. virtual void OnResize();
  45. /// Set font and font size.
  46. bool SetFont(const String& fontName, int size = DEFAULT_FONT_SIZE);
  47. /// Set font and font size.
  48. bool SetFont(Font* font, int size = DEFAULT_FONT_SIZE);
  49. /// Set text. Text is assumed to be either ASCII or UTF8-encoded.
  50. void SetText(const String& text);
  51. /// Set row alignment.
  52. void SetTextAlignment(HorizontalAlignment align);
  53. /// Set row spacing, 1.0 for original font spacing.
  54. void SetRowSpacing(float spacing);
  55. /// Set wordwrap. In wordwrap mode the text element will respect its current width. Otherwise it resizes itself freely.
  56. void SetWordwrap(bool enable);
  57. /// Set selection. When length is not provided, select until the text ends.
  58. void SetSelection(unsigned start, unsigned length = M_MAX_UNSIGNED);
  59. /// Clear selection.
  60. void ClearSelection();
  61. /// Set selection background color. Color with 0 alpha (default) disables.
  62. void SetSelectionColor(const Color& color);
  63. /// Set hover background color. Color with 0 alpha (default) disables.
  64. void SetHoverColor(const Color& color);
  65. /// Return font.
  66. Font* GetFont() const { return font_; }
  67. /// Return font size.
  68. int GetFontSize() const { return fontSize_; }
  69. /// Return text.
  70. const String& GetText() const { return text_; }
  71. /// Return row alignment.
  72. HorizontalAlignment GetTextAlignment() const { return textAlignment_; }
  73. /// Return row spacing.
  74. float GetRowSpacing() const { return rowSpacing_; }
  75. /// Return wordwrap mode.
  76. bool GetWordwrap() const { return wordWrap_; }
  77. /// Return selection start.
  78. unsigned GetSelectionStart() const { return selectionStart_; }
  79. /// Return selection length.
  80. unsigned GetSelectionLength() const { return selectionLength_; }
  81. /// Return selection background color.
  82. const Color& GetSelectionColor() const { return selectionColor_; }
  83. /// Return hover background color.
  84. const Color& GetHoverColor() const { return hoverColor_; }
  85. /// Return row height.
  86. int GetRowHeight() const { return rowHeight_; }
  87. /// Return number of rows.
  88. unsigned GetNumRows() const { return rowWidths_.Size(); }
  89. /// Return width of each row.
  90. const PODVector<int>& GetRowWidths() const { return rowWidths_; }
  91. /// Return position of each character.
  92. const PODVector<IntVector2>& GetCharPositions() const { return charPositions_; }
  93. /// Return size of each character.
  94. const PODVector<IntVector2>& GetCharSizes() const { return charSizes_; }
  95. /// Set font attribute.
  96. void SetFontAttr(ResourceRef value);
  97. /// Return font attribute.
  98. ResourceRef GetFontAttr() const;
  99. protected:
  100. /// Update text when text, font or spacing changed.
  101. void UpdateText(bool inResize = false);
  102. /// Validate text selection to be within the text.
  103. void ValidateSelection();
  104. /// Return row start X position.
  105. int GetRowStartPosition(unsigned rowIndex) const;
  106. /// Font.
  107. SharedPtr<Font> font_;
  108. /// Font size.
  109. int fontSize_;
  110. /// UTF-8 encoded text.
  111. String text_;
  112. /// Text as Unicode characters.
  113. PODVector<unsigned> unicodeText_;
  114. /// Text modified into printed form.
  115. PODVector<unsigned> printText_;
  116. /// Row alignment.
  117. HorizontalAlignment textAlignment_;
  118. /// Row spacing.
  119. float rowSpacing_;
  120. /// Wordwrap mode.
  121. bool wordWrap_;
  122. /// Selection start.
  123. unsigned selectionStart_;
  124. /// Selection length.
  125. unsigned selectionLength_;
  126. /// Selection background color.
  127. Color selectionColor_;
  128. /// Hover background color.
  129. Color hoverColor_;
  130. /// Row height.
  131. int rowHeight_;
  132. /// Row widths.
  133. PODVector<int> rowWidths_;
  134. /// Positions of each character.
  135. PODVector<IntVector2> charPositions_;
  136. /// Sizes of each character.
  137. PODVector<IntVector2> charSizes_;
  138. };
  139. }