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()); 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(){ public void setBackground(){
@ -99,12 +99,12 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp
} }
public int computeStrokeWidth(float widthInPercent){ 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)); return (int)((maxSize/2) * (widthInPercent/100));
} }
public float computeCornerRadius(float radiusInPercent){ 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); return (minSize/2) * (radiusInPercent/100);
} }
@ -112,8 +112,8 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp
public void setLayoutParams(ViewGroup.LayoutParams params) { public void setLayoutParams(ViewGroup.LayoutParams params) {
super.setLayoutParams(params); super.setLayoutParams(params);
mProperties.width = params.width; mProperties.setWidth(params.width);
mProperties.height = params.height; mProperties.setHeight(params.height);
setBackground(); setBackground();

View File

@ -1,6 +1,5 @@
package net.kdt.pojavlaunch.customcontrols; package net.kdt.pojavlaunch.customcontrols;
import android.graphics.Color;
import android.util.*; import android.util.*;
import java.util.*; import java.util.*;
import net.kdt.pojavlaunch.*; import net.kdt.pojavlaunch.*;
@ -70,14 +69,14 @@ public class ControlData implements Cloneable
public String name; public String name;
public float x; public float x;
public float y; public float y;
public float width; private float width; //Dp instead of Px now
public float height; private float height; //Dp instead of Px now
public int[] keycodes; //Should store up to 4 keys public int[] keycodes; //Should store up to 4 keys
public float opacity; //Alpha value from 0 to 1; public float opacity; //Alpha value from 0 to 1;
public int bgColor; public int bgColor;
public int strokeColor; public int strokeColor;
public int strokeWidth; public int strokeWidth; //0-100%
public float cornerRadius; public float cornerRadius; //0-100%
public boolean holdCtrl; public boolean holdCtrl;
public boolean holdAlt; public boolean holdAlt;
@ -93,7 +92,7 @@ public class ControlData implements Cloneable
} }
public ControlData(String name, int[] keycodes, float x, float y) { 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) { 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) { 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) { 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) { 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) { 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) { 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){ 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) { 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<>(); Map<String, String> keyValueMap = new ArrayMap<>();
keyValueMap.put("top", "0"); keyValueMap.put("top", "0");
keyValueMap.put("left", "0"); keyValueMap.put("left", "0");
keyValueMap.put("right", Float.toString(CallbackBridge.physicalWidth - width)); keyValueMap.put("right", Float.toString(CallbackBridge.physicalWidth - getWidth()));
keyValueMap.put("bottom", Float.toString(CallbackBridge.physicalHeight - height)); keyValueMap.put("bottom", Float.toString(CallbackBridge.physicalHeight - getHeight()));
keyValueMap.put("width", Float.toString(width)); keyValueMap.put("width", Float.toString(getWidth()));
keyValueMap.put("height", Float.toString(height)); keyValueMap.put("height", Float.toString(getHeight()));
keyValueMap.put("screen_width", Integer.toString(CallbackBridge.physicalWidth)); keyValueMap.put("screen_width", Integer.toString(CallbackBridge.physicalWidth));
keyValueMap.put("screen_height", Integer.toString(CallbackBridge.physicalHeight)); keyValueMap.put("screen_height", Integer.toString(CallbackBridge.physicalHeight));
keyValueMap.put("margin", Integer.toString((int) Tools.dpToPx(2))); keyValueMap.put("margin", Integer.toString((int) Tools.dpToPx(2)));
@ -213,4 +212,23 @@ public class ControlData implements Cloneable
} }
return inflatedArray; 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){ private ControlData filterProperties(ControlData properties){
properties.isDynamicBtn = false; properties.isDynamicBtn = false;
properties.width = drawerData.properties.width; properties.setWidth(drawerData.properties.getWidth());
properties.height = drawerData.properties.height; properties.setHeight(drawerData.properties.getHeight());
return properties; return properties;
} }
@ -97,22 +97,22 @@ public class ControlDrawer extends ControlButton {
for(int i=0; i < buttons.size(); ++i){ for(int i=0; i < buttons.size(); ++i){
switch (drawerData.orientation){ switch (drawerData.orientation){
case RIGHT: 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); buttons.get(i).setTranslationY(drawerData.properties.y);
break; break;
case LEFT: 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); buttons.get(i).setTranslationY(drawerData.properties.y);
break; break;
case UP: 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); buttons.get(i).setTranslationX(drawerData.properties.x);
break; break;
case DOWN: 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); buttons.get(i).setTranslationX(drawerData.properties.x);
break; break;
} }
@ -124,8 +124,8 @@ public class ControlDrawer extends ControlButton {
private void resizeButtons(){ private void resizeButtons(){
if (buttons == null) return; if (buttons == null) return;
for(ControlSubButton subButton : buttons){ for(ControlSubButton subButton : buttons){
subButton.mProperties.width = mProperties.width; subButton.mProperties.setWidth(mProperties.getWidth());
subButton.mProperties.height = mProperties.height; subButton.mProperties.setHeight(mProperties.getHeight());
subButton.updateProperties(); subButton.updateProperties();
} }

View File

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

View File

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

View File

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