Skip to content

Commit

Permalink
Add MapHudCard
Browse files Browse the repository at this point in the history
  • Loading branch information
Chumva committed Dec 16, 2024
1 parent bd3e864 commit 164f291
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 22 deletions.
13 changes: 13 additions & 0 deletions OsmAnd/res/layout/map_hud_card.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map_hud_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<net.osmand.plus.views.controls.MapHudLayout
android:id="@+id/map_hud_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/default_buttons_hud_preview" />

</LinearLayout>
1 change: 1 addition & 0 deletions OsmAnd/res/values/sizes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -478,5 +478,6 @@
<dimen name="bottom_space_height">160dp</dimen>
<dimen name="external_devices_empty_content_centre_margin">126dp</dimen>
<dimen name="map_button_appearance_image_height">130dp</dimen>
<dimen name="default_buttons_hud_preview">290dp</dimen>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static android.graphics.drawable.GradientDrawable.RECTANGLE;
import static android.widget.ImageView.ScaleType.CENTER;
import static net.osmand.plus.quickaction.ButtonAppearanceParams.BIG_SIZE_DP;
import static net.osmand.plus.quickaction.ButtonAppearanceParams.ORIGINAL_VALUE;
import static net.osmand.plus.quickaction.ButtonAppearanceParams.ROUND_RADIUS_DP;
import static net.osmand.plus.quickaction.ButtonAppearanceParams.TRANSPARENT_ALPHA;
import static net.osmand.plus.views.layers.ContextMenuLayer.VIBRATE_SHORT;
Expand Down Expand Up @@ -101,11 +102,13 @@ public MapButton(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}

