mirror of
https://github.com/SimpleMobileTools/Simple-Launcher.git
synced 2025-06-05 21:59:15 +02:00
adding a helper function for getting the closest grid cell
This commit is contained in:
@ -43,7 +43,7 @@ android {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'com.github.SimpleMobileTools:Simple-Commons:e93092b380'
|
implementation 'com.github.SimpleMobileTools:Simple-Commons:b826e8e020'
|
||||||
|
|
||||||
kapt "androidx.room:room-compiler:2.4.3"
|
kapt "androidx.room:room-compiler:2.4.3"
|
||||||
implementation "androidx.room:room-runtime: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) }
|
val center = gridCenters.minBy { Math.abs(it.first - x) + Math.abs(it.second - y) }
|
||||||
var redrawIcons = false
|
var redrawIcons = false
|
||||||
|
|
||||||
// convert stuff like 102x192 to grid cells like 0x1
|
val gridCells = getClosestGridCells(center)
|
||||||
rowXCoords.forEachIndexed { xIndex, xCell ->
|
if (gridCells != null) {
|
||||||
rowYCoords.forEachIndexed { yIndex, yCell ->
|
val xIndex = gridCells.first
|
||||||
if (xCell + rowWidth / 2 == center.first && yCell + rowHeight / 2 == center.second) {
|
val yIndex = gridCells.second
|
||||||
// check if the destination grid item is empty
|
// check if the destination grid item is empty
|
||||||
val targetGridItem = gridItems.firstOrNull { it.left == xIndex && it.top == yIndex }
|
val targetGridItem = gridItems.firstOrNull { it.left == xIndex && it.top == yIndex }
|
||||||
if (targetGridItem == null) {
|
if (targetGridItem == null) {
|
||||||
@ -129,8 +129,6 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
|
|||||||
redrawIcons = true
|
redrawIcons = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
draggedItem = null
|
draggedItem = null
|
||||||
if (redrawIcons) {
|
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")
|
@SuppressLint("DrawAllocation")
|
||||||
override fun onDraw(canvas: Canvas) {
|
override fun onDraw(canvas: Canvas) {
|
||||||
super.onDraw(canvas)
|
super.onDraw(canvas)
|
||||||
|
Reference in New Issue
Block a user