adding a helper function for getting the closest grid cell
This commit is contained in:
parent
115b60c52e
commit
5d85b9d7b4
|
@ -43,7 +43,7 @@ android {
|
|||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'com.github.SimpleMobileTools:Simple-Commons:e93092b380'
|
||||
implementation 'com.github.SimpleMobileTools:Simple-Commons:b826e8e020'
|
||||
|
||||
kapt "androidx.room:room-compiler:2.4.3"
|
||||
implementation "androidx.room:room-runtime:2.4.3"
|
||||
|
|
|
@ -86,10 +86,10 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
|
|||
val center = gridCenters.minBy { Math.abs(it.first - x) + Math.abs(it.second - y) }
|
||||
var redrawIcons = false
|
||||
|
||||
// convert stuff like 102x192 to grid cells like 0x1
|
||||
rowXCoords.forEachIndexed { xIndex, xCell ->
|
||||
rowYCoords.forEachIndexed { yIndex, yCell ->
|
||||
if (xCell + rowWidth / 2 == center.first && yCell + rowHeight / 2 == center.second) {
|
||||
val gridCells = getClosestGridCells(center)
|
||||
if (gridCells != null) {
|
||||
val xIndex = gridCells.first
|
||||
val yIndex = gridCells.second
|
||||
// check if the destination grid item is empty
|
||||
val targetGridItem = gridItems.firstOrNull { it.left == xIndex && it.top == yIndex }
|
||||
if (targetGridItem == null) {
|
||||
|
@ -129,8 +129,6 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
|
|||
redrawIcons = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
draggedItem = null
|
||||
if (redrawIcons) {
|
||||
|
@ -138,6 +136,19 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
|
|||
}
|
||||
}
|
||||
|
||||
// convert stuff like 102x192 to grid cells like 0x1
|
||||
private fun getClosestGridCells(center: Pair<Int, Int>): Pair<Int, Int>? {
|
||||
rowXCoords.forEachIndexed { xIndex, xCell ->
|
||||
rowYCoords.forEachIndexed { yIndex, yCell ->
|
||||
if (xCell + rowWidth / 2 == center.first && yCell + rowHeight / 2 == center.second) {
|
||||
return Pair(xIndex, yIndex)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
@SuppressLint("DrawAllocation")
|
||||
override fun onDraw(canvas: Canvas) {
|
||||
super.onDraw(canvas)
|
||||
|
|
Loading…
Reference in New Issue