InputAndroid.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright (C) 2009-2023, 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/Window/InputAndroid.h>
  6. #include <AnKi/Window/NativeWindowAndroid.h>
  7. #include <AnKi/Util/Logger.h>
  8. namespace anki {
  9. template<>
  10. template<>
  11. Input& MakeSingletonPtr<Input>::allocateSingleton<>()
  12. {
  13. ANKI_ASSERT(m_global == nullptr);
  14. m_global = new InputAndroid;
  15. #if ANKI_ASSERTIONS_ENABLED
  16. ++g_singletonsAllocated;
  17. #endif
  18. return *m_global;
  19. }
  20. template<>
  21. void MakeSingletonPtr<Input>::freeSingleton()
  22. {
  23. if(m_global)
  24. {
  25. delete static_cast<InputAndroid*>(m_global);
  26. m_global = nullptr;
  27. #if ANKI_ASSERTIONS_ENABLED
  28. --g_singletonsAllocated;
  29. #endif
  30. }
  31. }
  32. Error Input::init()
  33. {
  34. return static_cast<InputAndroid*>(this)->initInternal();
  35. }
  36. Error Input::handleEvents()
  37. {
  38. for(U32& k : m_touchPointers)
  39. {
  40. if(k)
  41. {
  42. ++k;
  43. }
  44. }
  45. int ident;
  46. int events;
  47. android_poll_source* source;
  48. while((ident = ALooper_pollAll(0, nullptr, &events, reinterpret_cast<void**>(&source))) >= 0)
  49. {
  50. if(source != nullptr)
  51. {
  52. source->process(g_androidApp, source);
  53. }
  54. }
  55. return Error::kNone;
  56. }
  57. void Input::moveCursor(const Vec2& posNdc)
  58. {
  59. m_mousePosNdc = posNdc;
  60. m_mousePosWin = UVec2((posNdc * 0.5f + 0.5f) * Vec2(F32(NativeWindow::getSingleton().getWidth()), F32(NativeWindow::getSingleton().getHeight())));
  61. }
  62. void Input::hideCursor([[maybe_unused]] Bool hide)
  63. {
  64. // do nothing
  65. }
  66. Bool Input::hasTouchDevice() const
  67. {
  68. return true;
  69. }
  70. Error InputAndroid::initInternal()
  71. {
  72. g_androidApp->userData = this;
  73. g_androidApp->onAppCmd = [](android_app* app, int32_t cmd) {
  74. InputAndroid* self = static_cast<InputAndroid*>(app->userData);
  75. self->handleAndroidEvents(app, cmd);
  76. };
  77. g_androidApp->onInputEvent = [](android_app* app, AInputEvent* event) -> int {
  78. InputAndroid* self = static_cast<InputAndroid*>(app->userData);
  79. return self->handleAndroidInput(app, event);
  80. };
  81. return Error::kNone;
  82. }
  83. void InputAndroid::handleAndroidEvents([[maybe_unused]] android_app* app, int32_t cmd)
  84. {
  85. switch(cmd)
  86. {
  87. case APP_CMD_TERM_WINDOW:
  88. case APP_CMD_LOST_FOCUS:
  89. addEvent(InputEvent::kWindowClosed);
  90. break;
  91. }
  92. }
  93. int InputAndroid::handleAndroidInput([[maybe_unused]] android_app* app, AInputEvent* event)
  94. {
  95. const I32 type = AInputEvent_getType(event);
  96. const I32 source = AInputEvent_getSource(event);
  97. I32 handled = 0;
  98. switch(type)
  99. {
  100. case AINPUT_EVENT_TYPE_KEY:
  101. // TODO
  102. break;
  103. case AINPUT_EVENT_TYPE_MOTION:
  104. {
  105. const I32 pointer = AMotionEvent_getAction(event);
  106. const I32 action = pointer & AMOTION_EVENT_ACTION_MASK;
  107. const I32 index = (pointer & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
  108. if(source & AINPUT_SOURCE_JOYSTICK)
  109. {
  110. // TODO
  111. }
  112. else if(source & AINPUT_SOURCE_TOUCHSCREEN)
  113. {
  114. auto update = [event, this](U32 index, U32 pressValue) {
  115. const F32 x = AMotionEvent_getX(event, index);
  116. const F32 y = AMotionEvent_getY(event, index);
  117. const I32 id = AMotionEvent_getPointerId(event, index);
  118. m_touchPointerPosWin[id] = UVec2(U32(x), U32(y));
  119. m_touchPointerPosNdc[id].x() = F32(x) / F32(NativeWindow::getSingleton().getWidth()) * 2.0f - 1.0f;
  120. m_touchPointerPosNdc[id].y() = -(F32(y) / F32(NativeWindow::getSingleton().getHeight()) * 2.0f - 1.0f);
  121. if(pressValue == 0 || pressValue == 1)
  122. {
  123. m_touchPointers[id] = pressValue;
  124. }
  125. };
  126. switch(action)
  127. {
  128. case AMOTION_EVENT_ACTION_DOWN:
  129. case AMOTION_EVENT_ACTION_POINTER_DOWN:
  130. update(index, 1);
  131. break;
  132. case AMOTION_EVENT_ACTION_MOVE:
  133. {
  134. const U32 count = U32(AMotionEvent_getPointerCount(event));
  135. for(U32 i = 0; i < count; i++)
  136. {
  137. update(i, 2);
  138. }
  139. break;
  140. }
  141. case AMOTION_EVENT_ACTION_UP:
  142. case AMOTION_EVENT_ACTION_POINTER_UP:
  143. update(index, 0);
  144. break;
  145. default:
  146. break;
  147. }
  148. }
  149. break;
  150. }
  151. default:
  152. break;
  153. }
  154. return handled;
  155. }
  156. } // end namespace anki