Skip to content

Commit

Permalink
Improve nearest key computation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Julow committed May 7, 2022
1 parent 8cc613e commit 6721d40
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 79 deletions.
108 changes: 32 additions & 76 deletions srcs/juloo.keyboard2/KeyboardData.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,102 +195,58 @@ public Key scaleWidth(float s)
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;
}
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;
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;
// 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;
}
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;
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;
// 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;
}
}

Expand Down
6 changes: 3 additions & 3 deletions srcs/juloo.keyboard2/Pointers.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,12 @@ public void onTouchMove(float x, float y, int pointerId)
// |\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);
Expand Down

0 comments on commit 6721d40

Please sign in to comment.