TextControls.h 7.4 KB

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