Text.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. public:
  32. //! Construct with initial text and name
  33. Text(const std::string& text = std::string(), const std::string& name = std::string());
  34. //! Destruct
  35. virtual ~Text();
  36. //! Load parameters from an XML file
  37. virtual XMLElement loadParameters(XMLFile* file, const std::string& elementName, ResourceCache* cache);
  38. //! Return UI rendering batches
  39. virtual void getBatches(std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, const IntRect& currentScissor);
  40. //! Set font and font size
  41. bool setFont(Font* font, int size = DEFAULT_FONT_SIZE);
  42. //! Set maximum row width
  43. void setMaxWidth(int maxWidth);
  44. //! Set text
  45. void setText(const std::string& text);
  46. //! Set row alignment
  47. void setTextAlignment(HorizontalAlignment align);
  48. //! Set row spacing, 1.0 for original font spacing
  49. void setTextSpacing(float spacing);
  50. //! Return font
  51. Font* getFont() const { return mFont; }
  52. //! Return font size
  53. int getFontSize() const { return mFontSize; }
  54. //! Return maximum row width
  55. int getMaxWidth() const { return mMaxWidth; }
  56. //! Return text
  57. const std::string& getText() const { return mText; }
  58. //! Return row alignment
  59. HorizontalAlignment getTextAlignment() const { return mTextAlignment; }
  60. //! Return row spacing
  61. float getTextSpacing() const { return mTextSpacing; }
  62. //! Return number of rows
  63. unsigned getNumRows() const { return mRowWidths.size(); }
  64. //! Return width of each row
  65. const std::vector<int>& getRowWidths() const { return mRowWidths; }
  66. //! Return row height
  67. int getRowHeight() const;
  68. protected:
  69. //! Calculate text size
  70. void calculateTextSize();
  71. //! Return row start X position
  72. int getRowStartPosition(unsigned rowIndex) const;
  73. //! Font
  74. SharedPtr<Font> mFont;
  75. //! Font size
  76. int mFontSize;
  77. //! Maximum row width
  78. int mMaxWidth;
  79. //! Text
  80. std::string mText;
  81. //! Text modified into printed form
  82. std::string mPrintText;
  83. //! Row alignment
  84. HorizontalAlignment mTextAlignment;
  85. //! Row spacing
  86. float mTextSpacing;
  87. //! Row widths
  88. std::vector<int> mRowWidths;
  89. };
  90. #endif // UI_STATICTEXT_H