Switch button size to dp to allow better scalability across resolutions

This commit is contained in:
SerpentSpirale 2021-05-11 19:46:50 +02:00
parent e67ffe77d0
commit 2ed45d4b17
6 changed files with 61 additions and 43 deletions

View File

@ -86,7 +86,7 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp
properties.specialButtonListener.getClass().getName());
}
setLayoutParams(new FrameLayout.LayoutParams((int) properties.width, (int) properties.height));
setLayoutParams(new FrameLayout.LayoutParams((int) properties.getWidth(), (int) properties.getHeight() ));
}
public void setBackground(){
@ -99,12 +99,12 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp
}
public int computeStrokeWidth(float widthInPercent){
float maxSize = Math.max(mProperties.width, mProperties.height);
float maxSize = Math.max(mProperties.getWidth(), mProperties.getHeight());
return (int)((maxSize/2) * (widthInPercent/100));
}
public float computeCornerRadius(float radiusInPercent){
float minSize = Math.min(mProperties.width, mProperties.height);
float minSize = Math.min(mProperties.getWidth(), mProperties.getHeight());
return (minSize/2) * (radiusInPercent/100);
}
@ -112,8 +112,8 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp
public void setLayoutParams(ViewGroup.LayoutParams params) {
super.setLayoutParams(params);
mProperties.width = params.width;
mProperties.height = params.height;
mProperties.setWidth(params.width);
mProperties.setHeight(params.height);
setBackground();

View File

@ -1,6 +1,5 @@
package net.kdt.pojavlaunch.customcontrols;
import android.graphics.Color;
import android.util.*;
import java.util.*;
import net.kdt.pojavlaunch.*;
@ -70,14 +69,14 @@ public class ControlData implements Cloneable
public String name;
public float x;
public float y;
public float width;
public float height;
public int[] keycodes; //Should store up to 4 keys
public float opacity; //Alpha value from 0 to 1;
private float width; //Dp instead of Px now
private float height; //Dp instead of Px now
public int[] keycodes; //Should store up to 4 keys
public float opacity; //Alpha value from 0 to 1;
public int bgColor;
public int strokeColor;
public int strokeWidth;
public float cornerRadius;
public int strokeWidth; //0-100%
public float cornerRadius; //0-100%
public boolean holdCtrl;
public boolean holdAlt;
@ -93,7 +92,7 @@ public class ControlData implements Cloneable
}
public ControlData(String name, int[] keycodes, float x, float y) {
this(name, keycodes, x, y, Tools.dpToPx(50), Tools.dpToPx(50));
this(name, keycodes, x, y, 50, 50);
}
public ControlData(android.content.Context ctx, int resId, int[] keycodes, float x, float y, boolean isSquare) {
@ -101,7 +100,7 @@ public class ControlData implements Cloneable
}
public ControlData(String name, int[] keycodes, float x, float y, boolean isSquare) {
this(name, keycodes, x, y, isSquare ? Tools.dpToPx(50) : Tools.dpToPx(80), isSquare ? Tools.dpToPx(50) : Tools.dpToPx(30));
this(name, keycodes, x, y, isSquare ? 50 : 80, isSquare ? 50 : 30);
}
public ControlData(String name, int[] keycodes, float x, float y, float width, float height) {
@ -110,7 +109,7 @@ public class ControlData implements Cloneable
}
public ControlData(String name, int[] keycodes, String dynamicX, String dynamicY) {
this(name, keycodes, dynamicX, dynamicY, Tools.dpToPx(50), Tools.dpToPx(50), false);
this(name, keycodes, dynamicX, dynamicY, 50, 50, false);
}
public ControlData(android.content.Context ctx, int resId, int[] keycodes, String dynamicX, String dynamicY, boolean isSquare) {
@ -118,11 +117,11 @@ public class ControlData implements Cloneable
}
public ControlData(String name, int[] keycodes, String dynamicX, String dynamicY, boolean isSquare) {
this(name, keycodes, dynamicX, dynamicY, isSquare ? Tools.dpToPx(50) : Tools.dpToPx(80), isSquare ? Tools.dpToPx(50) : Tools.dpToPx(30), false);
this(name, keycodes, dynamicX, dynamicY, isSquare ? 50 : 80, isSquare ? 50 : 30, false);
}
public ControlData(String name, int[] keycodes, String dynamicX, String dynamicY, float width, float height, boolean isToggle){
this(name, keycodes, dynamicX, dynamicY, width, height, isToggle, 1,0x4D000000, 0xFFFFFFFF,0,Tools.dpToPx(0));
this(name, keycodes, dynamicX, dynamicY, width, height, isToggle, 1,0x4D000000, 0xFFFFFFFF,0,0);
}
public ControlData(String name, int[] keycodes, String dynamicX, String dynamicY, float width, float height, boolean isToggle, float opacity, int bgColor, int strokeColor, int strokeWidth, float cornerRadius) {
@ -161,10 +160,10 @@ public class ControlData implements Cloneable
Map<String, String> keyValueMap = new ArrayMap<>();
keyValueMap.put("top", "0");
keyValueMap.put("left", "0");
keyValueMap.put("right", Float.toString(CallbackBridge.physicalWidth - width));
keyValueMap.put("bottom", Float.toString(CallbackBridge.physicalHeight - height));
keyValueMap.put("width", Float.toString(width));
keyValueMap.put("height", Float.toString(height));
keyValueMap.put("right", Float.toString(CallbackBridge.physicalWidth - getWidth()));
keyValueMap.put("bottom", Float.toString(CallbackBridge.physicalHeight - getHeight()));
keyValueMap.put("width", Float.toString(getWidth()));
keyValueMap.put("height", Float.toString(getHeight()));
keyValueMap.put("screen_width", Integer.toString(CallbackBridge.physicalWidth));
keyValueMap.put("screen_height", Integer.toString(CallbackBridge.physicalHeight));
keyValueMap.put("margin", Integer.toString((int) Tools.dpToPx(2)));
@ -213,4 +212,23 @@ public class ControlData implements Cloneable
}
return inflatedArray;
}
//Getters || setters (with conversion for ease of use)
public float getWidth() {
return Tools.dpToPx(width);
}
public float getHeight(){
return Tools.dpToPx(height);
}
public void setWidth(float widthInPx){
width = Tools.pxToDp(widthInPx);
}
public void setHeight(float heightInPx){
height = Tools.pxToDp(heightInPx);
}
}

