v0.9.4: added support for input multiplexing

This commit is contained in:
Evan Debenham 2021-07-08 12:19:15 -04:00
parent a492c565d0
commit bcdbf8ef43
2 changed files with 52 additions and 15 deletions

View File

@ -21,18 +21,62 @@
package com.watabou.input; package com.watabou.input;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input; import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputAdapter; import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.InputProcessor;
import com.watabou.noosa.Game; import com.watabou.noosa.Game;
import com.watabou.utils.PointF; import com.watabou.utils.PointF;
public class InputHandler extends InputAdapter { public class InputHandler extends InputAdapter {
private InputMultiplexer multiplexer;
public InputHandler( Input input ){ public InputHandler( Input input ){
input.setInputProcessor( this ); //An input multiplexer, with additional coord tweaks for power saver mode
multiplexer = new InputMultiplexer(){
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
screenX /= (Game.dispWidth / (float)Game.width);
screenY /= (Game.dispHeight / (float)Game.height);
return super.touchDown(screenX, screenY, pointer, button);
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
screenX /= (Game.dispWidth / (float)Game.width);
screenY /= (Game.dispHeight / (float)Game.height);
return super.touchDragged(screenX, screenY, pointer);
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
screenX /= (Game.dispWidth / (float)Game.width);
screenY /= (Game.dispHeight / (float)Game.height);
return super.touchUp(screenX, screenY, pointer, button);
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
screenX /= (Game.dispWidth / (float)Game.width);
screenY /= (Game.dispHeight / (float)Game.height);
return super.mouseMoved(screenX, screenY);
}
};
input.setInputProcessor(multiplexer);
addInputProcessor(this);
input.setCatchKey( Input.Keys.BACK, true); input.setCatchKey( Input.Keys.BACK, true);
input.setCatchKey( Input.Keys.MENU, true); input.setCatchKey( Input.Keys.MENU, true);
} }
public void addInputProcessor(InputProcessor processor){
multiplexer.addProcessor(0, processor);
}
public void removeInputProcessor(InputProcessor processor){
multiplexer.removeProcessor(processor);
}
public void processAllEvents(){ public void processAllEvents(){
PointerEvent.processPointerEvents(); PointerEvent.processPointerEvents();
@ -46,24 +90,19 @@ public class InputHandler extends InputAdapter {
@Override @Override
public synchronized boolean touchDown(int screenX, int screenY, int pointer, int button) { public synchronized boolean touchDown(int screenX, int screenY, int pointer, int button) {
screenX /= (Game.dispWidth / (float)Game.width); Gdx.input.setOnscreenKeyboardVisible(false); //in-game events never need keyboard, so hide it
screenY /= (Game.dispHeight / (float)Game.height);
PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, true)); PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, true));
return true; return true;
} }
@Override @Override
public synchronized boolean touchUp(int screenX, int screenY, int pointer, int button) { public synchronized boolean touchUp(int screenX, int screenY, int pointer, int button) {
screenX /= (Game.dispWidth / (float)Game.width);
screenY /= (Game.dispHeight / (float)Game.height);
PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, false)); PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, false));
return true; return true;
} }
@Override @Override
public synchronized boolean touchDragged(int screenX, int screenY, int pointer) { public synchronized boolean touchDragged(int screenX, int screenY, int pointer) {
screenX /= (Game.dispWidth / (float)Game.width);
screenY /= (Game.dispHeight / (float)Game.height);
PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, true)); PointerEvent.addPointerEvent(new PointerEvent(screenX, screenY, pointer, true));
return true; return true;
} }
@ -73,8 +112,6 @@ public class InputHandler extends InputAdapter {
@Override @Override
public boolean mouseMoved(int screenX, int screenY) { public boolean mouseMoved(int screenX, int screenY) {
screenX /= (Game.dispWidth / (float)Game.width);
screenY /= (Game.dispHeight / (float)Game.height);
pointerHoverPos.x = screenX; pointerHoverPos.x = screenX;
pointerHoverPos.y = screenY; pointerHoverPos.y = screenY;
return true; return true;

View File

@ -78,8 +78,8 @@ public class Game implements ApplicationListener {
public static float elapsed = 0f; public static float elapsed = 0f;
public static float timeTotal = 0f; public static float timeTotal = 0f;
public static long realTime = 0; public static long realTime = 0;
protected static InputHandler inputHandler; public static InputHandler inputHandler;
public static PlatformSupport platform; public static PlatformSupport platform;
@ -95,9 +95,9 @@ public class Game implements ApplicationListener {
density = Gdx.graphics.getDensity(); density = Gdx.graphics.getDensity();
dispHeight = Gdx.graphics.getDisplayMode().height; dispHeight = Gdx.graphics.getDisplayMode().height;
dispWidth = Gdx.graphics.getDisplayMode().width; dispWidth = Gdx.graphics.getDisplayMode().width;
inputHandler = new InputHandler( Gdx.input ); inputHandler = new InputHandler( Gdx.input );
//refreshes texture and vertex data stored on the gpu //refreshes texture and vertex data stored on the gpu
versionContextRef = Gdx.graphics.getGLVersion(); versionContextRef = Gdx.graphics.getGLVersion();
Blending.useDefault(); Blending.useDefault();
@ -123,7 +123,7 @@ public class Game implements ApplicationListener {
} }
if (height != Game.height || width != Game.width) { if (height != Game.height || width != Game.width) {
Game.width = width; Game.width = width;
Game.height = height; Game.height = height;