further improving some collision checks

This commit is contained in:
tibbi 2022-10-04 20:00:45 +02:00
parent abc838f307
commit dccad9ef90
1 changed files with 28 additions and 16 deletions

View File

@ -136,11 +136,13 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
val closestCellX = roundToClosestMultiplyOfNumber(wantedLeft - sideMargins.left, cellWidth) / cellWidth val closestCellX = roundToClosestMultiplyOfNumber(wantedLeft - sideMargins.left, cellWidth) / cellWidth
var areAllCellsFree = true var areAllCellsFree = true
for (xCell in closestCellX until cellsRect.right) {
for (yCell in cellsRect.top until cellsRect.bottom) { for (yCell in cellsRect.top until cellsRect.bottom) {
if (occupiedCells.contains(Pair(closestCellX, yCell))) { if (occupiedCells.contains(Pair(xCell, yCell))) {
areAllCellsFree = false areAllCellsFree = false
} }
} }
}
if (areAllCellsFree && cellsRect.left != closestCellX) { if (areAllCellsFree && cellsRect.left != closestCellX) {
cellsRect.left = closestCellX cellsRect.left = closestCellX
@ -160,10 +162,12 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
val closestCellY = roundToClosestMultiplyOfNumber(wantedTop - sideMargins.top, cellHeight) / cellHeight val closestCellY = roundToClosestMultiplyOfNumber(wantedTop - sideMargins.top, cellHeight) / cellHeight
var areAllCellsFree = true var areAllCellsFree = true
for (xCell in cellsRect.left until cellsRect.right) { for (xCell in cellsRect.left until cellsRect.right) {
if (occupiedCells.contains(Pair(xCell, closestCellY))) { for (yCell in closestCellY until cellsRect.bottom) {
if (occupiedCells.contains(Pair(xCell, yCell))) {
areAllCellsFree = false areAllCellsFree = false
} }
} }
}
if (areAllCellsFree && cellsRect.top != closestCellY) { if (areAllCellsFree && cellsRect.top != closestCellY) {
cellsRect.top = closestCellY cellsRect.top = closestCellY
@ -182,11 +186,13 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
val closestCellX = roundToClosestMultiplyOfNumber(wantedRight - sideMargins.left, cellWidth) / cellWidth - 1 val closestCellX = roundToClosestMultiplyOfNumber(wantedRight - sideMargins.left, cellWidth) / cellWidth - 1
var areAllCellsFree = true var areAllCellsFree = true
for (xCell in cellsRect.left until closestCellX + 1) {
for (yCell in cellsRect.top until cellsRect.bottom) { for (yCell in cellsRect.top until cellsRect.bottom) {
if (occupiedCells.contains(Pair(closestCellX, yCell))) { if (occupiedCells.contains(Pair(xCell, yCell))) {
areAllCellsFree = false areAllCellsFree = false
} }
} }
}
if (areAllCellsFree && cellsRect.right != closestCellX) { if (areAllCellsFree && cellsRect.right != closestCellX) {
cellsRect.right = closestCellX cellsRect.right = closestCellX
@ -221,11 +227,13 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
val wantedLeft = roundToClosestMultiplyOfNumber(frameRect.left - sideMargins.left, cellWidth) val wantedLeft = roundToClosestMultiplyOfNumber(frameRect.left - sideMargins.left, cellWidth)
val wantedLeftCellX = wantedLeft / cellWidth val wantedLeftCellX = wantedLeft / cellWidth
var areAllCellsFree = true var areAllCellsFree = true
for (xCell in wantedLeftCellX until cellsRect.right) {
for (yCell in cellsRect.top until cellsRect.bottom) { for (yCell in cellsRect.top until cellsRect.bottom) {
if (occupiedCells.contains(Pair(wantedLeftCellX, yCell))) { if (occupiedCells.contains(Pair(xCell, yCell))) {
areAllCellsFree = false areAllCellsFree = false
} }
} }
}
if (areAllCellsFree) { if (areAllCellsFree) {
frameRect.left = wantedLeft + sideMargins.left frameRect.left = wantedLeft + sideMargins.left
@ -239,10 +247,12 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
val wantedTopCellY = wantedTop / cellHeight val wantedTopCellY = wantedTop / cellHeight
var areAllCellsFree = true var areAllCellsFree = true
for (xCell in cellsRect.left until cellsRect.right) { for (xCell in cellsRect.left until cellsRect.right) {
if (occupiedCells.contains(Pair(xCell, wantedTopCellY))) { for (yCell in wantedTopCellY until cellsRect.bottom) {
if (occupiedCells.contains(Pair(xCell, yCell))) {
areAllCellsFree = false areAllCellsFree = false
} }
} }
}
if (areAllCellsFree) { if (areAllCellsFree) {
frameRect.top = wantedTop + sideMargins.top frameRect.top = wantedTop + sideMargins.top
@ -255,11 +265,13 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
val wantedRight = roundToClosestMultiplyOfNumber(frameRect.right - sideMargins.left, cellWidth) val wantedRight = roundToClosestMultiplyOfNumber(frameRect.right - sideMargins.left, cellWidth)
val wantedRightCellX = wantedRight / cellWidth - 1 val wantedRightCellX = wantedRight / cellWidth - 1
var areAllCellsFree = true var areAllCellsFree = true
for (xCell in cellsRect.left until wantedRightCellX + 1) {
for (yCell in cellsRect.top until cellsRect.bottom) { for (yCell in cellsRect.top until cellsRect.bottom) {
if (occupiedCells.contains(Pair(wantedRightCellX, yCell))) { if (occupiedCells.contains(Pair(xCell, yCell))) {
areAllCellsFree = false areAllCellsFree = false
} }
} }
}
if (areAllCellsFree) { if (areAllCellsFree) {
frameRect.right = wantedRight + sideMargins.left frameRect.right = wantedRight + sideMargins.left