Text.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #ifndef UI_TEXT_H
  24. #define UI_TEXT_H
  25. #include "UIElement.h"
  26. static const int DEFAULT_FONT_SIZE = 12;
  27. class Font;
  28. //! An UI element that displays text
  29. class Text : public UIElement
  30. {
  31. DEFINE_TYPE(Text);
  32. public:
  33. //! Construct with name and initial text
  34. Text(const std::string& name = std::string(), const std::string& text = std::string());
  35. //! Destruct
  36. virtual ~Text();
  37. //! Set UI element style from XML data
  38. virtual void setStyle(const XMLElement& element, ResourceCache* cache);
  39. //! Return UI rendering batches
  40. virtual void getBatches(std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, const IntRect& currentScissor);
  41. //! React to resize
  42. virtual void onResize();
  43. //! Set font and font size
  44. bool setFont(Font* font, int size = DEFAULT_FONT_SIZE);
  45. //! Set text
  46. void setText(const std::string& text);
  47. //! Set row alignment
  48. void setTextAlignment(HorizontalAlignment align);
  49. //! Set row spacing, 1.0 for original font spacing
  50. void setRowSpacing(float spacing);
  51. //! Set wordwrap. In wordwrap mode the text element will respect its current width. Otherwise it resizes itself freely
  52. void setWordwrap(bool enable);
  53. //! Set selection
  54. void setSelection(unsigned start, unsigned length);
  55. //! Clear selection
  56. void clearSelection();
  57. //! Set selection background color. Color with 0 alpha (default) disables
  58. void setSelectionColor(const Color& color);
  59. //! Set hover background color. Color with 0 alpha (default) disables
  60. void setHoverColor(const Color& color);
  61. //! Return font
  62. Font* getFont() const { return mFont; }
  63. //! Return font size
  64. int getFontSize() const { return mFontSize; }
  65. //! Return text
  66. const std::string& getText() const { return mText; }
  67. //! Return row alignment
  68. HorizontalAlignment getTextAlignment() const { return mTextAlignment; }
  69. //! Return row spacing
  70. float getRowSpacing() const { return mRowSpacing; }
  71. //! Return wordwrap mode
  72. bool getWordwrap() const { return mWordwrap; }
  73. //! Return selection start
  74. unsigned getSelectionStart() const { return mSelectionStart; }
  75. //! Return selection length
  76. unsigned getSelectionLength() const { return mSelectionLength; }
  77. //! Return selection background color
  78. const Color& getSelectionColor() const { return mSelectionColor; }
  79. //! Return hover background color
  80. const Color& getHoverColor() const { return mHoverColor; }
  81. //! Return row height
  82. int getRowHeight() const { return mRowHeight; }
  83. //! Return number of rows
  84. unsigned getNumRows() const { return mRowWidths.size(); }
  85. //! Return width of each row
  86. const std::vector<int>& getRowWidths() const { return mRowWidths; }
  87. //! Return position of each character
  88. const std::vector<IntVector2>& getCharPositions() const { return mCharPositions; }
  89. //! Return size of each character
  90. const std::vector<IntVector2>& getCharSizes() const { return mCharSizes; }
  91. protected:
  92. //! Update text when text, font or spacing changed
  93. void updateText(bool inResize = false);
  94. //! Validate text selection to be within the text
  95. void validateSelection();
  96. //! Return row start X position
  97. int getRowStartPosition(unsigned rowIndex) const;
  98. //! Font
  99. SharedPtr<Font> mFont;
  100. //! Font size
  101. int mFontSize;
  102. //! Text
  103. std::string mText;
  104. //! Text modified into printed form
  105. std::string mPrintText;
  106. //! Row alignment
  107. HorizontalAlignment mTextAlignment;
  108. //! Row spacing
  109. float mRowSpacing;
  110. //! Wordwrap mode
  111. bool mWordwrap;
  112. //! Selection start
  113. unsigned mSelectionStart;
  114. //! Selection length
  115. unsigned mSelectionLength;
  116. //! Selection background color
  117. Color mSelectionColor;
  118. //! Hover background color
  119. Color mHoverColor;
  120. //! Row height
  121. int mRowHeight;
  122. //! Row widths
  123. std::vector<int> mRowWidths;
  124. //! Positions of each character
  125. std::vector<IntVector2> mCharPositions;
  126. //! Sizes of each character
  127. std::vector<IntVector2> mCharSizes;
  128. };
  129. #endif // UI_STATICTEXT_H