Text.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. //! Set font and font size
  42. bool setFont(Font* font, int size = DEFAULT_FONT_SIZE);
  43. //! Set maximum row width
  44. void setMaxWidth(int maxWidth);
  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 setTextSpacing(float spacing);
  51. //! Return font
  52. Font* getFont() const { return mFont; }
  53. //! Return font size
  54. int getFontSize() const { return mFontSize; }
  55. //! Return maximum row width
  56. int getMaxWidth() const { return mMaxWidth; }
  57. //! Return text
  58. const std::string& getText() const { return mText; }
  59. //! Return row alignment
  60. HorizontalAlignment getTextAlignment() const { return mTextAlignment; }
  61. //! Return row spacing
  62. float getTextSpacing() const { return mTextSpacing; }
  63. //! Return number of rows
  64. unsigned getNumRows() const { return mRowWidths.size(); }
  65. //! Return width of each row
  66. const std::vector<int>& getRowWidths() const { return mRowWidths; }
  67. //! Return row height
  68. int getRowHeight() const;
  69. protected:
  70. //! Calculate text size
  71. void calculateTextSize();
  72. //! Return row start X position
  73. int getRowStartPosition(unsigned rowIndex) const;
  74. //! Font
  75. SharedPtr<Font> mFont;
  76. //! Font size
  77. int mFontSize;
  78. //! Maximum row width
  79. int mMaxWidth;
  80. //! Text
  81. std::string mText;
  82. //! Text modified into printed form
  83. std::string mPrintText;
  84. //! Row alignment
  85. HorizontalAlignment mTextAlignment;
  86. //! Row spacing
  87. float mTextSpacing;
  88. //! Row widths
  89. std::vector<int> mRowWidths;
  90. };
  91. #endif // UI_STATICTEXT_H