InputSourceText.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * InputSourceText.cpp, 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. #include "StdInc.h"
  11. #include "InputSourceText.h"
  12. #include "../CMT.h"
  13. #include "../gui/CGuiHandler.h"
  14. #include "../gui/EventDispatcher.h"
  15. #include "../render/IScreenHandler.h"
  16. #include "../renderSDL/SDL_Extensions.h"
  17. #include "../../lib/Rect.h"
  18. #include <SDL_events.h>
  19. InputSourceText::InputSourceText()
  20. {
  21. // For whatever reason, in SDL text input is considered to be active by default at least on desktop platforms
  22. // Apparently fixed in SDL3, but until then we need a workaround
  23. SDL_StopTextInput();
  24. }
  25. void InputSourceText::handleEventTextInput(const SDL_TextInputEvent & text)
  26. {
  27. GH.events().dispatchTextInput(text.text);
  28. }
  29. void InputSourceText::handleEventTextEditing(const SDL_TextEditingEvent & text)
  30. {
  31. GH.events().dispatchTextEditing(text.text);
  32. }
  33. void InputSourceText::startTextInput(const Rect & whereInput)
  34. {
  35. GH.dispatchMainThread([whereInput]()
  36. {
  37. Rect rectInScreenCoordinates = GH.screenHandler().convertLogicalPointsToWindow(whereInput);
  38. SDL_Rect textInputRect = CSDL_Ext::toSDL(rectInScreenCoordinates);
  39. SDL_SetTextInputRect(&textInputRect);
  40. if (SDL_IsTextInputActive() == SDL_FALSE)
  41. {
  42. SDL_StartTextInput();
  43. }
  44. });
  45. }
  46. void InputSourceText::stopTextInput()
  47. {
  48. GH.dispatchMainThread([]()
  49. {
  50. if (SDL_IsTextInputActive() == SDL_TRUE)
  51. {
  52. SDL_StopTextInput();
  53. }
  54. });
  55. }