InputSourceText.cpp 1.5 KB

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