TextControls.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. /// Resizes text box to minimal size needed to fit current text
  100. /// No effect if text is too large to fit and requires slider
  101. void trimToFit();
  102. void setText(const std::string & Txt);
  103. void sliderMoved(int to);
  104. };
  105. /// Status bar which is shown at the bottom of the in-game screens
  106. class CGStatusBar : public CLabel, public std::enable_shared_from_this<CGStatusBar>, public IStatusBar
  107. {
  108. std::string hoverText;
  109. std::string consoleText;
  110. bool enteringText;
  111. CGStatusBar(std::shared_ptr<CIntObject> background_, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::CENTER, const ColorRGBA & Color = Colors::WHITE);
  112. CGStatusBar(int x, int y, const ImagePath & name, int maxw = -1);
  113. CGStatusBar(int x, int y);
  114. //make CLabel API private
  115. using CLabel::getText;
  116. using CLabel::setAutoRedraw;
  117. using CLabel::setText;
  118. using CLabel::setColor;
  119. using CLabel::getWidth;
  120. protected:
  121. Point getBorderSize() override;
  122. void clickPressed(const Point & cursorPosition) override;
  123. public:
  124. ~CGStatusBar();
  125. template<typename ...Args>
  126. static std::shared_ptr<CGStatusBar> create(Args... args)
  127. {
  128. std::shared_ptr<CGStatusBar> ret{new CGStatusBar{args...}};
  129. return ret;
  130. }
  131. void show(Canvas & to) override;
  132. void activate() override;
  133. void deactivate() override;
  134. // IStatusBar interface
  135. void write(const std::string & Text) override;
  136. void clearIfMatching(const std::string & Text) override;
  137. void clear() override;
  138. void setEnteringMode(bool on) override;
  139. void setEnteredText(const std::string & text) override;
  140. };
  141. /// UIElement which can get input focus
  142. class CFocusable : public virtual CIntObject
  143. {
  144. static std::atomic<int> usageIndex;
  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. void removeFocus(); //remove focus
  150. bool hasFocus() const;
  151. void focusGot();
  152. void focusLost();
  153. static std::list<CFocusable *> focusables; //all existing objs
  154. static CFocusable * inputWithFocus; //who has focus now
  155. CFocusable();
  156. ~CFocusable();
  157. };
  158. /// Text input box where players can enter text
  159. class CTextInput : public CLabel, public CFocusable
  160. {
  161. std::string newText;
  162. std::string helpBox; //for right-click help
  163. protected:
  164. std::string visibleText() override;
  165. public:
  166. CFunctionList<void(const std::string &)> cb;
  167. CFunctionList<void(std::string &, const std::string &)> filters;
  168. void setText(const std::string & nText) override;
  169. void setText(const std::string & nText, bool callCb);
  170. void setHelpText(const std::string &);
  171. CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB, ETextAlignment alignment, bool giveFocusToInput);
  172. CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName, const CFunctionList<void(const std::string &)> & CB);
  173. CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf);
  174. void clickPressed(const Point & cursorPosition) override;
  175. void keyPressed(EShortcut key) override;
  176. void showPopupWindow(const Point & cursorPosition) override;
  177. //bool captureThisKey(EShortcut key) override;
  178. void textInputed(const std::string & enteredText) override;
  179. void textEdited(const std::string & enteredText) override;
  180. //Filter that will block all characters not allowed in filenames
  181. static void filenameFilter(std::string & text, const std::string & oldText);
  182. //Filter that will allow only input of numbers in range min-max (min-max are allowed)
  183. //min-max should be set via something like std::bind
  184. static void numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue);
  185. friend class CKeyboardFocusListener;
  186. };