TextControls.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/SDL_Extensions.h"
  13. #include "../../lib/FunctionList.h"
  14. class CSlider;
  15. /// Base class for all text-related widgets.
  16. /// Controls text blitting-related options
  17. class CTextContainer : public virtual CIntObject
  18. {
  19. protected:
  20. /// returns size of border, for left- or right-aligned text
  21. virtual Point getBorderSize() = 0;
  22. /// do actual blitting of line. Text "what" will be placed at "where" and aligned according to alignment
  23. void blitLine(SDL_Surface * to, Rect where, std::string what);
  24. CTextContainer(ETextAlignment alignment, EFonts font, SDL_Color color);
  25. public:
  26. ETextAlignment alignment;
  27. EFonts font;
  28. SDL_Color color; // default font color. Can be overridden by placing "{}" into the string
  29. };
  30. /// Label which shows text
  31. class CLabel : public CTextContainer
  32. {
  33. protected:
  34. Point getBorderSize() override;
  35. virtual std::string visibleText();
  36. std::shared_ptr<CPicture> background;
  37. std::string text;
  38. bool autoRedraw; //whether control will redraw itself on setTxt
  39. public:
  40. std::string getText();
  41. virtual void setAutoRedraw(bool option);
  42. virtual void setText(const std::string & Txt);
  43. virtual void setColor(const SDL_Color & Color);
  44. size_t getWidth();
  45. CLabel(int x = 0, int y = 0, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::TOPLEFT,
  46. const SDL_Color & Color = Colors::WHITE, const std::string & Text = "");
  47. void showAll(SDL_Surface * to) override; //shows statusbar (with current text)
  48. };
  49. /// Small helper class to manage group of similar labels
  50. class CLabelGroup : public CIntObject
  51. {
  52. std::vector<std::shared_ptr<CLabel>> labels;
  53. EFonts font;
  54. ETextAlignment align;
  55. SDL_Color color;
  56. public:
  57. CLabelGroup(EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::TOPLEFT, const SDL_Color & Color = Colors::WHITE);
  58. void add(int x = 0, int y = 0, const std::string & text = "");
  59. size_t currentSize() const;
  60. };
  61. /// Multi-line label that can display multiple lines of text
  62. /// If text is too big to fit into requested area remaining part will not be visible
  63. class CMultiLineLabel : public CLabel
  64. {
  65. // text to blit, split into lines that are no longer than widget width
  66. std::vector<std::string> lines;
  67. // area of text that actually will be printed, default is widget size
  68. Rect visibleSize;
  69. void splitText(const std::string & Txt, bool redrawAfter);
  70. Rect getTextLocation();
  71. public:
  72. // total size of text, x = longest line of text, y = total height of lines
  73. Point textSize;
  74. CMultiLineLabel(Rect position, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::TOPLEFT, const SDL_Color & Color = Colors::WHITE, const std::string & Text = "");
  75. void setText(const std::string & Txt) override;
  76. void showAll(SDL_Surface * to) override;
  77. void setVisibleSize(Rect visibleSize, bool redrawElement = true);
  78. // scrolls text visible in widget. Positive value will move text up
  79. void scrollTextTo(int distance, bool redrawAfterScroll = true);
  80. void scrollTextBy(int distance);
  81. };
  82. /// a multi-line label that tries to fit text with given available width and height;
  83. /// if not possible, it creates a slider for scrolling text
  84. class CTextBox : public CIntObject
  85. {
  86. int sliderStyle;
  87. public:
  88. std::shared_ptr<CMultiLineLabel> label;
  89. std::shared_ptr<CSlider> slider;
  90. CTextBox(std::string Text, const Rect & rect, int SliderStyle, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::TOPLEFT, const SDL_Color & Color = Colors::WHITE);
  91. void resize(Point newSize);
  92. void setText(const std::string & Txt);
  93. void sliderMoved(int to);
  94. };
  95. /// Status bar which is shown at the bottom of the in-game screens
  96. class CGStatusBar : public CLabel, public std::enable_shared_from_this<CGStatusBar>, public IStatusBar
  97. {
  98. std::string hoverText;
  99. std::string consoleText;
  100. bool enteringText;
  101. void init();
  102. CGStatusBar(std::shared_ptr<CPicture> background_, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::CENTER, const SDL_Color & Color = Colors::WHITE);
  103. CGStatusBar(int x, int y, std::string name, int maxw = -1);
  104. //make CLabel API private
  105. using CLabel::getText;
  106. using CLabel::setAutoRedraw;
  107. using CLabel::setText;
  108. using CLabel::setColor;
  109. using CLabel::getWidth;
  110. protected:
  111. Point getBorderSize() override;
  112. void clickLeft(tribool down, bool previousState) override;
  113. private:
  114. std::function<void()> onClick;
  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, SDL_Surface * srf = nullptr);
  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. };