Skip to content

Commit

Permalink
Test new buttons grid algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Chumva committed Oct 16, 2024
1 parent 1a393ff commit b506f9e
Showing 1 changed file with 88 additions and 12 deletions.
100 changes: 88 additions & 12 deletions OsmAnd/src/net/osmand/plus/views/controls/MapHudLayout.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,17 +13,26 @@
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;
import net.osmand.plus.utils.AndroidUtils;
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) {
Expand All @@ -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() {
Expand All @@ -55,20 +72,81 @@ public void updateButton(@NonNull MapButton button, boolean save) {
QuadTree<QuadRect> 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<QuadRect> intersections) {
int width = getMeasuredWidth();
Expand Down Expand Up @@ -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);
}
Expand All @@ -127,8 +204,7 @@ private QuadTree<QuadRect> initBoundIntersections(@NonNull MapButton button) {
return intersections;
}

private void initBoundIntersections(@NonNull ViewGroup parent, @NonNull MapButton button,
@NonNull QuadTree<QuadRect> intersections, boolean recursive) {
private void initBoundIntersections(@NonNull ViewGroup parent, @NonNull MapButton button, @NonNull QuadTree<QuadRect> intersections, boolean recursive) {
for (int i = 0; i < parent.getChildCount(); i++) {
View child = parent.getChildAt(i);
if (child != button && child.getVisibility() == VISIBLE) {
Expand Down

0 comments on commit b506f9e

Please sign in to comment.