v2.0.1: improved button logic, preventing simultaneous button presses
This commit is contained in:
@@ -40,29 +40,35 @@ public class Button extends Component {
|
|||||||
|
|
||||||
protected PointerArea hotArea;
|
protected PointerArea hotArea;
|
||||||
protected Tooltip hoverTip;
|
protected Tooltip hoverTip;
|
||||||
|
|
||||||
protected boolean pressed;
|
//only one button should be pressed at a time
|
||||||
|
protected static Button pressedButton;
|
||||||
protected float pressTime;
|
protected float pressTime;
|
||||||
protected boolean processed;
|
protected boolean clickReady;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void createChildren() {
|
protected void createChildren() {
|
||||||
hotArea = new PointerArea( 0, 0, 0, 0 ) {
|
hotArea = new PointerArea( 0, 0, 0, 0 ) {
|
||||||
@Override
|
@Override
|
||||||
protected void onPointerDown( PointerEvent event ) {
|
protected void onPointerDown( PointerEvent event ) {
|
||||||
pressed = true;
|
pressedButton = Button.this;
|
||||||
pressTime = 0;
|
pressTime = 0;
|
||||||
processed = false;
|
clickReady = true;
|
||||||
Button.this.onPointerDown();
|
Button.this.onPointerDown();
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
protected void onPointerUp( PointerEvent event ) {
|
protected void onPointerUp( PointerEvent event ) {
|
||||||
pressed = false;
|
if (pressedButton == Button.this){
|
||||||
|
pressedButton = null;
|
||||||
|
} else {
|
||||||
|
//cancel any potential click, only one button can be activated at a time
|
||||||
|
clickReady = false;
|
||||||
|
}
|
||||||
Button.this.onPointerUp();
|
Button.this.onPointerUp();
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
protected void onClick( PointerEvent event ) {
|
protected void onClick( PointerEvent event ) {
|
||||||
if (!processed) {
|
if (clickReady) {
|
||||||
killTooltip();
|
killTooltip();
|
||||||
switch (event.button){
|
switch (event.button){
|
||||||
case PointerEvent.LEFT: default:
|
case PointerEvent.LEFT: default:
|
||||||
@@ -114,14 +120,16 @@ public class Button extends Component {
|
|||||||
public boolean onSignal ( KeyEvent event ) {
|
public boolean onSignal ( KeyEvent event ) {
|
||||||
if ( active && KeyBindings.getActionForKey( event ) == keyAction()){
|
if ( active && KeyBindings.getActionForKey( event ) == keyAction()){
|
||||||
if (event.pressed){
|
if (event.pressed){
|
||||||
pressed = true;
|
pressedButton = Button.this;
|
||||||
pressTime = 0;
|
pressTime = 0;
|
||||||
processed = false;
|
clickReady = true;
|
||||||
Button.this.onPointerDown();
|
Button.this.onPointerDown();
|
||||||
} else {
|
} else {
|
||||||
Button.this.onPointerUp();
|
Button.this.onPointerUp();
|
||||||
if (pressed && !processed) onClick();
|
if (pressedButton == Button.this) {
|
||||||
pressed = false;
|
pressedButton = null;
|
||||||
|
if (clickReady) onClick();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -141,40 +149,22 @@ public class Button extends Component {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private float lastUpdateTime = Float.POSITIVE_INFINITY;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update() {
|
public void update() {
|
||||||
super.update();
|
super.update();
|
||||||
|
|
||||||
hotArea.active = visible;
|
hotArea.active = visible;
|
||||||
|
|
||||||
if (pressed) {
|
if (pressedButton == this && (pressTime += Game.elapsed) >= longClick) {
|
||||||
//if this button hasn't updated for a bit while held, it was probably deactivated.
|
pressedButton = null;
|
||||||
// cancel the hold action in these cases.
|
if (onLongClick()) {
|
||||||
if (Game.timeTotal - 0.5f >= lastUpdateTime){
|
|
||||||
hotArea.reset();
|
hotArea.reset();
|
||||||
pressed = false;
|
clickReady = false; //did a long click, can't do a regular one
|
||||||
onPointerUp();
|
onPointerUp();
|
||||||
|
|
||||||
return;
|
Game.vibrate( 50 );
|
||||||
} else {
|
|
||||||
lastUpdateTime = Game.timeTotal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pressed && (pressTime += Game.elapsed) >= longClick) {
|
|
||||||
pressed = false;
|
|
||||||
if (onLongClick()) {
|
|
||||||
|
|
||||||
hotArea.reset();
|
|
||||||
processed = true;
|
|
||||||
onPointerUp();
|
|
||||||
|
|
||||||
Game.vibrate( 50 );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
lastUpdateTime = Float.POSITIVE_INFINITY;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user