TextControls.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * TextControls.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../gui/CIntObject.h"
  12. #include "../gui/TextAlignment.h"
  13. #include "../render/Colors.h"
  14. #include "../render/EFont.h"
  15. #include "../../lib/filesystem/ResourcePath.h"
  16. class IImage;
  17. class CSlider;
  18. /// Base class for all text-related widgets.
  19. /// Controls text blitting-related options
  20. class CTextContainer : public virtual CIntObject
  21. {
  22. protected:
  23. /// returns size of border, for left- or right-aligned text
  24. virtual Point getBorderSize() = 0;
  25. /// do actual blitting of line. Text "what" will be placed at "where" and aligned according to alignment
  26. void blitLine(Canvas & to, Rect where, std::string what);
  27. CTextContainer(ETextAlignment alignment, EFonts font, ColorRGBA color);
  28. public:
  29. ETextAlignment alignment;
  30. EFonts font;
  31. ColorRGBA color; // default font color. Can be overridden by placing "{}" into the string
  32. };
  33. /// Label which shows text
  34. class CLabel : public CTextContainer
  35. {
  36. protected:
  37. Point getBorderSize() override;
  38. virtual std::string visibleText();
  39. virtual void trimText();
  40. std::shared_ptr<CIntObject> background;
  41. std::string text;
  42. int maxWidth;
  43. bool autoRedraw; //whether control will redraw itself on setTxt
  44. public:
  45. std::string getText();
  46. virtual void setAutoRedraw(bool option);
  47. virtual void setText(const std::string & Txt);
  48. virtual void setMaxWidth(int width);
  49. virtual void setColor(const ColorRGBA & Color);
  50. void clear();
  51. size_t getWidth();
  52. CLabel(int x = 0, int y = 0, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::TOPLEFT,
  53. const ColorRGBA & Color = Colors::WHITE, const std::string & Text = "", int maxWidth = 0);
  54. void showAll(Canvas & to) override; //shows statusbar (with current text)
  55. };
  56. /// Small helper class to manage group of similar labels
  57. class CLabelGroup : public CIntObject
  58. {
  59. std::vector<std::shared_ptr<CLabel>> labels;
  60. EFonts font;
  61. ETextAlignment align;
  62. ColorRGBA color;
  63. public:
  64. CLabelGroup(EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::TOPLEFT, const ColorRGBA & Color = Colors::WHITE);
  65. void add(int x = 0, int y = 0, const std::string & text = "");
  66. size_t currentSize() const;
  67. };
  68. /// Multi-line label that can display multiple lines of text
  69. /// If text is too big to fit into requested area remaining part will not be visible
  70. class CMultiLineLabel : public CLabel
  71. {
  72. // text to blit, split into lines that are no longer than widget width
  73. std::vector<std::string> lines;
  74. // area of text that actually will be printed, default is widget size
  75. Rect visibleSize;
  76. void splitText(const std::string & Txt, bool redrawAfter);
  77. Rect getTextLocation();
  78. public:
  79. // total size of text, x = longest line of text, y = total height of lines
  80. Point textSize;
  81. CMultiLineLabel(Rect position, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::TOPLEFT, const ColorRGBA & Color = Colors::WHITE, const std::string & Text = "");
  82. void setText(const std::string & Txt) override;
  83. std::vector<std::string> getLines();
  84. void showAll(Canvas & to) override;
  85. void setVisibleSize(Rect visibleSize, bool redrawElement = true);
  86. // scrolls text visible in widget. Positive value will move text up
  87. void scrollTextTo(int distance, bool redrawAfterScroll = true);
  88. void scrollTextBy(int distance);
  89. };
  90. /// a multi-line label that tries to fit text with given available width and height;
  91. /// if not possible, it creates a slider for scrolling text
  92. class CTextBox : public CIntObject
  93. {
  94. int sliderStyle;
  95. public:
  96. std::shared_ptr<CMultiLineLabel> label;
  97. std::shared_ptr<CSlider> slider;
  98. CTextBox(std::string Text, const Rect & rect, int SliderStyle, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::TOPLEFT, const ColorRGBA & Color = Colors::WHITE);
  99. void resize(Point newSize);
  100. /// Resizes text box to minimal size needed to fit current text
  101. /// No effect if text is too large to fit and requires slider
  102. void trimToFit();
  103. void setText(const std::string & Txt);
  104. void sliderMoved(int to);
  105. };
  106. /// Status bar which is shown at the bottom of the in-game screens
  107. class CGStatusBar : public CLabel, public std::enable_shared_from_this<CGStatusBar>, public IStatusBar
  108. {
  109. std::string hoverText;
  110. std::string consoleText;
  111. bool enteringText;
  112. CGStatusBar(std::shared_ptr<CIntObject> background_, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::CENTER, const ColorRGBA & Color = Colors::WHITE);
  113. CGStatusBar(int x, int y, const ImagePath & name, int maxw = -1);
  114. CGStatusBar(int x, int y);
  115. //make CLabel API private
  116. using CLabel::getText;
  117. using CLabel::setAutoRedraw;
  118. using CLabel::setText;
  119. using CLabel::setColor;
  120. using CLabel::getWidth;
  121. protected:
  122. Point getBorderSize() override;
  123. void clickPressed(const Point & cursorPosition) override;
  124. public:
  125. ~CGStatusBar();
  126. template<typename ...Args>
  127. static std::shared_ptr<CGStatusBar> create(Args... args)
  128. {
  129. std::shared_ptr<CGStatusBar> ret{new CGStatusBar{args...}};
  130. return ret;
  131. }
  132. void show(Canvas & to) override;
  133. void activate() override;
  134. void deactivate() override;
  135. // IStatusBar interface
  136. void write(const std::string & Text) override;
  137. void clearIfMatching(const std::string & Text) override;
  138. void clear() override;
  139. void setEnteringMode(bool on) override;
  140. void setEnteredText(const std::string & text) override;
  141. };