TextControls.h 7.4 KB

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