v1.2.0: implemented event/button functionality for right/middle click

This commit is contained in:
Evan Debenham 2022-02-11 15:18:02 -05:00
parent cf638965a4
commit a1a5ff3de8
3 changed files with 43 additions and 7 deletions

View File

@ -90,13 +90,13 @@ public class InputHandler extends InputAdapter {
@Override
public synchronized boolean touchDown(int screenX, int screenY, int pointer, int button) {
Gdx.input.setOnscreenKeyboardVisible(false); //in-game events never need keyboard, so hide it
PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, PointerEvent.Type.DOWN));
PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, PointerEvent.Type.DOWN, button));
return true;
}
@Override
public synchronized boolean touchUp(int screenX, int screenY, int pointer, int button) {
PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, PointerEvent.Type.UP));
PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, PointerEvent.Type.UP, button));
return true;
}
@ -142,7 +142,7 @@ public class InputHandler extends InputAdapter {
@Override
public boolean scrolled(float amountX, float amountY) {
ScrollEvent.addScrollEvent( new ScrollEvent(PointerEvent.lastHoverPos, amountY));
ScrollEvent.addScrollEvent( new ScrollEvent(PointerEvent.currentHoverPos(), amountY));
return true;
}
}

View File

@ -21,6 +21,7 @@
package com.watabou.input;
import com.badlogic.gdx.Input;
import com.watabou.utils.PointF;
import com.watabou.utils.Signal;
@ -35,17 +36,29 @@ public class PointerEvent {
HOVER
}
//buttons
public static final int NONE = -1;
public static final int LEFT = Input.Buttons.LEFT;
public static final int RIGHT = Input.Buttons.RIGHT;
public static final int MIDDLE = Input.Buttons.MIDDLE;
public PointF start;
public PointF current;
public int id;
public Type type;
public int button;
public boolean handled; //for hover events, to ensure hover always ends even with overlapping elements
public PointerEvent( int x, int y, int id, Type type){
this(x, y, id, type, NONE);
}
public PointerEvent( int x, int y, int id, Type type, int button){
start = current = new PointF(x, y);
this.id = id;
this.type = type;
handled = false;
this.button = button;
}
public void update( PointerEvent other ){
@ -87,7 +100,12 @@ public class PointerEvent {
// Accumulated pointer events
private static ArrayList<PointerEvent> pointerEvents = new ArrayList<>();
private static HashMap<Integer, PointerEvent> activePointers = new HashMap<>();
static PointF lastHoverPos = new PointF();
private static PointF lastHoverPos = new PointF();
public static PointF currentHoverPos(){
return lastHoverPos.clone();
}
public static synchronized void addPointerEvent( PointerEvent event ){
pointerEvents.add( event );

View File

@ -62,7 +62,19 @@ public class Button extends Component {
@Override
protected void onClick( PointerEvent event ) {
if (!processed) {
Button.this.onClick();
killTooltip();
switch (event.button){
case PointerEvent.LEFT: default:
Button.this.onClick();
break;
case PointerEvent.RIGHT:
Button.this.onRightClick();
break;
case PointerEvent.MIDDLE:
Button.this.onMiddleClick();
break;
}
}
}
@ -131,7 +143,13 @@ public class Button extends Component {
protected void onPointerDown() {}
protected void onPointerUp() {}
protected void onClick() {}
protected void onClick() {} //left click, default key type
protected void onRightClick() {
onClick();
}
protected void onMiddleClick() {
onClick();
}
protected boolean onLongClick() {
return false;
}