Fix drawers not being snappable

This commit is contained in:
SerpentSpirale 2021-07-29 17:21:38 +02:00 committed by ArtDev
parent 8b0db45d02
commit b74d9bd1f4
2 changed files with 39 additions and 7 deletions

View File

@ -325,6 +325,25 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp
return super.onTouchEvent(event);
}
/**
* Passe a series of checks to determine if the ControlButton is available to be snapped on.
*
* @param button
* @return whether or not the button
*/
protected boolean canSnap(ControlButton button){
float MIN_DISTANCE = Tools.dpToPx(8);
if(button == this) return false;
if(com.google.android.material.math.MathUtils.dist(
button.getX() + button.getWidth()/2f,
button.getY() + button.getHeight()/2f,
getX() + getWidth()/2f,
getY() + getHeight()/2f) > Math.max(button.getWidth()/2f + getWidth()/2f, button.getHeight()/2f + getHeight()/2f) + MIN_DISTANCE) return false;
return true;
}
/**
* Try to snap, then align to neighboring buttons, given the provided coordinates.
* The new position is automatically applied to the View,
@ -335,19 +354,14 @@ public class ControlButton extends androidx.appcompat.widget.AppCompatButton imp
*/
protected void snapAndAlign(float x, float y){
//Time to snap !
float MIN_DISTANCE = Tools.dpToPx(10);
float MIN_DISTANCE = Tools.dpToPx(8);
setX(x);
setY(y);
for(ControlButton button : ((ControlLayout) getParent()).getButtonChildren()){
//Step 1: Filter unwanted buttons
if(button == this) continue;
if(com.google.android.material.math.MathUtils.dist(
button.getX() + button.getWidth()/2f,
button.getY() + button.getHeight()/2f,
getX() + getWidth()/2f,
getY() + getHeight()/2f) > Math.max(button.getWidth()/2f + getWidth()/2f, button.getHeight()/2f + getHeight()/2f) + MIN_DISTANCE) continue;
if(!canSnap(button)) continue;
//Step 2: Get Coordinates
float button_top = button.getY();

View File

@ -101,6 +101,19 @@ public class ControlDrawer extends ControlButton {
resizeButtons();
}
/**
* Check whether or not the button passed as a parameter belongs to this drawer.
*
* @param button The button to look for
* @return Whether the button is in the buttons list of the drawer.
*/
public boolean containsChild(ControlButton button){
for(ControlButton childButton : buttons){
if (childButton == button) return true;
}
return false;
}
@Override
public ControlData preProcessProperties(ControlData properties, ControlLayout layout) {
ControlData data = super.preProcessProperties(properties, layout);
@ -129,6 +142,11 @@ public class ControlDrawer extends ControlButton {
return super.onTouchEvent(event);
}
@Override
protected boolean canSnap(ControlButton button) {
return super.canSnap(button) && !containsChild(button);
}
@Override
public void setX(float x) {
super.setX(x);