Cleanup of buttons

This commit is contained in:
SerpentSpirale
2021-06-30 21:27:10 +02:00
parent 1fac5eb634
commit 01a710485c
4 changed files with 208 additions and 125 deletions

View File

@@ -31,11 +31,8 @@ public class ControlLayout extends FrameLayout
}
public void hideAllHandleViews() {
for (int i = 0; i < getChildCount(); i++) {
View view = getChildAt(i);
if (view instanceof ControlButton) {
((ControlButton) view).getHandleView().hide();
}
for(ControlButton button : getButtonChildren()){
button.getHandleView().hide();
}
}
@@ -44,9 +41,9 @@ public class ControlLayout extends FrameLayout
}
public void loadLayout(CustomControls controlLayout) {
if (mModifiable) {
if (mModifiable)
hideAllHandleViews();
}
removeAllButtons();
if(mLayout != null) {
mLayout.mControlDataList = null;
@@ -174,17 +171,10 @@ public class ControlLayout extends FrameLayout
}
private void removeAllButtons() {
List<View> viewList = new ArrayList<>();
View v;
for(int i = 0; i < getChildCount(); i++) {
v = getChildAt(i);
if(v instanceof ControlButton) viewList.add(v);
for(View v : getButtonChildren()){
removeView(v);
}
v = null;
for(View v2 : viewList) {
removeView(v2);
}
viewList = null;
System.gc();
//i wanna be sure that all the removed Views will be removed after a reload
//because if frames will slowly go down after many control changes it will be warm and bad
@@ -232,38 +222,17 @@ public class ControlLayout extends FrameLayout
if (mModifiable) return; // Not using on custom controls activity
mControlVisible = isVisible;
int visibilityState = isVisible ? View.VISIBLE : View.GONE;
for (int i = 0; i < getChildCount(); i++) {
View view = getChildAt(i);
if(view instanceof ControlSubButton){
view.setVisibility(isVisible ? (((ControlSubButton)view).parentDrawer.areButtonsVisible ? VISIBLE : GONE) : View.GONE);
continue;
}
if(view instanceof ControlDrawer){
view.setVisibility(visibilityState);
continue;
}
if (view instanceof ControlButton && ((ControlButton) view).getProperties().isHideable) {
view.setVisibility(visibilityState);
}
for(ControlButton button : getButtonChildren()){
button.setVisible(isVisible);
}
}
public void setModifiable(boolean z) {
mModifiable = z;
for (int i = 0; i < getChildCount(); i++) {
View v = getChildAt(i);
if (v instanceof ControlButton) {
ControlButton cv = ((ControlButton) v);
cv.setModifiable(z);
if (!z) {
cv.setAlpha(cv.getProperties().opacity);
}
}
public void setModifiable(boolean isModifiable) {
mModifiable = isModifiable;
for(ControlButton button : getButtonChildren()){
button.setModifiable(isModifiable);
if (!isModifiable)
button.setAlpha(button.getProperties().opacity);
}
}
@@ -273,5 +242,71 @@ public class ControlLayout extends FrameLayout
public void setModified(boolean isModified) {
if (mActivity != null) mActivity.isModified = isModified;
}
private ArrayList<ControlButton> getButtonChildren(){
ArrayList<ControlButton> children = new ArrayList<>();
for(int i=0; i<getChildCount(); ++i){
View v = getChildAt(i);
if(v instanceof ControlButton)
children.add(((ControlButton) v));
}
return children;
}
ControlButton lastControlButton = null;
@Override
public boolean onTouchEvent(MotionEvent ev) {
System.out.println("ON_TOUCH_LAYOUT");
if(ev.getActionMasked() == MotionEvent.ACTION_UP || ev.getActionMasked() == MotionEvent.ACTION_CANCEL){
if(lastControlButton != null) lastControlButton.onTouchEvent(ev);
lastControlButton = null;
return true;
}
if(ev.getActionMasked() != MotionEvent.ACTION_MOVE) return false;
ArrayList<ControlButton> children = getButtonChildren();
Log.d("getX LAYOUT_CONTROL", String.valueOf(ev.getX()));
Log.d("getY LAYOUT_CONTROL", String.valueOf(ev.getY()));
Log.d("getRawX LAYOUT_CONTROL", String.valueOf(ev.getRawX()));
Log.d("getRawY LAYOUT_CONTROL", String.valueOf(ev.getRawY()));
if(lastControlButton != null){
if( ev.getRawX() > lastControlButton.getX() && ev.getRawX() < lastControlButton.getX() + lastControlButton.getWidth() &&
ev.getRawY() > lastControlButton.getY() && ev.getRawY() < lastControlButton.getY() + lastControlButton.getHeight()){
return true;
}
}
for(ControlButton button : children){
/*
Log.d("Left: LAYOUT_CONTROL", String.valueOf(button.getLeft()));
Log.d("TOP: LAYOUT_CONTROL", String.valueOf(button.getTop()));
Log.d("RIGHT: LAYOUT_CONTROL", String.valueOf(button.getRight()));
Log.d("BOTTOM: LAYOUT_CONTROL", String.valueOf(button.getBottom()));
*/
if( ev.getRawX() > button.getX() && ev.getRawX() < button.getX() + button.getWidth() &&
ev.getRawY() > button.getY() && ev.getRawY() < button.getY() + button.getHeight()){
System.out.println("FOUND ONE !");
//Button hovered;
if(!button.equals(lastControlButton)){
ev.setAction(MotionEvent.ACTION_POINTER_UP);
if (lastControlButton != null) lastControlButton.onTouchEvent(ev);
ev.setAction(MotionEvent.ACTION_POINTER_DOWN);
button.onTouchEvent(ev);
lastControlButton = button;
}
return true;
}
}
return false;
//We actually could go through all our children here.
//This isn't the most performance friendly stuff though.
//return true;
}
}

View File

@@ -1,9 +1,7 @@
package net.kdt.pojavlaunch.customcontrols.buttons;
import android.content.*;
import android.graphics.*;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.util.*;
import android.view.*;
import android.view.View.*;
@@ -12,7 +10,6 @@ import android.widget.*;
import net.kdt.pojavlaunch.customcontrols.ControlData;
import net.kdt.pojavlaunch.customcontrols.ControlLayout;
import net.kdt.pojavlaunch.customcontrols.handleview.*;
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
import net.kdt.pojavlaunch.*;
import org.lwjgl.glfw.*;
@@ -27,7 +24,8 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp
protected boolean mModifiable = false;
protected boolean mCanTriggerLongClick = true;
protected boolean mChecked = false;
protected boolean isToggled = false;
protected boolean isPointerOutOfBounds = false;
public ControlButton(ControlLayout layout, ControlData properties) {
super(layout.getContext());
@@ -70,6 +68,7 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp
// com.android.internal.R.string.delete
// android.R.string.
setText(properties.name);
if (changePos) {
setTranslationX(moveX = properties.x);
setTranslationY(moveY = properties.y);
@@ -101,14 +100,37 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp
setBackground(gd);
}
public int computeStrokeWidth(float widthInPercent){
float maxSize = Math.max(mProperties.getWidth(), mProperties.getHeight());
return (int)((maxSize/2) * (widthInPercent/100));
public void setModifiable(boolean isModifiable) {
mModifiable = isModifiable;
}
public float computeCornerRadius(float radiusInPercent){
float minSize = Math.min(mProperties.getWidth(), mProperties.getHeight());
return (minSize/2) * (radiusInPercent/100);
private void setModified(boolean modified) {
if (getParent() != null)
((ControlLayout) getParent()).setModified(modified);
}
private void setHolding(boolean isDown) {
if (mProperties.holdAlt) {
CallbackBridge.holdingAlt = isDown;
MainActivity.sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_LEFT_ALT,0,isDown);
System.out.println("holdingAlt="+CallbackBridge.holdingAlt);
} if (mProperties.keycodes[0] == LWJGLGLFWKeycode.GLFW_KEY_CAPS_LOCK) {
CallbackBridge.holdingCapslock = isDown;
//MainActivity.sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_CAPS_LOCK,0,isDown);
System.out.println("holdingCapslock="+CallbackBridge.holdingCapslock);
} if (mProperties.holdCtrl) {
CallbackBridge.holdingCtrl = isDown;
MainActivity.sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_LEFT_CONTROL,0,isDown);
System.out.println("holdingCtrl="+CallbackBridge.holdingCtrl);
} if (mProperties.keycodes[0] == LWJGLGLFWKeycode.GLFW_KEY_NUM_LOCK) {
CallbackBridge.holdingNumlock = isDown;
//MainActivity.sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_NUM_LOCK,0,isDown);
System.out.println("holdingNumlock="+CallbackBridge.holdingNumlock);
} if (mProperties.holdShift) {
CallbackBridge.holdingShift = isDown;
MainActivity.sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_LEFT_SHIFT,0,isDown);
System.out.println("holdingShift="+CallbackBridge.holdingShift);
}
}
@Override
@@ -128,13 +150,18 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp
setModified(true);
}
public void setVisible(boolean isVisible){
if(mProperties.isHideable)
setVisibility(isVisible ? VISIBLE : GONE);
}
@Override
public void setTranslationX(float x) {
super.setTranslationX(x);
if (!mProperties.isDynamicBtn) {
mProperties.x = x;
mProperties.dynamicX = Float.toString(x / CallbackBridge.physicalWidth) + " * ${screen_width}";
mProperties.dynamicX = x / CallbackBridge.physicalWidth + " * ${screen_width}";
setModified(true);
}
}
@@ -145,7 +172,7 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp
if (!mProperties.isDynamicBtn) {
mProperties.y = y;
mProperties.dynamicY = Float.toString(y / CallbackBridge.physicalHeight) + " * ${screen_height}";
mProperties.dynamicY = y / CallbackBridge.physicalHeight + " * ${screen_height}";
setModified(true);
}
}
@@ -157,7 +184,7 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mChecked) {
if (isToggled) {
canvas.drawRoundRect(0, 0, getWidth(), getHeight(), mProperties.cornerRadius, mProperties.cornerRadius, mRectPaint);
}
}
@@ -183,35 +210,11 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp
return mCanTriggerLongClick;
}
private void setHolding(boolean isDown) {
if (mProperties.holdAlt) {
CallbackBridge.holdingAlt = isDown;
MainActivity.sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_LEFT_ALT,0,isDown);
System.out.println("holdingAlt="+CallbackBridge.holdingAlt);
} if (mProperties.keycodes[0] == LWJGLGLFWKeycode.GLFW_KEY_CAPS_LOCK) {
CallbackBridge.holdingCapslock = isDown;
//MainActivity.sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_CAPS_LOCK,0,isDown);
System.out.println("holdingCapslock="+CallbackBridge.holdingCapslock);
} if (mProperties.holdCtrl) {
CallbackBridge.holdingCtrl = isDown;
MainActivity.sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_LEFT_CONTROL,0,isDown);
System.out.println("holdingCtrl="+CallbackBridge.holdingCtrl);
} if (mProperties.keycodes[0] == LWJGLGLFWKeycode.GLFW_KEY_NUM_LOCK) {
CallbackBridge.holdingNumlock = isDown;
//MainActivity.sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_NUM_LOCK,0,isDown);
System.out.println("holdingNumlock="+CallbackBridge.holdingNumlock);
} if (mProperties.holdShift) {
CallbackBridge.holdingShift = isDown;
MainActivity.sendKeyPress(LWJGLGLFWKeycode.GLFW_KEY_LEFT_SHIFT,0,isDown);
System.out.println("holdingShift="+CallbackBridge.holdingShift);
}
}
protected float moveX, moveY;
protected float downX, downY;
@Override
public boolean onTouchEvent(MotionEvent event) {
System.out.println("IS BEING TOUCHED");
if (!mModifiable) {
mCanTriggerLongClick = false;
if (event.getAction() == MotionEvent.ACTION_MOVE && CallbackBridge.isGrabbing() && mProperties.passThruEnabled) {
@@ -223,6 +226,35 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp
}
switch (event.getActionMasked()) {
case MotionEvent.ACTION_MOVE:
//If out of bounds
if(event.getX() < getLeft() || event.getX() > getRight() ||
event.getY() < getTop() || event.getY() > getBottom()){
if(mProperties.isSwipeable && !isPointerOutOfBounds){
//Remove keys
if(!triggerToggle(event)) {
setHolding(false);
sendKeyPresses(event, false);
}
}
isPointerOutOfBounds = true;
((ControlLayout) getParent()).onTouchEvent(event);
break;
}
//Else if we now are in bounds
if(isPointerOutOfBounds) {
((ControlLayout) getParent()).onTouchEvent(event);
//RE-press the button
if(mProperties.isSwipeable && !mProperties.isToggle){
setHolding(true);
sendKeyPresses(event, true);
}
}
isPointerOutOfBounds = false;
break;
case MotionEvent.ACTION_DOWN: // 0
case MotionEvent.ACTION_POINTER_DOWN: // 5
if(!mProperties.isToggle){
@@ -233,16 +265,15 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp
case MotionEvent.ACTION_UP: // 1
case MotionEvent.ACTION_CANCEL: // 3
case MotionEvent.ACTION_POINTER_UP: // 6
if(mProperties.isToggle){
mChecked = !mChecked;
invalidate();
setHolding(mChecked);
sendKeyPresses(event, mChecked);
break;
if(isPointerOutOfBounds) ((ControlLayout) getParent()).onTouchEvent(event);
isPointerOutOfBounds = false;
if(!triggerToggle(event)) {
setHolding(false);
sendKeyPresses(event, false);
}
setHolding(false);
sendKeyPresses(event,false);
break;
default:
return false;
}
@@ -278,6 +309,28 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp
return super.onTouchEvent(event);
}
public int computeStrokeWidth(float widthInPercent){
float maxSize = Math.max(mProperties.getWidth(), mProperties.getHeight());
return (int)((maxSize/2) * (widthInPercent/100));
}
public float computeCornerRadius(float radiusInPercent){
float minSize = Math.min(mProperties.getWidth(), mProperties.getHeight());
return (minSize/2) * (radiusInPercent/100);
}
public boolean triggerToggle(MotionEvent event){
//returns true a the toggle system is triggered
if(mProperties.isToggle){
isToggled = !isToggled;
invalidate();
setHolding(isToggled);
sendKeyPresses(event, isToggled);
return true;
}
return false;
}
public void sendKeyPresses(MotionEvent event, boolean isDown){
for(int keycode : mProperties.keycodes){
if(keycode >= 0){
@@ -288,13 +341,4 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp
}
}
public void setModifiable(boolean isModifiable) {
mModifiable = isModifiable;
}
private void setModified(boolean modified) {
if (getParent() != null) {
((ControlLayout) getParent()).setModified(modified);
}
}
}

View File

@@ -59,36 +59,16 @@ public class ControlDrawer extends ControlButton {
}
private void setControlButtonVisibility(ControlButton button, boolean isVisible){
button.setVisibility(isVisible ? VISIBLE : GONE);
button.setVisible(isVisible);
}
private void switchButtonVisibility(){
areButtonsVisible = !areButtonsVisible;
int visibility = areButtonsVisible ? View.VISIBLE : View.GONE;
for(ControlButton button : buttons){
button.setVisibility(visibility);
button.setVisible(areButtonsVisible);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(!mModifiable){
switch (event.getActionMasked()){
case MotionEvent.ACTION_UP: // 1
case MotionEvent.ACTION_POINTER_UP: // 6
switchButtonVisibility();
break;
}
return true;
}
boolean isHandled = super.onTouchEvent(event);
//syncButtons();
return isHandled;
}
//Syncing stuff
private void alignButtons(){
@@ -135,6 +115,27 @@ public class ControlDrawer extends ControlButton {
resizeButtons();
}
@Override
public void setVisible(boolean isVisible) {
//TODO replicate changes to his children ?
setVisibility(isVisible ? VISIBLE : GONE);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(!mModifiable){
switch (event.getActionMasked()){
case MotionEvent.ACTION_UP: // 1
case MotionEvent.ACTION_POINTER_UP: // 6
switchButtonVisibility();
break;
}
return true;
}
return super.onTouchEvent(event);
}
@Override
public void setTranslationX(float x) {
super.setTranslationX(x);

View File

@@ -2,6 +2,7 @@ package net.kdt.pojavlaunch.customcontrols.buttons;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import net.kdt.pojavlaunch.customcontrols.ControlData;
@@ -30,8 +31,10 @@ public class ControlSubButton extends ControlButton {
setProperties(mProperties, false);
}
@Override
public void setVisible(boolean isVisible) {
setVisibility(isVisible ? (parentDrawer.areButtonsVisible ? VISIBLE : GONE) : View.GONE);
}
@Override
public void setLayoutParams(ViewGroup.LayoutParams params) {