TextControls.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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(EAlignment alignment, EFonts font, SDL_Color color);
  25. public:
  26. EAlignment 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. CPicture * bg;
  37. public:
  38. std::string text;
  39. bool autoRedraw; //whether control will redraw itself on setTxt
  40. std::string getText();
  41. virtual void setText(const std::string & Txt);
  42. CLabel(int x = 0, int y = 0, EFonts Font = FONT_SMALL, EAlignment Align = TOPLEFT, const SDL_Color & Color = Colors::WHITE, const std::string & Text = "");
  43. void showAll(SDL_Surface * to) override; //shows statusbar (with current text)
  44. };
  45. /// Small helper class to manage group of similar labels
  46. class CLabelGroup : public CIntObject
  47. {
  48. std::list<CLabel *> labels;
  49. EFonts font;
  50. EAlignment align;
  51. SDL_Color color;
  52. public:
  53. CLabelGroup(EFonts Font = FONT_SMALL, EAlignment Align = TOPLEFT, const SDL_Color & Color = Colors::WHITE);
  54. void add(int x = 0, int y = 0, const std::string & text = "");
  55. };
  56. /// Multi-line label that can display multiple lines of text
  57. /// If text is too big to fit into requested area remaining part will not be visible
  58. class CMultiLineLabel : public CLabel
  59. {
  60. // text to blit, split into lines that are no longer than widget width
  61. std::vector<std::string> lines;
  62. // area of text that actually will be printed, default is widget size
  63. Rect visibleSize;
  64. void splitText(const std::string & Txt);
  65. Rect getTextLocation();
  66. public:
  67. // total size of text, x = longest line of text, y = total height of lines
  68. Point textSize;
  69. CMultiLineLabel(Rect position, EFonts Font = FONT_SMALL, EAlignment Align = TOPLEFT, const SDL_Color & Color = Colors::WHITE, const std::string & Text = "");
  70. void setText(const std::string & Txt) override;
  71. void showAll(SDL_Surface * to) override;
  72. void setVisibleSize(Rect visibleSize);
  73. // scrolls text visible in widget. Positive value will move text up
  74. void scrollTextTo(int distance);
  75. void scrollTextBy(int distance);
  76. };
  77. /// a multi-line label that tries to fit text with given available width and height;
  78. /// if not possible, it creates a slider for scrolling text
  79. class CTextBox : public CIntObject
  80. {
  81. int sliderStyle;
  82. public:
  83. CMultiLineLabel * label;
  84. CSlider * slider;
  85. CTextBox(std::string Text, const Rect & rect, int SliderStyle, EFonts Font = FONT_SMALL, EAlignment Align = TOPLEFT, const SDL_Color & Color = Colors::WHITE);
  86. void resize(Point newSize);
  87. void setText(const std::string & Txt);
  88. void sliderMoved(int to);
  89. };
  90. /// Status bar which is shown at the bottom of the in-game screens
  91. class CGStatusBar : public CLabel
  92. {
  93. bool textLock; //Used for blocking changes to the text
  94. void init();
  95. CGStatusBar * oldStatusBar;
  96. protected:
  97. Point getBorderSize() override;
  98. public:
  99. void clear(); //clears statusbar and refreshes
  100. void setText(const std::string & Text) override; //prints text and refreshes statusbar
  101. void show(SDL_Surface * to) override; //shows statusbar (with current text)
  102. CGStatusBar(CPicture * BG, EFonts Font = FONT_SMALL, EAlignment Align = CENTER, const SDL_Color & Color = Colors::WHITE); //given CPicture will be captured by created sbar and it's pos will be used as pos for sbar
  103. CGStatusBar(int x, int y, std::string name, int maxw = -1);
  104. ~CGStatusBar();
  105. void lock(bool shouldLock); //If true, current text cannot be changed until lock(false) is called
  106. };
  107. /// UIElement which can get input focus
  108. class CFocusable : public virtual CIntObject
  109. {
  110. protected:
  111. virtual void focusGot(){};
  112. virtual void focusLost(){};
  113. public:
  114. bool focus; //only one focusable control can have focus at one moment
  115. void giveFocus(); //captures focus
  116. void moveFocus(); //moves focus to next active control (may be used for tab switching)
  117. static std::list<CFocusable *> focusables; //all existing objs
  118. static CFocusable * inputWithFocus; //who has focus now
  119. CFocusable();
  120. ~CFocusable();
  121. };
  122. /// Text input box where players can enter text
  123. class CTextInput : public CLabel, public CFocusable
  124. {
  125. std::string newText;
  126. protected:
  127. std::string visibleText() override;
  128. void focusGot() override;
  129. void focusLost() override;
  130. public:
  131. CFunctionList<void(const std::string &)> cb;
  132. CFunctionList<void(std::string &, const std::string &)> filters;
  133. void setText(const std::string & nText, bool callCb = false);
  134. CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB);
  135. CTextInput(const Rect & Pos, const Point & bgOffset, const std::string & bgName, const CFunctionList<void(const std::string &)> & CB);
  136. CTextInput(const Rect & Pos, SDL_Surface * srf = nullptr);
  137. void clickLeft(tribool down, bool previousState) override;
  138. void keyPressed(const SDL_KeyboardEvent & key) override;
  139. bool captureThisEvent(const SDL_KeyboardEvent & key) override;
  140. void textInputed(const SDL_TextInputEvent & event) override;
  141. void textEdited(const SDL_TextEditingEvent & event) override;
  142. //Filter that will block all characters not allowed in filenames
  143. static void filenameFilter(std::string & text, const std::string & oldText);
  144. //Filter that will allow only input of numbers in range min-max (min-max are allowed)
  145. //min-max should be set via something like std::bind
  146. static void numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue);
  147. };