From b506f9e4925b5290664adc15d4d67089a4f24122 Mon Sep 17 00:00:00 2001 From: chumv Date: Wed, 16 Oct 2024 22:39:54 +0300 Subject: [PATCH] Test new buttons grid algorithm --- .../plus/views/controls/MapHudLayout.java | 100 +++++++++++++++--- 1 file changed, 88 insertions(+), 12 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/views/controls/MapHudLayout.java b/OsmAnd/src/net/osmand/plus/views/controls/MapHudLayout.java index 7202ddc85b0..663df0b8ff5 100644 --- a/OsmAnd/src/net/osmand/plus/views/controls/MapHudLayout.java +++ b/OsmAnd/src/net/osmand/plus/views/controls/MapHudLayout.java @@ -1,6 +1,9 @@ package net.osmand.plus.views.controls; import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; import android.graphics.Rect; import android.util.AttributeSet; import android.view.View; @@ -10,6 +13,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import net.osmand.PlatformUtil; import net.osmand.data.QuadRect; import net.osmand.data.QuadTree; import net.osmand.plus.R; @@ -17,10 +21,18 @@ import net.osmand.plus.views.controls.maphudbuttons.MapButton; import net.osmand.plus.views.layers.base.OsmandMapLayer; +import org.apache.commons.logging.Log; +import org.jetbrains.annotations.NotNull; + +import java.util.Locale; + public class MapHudLayout extends FrameLayout { + private static final Log LOG = PlatformUtil.getLog(MapHudLayout.class); + private static final double[][] DIRECTIONS = getAvailableDirections(); + private final Paint gridPaint; private final int gridSize; public MapHudLayout(@NonNull Context context) { @@ -38,7 +50,12 @@ public MapHudLayout(@NonNull Context context, @Nullable AttributeSet attrs, int public MapHudLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); - this.gridSize = AndroidUtils.dpToPx(context, 8); + this.gridSize = AndroidUtils.dpToPx(context, 48); + + this.gridPaint = new Paint(); + gridPaint.setColor(Color.BLACK); + gridPaint.setStrokeWidth(1f); + setWillNotDraw(false); } public void updateButtons() { @@ -55,20 +72,81 @@ public void updateButton(@NonNull MapButton button, boolean save) { QuadTree intersections = initBoundIntersections(button); LayoutParams params = (LayoutParams) button.getLayoutParams(); - if (OsmandMapLayer.intersects(intersections, currentBounds, false)) { - params = updateButtonPosition(button, intersections); - } - if (!isSnappedToGrid(params.rightMargin) || !isSnappedToGrid(params.bottomMargin)) { - params.rightMargin = snapToGrid(params.rightMargin); - params.bottomMargin = snapToGrid(params.bottomMargin); +// if (OsmandMapLayer.intersects(intersections, currentBounds, false)) { +// params = updateButtonPosition(button, intersections); +// } +// if (!isSnappedToGrid(params.rightMargin) || !isSnappedToGrid(params.bottomMargin)) { +// params.rightMargin = snapToGrid(params.rightMargin); +// params.bottomMargin = snapToGrid(params.bottomMargin); +// } + + int width = button.getMeasuredWidth(); + int height = button.getMeasuredHeight(); + + if (width > 0 && height > 0) { + int[] a = transformCoordinates(params.rightMargin, params.bottomMargin, width, height, gridSize); + if ((params.rightMargin != a[0]) || (params.bottomMargin != a[1])) { + params.rightMargin = a[0]; + params.bottomMargin = a[1]; + button.setLayoutParams(params); + } } - button.setLayoutParams(params); if (save) { button.saveMargins(); } } + public static int[] transformCoordinates(int x, int y, int w, int h, int cellSize) { + int nx = roundCoordinate(x, w, cellSize); + int ny = roundCoordinate(y, h, cellSize); + System.out.printf("%d %d (%d, %d)-> %d %d\n", x, y, w, h, nx, ny); + LOG.debug(String.format(Locale.US, "%d %d (%d, %d)-> %d %d\n", x, y, w, h, nx, ny)); + + int[] a = new int[2]; + a[0] = nx; + a[1] = ny; + + return a; + } + + public static int roundCoordinate(int coord, int screenSize, int cellSize) { + int start = coord - coord % cellSize; + if (2 * start < screenSize) { + return start; + } + int end = (screenSize - coord); + end = end - end % cellSize; + int ret = screenSize - end - cellSize; + if (ret * 2 < screenSize) { + ret += cellSize; + } + return ret; + } + + @Override + protected void onDraw(@NotNull Canvas canvas) { + super.onDraw(canvas); +// drawGridLines(canvas); + } + + private void drawGridLines(Canvas canvas) { + int width = getWidth(); + int height = getHeight(); + + // Draw vertical grid lines + for (int x = 0; x < width; x += gridSize) { + int nx = roundCoordinate(x, width, gridSize); // Round to nearest grid point + canvas.drawLine(nx, 0, nx, height, gridPaint); // Draw vertical line + } + + // Draw horizontal grid lines + for (int y = 0; y < height; y += gridSize) { + int ny = roundCoordinate(y, height, gridSize); // Round to nearest grid point + canvas.drawLine(0, ny, width, ny, gridPaint); // Draw horizontal line + } + } + @NonNull private LayoutParams updateButtonPosition(@NonNull MapButton button, @NonNull QuadTree intersections) { int width = getMeasuredWidth(); @@ -111,8 +189,7 @@ private QuadRect getRect(@NonNull View view) { Rect rect = AndroidUtils.getViewBoundOnWindow(view); if (view instanceof MapButton button) { int radius = button.getShadowRadius(); - return new QuadRect(rect.left - radius, rect.top - radius, - rect.right + radius, rect.bottom + radius); + return new QuadRect(rect.left - radius, rect.top - radius, rect.right + radius, rect.bottom + radius); } else { return new QuadRect(rect.left, rect.top, rect.right, rect.bottom); } @@ -127,8 +204,7 @@ private QuadTree initBoundIntersections(@NonNull MapButton button) { return intersections; } - private void initBoundIntersections(@NonNull ViewGroup parent, @NonNull MapButton button, - @NonNull QuadTree intersections, boolean recursive) { + private void initBoundIntersections(@NonNull ViewGroup parent, @NonNull MapButton button, @NonNull QuadTree intersections, boolean recursive) { for (int i = 0; i < parent.getChildCount(); i++) { View child = parent.getChildAt(i); if (child != button && child.getVisibility() == VISIBLE) {