Skip to content

Instantly share code, notes, and snippets.

@Lomeli12
Created April 1, 2018 06:15
Show Gist options
  • Save Lomeli12/f7aa88b7f370cdf50598ac4fcd7ebe85 to your computer and use it in GitHub Desktop.
Save Lomeli12/f7aa88b7f370cdf50598ac4fcd7ebe85 to your computer and use it in GitHub Desktop.
LibGDX PS4 Controller Reference
import com.badlogic.gdx.controllers.Controller
import com.badlogic.gdx.controllers.ControllerAdapter
import com.badlogic.gdx.controllers.PovDirection
import ktx.log.info
object ControllerInput : ControllerAdapter() {
override fun connected(controller: Controller?) {
if (controller != null) info { "Controller Connected: $controller" }
}
override fun disconnected(controller: Controller?) {
info { "Controller Disconnected" }
}
/**
* For Future Reference. PS4 Buttons to Code
* Square = 0
* Cross = 1
* Circle = 2
* Triangle = 3
* L1 = 4
* R1 = 5
* L2 = 6
* R2 = 7
* Share = 8
* Options = 9
* L3 = 10
* R3 = 11
* PS Button = 12
* Touch Pad = 13
*/
override fun buttonDown(controller: Controller?, buttonIndex: Int): Boolean {
if (controller != null) info { "Controller: $controller | Button Down ID: $buttonIndex" }
return super.buttonDown(controller, buttonIndex)
}
override fun buttonUp(controller: Controller?, buttonIndex: Int): Boolean {
if (controller != null) info { "Controller: $controller | Button Up ID: $buttonIndex" }
return super.buttonUp(controller, buttonIndex)
}
/**
* For Future Reference. PS4 Buttons to Code
* Right Stick Up = Axis 0, Negative Value
* Right Stick Down = Axis 0, Positive Value
* Right Stick Left = Axis 1, Negative Value
* Right Stick Right = Axis 1, Positive Value
* Left Stick Up = Axis 2, Negative Value
* Left Stick Down = Axis 2, Positive Value
* Left Stick Left = Axis 3, Negative Value
* Left Stick Right = Axis 3, Positive Value
*/
override fun axisMoved(controller: Controller?, axisIndex: Int, value: Float): Boolean {
// Only pass through large changes so console isn't spammed by tiny movements from just holding the controller
if (controller != null && (value > 0.5f || value < -0.5F)) info { "Controller: $controller | Axis: $axisIndex | Value: $value" }
return super.axisMoved(controller, axisIndex, value)
}
/**
* For Future Reference. PS4 Buttons to Code
* D-Pad Up = North
* D-Pad Down = South
* D-Pad Left = West
* D-Pad Right = East
* Nothing down = Center
* Can combine (eq Up + Left = Northwest, Down + Right = Southeast, etc)
*/
override fun povMoved(controller: Controller?, povIndex: Int, value: PovDirection?): Boolean {
if (controller != null) info { "Controller: $controller | POV: $povIndex | Value: $value" }
return super.povMoved(controller, povIndex, value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment