CTextInput.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 final : public CFocusable
  39. {
  40. using TextEditedCallback = std::function<void(const std::string &)>;
  41. using TextFilterCallback = std::function<void(std::string &, const std::string &)>;
  42. std::string currentText;
  43. std::string composedText;
  44. ETextAlignment originalAlignment;
  45. std::shared_ptr<CPicture> background;
  46. std::shared_ptr<CLabel> label;
  47. TextEditedCallback onTextEdited;
  48. TextFilterCallback onTextFiltering;
  49. CFunctionList<void()> callbackPopup;
  50. //Filter that will block all characters not allowed in filenames
  51. static void filenameFilter(std::string & text, const std::string & oldText);
  52. //Filter that will allow only input of numbers in range min-max (min-max are allowed)
  53. //min-max should be set via something like std::bind
  54. static void numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue);
  55. std::string getVisibleText() const;
  56. void createLabel(bool giveFocusToInput);
  57. void updateLabel();
  58. void clickPressed(const Point & cursorPosition) final;
  59. void textInputted(const std::string & enteredText) final;
  60. void textEdited(const std::string & enteredText) final;
  61. void onFocusGot() final;
  62. void onFocusLost() final;
  63. void showPopupWindow(const Point & cursorPosition) final;
  64. CTextInput(const Rect & Pos);
  65. public:
  66. CTextInput(const Rect & Pos, EFonts font, ETextAlignment alignment, bool giveFocusToInput);
  67. CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName);
  68. CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf);
  69. /// Returns currently entered text. May not match visible text
  70. const std::string & getText() const;
  71. void setText(const std::string & nText);
  72. /// Set callback that will be called whenever player enters new text
  73. void setCallback(const TextEditedCallback & cb);
  74. /// Set callback when player want to open popup
  75. void setPopupCallback(const std::function<void()> & cb);
  76. /// Enables filtering entered text that ensures that text is valid filename (existing or not)
  77. void setFilterFilename();
  78. /// Enable filtering entered text that ensures that text is valid number in provided range [min, max]
  79. void setFilterNumber(int minValue, int maxValue);
  80. void setFont(EFonts Font);
  81. void setColor(const ColorRGBA & Color);
  82. void setAlignment(ETextAlignment alignment);
  83. // CIntObject interface impl
  84. void keyPressed(EShortcut key) final;
  85. void activate() final;
  86. void deactivate() final;
  87. };