v1.2.0: implemented basic controller support

This commit is contained in:
Evan Debenham 2022-02-18 12:08:27 -05:00
parent ccfb7ddfad
commit a010144551
12 changed files with 218 additions and 6 deletions

View File

@ -7,5 +7,6 @@ dependencies {
//TODO migrate this to implementation from api
//in order to do this I have to remove 100% of libGDX API access from core
api "com.badlogicgames.gdx:gdx:$gdxVersion"
api "com.badlogicgames.gdx-controllers:gdx-controllers-core:$gdxControllersVersion"
implementation "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
}

View File

@ -0,0 +1,137 @@
/*
* Pixel Dungeon
* Copyright (C) 2012-2015 Oleg Dolya
*
* Shattered Pixel Dungeon
* Copyright (C) 2014-2022 Evan Debenham
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package com.watabou.input;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.controllers.Controller;
import com.badlogic.gdx.controllers.ControllerListener;
import com.badlogic.gdx.controllers.ControllerMapping;
import com.watabou.utils.DeviceCompat;
import com.watabou.utils.PointF;
import java.lang.annotation.Target;
public class ControllerHandler implements ControllerListener {
public static boolean controllersSupported() {
if (DeviceCompat.isAndroid() && Gdx.app.getVersion() < 16) {
return false;
} else {
return true;
}
}
@Override
public void connected(Controller controller) {
}
@Override
public void disconnected(Controller controller) {
}
@Override
public boolean buttonDown(Controller controller, int buttonCode) {
int keyCode = buttonToKey(controller, buttonCode);
if (keyCode != Input.Keys.UNKNOWN){
KeyEvent.addKeyEvent(new KeyEvent(keyCode, true));
return true;
}
return false;
}
@Override
public boolean buttonUp(Controller controller, int buttonCode) {
int keyCode = buttonToKey(controller, buttonCode);
if (keyCode != Input.Keys.UNKNOWN){
KeyEvent.addKeyEvent(new KeyEvent(keyCode, false));
return true;
}
return false;
}
public static PointF leftStickPosition = new PointF();
public static PointF rightStickPosition = new PointF();
private float L2Trigger = 0f;
private float R2Trigger = 0f;
@Override
public boolean axisMoved(Controller controller, int axisCode, float value) {
ControllerMapping mapping = controller.getMapping();
if (mapping.axisRightX == axisCode) rightStickPosition.x = value;
else if (mapping.axisRightY == axisCode) rightStickPosition.y = value;
else if (mapping.axisLeftX == axisCode) leftStickPosition.x = value;
else if (mapping.axisLeftY == axisCode) leftStickPosition.y = value;
//L2 and R2 triggers on Desktop
else if (axisCode == 4) {
if (L2Trigger < 0.5f && value >= 0.5f){
KeyEvent.addKeyEvent(new KeyEvent(Input.Keys.BUTTON_L2, true));
} else if (L2Trigger >= 0.5f && value < 0.5f){
KeyEvent.addKeyEvent(new KeyEvent(Input.Keys.BUTTON_L2, false));
}
L2Trigger = value;
} else if (axisCode == 5){
if (R2Trigger < 0.5f && value >= 0.5f){
KeyEvent.addKeyEvent(new KeyEvent(Input.Keys.BUTTON_R2, true));
} else if (R2Trigger >= 0.5f && value < 0.5f){
KeyEvent.addKeyEvent(new KeyEvent(Input.Keys.BUTTON_R2, false));
}
R2Trigger = value;
}
return true;
}
//converts controller button codes to keyEvent codes
public static int buttonToKey(Controller controller, int keyCode){
ControllerMapping mapping = controller.getMapping();
if (keyCode == mapping.buttonA) return Input.Keys.BUTTON_A;
if (keyCode == mapping.buttonB) return Input.Keys.BUTTON_B;
//C button?
if (keyCode == mapping.buttonX) return Input.Keys.BUTTON_X;
if (keyCode == mapping.buttonY) return Input.Keys.BUTTON_Y;
if (keyCode == mapping.buttonBack) return Input.Keys.BUTTON_SELECT;
if (keyCode == mapping.buttonStart) return Input.Keys.BUTTON_START;
if (keyCode == mapping.buttonL1) return Input.Keys.BUTTON_L1;
if (keyCode == mapping.buttonL2) return Input.Keys.BUTTON_L2;
if (keyCode == mapping.buttonR1) return Input.Keys.BUTTON_R1;
if (keyCode == mapping.buttonR2) return Input.Keys.BUTTON_R2;
if (keyCode == mapping.buttonDpadUp) return Input.Keys.DPAD_UP;
if (keyCode == mapping.buttonDpadLeft) return Input.Keys.DPAD_LEFT;
if (keyCode == mapping.buttonDpadDown) return Input.Keys.DPAD_DOWN;
if (keyCode == mapping.buttonDpadRight) return Input.Keys.DPAD_RIGHT;
if (keyCode == mapping.buttonLeftStick) return Input.Keys.BUTTON_THUMBL;
if (keyCode == mapping.buttonRightStick) return Input.Keys.BUTTON_THUMBR;
return Input.Keys.UNKNOWN;
}
}

View File

@ -24,12 +24,14 @@ package com.watabou.noosa;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.controllers.Controllers;
import com.badlogic.gdx.graphics.glutils.GLVersion;
import com.badlogic.gdx.utils.TimeUtils;
import com.watabou.glscripts.Script;
import com.watabou.gltextures.TextureCache;
import com.watabou.glwrap.Blending;
import com.watabou.glwrap.Vertexbuffer;
import com.watabou.input.ControllerHandler;
import com.watabou.input.InputHandler;
import com.watabou.input.PointerEvent;
import com.watabou.noosa.audio.Music;
@ -100,6 +102,9 @@ public class Game implements ApplicationListener {
dispWidth = Gdx.graphics.getDisplayMode().width;
inputHandler = new InputHandler( Gdx.input );
if (ControllerHandler.controllersSupported()){
Controllers.addListener(new ControllerHandler());
}
//refreshes texture and vertex data stored on the gpu
versionContextRef = Gdx.graphics.getGLVersion();

View File

@ -69,6 +69,7 @@ dependencies {
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"
implementation "com.badlogicgames.gdx-controllers:gdx-controllers-android:$gdxControllersVersion"
}
// called every time gradle gets executed, takes the native dependencies of

View File

@ -22,6 +22,9 @@
-keepnames class com.badlogic.gdx.graphics.Color { *; }
-keepnames class com.badlogic.gdx.scenes.scene2d.ui.TextField$TextFieldStyle { *; }
# needed for libGDX controllers
-keep class com.badlogic.gdx.controllers.android.AndroidControllers { *; }
-keepclassmembers class com.badlogic.gdx.backends.android.AndroidInput* {
<init>(com.badlogic.gdx.Application, android.content.Context, java.lang.Object, com.badlogic.gdx.backends.android.AndroidApplicationConfiguration);
}

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.shatteredpixel.shatteredpixeldungeon.android"
android:installLocation="auto"
android:targetSandboxVersion="2"
@ -12,6 +13,9 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- We perform a runtime check to ensure controllers are disabled on API 14 & 15 -->
<uses-sdk tools:overrideLibrary="com.badlogicgames.gdx.controllers" />
<!-- Note that the game doesn't truly support small screen resolutions,
it instead forces downscaling to work on these displays.-->
<supports-screens

View File

@ -24,6 +24,7 @@ allprojects {
appAndroidTargetSDK = 31
gdxVersion = '1.10.0'
gdxControllersVersion = '2.2.1'
robovmVersion = '2.3.14'
}
version = appVersionName

View File

@ -30,6 +30,7 @@ import com.shatteredpixel.shatteredpixeldungeon.actors.mobs.Mob;
import com.shatteredpixel.shatteredpixeldungeon.items.Heap;
import com.shatteredpixel.shatteredpixeldungeon.sprites.CharSprite;
import com.shatteredpixel.shatteredpixeldungeon.tiles.DungeonTilemap;
import com.watabou.input.ControllerHandler;
import com.watabou.input.GameAction;
import com.watabou.input.KeyBindings;
import com.watabou.input.KeyEvent;
@ -330,8 +331,10 @@ public class CellSelector extends ScrollArea {
if (heldDelay > 0){
heldDelay -= Game.elapsed;
}
if (heldAction1 != SPDAction.NONE && Dungeon.hero.ready){
boolean leftStickActive = Math.abs(ControllerHandler.leftStickPosition.x) >= .5f
|| Math.abs(ControllerHandler.leftStickPosition.y) >= 0.5f;
leftStickActive = leftStickActive && !GameScene.InterfaceBlockingHero();
if ((heldAction1 != SPDAction.NONE || leftStickActive) && Dungeon.hero.ready){
processKeyHold();
} else if (Dungeon.hero.ready) {
lastCellMoved = -1;
@ -350,7 +353,7 @@ public class CellSelector extends ScrollArea {
for (GameAction action : actions) {
cell += directionFromAction(action);
}
if (cell != Dungeon.hero.pos && cell != lastCellMoved){
lastCellMoved = cell;
select(cell, PointerEvent.LEFT);
@ -373,9 +376,39 @@ public class CellSelector extends ScrollArea {
if (action == SPDAction.NW) return -1-Dungeon.level.width();
else return 0;
}
//TODO controller stick movement would probably be improved if it used the 50ms delay, like key movement
public void processKeyHold() {
if (directionFromAction(heldAction1) + directionFromAction(heldAction2) != 0
//prioritize moving by controller stick over moving via keys
PointF leftStick = ControllerHandler.leftStickPosition;
boolean leftStickActive = Math.abs(leftStick.x) > 0.5f || Math.abs(leftStick.y) > 0.5f;
leftStickActive = leftStickActive && !GameScene.InterfaceBlockingHero();
if (leftStickActive) {
enabled = Dungeon.hero.ready = true;
Dungeon.observe();
//determine which direction to move in.
if (leftStick.x > 0.5f){
if (leftStick.y < -0.5f){
if (moveFromActions(SPDAction.NE)) Dungeon.hero.ready = false;
} else if (leftStick.y > 0.5f){
if (moveFromActions(SPDAction.SE)) Dungeon.hero.ready = false;
} else if (leftStick.x > 0.8f){
if (moveFromActions(SPDAction.E)) Dungeon.hero.ready = false;
}
} else if (leftStick.x < -0.5f){
if (leftStick.y < -0.5f){
if (moveFromActions(SPDAction.NW)) Dungeon.hero.ready = false;
} else if (leftStick.y > 0.5f){
if (moveFromActions(SPDAction.SW)) Dungeon.hero.ready = false;
} else if (leftStick.x < -0.8f){
if (moveFromActions(SPDAction.W)) Dungeon.hero.ready = false;
}
} else if (leftStick.y > 0.8f){
if (moveFromActions(SPDAction.S)) Dungeon.hero.ready = false;
} else if (leftStick.y < -0.8f){
if (moveFromActions(SPDAction.N)) Dungeon.hero.ready = false;
}
} else if (directionFromAction(heldAction1) + directionFromAction(heldAction2) != 0
&& heldDelay <= 0){
enabled = Dungeon.hero.ready = true;
Dungeon.observe();

View File

@ -21,6 +21,7 @@
package com.shatteredpixel.shatteredpixeldungeon.scenes;
import com.badlogic.gdx.Gdx;
import com.shatteredpixel.shatteredpixeldungeon.Assets;
import com.shatteredpixel.shatteredpixeldungeon.Badges;
import com.shatteredpixel.shatteredpixeldungeon.SPDSettings;
@ -32,6 +33,7 @@ import com.shatteredpixel.shatteredpixeldungeon.ui.Tooltip;
import com.shatteredpixel.shatteredpixeldungeon.ui.Window;
import com.watabou.gltextures.TextureCache;
import com.watabou.glwrap.Blending;
import com.watabou.input.ControllerHandler;
import com.watabou.input.PointerEvent;
import com.watabou.noosa.BitmapText;
import com.watabou.noosa.BitmapText.Font;
@ -44,6 +46,7 @@ import com.watabou.noosa.Visual;
import com.watabou.noosa.ui.Component;
import com.watabou.noosa.ui.Cursor;
import com.watabou.utils.GameMath;
import com.watabou.utils.PointF;
import com.watabou.utils.Reflection;
import java.util.ArrayList;
@ -155,7 +158,28 @@ public class PixelScene extends Scene {
Cursor.setCustomCursor(Cursor.Type.DEFAULT, defaultZoom);
}
private PointF fractionalMovement = new PointF();
@Override
public void update() {
super.update();
if (Math.abs(ControllerHandler.rightStickPosition.x) >= 0.1f
|| Math.abs(ControllerHandler.rightStickPosition.y) >= 0.1f) {
PointF curMouse = PointerEvent.currentHoverPos();
//cursor moves 500 scaled pixels per second at full speed, 50 at minimum speed
fractionalMovement.x += defaultZoom * 500 * Game.elapsed * ControllerHandler.rightStickPosition.x;
fractionalMovement.y += defaultZoom * 500 * Game.elapsed * ControllerHandler.rightStickPosition.y;
curMouse.x += (int)fractionalMovement.x;
curMouse.y += (int)fractionalMovement.y;
Gdx.input.setCursorPosition((int) curMouse.x, (int) curMouse.y);
fractionalMovement.x -= (int)fractionalMovement.x;
fractionalMovement.y -= (int)fractionalMovement.y;
} else {
fractionalMovement.set(0);
}
}
//FIXME this system currently only works for a subset of windows
private static ArrayList<Class<?extends Window>> savedWindows = new ArrayList<>();
private static Class<?extends PixelScene> savedClass = null;

View File

@ -114,6 +114,7 @@ dependencies {
implementation "com.badlogicgames.gdx:gdx-backend-lwjgl3:$gdxVersion"
implementation "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
implementation "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
implementation "com.badlogicgames.gdx-controllers:gdx-controllers-desktop:$gdxControllersVersion"
//we use LWJGL tinyFD directly to display crash messages and (for now) single-line text input
implementation "org.lwjgl:lwjgl-tinyfd:3.2.3"

View File

@ -49,4 +49,5 @@ dependencies {
implementation "com.badlogicgames.gdx:gdx-backend-robovm:$gdxVersion"
implementation "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-ios"
implementation "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-ios"
implementation "com.badlogicgames.gdx-controllers:gdx-controllers-ios:$gdxControllersVersion"
}

View File

@ -27,6 +27,7 @@
<forceLinkClasses>
<pattern>com.badlogic.gdx.scenes.scene2d.ui.*</pattern>
<pattern>com.badlogic.gdx.graphics.g3d.particles.**</pattern>
<pattern>com.badlogic.gdx.controllers.IosControllerManager</pattern>
<pattern>com.android.okhttp.HttpHandler</pattern>
<pattern>com.android.okhttp.HttpsHandler</pattern>
<pattern>com.android.org.conscrypt.**</pattern>