From b57c0b2762c18dbdc1997931b0ea2ca65d0b7ff8 Mon Sep 17 00:00:00 2001 From: Sebastian Franco Date: Tue, 28 Jun 2022 13:54:35 -0700 Subject: [PATCH] Fixing findNearestArea to account for the padding. The function findNearestArea calculates the corner of the cell but in that calculation it doesn't account for the padding in the cells. Also, change ignoreOccupied because the name says one thing and the code does the oposite so I change all true calls for false calls and change the if to !ignoreOccupied so the description of the parameter match the behaviour of the method. Also, removing unused method. Fix: 236134208 Fix: 236129504 Test: visualizing the components of findNearestArea in ag/19248816. Change-Id: Iac50fbc76b4fa2acda21894ecb976ba612d468fe --- src/com/android/launcher3/CellLayout.java | 43 +++++++++-------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/src/com/android/launcher3/CellLayout.java b/src/com/android/launcher3/CellLayout.java index 52dfcd40a9..52960a9e8e 100644 --- a/src/com/android/launcher3/CellLayout.java +++ b/src/com/android/launcher3/CellLayout.java @@ -829,8 +829,8 @@ public class CellLayout extends ViewGroup { final int hStartPadding = getPaddingLeft(); final int vStartPadding = getPaddingTop(); - result[0] = (x - hStartPadding) / mCellWidth; - result[1] = (y - vStartPadding) / mCellHeight; + result[0] = (x - hStartPadding) / (mCellWidth + mBorderSpace.x); + result[1] = (y - vStartPadding) / (mCellHeight + mBorderSpace.y); final int xAxis = mCountX; final int yAxis = mCountY; @@ -841,16 +841,6 @@ public class CellLayout extends ViewGroup { if (result[1] >= yAxis) result[1] = yAxis - 1; } - /** - * Given a point, return the cell that most closely encloses that point - * @param x X coordinate of the point - * @param y Y coordinate of the point - * @param result Array of 2 ints to hold the x and y coordinate of the cell - */ - void pointToCellRounded(int x, int y, int[] result) { - pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result); - } - /** * Given a cell coordinate, return the point that represents the upper left corner of that cell * @@ -1240,7 +1230,7 @@ public class CellLayout extends ViewGroup { */ int[] findNearestVacantArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY, int[] result, int[] resultSpan) { - return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, true, + return findNearestArea(pixelX, pixelY, minSpanX, minSpanY, spanX, spanY, false, result, resultSpan); } @@ -1262,9 +1252,10 @@ public class CellLayout extends ViewGroup { /** * Find a vacant area that will fit the given bounds nearest the requested * cell location. Uses Euclidean distance to score multiple vacant areas. - * - * @param pixelX The X location at which you want to search for a vacant area. - * @param pixelY The Y location at which you want to search for a vacant area. + * @param relativeXPos The X location relative to the Cell layout at which you want to search + * for a vacant area. + * @param relativeYPos The Y location relative to the Cell layout at which you want to search + * for a vacant area. * @param minSpanX The minimum horizontal span required * @param minSpanY The minimum vertical span required * @param spanX Horizontal span of the object. @@ -1275,15 +1266,15 @@ public class CellLayout extends ViewGroup { * @return The X, Y cell of a vacant area that can contain this object, * nearest the requested location. */ - private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, - int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) { + private int[] findNearestArea(int relativeXPos, int relativeYPos, int minSpanX, int minSpanY, + int spanX, int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) { lazyInitTempRectStack(); - // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds - // to the center of the item, but we are searching based on the top-left cell, so - // we translate the point over to correspond to the top-left. - pixelX -= mCellWidth * (spanX - 1) / 2f; - pixelY -= mCellHeight * (spanY - 1) / 2f; + // For items with a spanX / spanY > 1, the passed in point (relativeXPos, relativeYPos) + // corresponds to the center of the item, but we are searching based on the top-left cell, + // so we translate the point over to correspond to the top-left. + relativeXPos = (int) (relativeXPos - (mCellWidth + mBorderSpace.x) * (spanX - 1) / 2f); + relativeYPos = (int) (relativeYPos - (mCellHeight + mBorderSpace.y) * (spanY - 1) / 2f); // Keep track of best-scoring drop area final int[] bestXY = result != null ? result : new int[2]; @@ -1304,7 +1295,7 @@ public class CellLayout extends ViewGroup { for (int x = 0; x < countX - (minSpanX - 1); x++) { int ySize = -1; int xSize = -1; - if (ignoreOccupied) { + if (!ignoreOccupied) { // First, let's see if this thing fits anywhere for (int i = 0; i < minSpanX; i++) { for (int j = 0; j < minSpanY; j++) { @@ -1368,7 +1359,7 @@ public class CellLayout extends ViewGroup { } } validRegions.push(currentRect); - double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY); + double distance = Math.hypot(cellXY[0] - relativeXPos, cellXY[1] - relativeYPos); if ((distance <= bestDistance && !contained) || currentRect.contains(bestRect)) { @@ -2629,7 +2620,7 @@ public class CellLayout extends ViewGroup { * nearest the requested location. */ public int[] findNearestArea(int pixelX, int pixelY, int spanX, int spanY, int[] result) { - return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, false, result, null); + return findNearestArea(pixelX, pixelY, spanX, spanY, spanX, spanY, true, result, null); } boolean existsEmptyCell() {