InputAndroid.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Input/Input.h>
  6. #include <AnKi/Core/NativeWindowAndroid.h>
  7. #include <AnKi/Util/Logger.h>
  8. #include <AnKi/Core/App.h>
  9. #if ANKI_OS_ANDROID
  10. # include <android_native_app_glue.h>
  11. #endif
  12. namespace anki
  13. {
  14. static void handleAndroidEvents(android_app* app, int32_t cmd)
  15. {
  16. Input* input = static_cast<Input*>(app->userData);
  17. ANKI_ASSERT(input != nullptr);
  18. switch(cmd)
  19. {
  20. case APP_CMD_TERM_WINDOW:
  21. case APP_CMD_LOST_FOCUS:
  22. input->addEvent(InputEvent::WINDOW_CLOSED);
  23. break;
  24. }
  25. }
  26. Error Input::handleEvents()
  27. {
  28. int ident;
  29. int events;
  30. android_poll_source* source;
  31. while((ident = ALooper_pollAll(0, nullptr, &events, reinterpret_cast<void**>(&source))) >= 0)
  32. {
  33. if(source != nullptr)
  34. {
  35. source->process(g_androidApp, source);
  36. }
  37. }
  38. return Error::NONE;
  39. }
  40. Error Input::initInternal(NativeWindow* window)
  41. {
  42. ANKI_ASSERT(window);
  43. g_androidApp->userData = this;
  44. g_androidApp->onAppCmd = handleAndroidEvents;
  45. m_nativeWindow = window;
  46. return Error::NONE;
  47. }
  48. void Input::destroy()
  49. {
  50. }
  51. void Input::moveCursor(const Vec2& posNdc)
  52. {
  53. m_mousePosNdc = posNdc;
  54. m_mousePosWin =
  55. UVec2((posNdc * 0.5f + 0.5f) * Vec2(F32(m_nativeWindow->getWidth()), F32(m_nativeWindow->getHeight())));
  56. }
  57. void Input::hideCursor(Bool hide)
  58. {
  59. // do nothing
  60. }
  61. } // end namespace anki