CTextInput.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * CTextInput.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/EFont.h"
  14. #include "../../lib/filesystem/ResourcePath.h"
  15. #include "../../lib/FunctionList.h"
  16. class CLabel;
  17. class IImage;
  18. /// UIElement which can get input focus
  19. class CFocusable : public CIntObject
  20. {
  21. friend void removeFocusFromActiveInput();
  22. static std::atomic<int> usageIndex;
  23. static std::list<CFocusable *> focusables; //all existing objs
  24. static CFocusable * inputWithFocus; //who has focus now
  25. void focusGot();
  26. void focusLost();
  27. virtual void onFocusGot() = 0;
  28. virtual void onFocusLost() = 0;
  29. public:
  30. void giveFocus(); //captures focus
  31. void moveFocus(); //moves focus to next active control (may be used for tab switching)
  32. void removeFocus(); //remove focus
  33. bool hasFocus() const;
  34. CFocusable();
  35. ~CFocusable();
  36. };
  37. /// Text input box where players can enter text
  38. class CTextInput : public CFocusable
  39. {
  40. protected:
  41. using TextEditedCallback = std::function<void(const std::string &)>;
  42. using TextFilterCallback = std::function<void(std::string &, const std::string &)>;
  43. std::string currentText;
  44. std::string composedText;
  45. ETextAlignment originalAlignment;
  46. std::shared_ptr<CPicture> background;
  47. std::shared_ptr<CLabel> label;
  48. TextEditedCallback onTextEdited;
  49. TextFilterCallback onTextFiltering;
  50. CFunctionList<void()> callbackPopup;
  51. //Filter that will block all characters not allowed in filenames
  52. static void filenameFilter(std::string & text, const std::string & oldText);
  53. //Filter that will allow only input of numbers in range min-max (min-max are allowed)
  54. //min-max should be set via something like std::bind
  55. static void numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue);
  56. std::string getVisibleText() const;
  57. void createLabel(bool giveFocusToInput);
  58. void updateLabel();
  59. void clickPressed(const Point & cursorPosition) override;
  60. void textInputted(const std::string & enteredText) override;
  61. void textEdited(const std::string & enteredText) override;
  62. void onFocusGot() override;
  63. void onFocusLost() override;
  64. void showPopupWindow(const Point & cursorPosition) override;
  65. CTextInput(const Rect & Pos);
  66. public:
  67. CTextInput(const Rect & Pos, EFonts font, ETextAlignment alignment, bool giveFocusToInput);
  68. CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName);
  69. CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf);
  70. /// Returns currently entered text. May not match visible text
  71. const std::string & getText() const;
  72. void setText(const std::string & nText);
  73. /// Set callback that will be called whenever player enters new text
  74. void setCallback(const TextEditedCallback & cb);
  75. /// Set callback when player want to open popup
  76. void setPopupCallback(const std::function<void()> & cb);
  77. /// Enables filtering entered text that ensures that text is valid filename (existing or not)
  78. void setFilterFilename();
  79. /// Enable filtering entered text that ensures that text is valid number in provided range [min, max]
  80. void setFilterNumber(int minValue, int maxValue);
  81. void setFont(EFonts Font);
  82. void setColor(const ColorRGBA & Color);
  83. void setAlignment(ETextAlignment alignment);
  84. // CIntObject interface impl
  85. void keyPressed(EShortcut key) override;
  86. void activate() override;
  87. void deactivate() override;
  88. };
  89. class CTextInputWithConfirm final : public CTextInput
  90. {
  91. std::string initialText;
  92. std::function<void()> confirmCb;
  93. bool limitToRect;
  94. void confirm();
  95. public:
  96. CTextInputWithConfirm(const Rect & Pos, EFonts font, ETextAlignment alignment, std::string text, bool limitToRect, std::function<void()> confirmCallback);
  97. bool captureThisKey(EShortcut key) override;
  98. void keyPressed(EShortcut key) override;
  99. void clickReleased(const Point & cursorPosition) override;
  100. void clickPressed(const Point & cursorPosition) override;
  101. bool receiveEvent(const Point & position, int eventType) const override;
  102. void onFocusGot() override;
  103. void textInputted(const std::string & enteredText) override;
  104. };