Stop storing keydown data in KeyboardData.Key

This commit is contained in:
juloo 2015-08-01 18:21:10 +02:00
parent 0942556bad
commit 3b7141e3a0
2 changed files with 81 additions and 82 deletions

View File

@ -8,6 +8,7 @@ import android.util.DisplayMetrics;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import java.util.Vector;
public class Keyboard2View extends View public class Keyboard2View extends View
implements View.OnTouchListener implements View.OnTouchListener
@ -17,7 +18,7 @@ public class Keyboard2View extends View
private Keyboard2 _ime; private Keyboard2 _ime;
private KeyboardData _keyboard; private KeyboardData _keyboard;
private KeyValue _downValue; private Vector<KeyDown> _downKeys;
private float _verticalMargin; private float _verticalMargin;
private float _horizontalMargin; private float _horizontalMargin;
@ -35,7 +36,7 @@ public class Keyboard2View extends View
{ {
super(context, attrs); super(context, attrs);
DisplayMetrics dm = context.getResources().getDisplayMetrics(); DisplayMetrics dm = context.getResources().getDisplayMetrics();
_downValue = null; _downKeys = new Vector<KeyDown>();
_verticalMargin = getResources().getDimension(R.dimen.vertical_margin); _verticalMargin = getResources().getDimension(R.dimen.vertical_margin);
_horizontalMargin = getResources().getDimension(R.dimen.horizontal_margin); _horizontalMargin = getResources().getDimension(R.dimen.horizontal_margin);
_keyHeight = getResources().getDimension(R.dimen.key_height); _keyHeight = getResources().getDimension(R.dimen.key_height);
@ -92,24 +93,32 @@ public class Keyboard2View extends View
return (true); return (true);
} }
private KeyDown getKeyDown(int pointerId)
{
for (KeyDown k : _downKeys)
{
if (k.pointerId == pointerId)
return (k);
}
return (null);
}
private boolean isKeyDown(KeyboardData.Key key)
{
for (KeyDown k : _downKeys)
{
if (k.key == key)
return (true);
}
return (false);
}
private void onTouchMove(float moveX, float moveY, int pointerId) private void onTouchMove(float moveX, float moveY, int pointerId)
{ {
for (KeyboardData.Row row : _keyboard.getRows()) KeyDown k = getKeyDown(pointerId);
{
for (KeyboardData.Key k : row) if (k != null)
{ k.updateDown(moveX, moveY);
if (k.downPointer == pointerId)
{
KeyValue v = k.getDownValue(moveX, moveY);
if (v != k.downValue)
{
k.downValue = v;
if (v != null)
_downValue = v;
}
}
}
}
} }
private void onTouchDown(float touchX, float touchY, int pointerId) private void onTouchDown(float touchX, float touchY, int pointerId)
@ -125,17 +134,12 @@ public class Keyboard2View extends View
if (touchY < y || touchY >= (y + _keyHeight)) if (touchY < y || touchY >= (y + _keyHeight))
continue ; continue ;
x = (KEY_PER_ROW * _keyWidth - row.getWidth(_keyWidth)) / 2 + _horizontalMargin; x = (KEY_PER_ROW * _keyWidth - row.getWidth(_keyWidth)) / 2 + _horizontalMargin;
for (KeyboardData.Key k : row) for (KeyboardData.Key key : row)
{ {
keyW = _keyWidth * k.width; keyW = _keyWidth * key.width;
if (touchX >= x && touchX < (x + keyW) && k.downPointer == -1) if (touchX >= x && touchX < (x + keyW))
{ {
if (k.key0 != null) _downKeys.add(new KeyDown(pointerId, key, touchX, touchY));
_downValue = k.key0;
k.downPointer = pointerId;
k.downValue = k.key0;
k.downX = touchX;
k.downY = touchY;
invalidate(); invalidate();
return ; return ;
} }
@ -146,38 +150,17 @@ public class Keyboard2View extends View
private void onTouchUp(int pointerId) private void onTouchUp(int pointerId)
{ {
for (KeyboardData.Row row : _keyboard.getRows()) KeyDown k = getKeyDown(pointerId);
if (k != null)
{ {
for (KeyboardData.Key k : row) if (k.value != null)
{ _ime.handleKey(k.value);
if (k.downPointer == pointerId) _downKeys.remove(k);
{
if (k.downValue != null)
_ime.handleKey(k.downValue);
_downValue = null;
k.downPointer = -1;
nextDownValue();
invalidate(); invalidate();
return ; return ;
} }
} }
}
}
private void nextDownValue()
{
for (keyboardData.Row row : _keyboard.getRows())
{
for (KeyboardData.Key k : row)
{
if (k.downPointer != -1)
{
_downValue = k.downValue;
return ;
}
}
}
}
@Override @Override
public void onMeasure(int wSpec, int hSpec) public void onMeasure(int wSpec, int hSpec)
@ -205,7 +188,7 @@ public class Keyboard2View extends View
for (KeyboardData.Key k : row) for (KeyboardData.Key k : row)
{ {
float keyW = _keyWidth * k.width; float keyW = _keyWidth * k.width;
if (k.downPointer != -1) if (isKeyDown(k))
canvas.drawRect(x + _keyBgPadding, y + _keyBgPadding, canvas.drawRect(x + _keyBgPadding, y + _keyBgPadding,
x + keyW - _keyBgPadding, y + _keyHeight - _keyBgPadding, _keyDownBgPaint); x + keyW - _keyBgPadding, y + _keyHeight - _keyBgPadding, _keyDownBgPaint);
else else
@ -234,4 +217,46 @@ public class Keyboard2View extends View
y += _keyHeight; y += _keyHeight;
} }
} }
private class KeyDown
{
private static final float SUB_VALUE_DIST = 6f;
public int pointerId;
public KeyValue value;
public KeyboardData.Key key;
public float downX;
public float downY;
public KeyDown(int pointerId, KeyboardData.Key key, float x, float y)
{
this.pointerId = pointerId;
this.value = key.key0;
this.key = key;
this.downX = x;
this.downY = y;
}
public void updateDown(float x, float y)
{
value = getDownValue(x - downX, y - downY);
if (value == null)
value = key.key0;
}
private KeyValue getDownValue(float x, float y)
{
if ((Math.abs(x) + Math.abs(y)) < SUB_VALUE_DIST)
return (key.key0);
if (x < 0)
{
if (y < 0)
return (key.key1);
return (key.key3);
}
else if (y < 0)
return (key.key2);
return (key.key4);
}
}
} }

View File

@ -93,7 +93,6 @@ class KeyboardData
public Key(XmlResourceParser parser) throws Exception public Key(XmlResourceParser parser) throws Exception
{ {
downPointer = -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"));
@ -110,30 +109,5 @@ class KeyboardData
while (parser.next() != XmlResourceParser.END_TAG) while (parser.next() != XmlResourceParser.END_TAG)
continue ; continue ;
} }
// TODO move it in Keyboard2View
private static final float SUB_VALUE_DIST = 5f;
public int downPointer;
public KeyValue downValue;
public float downX;
public float downY;
public KeyValue getDownValue(float x, float y)
{
x -= downX;
y -= downY;
if ((Math.abs(x) + Math.abs(y)) < SUB_VALUE_DIST)
return (key0);
if (x < 0)
{
if (y < 0)
return ((key1 == null) ? key0 : key1);
return ((key3 == null) ? key0 : key3);
}
else if (y < 0)
return ((key2 == null) ? key0 : key2);
return ((key4 == null) ? key0 : key4);
}
} }
} }