TextControls.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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/FunctionList.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. std::shared_ptr<CIntObject> background;
  40. std::string text;
  41. bool autoRedraw; //whether control will redraw itself on setTxt
  42. public:
  43. std::string getText();
  44. virtual void setAutoRedraw(bool option);
  45. virtual void setText(const std::string & Txt);
  46. virtual void setColor(const ColorRGBA & Color);
  47. size_t getWidth();
  48. CLabel(int x = 0, int y = 0, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::TOPLEFT,
  49. const ColorRGBA & Color = Colors::WHITE, const std::string & Text = "");
  50. void showAll(Canvas & to) override; //shows statusbar (with current text)
  51. };
  52. /// Small helper class to manage group of similar labels
  53. class CLabelGroup : public CIntObject
  54. {
  55. std::vector<std::shared_ptr<CLabel>> labels;
  56. EFonts font;
  57. ETextAlignment align;
  58. ColorRGBA color;
  59. public:
  60. CLabelGroup(EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::TOPLEFT, const ColorRGBA & Color = Colors::WHITE);
  61. void add(int x = 0, int y = 0, const std::string & text = "");
  62. size_t currentSize() const;
  63. };
  64. /// Multi-line label that can display multiple lines of text
  65. /// If text is too big to fit into requested area remaining part will not be visible
  66. class CMultiLineLabel : public CLabel
  67. {
  68. // text to blit, split into lines that are no longer than widget width
  69. std::vector<std::string> lines;
  70. // area of text that actually will be printed, default is widget size
  71. Rect visibleSize;
  72. void splitText(const std::string & Txt, bool redrawAfter);
  73. Rect getTextLocation();
  74. public:
  75. // total size of text, x = longest line of text, y = total height of lines
  76. Point textSize;
  77. CMultiLineLabel(Rect position, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::TOPLEFT, const ColorRGBA & Color = Colors::WHITE, const std::string & Text = "");
  78. void setText(const std::string & Txt) override;
  79. void showAll(Canvas & to) override;
  80. void setVisibleSize(Rect visibleSize, bool redrawElement = true);
  81. // scrolls text visible in widget. Positive value will move text up
  82. void scrollTextTo(int distance, bool redrawAfterScroll = true);
  83. void scrollTextBy(int distance);
  84. };
  85. /// a multi-line label that tries to fit text with given available width and height;
  86. /// if not possible, it creates a slider for scrolling text
  87. class CTextBox : public CIntObject
  88. {
  89. int sliderStyle;
  90. public:
  91. std::shared_ptr<CMultiLineLabel> label;
  92. std::shared_ptr<CSlider> slider;
  93. CTextBox(std::string Text, const Rect & rect, int SliderStyle, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::TOPLEFT, const ColorRGBA & Color = Colors::WHITE);
  94. void resize(Point newSize);
  95. void setText(const std::string & Txt);
  96. void sliderMoved(int to);
  97. };
  98. /// Status bar which is shown at the bottom of the in-game screens
  99. class CGStatusBar : public CLabel, public std::enable_shared_from_this<CGStatusBar>, public IStatusBar
  100. {
  101. std::string hoverText;
  102. std::string consoleText;
  103. bool enteringText;
  104. CGStatusBar(std::shared_ptr<CIntObject> background_, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::CENTER, const ColorRGBA & Color = Colors::WHITE);
  105. CGStatusBar(int x, int y, std::string name, int maxw = -1);
  106. //make CLabel API private
  107. using CLabel::getText;
  108. using CLabel::setAutoRedraw;
  109. using CLabel::setText;
  110. using CLabel::setColor;
  111. using CLabel::getWidth;
  112. protected:
  113. Point getBorderSize() override;
  114. void clickPressed(const Point & cursorPosition) override;
  115. public:
  116. ~CGStatusBar();
  117. template<typename ...Args>
  118. static std::shared_ptr<CGStatusBar> create(Args... args)
  119. {
  120. std::shared_ptr<CGStatusBar> ret{new CGStatusBar{args...}};
  121. return ret;
  122. }
  123. void show(Canvas & to) override;
  124. void activate() override;
  125. void deactivate() override;
  126. // IStatusBar interface
  127. void write(const std::string & Text) override;
  128. void clearIfMatching(const std::string & Text) override;
  129. void clear() override;
  130. void setEnteringMode(bool on) override;
  131. void setEnteredText(const std::string & text) override;
  132. };
  133. class CFocusable;
  134. class IFocusListener
  135. {
  136. public:
  137. virtual void focusGot() {};
  138. virtual void focusLost() {};
  139. virtual ~IFocusListener() = default;
  140. };
  141. /// UIElement which can get input focus
  142. class CFocusable : public virtual CIntObject
  143. {
  144. private:
  145. std::shared_ptr<IFocusListener> focusListener;
  146. public:
  147. bool focus; //only one focusable control can have focus at one moment
  148. void giveFocus(); //captures focus
  149. void moveFocus(); //moves focus to next active control (may be used for tab switching)
  150. bool hasFocus() const;
  151. static std::list<CFocusable *> focusables; //all existing objs
  152. static CFocusable * inputWithFocus; //who has focus now
  153. CFocusable();
  154. CFocusable(std::shared_ptr<IFocusListener> focusListener);
  155. ~CFocusable();
  156. };
  157. class CTextInput;
  158. class CKeyboardFocusListener : public IFocusListener
  159. {
  160. private:
  161. static std::atomic<int> usageIndex;
  162. CTextInput * textInput;
  163. public:
  164. CKeyboardFocusListener(CTextInput * textInput);
  165. void focusGot() override;
  166. void focusLost() override;
  167. };
  168. /// Text input box where players can enter text
  169. class CTextInput : public CLabel, public CFocusable
  170. {
  171. std::string newText;
  172. protected:
  173. std::string visibleText() override;
  174. public:
  175. CFunctionList<void(const std::string &)> cb;
  176. CFunctionList<void(std::string &, const std::string &)> filters;
  177. void setText(const std::string & nText) override;
  178. void setText(const std::string & nText, bool callCb);
  179. CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB);
  180. CTextInput(const Rect & Pos, const Point & bgOffset, const std::string & bgName, const CFunctionList<void(const std::string &)> & CB);
  181. CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf);
  182. void clickPressed(const Point & cursorPosition) override;
  183. void keyPressed(EShortcut key) override;
  184. //bool captureThisKey(EShortcut key) override;
  185. void textInputed(const std::string & enteredText) override;
  186. void textEdited(const std::string & enteredText) override;
  187. //Filter that will block all characters not allowed in filenames
  188. static void filenameFilter(std::string & text, const std::string & oldText);
  189. //Filter that will allow only input of numbers in range min-max (min-max are allowed)
  190. //min-max should be set via something like std::bind
  191. static void numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue);
  192. friend class CKeyboardFocusListener;
  193. };