public MapButton(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
public MapButton(@NonNull Context context, @Nullable AttributeSet attrs,
@AttrRes int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}

public MapButton(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
public MapButton(@NonNull Context context, @Nullable AttributeSet attrs,
@AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);

this.app = (OsmandApplication) context.getApplicationContext();
Expand All @@ -121,7 +124,7 @@ public MapButton(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRe

@NonNull
protected ImageView setupImageView(@NonNull Context context, @Nullable AttributeSet attrs,
@AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
@AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
int imageSize = (int) getImageSize();
ImageView imageView = new ImageView(context, attrs, defStyleAttr, defStyleRes);
imageView.setClickable(false);
Expand Down Expand Up @@ -181,7 +184,13 @@ public ButtonAppearanceParams getAppearanceParams() {
public ButtonPositionSize getDefaultPositionSize() {
MapButtonState buttonState = getButtonState();
if (buttonState != null) {
return buttonState.getDefaultPositionSize();
ButtonPositionSize position = buttonState.getDefaultPositionSize();

int size = getSize();
size = (size / 8) + 1;
position.setSize(size, size);

return position;
}
return null;
}
Expand Down Expand Up @@ -253,7 +262,8 @@ protected void setIconColor(@ColorInt int iconColor) {
}
}

protected void setBackgroundColors(@ColorInt int backgroundColor, @ColorInt int backgroundPressedColor) {
protected void setBackgroundColors(@ColorInt int backgroundColor,
@ColorInt int backgroundPressedColor) {
if (this.backgroundColor != backgroundColor || this.backgroundPressedColor != backgroundPressedColor) {
this.backgroundColor = backgroundColor;
this.backgroundPressedColor = backgroundPressedColor;
Expand All @@ -269,8 +279,8 @@ public void update() {
updateColors(nightMode);

ButtonAppearanceParams params = getAppearanceParams();
if (invalidated || !Algorithms.objectEquals(appearanceParams, params) || customAppearanceParams != null) {
this.appearanceParams = customAppearanceParams != null ? customAppearanceParams : params;
if (invalidated || !Algorithms.objectEquals(appearanceParams, params)) {
this.appearanceParams = params;
this.invalidated = false;
updateContent();
}
Expand All @@ -290,7 +300,10 @@ protected void updateContent() {
}

protected void updateIcon() {
String iconName = appearanceParams.getIconName();
String iconName = customAppearanceParams != null ? customAppearanceParams.getIconName() : null;
if (Algorithms.isEmpty(iconName)) {
iconName = appearanceParams.getIconName();
}
int iconId = AndroidUtils.getDrawableId(app, iconName);
if (iconId == 0) {
iconId = RenderingIcons.getBigIconResourceId(iconName);
Expand Down Expand Up @@ -321,8 +334,8 @@ protected void updateSize(@NonNull View view, int size) {

protected void updateBackground() {
Context context = getContext();
float opacity = appearanceParams.getOpacity();
int cornerRadius = AndroidUtils.dpToPx(context, appearanceParams.getCornerRadius());
float opacity = getOpacity();
int cornerRadius = AndroidUtils.dpToPx(context, getCornerRadius());

GradientDrawable normal = new GradientDrawable();
normal.setShape(RECTANGLE);
Expand All @@ -342,7 +355,7 @@ protected void updateBackground() {
}

protected void updateShadow() {
int radius = AndroidUtils.dpToPx(getContext(), appearanceParams.getCornerRadius());
int radius = AndroidUtils.dpToPx(getContext(), getCornerRadius());
float[] outerRadius = new float[] {radius, radius, radius, radius, radius, radius, radius, radius};

ShapeDrawable drawable = new ShapeDrawable();
Expand All @@ -358,7 +371,7 @@ protected void updateShadow() {
protected void onDraw(@NotNull Canvas canvas) {
super.onDraw(canvas);

if (!nightMode && appearanceParams != null) {
if (!nightMode && (appearanceParams != null || customAppearanceParams != null)) {
drawShadow(canvas);
}
}
Expand All @@ -369,7 +382,7 @@ protected void drawShadow(@NotNull Canvas canvas) {
int width = getWidth();
int height = getHeight();
int padding = (int) shadowPadding;
int radius = AndroidUtils.dpToPx(getContext(), appearanceParams.getCornerRadius());
int radius = AndroidUtils.dpToPx(getContext(), getCornerRadius());

clipPath.reset();
clipPath.addRoundRect(padding, padding, width - padding, height - padding, radius, radius, Direction.CW);
Expand Down Expand Up @@ -420,8 +433,7 @@ public float getFrameSize() {
}

public float getImageSize() {
ButtonAppearanceParams params = appearanceParams != null ? appearanceParams : getAppearanceParams();
return AndroidUtils.dpToPxF(getContext(), params.getSize());
return AndroidUtils.dpToPxF(getContext(), getSize());
}

@NonNull
Expand Down Expand Up @@ -454,4 +466,43 @@ protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
visibilityListener.onVisibilityChanged(changedView, visibility);
}
}

public int getSize() {
if (customAppearanceParams != null) {
int size = customAppearanceParams.getSize();
if (size == ORIGINAL_VALUE) {
MapButtonState buttonState = getButtonState();
return buttonState != null ? buttonState.getDefaultSize() : createDefaultAppearanceParams().getSize();
}
return size;
}
ButtonAppearanceParams params = appearanceParams != null ? appearanceParams : getAppearanceParams();
return params.getSize();
}

public float getOpacity() {
if (customAppearanceParams != null) {
float opacity = customAppearanceParams.getOpacity();
if (opacity == ORIGINAL_VALUE) {
MapButtonState buttonState = getButtonState();
return buttonState != null ? buttonState.getDefaultOpacity() : createDefaultAppearanceParams().getOpacity();
}
return opacity;
}
ButtonAppearanceParams params = appearanceParams != null ? appearanceParams : getAppearanceParams();
return params.getOpacity();
}

public int getCornerRadius() {
if (customAppearanceParams != null) {
int cornerRadius = customAppearanceParams.getCornerRadius();
if (cornerRadius == ORIGINAL_VALUE) {
MapButtonState buttonState = getButtonState();
return buttonState != null ? buttonState.getDefaultCornerRadius() : createDefaultAppearanceParams().getCornerRadius();
}
return cornerRadius;
}
ButtonAppearanceParams params = appearanceParams != null ? appearanceParams : getAppearanceParams();
return params.getCornerRadius();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ protected boolean shouldShow() {

protected void updateBackground() {
Context context = getContext();
int cornerRadius = AndroidUtils.dpToPx(context, appearanceParams.getCornerRadius());
int cornerRadius = AndroidUtils.dpToPx(context, getCornerRadius());

GradientDrawable normal = new GradientDrawable();
normal.setShape(RECTANGLE);
normal.setColor(ColorUtilities.getColorWithAlpha(backgroundColor, appearanceParams.getOpacity()));
normal.setColor(ColorUtilities.getColorWithAlpha(backgroundColor, getOpacity()));
normal.setCornerRadius(cornerRadius);

GradientDrawable pressed = new GradientDrawable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class DefaultButtonsAppearanceFragment extends BaseOsmAndFragment impleme
private ButtonAppearanceParams appearanceParams;
private ButtonAppearanceParams originalAppearanceParams;

private MapHudCard mapHudCard;
private List<BaseCard> cards;
private DialogButton applyButton;

Expand Down Expand Up @@ -128,6 +129,8 @@ private void setupCards(@NonNull View view) {
container.removeAllViews();

MapActivity activity = requireMapActivity();
mapHudCard = new MapHudCard(activity, appearanceParams);
addCard(container, mapHudCard);
addCard(container, new CornerRadiusCard(activity, appearanceParams, true));
container.addView(themedInflater.inflate(R.layout.list_item_divider, container, false));
addCard(container, new ButtonSizeCard(activity, appearanceParams, true));
Expand Down Expand Up @@ -211,6 +214,10 @@ private ButtonAppearanceParams createAppearanceParams() {
@Override
public void onCardPressed(@NonNull BaseCard card) {
updateButtons();

if (mapHudCard != null) {
mapHudCard.updateContent();
}
}

@Override
Expand Down Expand Up @@ -240,6 +247,15 @@ public void onPause() {
}
}

@Override
public void onDestroyView() {
super.onDestroyView();

if (mapHudCard != null) {
mapHudCard.clearWidgets();
}
}

@Nullable
public MapActivity getMapActivity() {
FragmentActivity activity = getActivity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public void updateButton(@NonNull ButtonAppearanceParams appearanceParams) {
if (mapButton != null) {
customAppearanceParams = appearanceParams;
mapButton.setCustomAppearanceParams(appearanceParams);
mapButton.setInvalidated(true);
mapButton.update();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,20 @@ public String getId() {

@NonNull
public ButtonAppearanceParams createDefaultAppearanceParams() {
return new ButtonAppearanceParams(getDefaultIconName(), getDefaultSize(), getDefaultOpacity(), getDefaultCornerRadius());
MapButtonsHelper buttonsHelper = app.getMapButtonsHelper();
int size = buttonsHelper.getDefaultSizePref().get();
if (size <= 0) {
size = getDefaultSize();
}
float opacity = buttonsHelper.getDefaultOpacityPref().get();
if (opacity < 0) {
opacity = getDefaultOpacity();
}
int cornerRadius = buttonsHelper.getDefaultCornerRadiusPref().get();
if (cornerRadius < 0) {
cornerRadius = getDefaultCornerRadius();
}
return new ButtonAppearanceParams(getDefaultIconName(), size, opacity, cornerRadius);
}

@LayoutRes
Expand Down Expand Up @@ -211,10 +224,7 @@ private void updatePosition(@NonNull ButtonPositionSize position) {
if (value != null && value > 0) {
position.fromLongValue(value);
}
int size = sizePref.get();
if (size <= 0) {
size = getDefaultSize();
}
int size = createAppearanceParams().getSize();
size = (size / 8) + 1;
position.setSize(size, size);
}
Expand Down
Loading

0 comments on commit 164f291

Please sign in to comment.