InputSourceText.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 "../../lib/Rect.h"
  16. #include <SDL_events.h>
  17. #include <SDL_render.h>
  18. #ifdef VCMI_APPLE
  19. # include <dispatch/dispatch.h>
  20. #endif
  21. #ifdef VCMI_IOS
  22. # include "ios/utils.h"
  23. #endif
  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. #ifdef VCMI_APPLE
  35. dispatch_async(dispatch_get_main_queue(), ^{
  36. #endif
  37. // TODO ios: looks like SDL bug actually, try fixing there
  38. auto renderer = SDL_GetRenderer(mainWindow);
  39. float scaleX, scaleY;
  40. SDL_Rect viewport;
  41. SDL_RenderGetScale(renderer, &scaleX, &scaleY);
  42. SDL_RenderGetViewport(renderer, &viewport);
  43. #ifdef VCMI_IOS
  44. const auto nativeScale = iOS_utils::screenScale();
  45. scaleX /= nativeScale;
  46. scaleY /= nativeScale;
  47. #endif
  48. SDL_Rect rectInScreenCoordinates;
  49. rectInScreenCoordinates.x = (viewport.x + whereInput.x) * scaleX;
  50. rectInScreenCoordinates.y = (viewport.y + whereInput.y) * scaleY;
  51. rectInScreenCoordinates.w = whereInput.w * scaleX;
  52. rectInScreenCoordinates.h = whereInput.h * scaleY;
  53. SDL_SetTextInputRect(&rectInScreenCoordinates);
  54. if (SDL_IsTextInputActive() == SDL_FALSE)
  55. {
  56. SDL_StartTextInput();
  57. }
  58. #ifdef VCMI_APPLE
  59. });
  60. #endif
  61. }
  62. void InputSourceText::stopTextInput()
  63. {
  64. #ifdef VCMI_APPLE
  65. dispatch_async(dispatch_get_main_queue(), ^{
  66. #endif
  67. if (SDL_IsTextInputActive() == SDL_TRUE)
  68. {
  69. SDL_StopTextInput();
  70. }
  71. #ifdef VCMI_APPLE
  72. });
  73. #endif
  74. }