TextControls.h 7.4 KB

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