mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-07 20:31:51 +09:00
making xinput to actually work (tested with xbone gamepad)
This commit is contained in:
Binary file not shown.
BIN
lib/javadoc/jxinput-1.0.0-javadoc.jar
Normal file
BIN
lib/javadoc/jxinput-1.0.0-javadoc.jar
Normal file
Binary file not shown.
BIN
lib/jxinput-1.0.0.jar
Normal file
BIN
lib/jxinput-1.0.0.jar
Normal file
Binary file not shown.
BIN
lib/source/jxinput-1.0.0-sources.jar
Normal file
BIN
lib/source/jxinput-1.0.0-sources.jar
Normal file
Binary file not shown.
@@ -175,7 +175,7 @@ public class AppLoader implements ApplicationListener {
|
||||
public static TinyAlphNum fontSmallNumbers;
|
||||
|
||||
/** A gamepad. Multiple gamepads may controll this single virtualised gamepad. */
|
||||
public static float gamepadDeadzone = 0.1f;
|
||||
public static float gamepadDeadzone = 0.2f;
|
||||
|
||||
/**
|
||||
* For the events depends on rendering frame (e.g. flicker on post-hit invincibility)
|
||||
|
||||
@@ -28,6 +28,8 @@ object DefaultConfig {
|
||||
|
||||
|
||||
// control-gamepad
|
||||
jsonObject.addProperty("usexinput", true) // when FALSE, LT+RT input on xbox controller is impossible
|
||||
|
||||
jsonObject.addProperty("gamepadkeyn", 3)
|
||||
jsonObject.addProperty("gamepadkeyw", 2)
|
||||
jsonObject.addProperty("gamepadkeys", 0)
|
||||
@@ -38,6 +40,9 @@ object DefaultConfig {
|
||||
jsonObject.addProperty("gamepadselect", 6)
|
||||
jsonObject.addProperty("gamepadstart", 7)
|
||||
|
||||
jsonObject.addProperty("gamepadltrigger", 8)
|
||||
jsonObject.addProperty("gamepadrtrigger", 9)
|
||||
|
||||
jsonObject.addProperty("gamepadlstickx", 1)
|
||||
jsonObject.addProperty("gamepadlsticky", 0)
|
||||
jsonObject.addProperty("gamepadrstickx", 3)
|
||||
|
||||
29
src/net/torvald/terrarum/controller/GdxControllerAdapter.kt
Normal file
29
src/net/torvald/terrarum/controller/GdxControllerAdapter.kt
Normal file
@@ -0,0 +1,29 @@
|
||||
package net.torvald.terrarum.controller
|
||||
|
||||
import com.badlogic.gdx.controllers.Controller
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2019-02-09.
|
||||
*/
|
||||
class GdxControllerAdapter(val c: Controller): TerrarumController {
|
||||
|
||||
override fun getButton(index: Int): Boolean {
|
||||
return c.getButton(index)
|
||||
}
|
||||
|
||||
override fun getAxis(index: Int): Float {
|
||||
return c.getAxis(index)
|
||||
}
|
||||
|
||||
override fun getPov(): Int {
|
||||
TODO()
|
||||
}
|
||||
|
||||
override fun getName(): String {
|
||||
return c.name
|
||||
}
|
||||
|
||||
override fun setRumble(left: Float, right: Float) {
|
||||
return
|
||||
}
|
||||
}
|
||||
66
src/net/torvald/terrarum/controller/TerrarumController.kt
Normal file
66
src/net/torvald/terrarum/controller/TerrarumController.kt
Normal file
@@ -0,0 +1,66 @@
|
||||
package net.torvald.terrarum.controller
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2019-02-09.
|
||||
*/
|
||||
interface TerrarumController {
|
||||
|
||||
/**
|
||||
* 0, 1, 2, 3 : A B X Y
|
||||
*
|
||||
* 4, 5 : L/R Shoulder
|
||||
*
|
||||
* 6, 7 : Back Start
|
||||
*
|
||||
* 8, 9 : L/R Trigger
|
||||
*
|
||||
* DirectInput devices may need external Index-to-button mapping (just a config file)
|
||||
*
|
||||
*/
|
||||
fun getButton(index: Int): Boolean
|
||||
|
||||
/**
|
||||
* 0: Left Y
|
||||
*
|
||||
* 1: Left X
|
||||
*
|
||||
* 2: Right Y
|
||||
*
|
||||
* 3: Right X
|
||||
*
|
||||
* 4: Left Trigger
|
||||
*
|
||||
* 5: Right Trigger
|
||||
*
|
||||
* DirectInput devices may need external Index-to-button mapping (just a config file).
|
||||
*
|
||||
* Warning: Under DirectInput, Xbox controllers' LT and RT shares the same axis #4, and thus it's impossible to read LT+RT input.
|
||||
*
|
||||
* @return 0f..1f for the axis
|
||||
*/
|
||||
fun getAxis(index: Int): Float
|
||||
|
||||
/**
|
||||
* ```
|
||||
* 12 8 9
|
||||
* 4 0 1
|
||||
* 6 2 3
|
||||
* ```
|
||||
*/
|
||||
fun getPov(): Int
|
||||
fun getName(): String
|
||||
|
||||
fun setRumble(left: Float, right: Float)
|
||||
|
||||
companion object {
|
||||
const val POV_E = 1
|
||||
const val POV_S = 2
|
||||
const val POV_W = 4
|
||||
const val POV_N = 8
|
||||
|
||||
const val POV_SE = POV_E or POV_S
|
||||
const val POV_SW = POV_W or POV_S
|
||||
const val POV_NW = POV_W or POV_N
|
||||
const val POV_NE = POV_E or POV_N
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package net.torvald.terrarum.controller
|
||||
|
||||
import com.github.strikerx3.jxinput.XInputAxes
|
||||
import com.github.strikerx3.jxinput.XInputDevice
|
||||
import net.torvald.terrarum.AppLoader
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2019-02-09.
|
||||
*/
|
||||
class XinputControllerAdapter(val c: XInputDevice): TerrarumController {
|
||||
|
||||
override fun getButton(index: Int): Boolean {
|
||||
if (c.poll()) {
|
||||
val button = c.components.buttons
|
||||
|
||||
return when (index) {
|
||||
0 -> button.a
|
||||
1 -> button.b
|
||||
2 -> button.x
|
||||
3 -> button.y
|
||||
4 -> button.lShoulder
|
||||
5 -> button.rShoulder
|
||||
6 -> button.back
|
||||
7 -> button.start
|
||||
8 -> getAxis(4) >= AppLoader.gamepadDeadzone
|
||||
9 -> getAxis(5) >= AppLoader.gamepadDeadzone
|
||||
else -> throw UnsupportedOperationException("Unknown button: $index")
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
override fun getAxis(index: Int): Float {
|
||||
if (c.poll()) {
|
||||
val axes = c.components.axes
|
||||
|
||||
return when (index) {
|
||||
0 -> axes.ly
|
||||
1 -> axes.lx
|
||||
2 -> axes.ry
|
||||
3 -> axes.rx
|
||||
4 -> axes.lt
|
||||
5 -> axes.rt
|
||||
else -> throw UnsupportedOperationException("Unknown axis: $index")
|
||||
}
|
||||
}
|
||||
return -1f
|
||||
}
|
||||
|
||||
override fun getPov(): Int {
|
||||
if (c.poll()) {
|
||||
val axes = c.components.axes
|
||||
|
||||
return when (axes.dpad) {
|
||||
XInputAxes.DPAD_CENTER -> 0
|
||||
XInputAxes.DPAD_UP_LEFT -> TerrarumController.POV_NW
|
||||
XInputAxes.DPAD_UP -> TerrarumController.POV_N
|
||||
XInputAxes.DPAD_UP_RIGHT -> TerrarumController.POV_NE
|
||||
XInputAxes.DPAD_RIGHT -> TerrarumController.POV_E
|
||||
XInputAxes.DPAD_DOWN_RIGHT -> TerrarumController.POV_SE
|
||||
XInputAxes.DPAD_DOWN -> TerrarumController.POV_S
|
||||
XInputAxes.DPAD_DOWN_LEFT -> TerrarumController.POV_SW
|
||||
XInputAxes.DPAD_LEFT -> TerrarumController.POV_W
|
||||
else -> throw UnsupportedOperationException("Unknown pov: ${axes.dpad}")
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
override fun getName(): String {
|
||||
return "(XInput Compatible)"
|
||||
}
|
||||
|
||||
override fun setRumble(left: Float, right: Float) {
|
||||
c.setVibration((left * 65535f).roundToInt(), (right * 65535f).roundToInt())
|
||||
}
|
||||
}
|
||||
@@ -3,14 +3,11 @@ package net.torvald.terrarum.gamecontroller
|
||||
import com.badlogic.gdx.Gdx
|
||||
import com.badlogic.gdx.Input
|
||||
import com.badlogic.gdx.InputAdapter
|
||||
import com.badlogic.gdx.controllers.Controller
|
||||
import com.badlogic.gdx.controllers.ControllerListener
|
||||
import com.badlogic.gdx.controllers.Controllers
|
||||
import com.badlogic.gdx.controllers.PovDirection
|
||||
import com.badlogic.gdx.math.Vector3
|
||||
import net.torvald.terrarum.AppLoader
|
||||
import net.torvald.terrarum.AppLoader.printdbg
|
||||
import net.torvald.terrarum.Terrarum
|
||||
import net.torvald.terrarum.controller.TerrarumController
|
||||
import net.torvald.terrarum.floorInt
|
||||
import net.torvald.terrarum.gameactors.AVKey
|
||||
import net.torvald.terrarum.gameworld.fmod
|
||||
@@ -22,10 +19,12 @@ import net.torvald.terrarum.worlddrawer.WorldCamera
|
||||
/**
|
||||
* Created by minjaesong on 2015-12-31.
|
||||
*/
|
||||
class IngameController(val ingame: Ingame) : InputAdapter(), ControllerListener {
|
||||
class IngameController(val ingame: Ingame) : InputAdapter() {
|
||||
|
||||
|
||||
var hasController = true
|
||||
val hasGamepad: Boolean
|
||||
get() = gamepad != null
|
||||
var gamepad: TerrarumController? = null
|
||||
|
||||
// these four values can also be accessed with GameContainer.<varname>
|
||||
// e.g. gc.mouseTileX
|
||||
@@ -45,7 +44,6 @@ class IngameController(val ingame: Ingame) : InputAdapter(), ControllerListener
|
||||
|
||||
init {
|
||||
if (Controllers.getControllers().size == 0) {
|
||||
hasController = false
|
||||
printdbg(this, "Controller not found")
|
||||
}
|
||||
}
|
||||
@@ -211,39 +209,4 @@ class IngameController(val ingame: Ingame) : InputAdapter(), ControllerListener
|
||||
return true
|
||||
}
|
||||
|
||||
override fun connected(p0: Controller?) {
|
||||
hasController = true
|
||||
}
|
||||
|
||||
override fun buttonUp(p0: Controller?, p1: Int): Boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
override fun ySliderMoved(p0: Controller?, p1: Int, p2: Boolean): Boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
override fun accelerometerMoved(p0: Controller?, p1: Int, p2: Vector3?): Boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
override fun axisMoved(p0: Controller?, p1: Int, p2: Float): Boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
override fun disconnected(p0: Controller?) {
|
||||
hasController = false
|
||||
}
|
||||
|
||||
override fun xSliderMoved(p0: Controller?, p1: Int, p2: Boolean): Boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
override fun povMoved(p0: Controller?, p1: Int, p2: PovDirection?): Boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
override fun buttonDown(p0: Controller?, p1: Int): Boolean {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.badlogic.gdx.Gdx
|
||||
import com.badlogic.gdx.controllers.Controllers
|
||||
import com.badlogic.gdx.graphics.Camera
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
||||
import com.github.strikerx3.jxinput.XInputDevice
|
||||
import net.torvald.dataclass.CircularArray
|
||||
import net.torvald.terrarum.*
|
||||
import net.torvald.terrarum.AppLoader.printdbg
|
||||
@@ -11,6 +12,8 @@ import net.torvald.terrarum.blockproperties.BlockPropUtil
|
||||
import net.torvald.terrarum.blockstats.BlockStats
|
||||
import net.torvald.terrarum.concurrent.ThreadParallel
|
||||
import net.torvald.terrarum.console.Authenticator
|
||||
import net.torvald.terrarum.controller.GdxControllerAdapter
|
||||
import net.torvald.terrarum.controller.XinputControllerAdapter
|
||||
import net.torvald.terrarum.gameactors.Actor
|
||||
import net.torvald.terrarum.gameactors.ActorWithBody
|
||||
import net.torvald.terrarum.gamecontroller.IngameController
|
||||
@@ -269,7 +272,11 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
|
||||
|
||||
// make controls work
|
||||
Gdx.input.inputProcessor = ingameController
|
||||
Controllers.addListener(ingameController)
|
||||
ingameController.gamepad =
|
||||
if (AppLoader.getConfigBoolean("usexinput"))
|
||||
XinputControllerAdapter(XInputDevice.getDeviceFor(0))
|
||||
else
|
||||
GdxControllerAdapter(Controllers.getControllers()[0])
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package net.torvald.terrarum.modulebasegame.gameactors
|
||||
|
||||
import com.badlogic.gdx.Gdx
|
||||
import com.badlogic.gdx.controllers.Controllers
|
||||
import com.badlogic.gdx.graphics.Color
|
||||
import com.jme3.math.FastMath
|
||||
import net.torvald.spriteanimation.HasAssembledSprite
|
||||
@@ -226,9 +225,9 @@ open class ActorHumanoid(
|
||||
isRightDown = Gdx.input.isKeyPressed(AppLoader.getConfigInt("keyright"))
|
||||
isJumpDown = Gdx.input.isKeyPressed(AppLoader.getConfigInt("keyjump"))
|
||||
|
||||
if ((Terrarum.ingame as Ingame).ingameController.hasController) {
|
||||
val gamepad = Controllers.getControllers()[0]
|
||||
val gamepad = (Terrarum.ingame as Ingame).ingameController.gamepad
|
||||
|
||||
if (gamepad != null) {
|
||||
axisX = gamepad.getAxis(AppLoader.getConfigInt("gamepadlstickx"))
|
||||
axisY = gamepad.getAxis(AppLoader.getConfigInt("gamepadlsticky"))
|
||||
axisRX = gamepad.getAxis(AppLoader.getConfigInt("gamepadrstickx"))
|
||||
@@ -241,7 +240,7 @@ open class ActorHumanoid(
|
||||
if (Math.abs(axisRY) < AppLoader.gamepadDeadzone) axisRY = 0f
|
||||
|
||||
isJumpDown = Gdx.input.isKeyPressed(AppLoader.getConfigInt("keyjump")) ||
|
||||
gamepad.getAxis(AppLoader.getConfigInt("gamepadtriggeraxis")) < -AppLoader.gamepadDeadzone
|
||||
gamepad.getButton(AppLoader.getConfigInt("gamepadltrigger"))
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -253,7 +252,7 @@ open class ActorHumanoid(
|
||||
}
|
||||
|
||||
private inline val hasController: Boolean
|
||||
get() = if (isGamer) (Terrarum.ingame as Ingame).ingameController.hasController
|
||||
get() = if (isGamer) (Terrarum.ingame as Ingame).ingameController.hasGamepad
|
||||
else true
|
||||
|
||||
private fun processInput(delta: Float) {
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package net.torvald.terrarum.ui
|
||||
|
||||
import com.badlogic.gdx.Gdx
|
||||
import com.badlogic.gdx.controllers.Controllers
|
||||
import com.badlogic.gdx.graphics.Camera
|
||||
import com.badlogic.gdx.graphics.Color
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
||||
import net.torvald.terrarum.*
|
||||
import net.torvald.terrarum.Terrarum.mouseTileX
|
||||
import net.torvald.terrarum.Terrarum.mouseTileY
|
||||
import net.torvald.terrarum.controller.TerrarumController
|
||||
import net.torvald.terrarum.gameworld.GameWorld
|
||||
import net.torvald.terrarum.modulebasegame.Ingame
|
||||
import net.torvald.terrarum.modulebasegame.gameworld.GameWorldExtension
|
||||
@@ -192,10 +192,9 @@ class BasicDebugInfoWindow : UICanvas() {
|
||||
|
||||
batch.color = Color.WHITE
|
||||
|
||||
if ((Terrarum.ingame as? Ingame)?.ingameController?.hasController == true) {
|
||||
val gamepad = Controllers.getControllers()[0]
|
||||
|
||||
drawGamepadAxis(batch,
|
||||
val gamepad = (Terrarum.ingame as? Ingame)?.ingameController?.gamepad
|
||||
if (gamepad != null) {
|
||||
drawGamepadAxis(gamepad, batch,
|
||||
gamepad.getAxis(AppLoader.getConfigInt("gamepadlstickx")),
|
||||
gamepad.getAxis(AppLoader.getConfigInt("gamepadlsticky")),
|
||||
Terrarum.WIDTH - 135,
|
||||
@@ -311,7 +310,7 @@ class BasicDebugInfoWindow : UICanvas() {
|
||||
blendNormal(batch)
|
||||
}
|
||||
|
||||
private fun drawGamepadAxis(batch: SpriteBatch, axisX: Float, axisY: Float, uiX: Int, uiY: Int) {
|
||||
private fun drawGamepadAxis(gamepad: TerrarumController, batch: SpriteBatch, axisX: Float, axisY: Float, uiX: Int, uiY: Int) {
|
||||
val uiColour = ItemSlotImageFactory.CELLCOLOUR_BLACK
|
||||
val w = 128f
|
||||
val h = 128f
|
||||
@@ -319,11 +318,7 @@ class BasicDebugInfoWindow : UICanvas() {
|
||||
val halfH = h / 2f
|
||||
|
||||
val pointDX = axisX * halfW
|
||||
val pointDY = axisY * halfH
|
||||
|
||||
val gamepad = Controllers.getControllers()[0]
|
||||
val padName = if (gamepad.name.isEmpty()) "Gamepad"
|
||||
else gamepad.name
|
||||
val pointDY = -axisY * halfH
|
||||
|
||||
blendNormal(batch)
|
||||
|
||||
@@ -338,7 +333,7 @@ class BasicDebugInfoWindow : UICanvas() {
|
||||
}
|
||||
batch.begin()
|
||||
|
||||
Terrarum.fontSmallNumbers.draw(batch, padName, Terrarum.WIDTH - (padName.length) * 8f, uiY.toFloat() + h + 2)
|
||||
Terrarum.fontSmallNumbers.draw(batch, gamepad.getName(), Terrarum.WIDTH - (gamepad.getName().length) * 8f, uiY.toFloat() + h + 2)
|
||||
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user