handle collisions at resizing to the top

This commit is contained in:
tibbi 2022-10-04 19:49:01 +02:00
parent 72debab925
commit abc838f307
1 changed files with 37 additions and 4 deletions

View File

@ -146,15 +146,31 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
cellsRect.left = closestCellX cellsRect.left = closestCellX
cellChanged() cellChanged()
} }
frameRect.left = wantedLeft frameRect.left = wantedLeft
} }
DRAGGING_TOP -> { DRAGGING_TOP -> {
val newHeight = frameRect.bottom - event.rawY.toInt() val newHeight = frameRect.bottom - event.rawY.toInt()
if (newHeight >= minHeight) { val wantedTop = if (newHeight >= minHeight) {
frameRect.top = event.rawY.toInt() event.rawY.toInt()
} else { } else {
frameRect.top = frameRect.bottom - minHeight frameRect.bottom - minHeight
} }
val closestCellY = roundToClosestMultiplyOfNumber(wantedTop - sideMargins.top, cellHeight) / cellHeight
var areAllCellsFree = true
for (xCell in cellsRect.left until cellsRect.right) {
if (occupiedCells.contains(Pair(xCell, closestCellY))) {
areAllCellsFree = false
}
}
if (areAllCellsFree && cellsRect.top != closestCellY) {
cellsRect.top = closestCellY
cellChanged()
}
frameRect.top = wantedTop
} }
DRAGGING_RIGHT -> { DRAGGING_RIGHT -> {
val newWidth = event.rawX.toInt() - frameRect.left val newWidth = event.rawX.toInt() - frameRect.left
@ -176,6 +192,7 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
cellsRect.right = closestCellX cellsRect.right = closestCellX
cellChanged() cellChanged()
} }
frameRect.right = wantedRight frameRect.right = wantedRight
} }
DRAGGING_BOTTOM -> { DRAGGING_BOTTOM -> {
@ -217,7 +234,23 @@ class MyAppWidgetResizeFrame(context: Context, attrs: AttributeSet, defStyle: In
frameRect.left = cellsRect.left * cellWidth + sideMargins.left frameRect.left = cellsRect.left * cellWidth + sideMargins.left
} }
} }
DRAGGING_TOP -> frameRect.top = roundToClosestMultiplyOfNumber(frameRect.top - sideMargins.top, cellHeight) + sideMargins.top DRAGGING_TOP -> {
val wantedTop = roundToClosestMultiplyOfNumber(frameRect.top - sideMargins.top, cellHeight)
val wantedTopCellY = wantedTop / cellHeight
var areAllCellsFree = true
for (xCell in cellsRect.left until cellsRect.right) {
if (occupiedCells.contains(Pair(xCell, wantedTopCellY))) {
areAllCellsFree = false
}
}
if (areAllCellsFree) {
frameRect.top = wantedTop + sideMargins.top
cellsRect.top = wantedTopCellY
} else {
frameRect.top = cellsRect.top * cellHeight + sideMargins.top
}
}
DRAGGING_RIGHT -> { DRAGGING_RIGHT -> {
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