CTextInput.h 3.2 KB

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