Browse Source

mouseMoveEvent works now

(AMOTION_EVENT_ACTION_MOVE)
pull/527/head
nodoteve 5 years ago
parent
commit
c48ad328ae
  1. 80
      src/Magnum/Platform/AndroidApplication.cpp
  2. 48
      src/Magnum/Platform/AndroidApplication.h

80
src/Magnum/Platform/AndroidApplication.cpp

@ -248,34 +248,7 @@ std::int32_t AndroidApplication::inputEvent(android_app* state, AInputEvent* eve
AndroidApplication& app = *static_cast<Data*>(state->userData)->instance; AndroidApplication& app = *static_cast<Data*>(state->userData)->instance;
if(AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) { if(AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {
const std::int32_t action = AMotionEvent_getAction(event) & AMOTION_EVENT_ACTION_MASK; const std::int32_t action = AMotionEvent_getAction(event) & AMOTION_EVENT_ACTION_MASK;
switch(action) {
case AMOTION_EVENT_ACTION_DOWN:
case AMOTION_EVENT_ACTION_UP: {
/* On a touch screen move events aren't reported when the
finger is moving above (of course), so remember the position
always */
app._previousMouseMovePosition[0] = {Int(AMotionEvent_getX(event, 0)), Int(AMotionEvent_getY(event, 0))};
MouseEvent e(event);
action == AMOTION_EVENT_ACTION_DOWN ? app.mousePressEvent(e) : app.mouseReleaseEvent(e);
return e.isAccepted() ? 1 : 0;
}
case AMOTION_EVENT_ACTION_MOVE: {
Vector2i position{Int(AMotionEvent_getX(event, 0)), Int(AMotionEvent_getY(event, 0))};
MouseMoveEvent e{event,
app._previousMouseMovePosition[0] == Vector2i{-1} ? Vector2i{} :
position - app._previousMouseMovePosition[0]};
app._previousMouseMovePosition[0] = position;
app.mouseMoveEvent(e);
return e.isAccepted() ? 1 : 0;
}
/* Look here:
https://android-developers.googleblog.com/2010/06/making-sense-of-multitouch.html
for ACTION_POINTER_UP and ACTION_POINTER_DOWN */
case AMOTION_EVENT_ACTION_POINTER_DOWN:
case AMOTION_EVENT_ACTION_POINTER_UP: {
// Extract the index of the pointer that left the touch sensor // Extract the index of the pointer that left the touch sensor
// ! Don't mix up AMotionEvent_getAction(event) and 'action' ! // ! Don't mix up AMotionEvent_getAction(event) and 'action' !
@ -291,14 +264,59 @@ std::int32_t AndroidApplication::inputEvent(android_app* state, AInputEvent* eve
// (for what if we have a pointerIndex? // (for what if we have a pointerIndex?
// They are a bit different. // They are a bit different.
// Pointer id saves the order of touch events, // Pointer id saves the order of touch events,
// so if you would _release_ them in various orders, // so if you would _release_ fingers in various orders,
// the 'pointerId' will have the value of initial 'pointerIndex', which might be useful. // the 'pointerId' will have the value of initial 'pointerIndex', which might be useful.
// Btw, somehow 'pointerId' does not tell you the last released touch initial index) // Btw, somehow 'pointerId' does not tell you the last released touch initial index ---
// --- no, 'pointerId' actually tells it, but in AMOTION_EVENT_ACTION_UP|DOWN case)
std::int32_t pointerId = AMotionEvent_getPointerId(event, pointerIndex); std::int32_t pointerId = AMotionEvent_getPointerId(event, pointerIndex);
// I suppose pointerId less or eq to pointerIndex max val // I almost sure pointerId less or eq to pointerIndex max val
//MouseMoveEvent does not support id for now, but it probably should (see AMOTION_EVENT_ACTION_MOVE)
switch(action) {
case AMOTION_EVENT_ACTION_DOWN:
case AMOTION_EVENT_ACTION_UP: {
/* On a touch screen move events aren't reported when the
finger is moving above (of course), so remember the position
always */
MouseEvent e(event, pointerIndex, pointerId, pointerCount);
if(action == AMOTION_EVENT_ACTION_DOWN){
app._previousMouseMovePosition[pointerId] = {Int(AMotionEvent_getX(event, pointerIndex)),
Int(AMotionEvent_getY(event, pointerIndex))};
app.mousePressEvent(e);
}
else{
app._previousMouseMovePosition[pointerId] = Vector2i{-1};
app.mouseReleaseEvent(e);
}
return e.isAccepted() ? 1 : 0;
}
case AMOTION_EVENT_ACTION_MOVE: {
for(size_t k=0;k<pointerCount;++k){
const std::size_t pointerIndex=k;
std::int32_t pointerId = AMotionEvent_getPointerId(event, pointerIndex);
// position is used twice: inside MouseMoveEvent.position() and here
Vector2i position{Int(AMotionEvent_getX(event, pointerIndex)),
Int(AMotionEvent_getY(event, pointerIndex))};
MouseMoveEvent e{event,
app._previousMouseMovePosition[pointerId].x() == -1? Vector2i{} :
position - app._previousMouseMovePosition[pointerId],
pointerIndex, pointerId, pointerCount};
app._previousMouseMovePosition[pointerId] = position;
app.mouseMoveEvent(e);
}
// return e.isAccepted() ? 1 : 0;
return 0;
}
/* Look here:
https://android-developers.googleblog.com/2010/06/making-sense-of-multitouch.html
for ACTION_POINTER_UP and ACTION_POINTER_DOWN */
case AMOTION_EVENT_ACTION_POINTER_DOWN:
case AMOTION_EVENT_ACTION_POINTER_UP: {
if(pointerId >= arraySize(app._previousMouseMovePosition)) if(pointerId >= arraySize(app._previousMouseMovePosition))
Containers::arrayAppend(app._previousMouseMovePosition, Containers::arrayAppend(app._previousMouseMovePosition,
{Int(AMotionEvent_getX(event, pointerIndex)), {Int(AMotionEvent_getX(event, pointerIndex)),

48
src/Magnum/Platform/AndroidApplication.h

@ -814,28 +814,28 @@ class AndroidApplication::MouseEvent: public InputEvent {
Int(AMotionEvent_getY(_event, _pointerIndex))}; Int(AMotionEvent_getY(_event, _pointerIndex))};
} }
/** @brief Pointer Index Do we need both of them? /** @brief Pointer Index
* Do we really need brief descriptions for index, id, count? * note: generally Index != Id
* Index != Id
*/ */
std::size_t pointerIndex() const { return _pointerIndex; } std::size_t pointerIndex() const { return _pointerIndex; }
/** @brief Pointer Id /** @brief Pointer Id
* Index != Id * note: generally Index != Id
*/ */
std::size_t pointerId() const { return _pointerId; } std::size_t pointerId() const { return _pointerId; }
/** @brief Pointer count */ /** @brief Number of pointers */
std::size_t pointerCount() const { return _pointerCount; } std::size_t pointerCount() const { return _pointerCount; }
private: private:
// Did it almost like MouseMoveEvent with _relativePosition // Did it almost like MouseMoveEvent with _relativePosition
explicit MouseEvent(AInputEvent* event, std::size_t pointerIndex = 0, std::int32_t pointerId = 0, std::size_t pointerCount = 1): explicit MouseEvent(AInputEvent* event,
/* why not {}, but () instead? */ InputEvent(event), std::size_t pointerIndex = 0, std::int32_t pointerId = 0, std::size_t pointerCount = 1):
_pointerId{pointerId},_pointerIndex{pointerIndex},_pointerCount{pointerCount} {} InputEvent(event),
_pointerIndex{pointerIndex}, _pointerId{pointerId}, _pointerCount{pointerCount} {}
const std::int32_t _pointerId;
const std::size_t _pointerIndex; const std::size_t _pointerIndex;
const std::int32_t _pointerId;
const std::size_t _pointerCount; const std::size_t _pointerCount;
}; };
@ -899,8 +899,12 @@ class AndroidApplication::MouseMoveEvent: public InputEvent {
/** @brief Position */ /** @brief Position */
Vector2i position() const { Vector2i position() const {
return {Int(AMotionEvent_getX(_event, 0)), // aw, that was painful
Int(AMotionEvent_getY(_event, 0))}; // return {Int(AMotionEvent_getX(_event, 0)),
// Int(AMotionEvent_getY(_event, 0))};
return {Int(AMotionEvent_getX(_event, _pointerIndex)),
Int(AMotionEvent_getY(_event, _pointerIndex))};
} }
/** /**
@ -923,10 +927,30 @@ class AndroidApplication::MouseMoveEvent: public InputEvent {
#endif #endif
} }
/** @brief Pointer Index
* note: generally Index != Id
*/
std::size_t pointerIndex() const { return _pointerIndex; }
/** @brief Pointer Id
* note: generally Index != Id
*/
std::size_t pointerId() const { return _pointerId; }
/** @brief Number of pointers */
std::size_t pointerCount() const { return _pointerCount; }
private: private:
explicit MouseMoveEvent(AInputEvent* event, Vector2i relativePosition): InputEvent{event}, _relativePosition{relativePosition} {} explicit MouseMoveEvent(AInputEvent* event, Vector2i relativePosition,
std::size_t pointerIndex = 0, std::int32_t pointerId = 0, std::size_t pointerCount = 1):
InputEvent{event}, _relativePosition{relativePosition},
_pointerIndex{pointerIndex}, _pointerId{pointerId}, _pointerCount{pointerCount} {}
const Vector2i _relativePosition; const Vector2i _relativePosition;
const std::size_t _pointerIndex;
const std::int32_t _pointerId;
const std::size_t _pointerCount;
}; };
CORRADE_ENUMSET_OPERATORS(AndroidApplication::MouseMoveEvent::Buttons) CORRADE_ENUMSET_OPERATORS(AndroidApplication::MouseMoveEvent::Buttons)

Loading…
Cancel
Save