Extend ControlPositions with centered controls

This commit is contained in:
GeorgH93 2021-07-21 19:11:00 +02:00
parent e05cd13126
commit 26295902f9
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
2 changed files with 21 additions and 7 deletions

View File

@ -24,14 +24,18 @@ public enum ControlPosition
// Horizontal controls
TOP_LEFT(true),
TOP_RIGHT(true),
TOP_CENTER(true),
BOTTOM_LEFT(true),
BOTTOM_RIGHT(true),
BOTTOM_CENTER(true),
// Vertical controls
LEFT_TOP(false),
LEFT_BOTTOM(false),
LEFT_CENTER(false),
RIGHT_TOP(false),
RIGHT_BOTTOM(false);
RIGHT_BOTTOM(false),
RIGHT_CENTER(false);
@Getter private final boolean vertical, horizontal;
@ -44,19 +48,22 @@ public enum ControlPosition
public int[] getControlIds(int controlCount, int rowCount)
{
assert(controlCount <= 9);
int startId = -1, stride = isVertical() ? 9 : 1;
int startId = 0, stride = isVertical() ? 9 : 1;
switch(this)
{
case TOP_LEFT: startId = 0; break;
// Horizontal controls
case TOP_RIGHT: startId = 9 - controlCount; break;
case BOTTOM_LEFT: startId = (rowCount - 1) * 9; break;
case BOTTOM_RIGHT: startId = (rowCount * 9) - controlCount; break;
case LEFT_TOP: startId = 0; break;
case LEFT_BOTTOM: startId = (rowCount - controlCount) * 9; break;
case BOTTOM_CENTER: startId = ((rowCount - 1) * 9); // Fallthrough to not duplicate same calculation
case TOP_CENTER: startId += (9 - controlCount) / 2; break;
// Vertical controls
case RIGHT_TOP: startId = 8; break;
case RIGHT_BOTTOM: startId = ((rowCount - controlCount) * 9) + 8; break;
case RIGHT_BOTTOM: startId = 8;
case LEFT_BOTTOM: startId += (rowCount - controlCount) * 9; break;
case RIGHT_CENTER: startId = 8; // Fallthrough to not duplicate same calculation
case LEFT_CENTER: startId += ((rowCount - controlCount) / 2) * 9; break;
}
assert(startId != -1);
int[] ids = new int[controlCount];
for(int i = 0, id = startId; i < controlCount; i++, id += stride)

View File

@ -26,14 +26,21 @@ class ControlPositionTest
@Test
void testGetControlIds()
{
// Horizontal controls
assertArrayEquals(new int[]{0, 1, 2, 3, 4}, ControlPosition.TOP_LEFT.getControlIds(5, 6));
assertArrayEquals(new int[]{4, 5, 6, 7, 8}, ControlPosition.TOP_RIGHT.getControlIds(5, 6));
assertArrayEquals(new int[]{3, 4, 5}, ControlPosition.TOP_CENTER.getControlIds(3, 6));
assertArrayEquals(new int[]{45, 46, 47}, ControlPosition.BOTTOM_LEFT.getControlIds(3, 6));
assertArrayEquals(new int[]{51, 52, 53}, ControlPosition.BOTTOM_RIGHT.getControlIds(3, 6));
assertArrayEquals(new int[]{48, 49, 50}, ControlPosition.BOTTOM_CENTER.getControlIds(3, 6));
// Vertical controls
assertArrayEquals(new int[]{0, 9, 18}, ControlPosition.LEFT_TOP.getControlIds(3, 6));
assertArrayEquals(new int[]{27, 36, 45}, ControlPosition.LEFT_BOTTOM.getControlIds(3, 6));
assertArrayEquals(new int[]{9, 18, 27}, ControlPosition.LEFT_CENTER.getControlIds(3, 6));
assertArrayEquals(new int[]{8, 17, 26}, ControlPosition.RIGHT_TOP.getControlIds(3, 6));
assertArrayEquals(new int[]{35, 44, 53}, ControlPosition.RIGHT_BOTTOM.getControlIds(3, 6));
assertArrayEquals(new int[]{17, 26, 35}, ControlPosition.RIGHT_CENTER.getControlIds(3, 6));
}
@Test