Add ControlDrawer.java and ControlDrawerData.java

This commit is contained in:
SerpentSpirale 2021-04-30 11:51:22 +02:00
parent e2f35f3f6f
commit 5dee2524df
2 changed files with 235 additions and 0 deletions

View File

@ -0,0 +1,161 @@
package net.kdt.pojavlaunch.customcontrols;
import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import java.util.ArrayList;
public class ControlDrawer extends ControlButton {
public ArrayList<ControlSubButton> buttons;
public ControlDrawerData drawerData;
public ControlLayout mLayout;
private boolean areButtonsVisible = false;
public ControlDrawer(ControlLayout layout, ControlDrawerData drawerData) {
super(layout, drawerData.properties);
buttons = new ArrayList<>(/*drawerData.buttonProperties.size()*/);
mLayout = layout;
this.drawerData = drawerData;
//Filter unwanted values before instantiating the button
for(int i=0; i < buttons.size(); ++i){
drawerData.buttonProperties.set(i, filterProperties(drawerData.buttonProperties.get(i)));
addButton(drawerData.buttonProperties.get(i));
}
}
private ControlData filterProperties(ControlData properties){
properties.isDynamicBtn = false;
properties.width = drawerData.properties.width;
properties.height = drawerData.properties.height;
return properties;
}
public void addButton(ControlData properties){
addButton(new ControlSubButton(mLayout, properties, this));
}
public void addButton(ControlSubButton button){
buttons.add(button);
setControlButtonVisibility(button, mModifiable || areButtonsVisible);
syncButtons();
}
private void setControlButtonVisibility(ControlButton button, boolean isVisible){
button.setVisibility(isVisible ? VISIBLE : GONE);
}
private void switchButtonVisibility(){
areButtonsVisible = !areButtonsVisible;
int visibility = areButtonsVisible ? View.VISIBLE : View.GONE;
for(ControlButton button : buttons){
button.setVisibility(visibility);
}
}
@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(){
if(buttons == null) return;
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).setTranslationY(drawerData.properties.y);
break;
case LEFT:
buttons.get(i).setTranslationX( (drawerData.properties.x - drawerData.properties.width) - drawerData.properties.width*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).setTranslationX(drawerData.properties.x);
break;
case DOWN:
buttons.get(i).setTranslationY( (drawerData.properties.y + drawerData.properties.height) + drawerData.properties.height*i );
buttons.get(i).setTranslationX(drawerData.properties.x);
break;
}
buttons.get(i).updateProperties();
}
}
private void resizeButtons(){
if (buttons == null) return;
for(ControlSubButton subButton : buttons){
subButton.mProperties.width = mProperties.width;
subButton.mProperties.height = mProperties.height;
subButton.updateProperties();
}
}
private void syncButtons(){
alignButtons();
resizeButtons();
}
@Override
public void setTranslationX(float x) {
super.setTranslationX(x);
alignButtons();
}
@Override
public void setTranslationY(float y) {
super.setTranslationY(y);
alignButtons();
}
@Override
public void setLayoutParams(ViewGroup.LayoutParams params) {
super.setLayoutParams(params);
syncButtons();
}
//Getters
public ControlDrawerData getDrawerData() {
return drawerData;
}
}

View File

@ -0,0 +1,74 @@
package net.kdt.pojavlaunch.customcontrols;
import java.util.ArrayList;
import static net.kdt.pojavlaunch.customcontrols.ControlDrawerData.Orientation.DOWN;
import static net.kdt.pojavlaunch.customcontrols.ControlDrawerData.Orientation.LEFT;
import static net.kdt.pojavlaunch.customcontrols.ControlDrawerData.Orientation.RIGHT;
import static net.kdt.pojavlaunch.customcontrols.ControlDrawerData.Orientation.UP;
public class ControlDrawerData {
public ArrayList<ControlData> buttonProperties;
public ControlData properties;
public Orientation orientation;
public enum Orientation {
DOWN,
LEFT,
UP,
RIGHT
}
public static Orientation[] getOrientations(){
return new Orientation[]{DOWN,LEFT,UP,RIGHT};
}
public static int orientationToInt(Orientation orientation){
switch (orientation){
case DOWN:
return 0;
case LEFT:
return 1;
case UP:
return 2;
case RIGHT:
return 3;
}
return -1;
}
public static Orientation intToOrientation(int by){
switch (by){
case 0:
return Orientation.DOWN;
case 1:
return Orientation.LEFT;
case 2:
return Orientation.UP;
case 3:
return RIGHT;
}
return null;
}
public ControlDrawerData(){
this(new ArrayList<>());
}
public ControlDrawerData(ArrayList<ControlData> buttonProperties){
this(buttonProperties, new ControlData());
}
public ControlDrawerData(ArrayList<ControlData> buttonProperties, ControlData properties){
this(buttonProperties, properties, Orientation.LEFT);
}
public ControlDrawerData(ArrayList<ControlData> buttonProperties, ControlData properties, Orientation orientation){
this.buttonProperties = buttonProperties;
this.properties = properties;
this.orientation = orientation;
}
}