mirror of
https://github.com/Julow/Unexpected-Keyboard.git
synced 2025-06-28 21:51:37 +02:00
Improve nearest key computation
getAtDirection was too hard to maintain and might contain bugs. Change slightly the meaning of directions and implement a the nearest key calculation as a loop.
This commit is contained in:
parent
bce0a98f62
commit
9a48acfe3e
@ -195,102 +195,58 @@ class KeyboardData
|
|||||||
return new Key(key0, key1, key2, key3, key4, width * s, shift, edgekeys);
|
return new Key(key0, key1, key2, key3, key4, width * s, shift, edgekeys);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private KeyValue getAtDirectionExact(int direction)
|
||||||
/* Get the KeyValue at the given direction. See Pointers.onTouchMove() for the represented direction */
|
|
||||||
public KeyValue getAtDirection(int direction)
|
|
||||||
{
|
{
|
||||||
if (direction == 0 || direction > 8) return key0;
|
if (edgekeys)
|
||||||
KeyValue key = null;
|
{
|
||||||
if (edgekeys) {
|
|
||||||
// \ 1 /
|
// \ 1 /
|
||||||
// \ /
|
// \ /
|
||||||
// 3 0 2
|
// 3 0 2
|
||||||
// / \
|
// / \
|
||||||
// / 4 \
|
// / 4 \
|
||||||
|
|
||||||
// first closer
|
|
||||||
switch (direction)
|
switch (direction)
|
||||||
{
|
{
|
||||||
case 2: case 3: key = key1; break;
|
case 2: case 3: return key1;
|
||||||
case 4: case 8: key = key2; break;
|
case 4: case 5: return key2;
|
||||||
case 1: case 5: key = key3; break;
|
case 6: case 7: return key4;
|
||||||
case 6: case 7: key = key4; break;
|
case 8: case 1: return key3;
|
||||||
}
|
}
|
||||||
if (key != null) return key;
|
|
||||||
// second closer
|
|
||||||
switch (direction)
|
|
||||||
{
|
|
||||||
case 1: case 4: key = key1; break;
|
|
||||||
case 3: case 7: key = key2; break;
|
|
||||||
case 2: case 6: key = key3; break;
|
|
||||||
case 5: case 8: key = key4; break;
|
|
||||||
}
|
|
||||||
if (key != null) return key;
|
|
||||||
// third closer
|
|
||||||
switch (direction)
|
|
||||||
{
|
|
||||||
case 5: case 8: key = key1; break;
|
|
||||||
case 2: case 6: key = key2; break;
|
|
||||||
case 3: case 7: key = key3; break;
|
|
||||||
case 1: case 4: key = key4; break;
|
|
||||||
}
|
|
||||||
if (key != null) return key;
|
|
||||||
// fourth closer
|
|
||||||
switch (direction)
|
|
||||||
{
|
|
||||||
case 6: case 7: key = key1; break;
|
|
||||||
case 1: case 5: key = key2; break;
|
|
||||||
case 4: case 8: key = key3; break;
|
|
||||||
case 2: case 3: key = key4; break;
|
|
||||||
}
|
|
||||||
if (key != null) return key;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// 1 | 2
|
// 1 | 2
|
||||||
// |
|
// |
|
||||||
// --0--
|
// --0--
|
||||||
// |
|
// |
|
||||||
// 3 | 4
|
// 3 | 4
|
||||||
// first closer
|
|
||||||
switch (direction)
|
switch (direction)
|
||||||
{
|
{
|
||||||
case 1: case 2: key = key1; break;
|
case 1: case 2: return key1;
|
||||||
case 3: case 4: key = key2; break;
|
case 3: case 4: return key2;
|
||||||
case 5: case 6: key = key3; break;
|
case 5: case 6: return key4;
|
||||||
case 7: case 8: key = key4; break;
|
case 7: case 8: return key3;
|
||||||
}
|
}
|
||||||
if (key != null) return key;
|
|
||||||
// second closer
|
|
||||||
switch (direction)
|
|
||||||
{
|
|
||||||
case 3: case 5: key = key1; break;
|
|
||||||
case 2: case 8: key = key2; break;
|
|
||||||
case 1: case 7: key = key3; break;
|
|
||||||
case 4: case 6: key = key4; break;
|
|
||||||
}
|
|
||||||
if (key != null) return key;
|
|
||||||
// third closer
|
|
||||||
switch (direction)
|
|
||||||
{
|
|
||||||
case 4: case 6: key = key1; break;
|
|
||||||
case 1: case 7: key = key2; break;
|
|
||||||
case 2: case 8: key = key3; break;
|
|
||||||
case 3: case 5: key = key4; break;
|
|
||||||
}
|
|
||||||
if (key != null) return key;
|
|
||||||
// fourth closer
|
|
||||||
switch (direction)
|
|
||||||
{
|
|
||||||
case 7: case 8: key = key1; break;
|
|
||||||
case 3: case 4: key = key2; break;
|
|
||||||
case 5: case 6: key = key3; break;
|
|
||||||
case 1: case 2: key = key4; break;
|
|
||||||
}
|
|
||||||
if (key != null) return key;
|
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
return key0;
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Get the KeyValue at the given direction. In case of swipe (!= 0), get the
|
||||||
|
* nearest KeyValue that is not key0. See Pointers.onTouchMove() for the
|
||||||
|
* represented direction.
|
||||||
|
*/
|
||||||
|
public KeyValue getAtDirection(int direction)
|
||||||
|
{
|
||||||
|
if (direction == 0)
|
||||||
|
return key0;
|
||||||
|
KeyValue k;
|
||||||
|
for (int i = 0; i > -2; i = (~i>>31) - i)
|
||||||
|
{
|
||||||
|
k = getAtDirectionExact(Math.floorMod(direction + i - 1, 8) + 1);
|
||||||
|
if (k != null)
|
||||||
|
return k;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -167,12 +167,12 @@ public final class Pointers implements Handler.Callback
|
|||||||
// |\2|3/|
|
// |\2|3/|
|
||||||
// |1\|/4|
|
// |1\|/4|
|
||||||
// |-----|
|
// |-----|
|
||||||
// |5/|\8|
|
// |8/|\5|
|
||||||
// |/6|7\|
|
// |/7|6\|
|
||||||
direction = 1;
|
direction = 1;
|
||||||
if (dy > 0) direction += 4;
|
|
||||||
if (dx > 0) direction += 2;
|
if (dx > 0) direction += 2;
|
||||||
if (dx > Math.abs(dy) || (dx < 0 && dx > -Math.abs(dy))) direction += 1;
|
if (dx > Math.abs(dy) || (dx < 0 && dx > -Math.abs(dy))) direction += 1;
|
||||||
|
if (dy > 0) direction = 9 - direction;
|
||||||
}
|
}
|
||||||
|
|
||||||
KeyValue newSelectedValue = ptr.key.getAtDirection(direction);
|
KeyValue newSelectedValue = ptr.key.getAtDirection(direction);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user