CTextInput.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. static std::atomic<int> usageIndex;
  21. static std::list<CFocusable *> focusables; //all existing objs
  22. static CFocusable * inputWithFocus; //who has focus now
  23. void focusGot();
  24. void focusLost();
  25. virtual void onFocusGot() = 0;
  26. virtual void onFocusLost() = 0;
  27. public:
  28. void giveFocus(); //captures focus
  29. void moveFocus(); //moves focus to next active control (may be used for tab switching)
  30. void removeFocus(); //remove focus
  31. bool hasFocus() const;
  32. CFocusable();
  33. ~CFocusable();
  34. };
  35. /// Text input box where players can enter text
  36. class CTextInput final : public CFocusable
  37. {
  38. using TextEditedCallback = std::function<void(const std::string &)>;
  39. using TextFilterCallback = std::function<void(std::string &, const std::string &)>;
  40. private:
  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();
  54. void createLabel(bool giveFocusToInput);
  55. void updateLabel();
  56. void clickPressed(const Point & cursorPosition) final;
  57. void textInputed(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, const TextEditedCallback& onTextEdited, 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. };