mirror of
https://github.com/Julow/Unexpected-Keyboard.git
synced 2024-11-25 08:43:24 +01:00
Handle multi touch event
This commit is contained in:
parent
8265c1c099
commit
702ae9d0c1
@ -95,48 +95,71 @@ public class Keyboard2View extends View
|
|||||||
float x;
|
float x;
|
||||||
float y;
|
float y;
|
||||||
float keyW;
|
float keyW;
|
||||||
boolean key_down;
|
|
||||||
int pointer_up;
|
|
||||||
|
|
||||||
if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_POINTER_UP)
|
switch (event.getActionMasked())
|
||||||
pointer_up = event.getActionIndex();
|
{
|
||||||
else
|
case MotionEvent.ACTION_UP:
|
||||||
pointer_up = -1;
|
case MotionEvent.ACTION_POINTER_UP:
|
||||||
y = _keyMargin;
|
onTouchUp(event.getPointerId(event.getActionIndex()));
|
||||||
|
break ;
|
||||||
|
case MotionEvent.ACTION_DOWN:
|
||||||
|
case MotionEvent.ACTION_POINTER_DOWN:
|
||||||
|
int p = event.getActionIndex();
|
||||||
|
onTouchDown(event.getX(p), event.getY(p), event.getPointerId(p));
|
||||||
|
break ;
|
||||||
|
default:
|
||||||
|
return (false);
|
||||||
|
}
|
||||||
|
return (true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onTouchDown(float touch_x, float touch_y, int pointerId)
|
||||||
|
{
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
float keyW;
|
||||||
|
|
||||||
|
y = -_keyHeight;
|
||||||
for (Row row : _rows)
|
for (Row row : _rows)
|
||||||
{
|
{
|
||||||
|
y += _keyHeight + _keyMargin;
|
||||||
|
if (touch_y < y || touch_y >= (y + _keyHeight))
|
||||||
|
continue ;
|
||||||
x = (KEY_PER_ROW * (_keyMargin + _keyWidth) - _keyMargin - row.getWidth()) / 2 + _keyMargin;
|
x = (KEY_PER_ROW * (_keyMargin + _keyWidth) - _keyMargin - row.getWidth()) / 2 + _keyMargin;
|
||||||
for (Key k : row)
|
for (Key k : row)
|
||||||
{
|
{
|
||||||
key_down = k.down;
|
|
||||||
keyW = _keyWidth * k.width;
|
keyW = _keyWidth * k.width;
|
||||||
for (int p = 0; p < event.getPointerCount(); p++)
|
if (touch_x >= x && touch_x < (x + keyW))
|
||||||
{
|
{
|
||||||
if (pointer_up != p && event.getX(p) >= x && event.getX(p) < (x + keyW)
|
if (k.down_pointer == -1)
|
||||||
&& event.getY(p) >= y && event.getY(p) < (y + _keyHeight))
|
|
||||||
{
|
|
||||||
if (!k.down && !key_down)
|
|
||||||
{
|
{
|
||||||
if (k.key0 != null)
|
if (k.key0 != null)
|
||||||
Keyboard2.log("Key down " + k.key0.getName());
|
Keyboard2.log("Key down " + k.key0.getName());
|
||||||
key_down = true;
|
k.down_pointer = pointerId;
|
||||||
invalidate();
|
invalidate();
|
||||||
|
return ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (k.down && key_down)
|
x += keyW + _keyMargin;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onTouchUp(int pointerId)
|
||||||
|
{
|
||||||
|
for (Row row : _rows)
|
||||||
|
{
|
||||||
|
for (Key k : row)
|
||||||
|
{
|
||||||
|
if (k.down_pointer == pointerId)
|
||||||
{
|
{
|
||||||
if (k.key0 != null)
|
if (k.key0 != null)
|
||||||
Keyboard2.log("Key up " + k.key0.getName());
|
Keyboard2.log("Key up " + k.key0.getName());
|
||||||
key_down = false;
|
k.down_pointer = -1;
|
||||||
invalidate();
|
invalidate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
k.down = key_down;
|
|
||||||
x += keyW + _keyMargin;
|
|
||||||
}
|
}
|
||||||
y += _keyHeight + _keyMargin;
|
|
||||||
}
|
|
||||||
return (true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -166,7 +189,7 @@ public class Keyboard2View extends View
|
|||||||
for (Key k : row)
|
for (Key k : row)
|
||||||
{
|
{
|
||||||
float keyW = _keyWidth * k.width;
|
float keyW = _keyWidth * k.width;
|
||||||
if (k.down)
|
if (k.down_pointer != -1)
|
||||||
canvas.drawRect(x, y, x + keyW, y + _keyHeight, _keyDownBgPaint);
|
canvas.drawRect(x, y, x + keyW, y + _keyHeight, _keyDownBgPaint);
|
||||||
else
|
else
|
||||||
canvas.drawRect(x, y, x + keyW, y + _keyHeight, _keyBgPaint);
|
canvas.drawRect(x, y, x + keyW, y + _keyHeight, _keyBgPaint);
|
||||||
@ -236,11 +259,11 @@ public class Keyboard2View extends View
|
|||||||
|
|
||||||
public float width;
|
public float width;
|
||||||
|
|
||||||
public boolean down;
|
public int down_pointer;
|
||||||
|
|
||||||
public Key(XmlResourceParser parser) throws Exception
|
public Key(XmlResourceParser parser) throws Exception
|
||||||
{
|
{
|
||||||
down = false;
|
down_pointer = -1;
|
||||||
key0 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key0"));
|
key0 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key0"));
|
||||||
key1 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key1"));
|
key1 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key1"));
|
||||||
key2 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key2"));
|
key2 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key2"));
|
||||||
|
Loading…
Reference in New Issue
Block a user