I'm trying to implement a virtual DPad in android, but I'm still very confused about how do I keep track of each trouch point, from the moment it is pressed, till it is released.
In my code, I test if there is a "DOWN" or "MOVED" event, then if one of this events collides with one of the 4 rectangles of my DPad, I set the pressedDirectin variable to the side of the DPad which the user pressed.
The thing I'm not sure, is how do I get track of the touch points, I want to store the ID of the touch point which collided with the DPad direction, so when this ID is moved I update the direction of the DPad and when it gets released I set the Direction to None.
I don't want my DPad to handle multiple directions, but I still didn't get how to handle the multiple touch points, so I can know exacly which touch point is touching the DPad and store it's ID so I know when it is released.
Is the onTouchEvent called for every touch point, or all touch points comes in a single call? I still don't get it.
Here is the code I wrote so far, it works, but guess that it will only handle one touch point.
public boolean onTouchEvent(MotionEvent event)
{
if (event.getPointerCount() == 0)
return true;
int touchX = -1;
int touchY = -1;
pressedDirection = DPadDirection.None;
int actionCode = event.getAction() & MotionEvent.ACTION_MASK;
if (actionCode == MotionEvent.ACTION_UP)
{
if (event.getPointerId(0) == idDPad)
{
pressedDirection = DPadDirection.None;
idDPad = -1;
}
}
else if (actionCode == MotionEvent.ACTION_DOWN || actionCode == MotionEvent.ACTION_MOVE)
{
touchX = (int)event.getX();
touchY = (int)event.getY();
if (rightRect.contains(touchX, touchY))
pressedDirection = DPadDirection.Right;
else if (leftRect.contains(touchX, touchY))
pressedDirection = DPadDirection.Left;
else if (upRect.contains(touchX, touchY))
pressedDirection = DPadDirection.Up;
else if (downRect.contains(touchX, touchY))
pressedDirection = DPadDirection.Down;
if (pressedDirection != DPadDirection.None)
idDPad = event.getPointerId(0);
}
return true;
}