TextControls.h 6.0 KB

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