Lugdunum  0.1.0
WindowImplAndroid.cpp
Go to the documentation of this file.
2 #include <mutex>
3 
4 namespace lug {
5 namespace Window {
6 namespace priv {
7 
8 std::mutex WindowImpl::androidMutex;
9 std::condition_variable WindowImpl::cv;
10 std::queue<lug::Window::Event> WindowImpl::events;
11 AInputQueue* WindowImpl::inputQueue = nullptr;
12 ANativeWindow* WindowImpl::nativeWindow = nullptr;
13 ANativeActivity* WindowImpl::activity = nullptr;
14 
15 WindowImpl::WindowImpl(Window* win) : _parent{win} {}
16 
18  if (!WindowImpl::nativeWindow) {
19  std::unique_lock<std::mutex> lk(WindowImpl::androidMutex);
20  WindowImpl::cv.wait(lk);
21  }
22  _parent->_mode.width = ANativeWindow_getWidth(nativeWindow);
23  _parent->_mode.height = ANativeWindow_getHeight(nativeWindow);
24  return true;
25 }
26 
28 
29 ANativeWindow* WindowImpl::getWindow() {
30  return nativeWindow;
31 }
32 
33 // Credits to nvidia : https://github.com/NVIDIAGameWorks/GraphicsSamples/blob/master/extensions/src/NvGamepad/android/NvGamepadAndroid.cpp
34 float WindowImpl::MapCenteredAxis(AInputEvent* event, int32_t axis) {
35  const float deadZone = (8689.0f / 32768.0f); // 0,2651672363
36  float value = AMotionEvent_getAxisValue(event, axis, 0);
37  if (value > deadZone) {
38  return (value - deadZone) / (1.0f - deadZone);
39  } else if (value < -deadZone) {
40  return (value + deadZone) / (1.0f - deadZone);
41  } else {
42  return 0.0f;
43  }
44 }
45 
46 int32_t WindowImpl::HandleInput(lug::Window::Event& event, AInputEvent* androidEvent) {
47  event.touchScreen.drag = false;
48  event.touchScreen.pinch = false;
49  event.touchScreen.state = lug::Window::TouchScreenEvent::GestureState::None;
50 
51  if (AInputEvent_getType(androidEvent) == AINPUT_EVENT_TYPE_MOTION) {
52  event.type = Event::Type::TouchScreenChange;
53  ndk_helper::GESTURE_STATE doubleTapState = doubletap_detector_.Detect(androidEvent);
54  ndk_helper::GESTURE_STATE dragState = drag_detector_.Detect(androidEvent);
55  ndk_helper::GESTURE_STATE pinchState = pinch_detector_.Detect(androidEvent);
56 
57  // Double tap detector has a priority over other detectors
58  if (doubleTapState == ndk_helper::GESTURE_STATE_ACTION) {
59  event.touchScreen.doubleTap = !event.touchScreen.doubleTap;
60  event.touchScreen.state = static_cast<lug::Window::TouchScreenEvent::GestureState>(doubleTapState);
62  }
63  // Drag state
64  else if (dragState & ndk_helper::GESTURE_STATE_START ||
65  dragState & ndk_helper::GESTURE_STATE_MOVE ||
66  dragState & ndk_helper::GESTURE_STATE_END) {
67  event.touchScreen.drag = true;
68  event.touchScreen.state = static_cast<lug::Window::TouchScreenEvent::GestureState>(dragState);
70  }
71  // Pinch state
72  else if (pinchState & ndk_helper::GESTURE_STATE_START ||
73  pinchState & ndk_helper::GESTURE_STATE_MOVE ||
74  pinchState & ndk_helper::GESTURE_STATE_END) {
75  event.touchScreen.pinch = true;
76  event.touchScreen.state = static_cast<lug::Window::TouchScreenEvent::GestureState>(pinchState);
78  }
79 
80  return 1;
81  }
82 
83  return 0;
84 }
85 
86 
88 
89  if (inputQueue != nullptr) {
90  AInputEvent* androidEvent = nullptr;
91 
92  if (AInputQueue_getEvent(inputQueue, &androidEvent) >= 0) {
93  if (AInputQueue_preDispatchEvent(inputQueue, androidEvent)) {
94  return false;
95  }
96 
97  HandleInput(event, androidEvent);
98  LUG_LOG.info("WINDOWS ANDROID event coordinate {} {}", event.touchScreen.coordinates[0].x(), event.touchScreen.coordinates[0].y());
99  events.push(std::move(event));
100  AInputQueue_finishEvent(inputQueue, androidEvent, 1);
101  }
102  }
103 
104  if (!events.empty()) {
105  event = events.front();
106  events.pop();
107  return true;
108  }
109 
110  return false;
111 }
112 
113 void WindowImpl::setKeyRepeat(bool state) {
114  (void)state;
115  // TODO
116 }
117 
119  (void)visible;
120  // TODO
121 }
122 
123 void WindowImpl::setMousePos(const Math::Vec2i& mousePosition) {
124  (void)mousePosition;
125  // TODO
126 }
127 
128 } // namespace priv
129 } // namespace Window
130 } // namespace lug
Represents an event.
Definition: Event.hpp:89
int32_t HandleInput(lug::Window::Event &event, AInputEvent *androidEvent)
lug::Math::Vec2f coordinates[2]
The Touch coordinate.
Definition: Event.hpp:59
ndk_helper::DoubletapDetector doubletap_detector_
virtual GESTURE_STATE Detect(const AInputEvent *event)
bool init(const Window::InitInfo &initInfo)
static ANativeWindow * nativeWindow
Class for window.
Definition: Window.hpp:59
float MapCenteredAxis(AInputEvent *event, int32_t axis)
void setMousePos(const Math::Vec2i &mousePosition)
virtual GESTURE_STATE Detect(const AInputEvent *motion_event)
void setMouseCursorVisible(bool visible)
bool GetPointers(lug::Math::Vec2f &v1, lug::Math::Vec2f &v2)
virtual GESTURE_STATE Detect(const AInputEvent *event)
ndk_helper::DragDetector drag_detector_
int32_t GESTURE_STATE
static std::queue< lug::Window::Event > events
static ANativeActivity * activity
bool pollEvent(lug::Window::Event &event)
TouchScreenEvent touchScreen
Definition: Event.hpp:120
ndk_helper::PinchDetector pinch_detector_
#define LUG_LOG
Definition: Logger.hpp:73
bool GetPointer(lug::Math::Vec2f &v)
VideoMode _mode
Definition: Window.hpp:185
static std::condition_variable cv