TextControls.h 7.3 KB

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