making xinput to actually work (tested with xbone gamepad)

This commit is contained in:
minjaesong
2019-02-09 18:33:54 +09:00
parent d891afa1cd
commit 1730ebd0d7
14 changed files with 207 additions and 65 deletions

View 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
}
}

View 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
}
}

View File

@@ -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())
}
}