fixing fingers tracking on Android

This commit is contained in:
2017-04-24 08:06:20 +01:00
parent ea1bff1f10
commit d558bc1e04

View File

@@ -377,6 +377,19 @@ static int32_t engine_handle_input(struct android_app* app, AInputEvent* event)
>> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
int pointer_id = AMotionEvent_getPointerId(event, index);
int32_t count = AMotionEvent_getPointerCount(event);
auto findPointer = [](int id, AInputEvent* event)
{
int32_t count = AMotionEvent_getPointerCount(event);
int ret = -1;
for (int i = 0; i < count; i++)
{
LOG("pointer %d id %d == %d", i, id, AMotionEvent_getPointerId(event, i));
if (AMotionEvent_getPointerId(event, i) == id)
ret = i;
}
return ret;
return -1;
};
struct Pointer
{
int id = -1;
@@ -385,22 +398,26 @@ static int32_t engine_handle_input(struct android_app* app, AInputEvent* event)
};
static Pointer p0, p1;
static int tracked = 0;
float x = AMotionEvent_getX(event, index);
float y = AMotionEvent_getY(event, index);
//LOG("event source: %d", AInputEvent_getSource(event));
LOG("pointer id %d count %d", pointer_id, count);
MouseEvent e;
switch (action) {
case AMOTION_EVENT_ACTION_DOWN:
{
p0.id = AMotionEvent_getPointerId(event, 0);
p0.pos = {x, y};
p0.idx = index;
App::I.mouse_down(0, x, y);
tracked = 1;
LOG("first down");
return 1;
}
case AMOTION_EVENT_ACTION_POINTER_DOWN:
{
LOG("pointer down index %d", index);
if (count == 2)
{
p1.id = AMotionEvent_getPointerId(event, index);
p1.id = AMotionEvent_getPointerId(event, 1);
p1.idx = index;
p1.pos = {x, y};
tracked = 2;
@@ -409,15 +426,18 @@ static int32_t engine_handle_input(struct android_app* app, AInputEvent* event)
App::I.gesture_start(p0.pos, p1.pos);
}
return 1;
}
case AMOTION_EVENT_ACTION_UP:
{
tracked = 0;
p0.id = -1;
p1.id = -1;
App::I.mouse_up(0, x, y);
LOG("first up");
return 1;
}
case AMOTION_EVENT_ACTION_POINTER_UP:
if (p1.id == AMotionEvent_getPointerId(event, index))
if (p1.id == AMotionEvent_getPointerId(event, 1))
{
p1.id = -1;
LOG("second up");
@@ -425,22 +445,31 @@ static int32_t engine_handle_input(struct android_app* app, AInputEvent* event)
}
return 1;
case AMOTION_EVENT_ACTION_HOVER_MOVE: // pen move before touching
App::I.mouse_move(x, y);
LOG("single move");
return 1;
case AMOTION_EVENT_ACTION_MOVE:
if (tracked == 1)
if (count == 1)
{
App::I.mouse_move(x, y);
LOG("single move");
}
else
else if (count == 2)
{
if (p0.id == pointer_id)
int idx = findPointer(pointer_id, event);
LOG("pointer move index %d", idx);
if (p0.idx == idx)
{
LOG("first move");
float y = AMotionEvent_getY(event, 0);
float x = AMotionEvent_getX(event, 0);
p0.pos = {x, y};
}
if (p1.id == pointer_id)
if (p1.idx == idx)
{
LOG("second move");
float x = AMotionEvent_getX(event, 1);
float y = AMotionEvent_getY(event, 1);
p1.pos = {x, y};
}
App::I.gesture_move(p0.pos, p1.pos);