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