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:
Jules Aguillon 2022-05-08 01:18:53 +02:00
parent bce0a98f62
commit 9a48acfe3e
2 changed files with 35 additions and 79 deletions

View File

@ -195,102 +195,58 @@ class KeyboardData
return new Key(key0, key1, key2, key3, key4, width * s, shift, edgekeys);
}
/* Get the KeyValue at the given direction. See Pointers.onTouchMove() for the represented direction */
public KeyValue getAtDirection(int direction)
private KeyValue getAtDirectionExact(int direction)
{
if (direction == 0 || direction > 8) return key0;
KeyValue key = null;
if (edgekeys) {
if (edgekeys)
{
// \ 1 /
// \ /
// 3 0 2
// / \
// / 4 \
// first closer
switch (direction)
{
case 2: case 3: key = key1; break;
case 4: case 8: key = key2; break;
case 1: case 5: key = key3; break;
case 6: case 7: key = key4; break;
case 2: case 3: return key1;
case 4: case 5: return key2;
case 6: case 7: return key4;
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
{
// 1 | 2
// |
// --0--
// |
// |
// 3 | 4
// first closer
switch (direction)
{
case 1: case 2: key = key1; break;
case 3: case 4: key = key2; break;
case 5: case 6: key = key3; break;
case 7: case 8: key = key4; break;
case 1: case 2: return key1;
case 3: case 4: return key2;
case 5: case 6: return key4;
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 key0;
return null;
}
/*
* 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;
}
}

View File

@ -167,12 +167,12 @@ public final class Pointers implements Handler.Callback
// |\2|3/|
// |1\|/4|
// |-----|
// |5/|\8|
// |/6|7\|
// |8/|\5|
// |/7|6\|
direction = 1;
if (dy > 0) direction += 4;
if (dx > 0) direction += 2;
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);