View File

@ -42,8 +42,8 @@ public class ControlDrawer extends ControlButton {
private ControlData filterProperties(ControlData properties){
properties.isDynamicBtn = false;
properties.width = drawerData.properties.width;
properties.height = drawerData.properties.height;
properties.setWidth(drawerData.properties.getWidth());
properties.setHeight(drawerData.properties.getHeight());
return properties;
}
@ -97,22 +97,22 @@ public class ControlDrawer extends ControlButton {
for(int i=0; i < buttons.size(); ++i){
switch (drawerData.orientation){
case RIGHT:
buttons.get(i).setTranslationX( (drawerData.properties.x + drawerData.properties.width) + drawerData.properties.width*i );
buttons.get(i).setTranslationX( (drawerData.properties.x + drawerData.properties.getWidth()) + drawerData.properties.getWidth()*i );
buttons.get(i).setTranslationY(drawerData.properties.y);
break;
case LEFT:
buttons.get(i).setTranslationX( (drawerData.properties.x - drawerData.properties.width) - drawerData.properties.width*i );
buttons.get(i).setTranslationX( (drawerData.properties.x - drawerData.properties.getWidth()) - drawerData.properties.getWidth()*i );
buttons.get(i).setTranslationY(drawerData.properties.y);
break;
case UP:
buttons.get(i).setTranslationY( (drawerData.properties.y - drawerData.properties.height) - drawerData.properties.height*i );
buttons.get(i).setTranslationY( (drawerData.properties.y - drawerData.properties.getHeight()) - drawerData.properties.getHeight()*i );
buttons.get(i).setTranslationX(drawerData.properties.x);
break;
case DOWN:
buttons.get(i).setTranslationY( (drawerData.properties.y + drawerData.properties.height) + drawerData.properties.height*i );
buttons.get(i).setTranslationY( (drawerData.properties.y + drawerData.properties.getHeight()) + drawerData.properties.getHeight()*i );
buttons.get(i).setTranslationX(drawerData.properties.x);
break;
}
@ -124,8 +124,8 @@ public class ControlDrawer extends ControlButton {
private void resizeButtons(){
if (buttons == null) return;
for(ControlSubButton subButton : buttons){
subButton.mProperties.width = mProperties.width;
subButton.mProperties.height = mProperties.height;
subButton.mProperties.setWidth(mProperties.getWidth());
subButton.mProperties.setHeight(mProperties.getHeight());
subButton.updateProperties();
}

View File

@ -60,8 +60,8 @@ public class ControlLayout extends FrameLayout
//CONTROL BUTTON
for (ControlData button : controlLayout.mControlDataList) {
button.isHideable = button.keycodes[0] != ControlData.SPECIALBTN_TOGGLECTRL && button.keycodes[0] != ControlData.SPECIALBTN_VIRTUALMOUSE;
button.width = button.width / controlLayout.scaledAt * LauncherPreferences.PREF_BUTTONSIZE;
button.height = button.height / controlLayout.scaledAt * LauncherPreferences.PREF_BUTTONSIZE;
button.setWidth(button.getWidth() / controlLayout.scaledAt * LauncherPreferences.PREF_BUTTONSIZE);
button.setHeight(button.getHeight() / controlLayout.scaledAt * LauncherPreferences.PREF_BUTTONSIZE);
if (!button.isDynamicBtn) {
button.dynamicX = button.x / CallbackBridge.physicalWidth + " * ${screen_width}";
button.dynamicY = button.y / CallbackBridge.physicalHeight + " * ${screen_height}";
@ -73,8 +73,8 @@ public class ControlLayout extends FrameLayout
//CONTROL DRAWER
for(ControlDrawerData drawerData : controlLayout.mDrawerDataList){
drawerData.properties.isHideable = true;
drawerData.properties.width = drawerData.properties.width / controlLayout.scaledAt * LauncherPreferences.PREF_BUTTONSIZE;
drawerData.properties.height = drawerData.properties.height / controlLayout.scaledAt * LauncherPreferences.PREF_BUTTONSIZE;
drawerData.properties.setWidth(drawerData.properties.getWidth() / controlLayout.scaledAt * LauncherPreferences.PREF_BUTTONSIZE);
drawerData.properties.setHeight(drawerData.properties.getHeight() / controlLayout.scaledAt * LauncherPreferences.PREF_BUTTONSIZE);
if (!drawerData.properties.isDynamicBtn) {
drawerData.properties.dynamicX = drawerData.properties.x / CallbackBridge.physicalWidth + " * ${screen_width}";
drawerData.properties.dynamicY = drawerData.properties.y / CallbackBridge.physicalHeight + " * ${screen_height}";
@ -85,8 +85,8 @@ public class ControlLayout extends FrameLayout
//CONTROL SUB BUTTON
for (ControlData subButton : drawerData.buttonProperties){
subButton.isHideable = subButton.keycodes[0] != ControlData.SPECIALBTN_TOGGLECTRL && subButton.keycodes[0] != ControlData.SPECIALBTN_VIRTUALMOUSE;
subButton.width = subButton.width / controlLayout.scaledAt * LauncherPreferences.PREF_BUTTONSIZE;
subButton.height = subButton.height / controlLayout.scaledAt * LauncherPreferences.PREF_BUTTONSIZE;
subButton.setWidth(subButton.getWidth() / controlLayout.scaledAt * LauncherPreferences.PREF_BUTTONSIZE);
subButton.setHeight(subButton.getHeight() / controlLayout.scaledAt * LauncherPreferences.PREF_BUTTONSIZE);
if (!subButton.isDynamicBtn) {
subButton.dynamicX = subButton.x / CallbackBridge.physicalWidth + " * ${screen_width}";
subButton.dynamicY = subButton.y / CallbackBridge.physicalHeight + " * ${screen_height}";

View File

@ -19,8 +19,8 @@ public class ControlSubButton extends ControlButton {
}
private void filterProperties(){
mProperties.height = parentDrawer.getProperties().height;
mProperties.width = parentDrawer.getProperties().width;
mProperties.setHeight(parentDrawer.getProperties().getHeight());
mProperties.setWidth(parentDrawer.getProperties().getWidth());
mProperties.isDynamicBtn = false;
setProperties(mProperties, false);
@ -32,8 +32,8 @@ public class ControlSubButton extends ControlButton {
@Override
public void setLayoutParams(ViewGroup.LayoutParams params) {
if(parentDrawer != null){
params.width = (int)parentDrawer.mProperties.width;
params.height = (int)parentDrawer.mProperties.height;
params.width = (int)parentDrawer.mProperties.getWidth();
params.height = (int)parentDrawer.mProperties.getHeight();
}
super.setLayoutParams(params);

View File

@ -224,8 +224,8 @@ public class EditControlButtonPopup {
checkToggle.setChecked(properties.isToggle);
checkPassThrough.setChecked(properties.passThruEnabled);
editWidth.setText(Float.toString(properties.width));
editHeight.setText(Float.toString(properties.height));
editWidth.setText(Float.toString(properties.getWidth()));
editHeight.setText(Float.toString(properties.getHeight()));
editDynamicX.setEnabled(properties.isDynamicBtn);
editDynamicY.setEnabled(properties.isDynamicBtn);
@ -306,8 +306,8 @@ public class EditControlButtonPopup {
properties.isToggle = checkToggle.isChecked();
properties.passThruEnabled = checkPassThrough.isChecked();
properties.width = Float.parseFloat(editWidth.getText().toString());
properties.height = Float.parseFloat(editHeight.getText().toString());
properties.setWidth(Float.parseFloat(editWidth.getText().toString()));
properties.setHeight(Float.parseFloat(editHeight.getText().toString()));
properties.isDynamicBtn = checkDynamicPosition.isChecked();
properties.dynamicX = editDynamicX.getText().toString().isEmpty() ? properties.dynamicX = Float.toString(properties.x) : editDynamicX.getText().toString();