internal/gamepaddb: fix button assignments on Android

Updates #2309
This commit is contained in:
Hajime Hoshi 2022-09-07 14:33:29 +09:00
parent e0d559f27e
commit e161b28bff
5 changed files with 74 additions and 108 deletions

View File

@ -545,14 +545,8 @@ public class EbitenView extends ViewGroup implements InputManager.InputDeviceLis
return; return;
} }
boolean[] keyExistences = inputDevice.hasKeys(gamepadButtons); // Use the same number of SDL buttons. See internal/gamepaddb/sdl.go.
int nbuttons = 0; int nbuttons = 15;
for (int i = 0; i < gamepadButtons.length; i++) {
if (!keyExistences[i]) {
break;
}
nbuttons++;
}
int naxes = 0; int naxes = 0;
int nhats2 = 0; int nhats2 = 0;

File diff suppressed because one or more lines are too long

View File

@ -581,37 +581,7 @@ func Update(mappingData []byte) error {
} }
func addAndroidDefaultMappings(id string) bool { func addAndroidDefaultMappings(id string) bool {
// See https://github.com/libsdl-org/SDL/blob/120c76c84bbce4c1bfed4e9eb74e10678bd83120/include/SDL_gamecontroller.h#L655-L680
const (
SDLControllerButtonA = 0
SDLControllerButtonB = 1
SDLControllerButtonX = 2
SDLControllerButtonY = 3
SDLControllerButtonBack = 4
SDLControllerButtonGuide = 5
SDLControllerButtonStart = 6
SDLControllerButtonLeftStick = 7
SDLControllerButtonRightStick = 8
SDLControllerButtonLeftShoulder = 9
SDLControllerButtonRightShoulder = 10
SDLControllerButtonDpadUp = 11
SDLControllerButtonDpadDown = 12
SDLControllerButtonDpadLeft = 13
SDLControllerButtonDpadRight = 14
)
// See https://github.com/libsdl-org/SDL/blob/120c76c84bbce4c1bfed4e9eb74e10678bd83120/include/SDL_gamecontroller.h#L550-L560
const (
SDLControllerAxisLeftX = 0
SDLControllerAxisLeftY = 1
SDLControllerAxisRightX = 2
SDLControllerAxisRightY = 3
SDLControllerAxisTriggerLeft = 4
SDLControllerAxisTriggerRight = 5
)
// See https://github.com/libsdl-org/SDL/blob/120c76c84bbce4c1bfed4e9eb74e10678bd83120/src/joystick/SDL_gamecontroller.c#L468-L568 // See https://github.com/libsdl-org/SDL/blob/120c76c84bbce4c1bfed4e9eb74e10678bd83120/src/joystick/SDL_gamecontroller.c#L468-L568
const faceButtonMask = ((1 << SDLControllerButtonA) | const faceButtonMask = ((1 << SDLControllerButtonA) |
(1 << SDLControllerButtonB) | (1 << SDLControllerButtonB) |
(1 << SDLControllerButtonX) | (1 << SDLControllerButtonX) |

44
internal/gamepaddb/sdl.go Normal file
View File

@ -0,0 +1,44 @@
// Copyright 2022 The Ebitengine Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gamepaddb
// See https://github.com/libsdl-org/SDL/blob/120c76c84bbce4c1bfed4e9eb74e10678bd83120/include/SDL_gamecontroller.h#L655-L680
const (
SDLControllerButtonA = 0
SDLControllerButtonB = 1
SDLControllerButtonX = 2
SDLControllerButtonY = 3
SDLControllerButtonBack = 4
SDLControllerButtonGuide = 5
SDLControllerButtonStart = 6
SDLControllerButtonLeftStick = 7
SDLControllerButtonRightStick = 8
SDLControllerButtonLeftShoulder = 9
SDLControllerButtonRightShoulder = 10
SDLControllerButtonDpadUp = 11
SDLControllerButtonDpadDown = 12
SDLControllerButtonDpadLeft = 13
SDLControllerButtonDpadRight = 14
)
// See https://github.com/libsdl-org/SDL/blob/120c76c84bbce4c1bfed4e9eb74e10678bd83120/include/SDL_gamecontroller.h#L550-L560
const (
SDLControllerAxisLeftX = 0
SDLControllerAxisLeftY = 1
SDLControllerAxisRightX = 2
SDLControllerAxisRightY = 3
SDLControllerAxisTriggerLeft = 4
SDLControllerAxisTriggerRight = 5
)

View File

@ -21,6 +21,7 @@ import (
"unicode" "unicode"
"github.com/hajimehoshi/ebiten/v2/internal/gamepad" "github.com/hajimehoshi/ebiten/v2/internal/gamepad"
"github.com/hajimehoshi/ebiten/v2/internal/gamepaddb"
"github.com/hajimehoshi/ebiten/v2/internal/ui" "github.com/hajimehoshi/ebiten/v2/internal/ui"
) )
@ -73,38 +74,18 @@ const (
// TODO: Can we map these values to the standard gamepad buttons? // TODO: Can we map these values to the standard gamepad buttons?
var androidKeyToGamepadButton = map[int]gamepad.Button{ var androidKeyToSDLButton = map[int]int{
keycodeButtonA: gamepad.Button0, keycodeButtonA: gamepaddb.SDLControllerButtonA,
keycodeButtonB: gamepad.Button1, keycodeButtonB: gamepaddb.SDLControllerButtonB,
keycodeButtonC: gamepad.Button2, keycodeButtonX: gamepaddb.SDLControllerButtonX,
keycodeButtonX: gamepad.Button3, keycodeButtonY: gamepaddb.SDLControllerButtonY,
keycodeButtonY: gamepad.Button4, keycodeButtonL1: gamepaddb.SDLControllerButtonLeftShoulder,
keycodeButtonZ: gamepad.Button5, keycodeButtonR1: gamepaddb.SDLControllerButtonRightShoulder,
keycodeButtonL1: gamepad.Button6, keycodeButtonThumbl: gamepaddb.SDLControllerButtonLeftStick,
keycodeButtonR1: gamepad.Button7, keycodeButtonThumbr: gamepaddb.SDLControllerButtonRightStick,
keycodeButtonL2: gamepad.Button8, keycodeButtonStart: gamepaddb.SDLControllerButtonStart,
keycodeButtonR2: gamepad.Button9, keycodeButtonSelect: gamepaddb.SDLControllerButtonBack,
keycodeButtonThumbl: gamepad.Button10, keycodeButtonMode: gamepaddb.SDLControllerButtonGuide,
keycodeButtonThumbr: gamepad.Button11,
keycodeButtonStart: gamepad.Button12,
keycodeButtonSelect: gamepad.Button13,
keycodeButtonMode: gamepad.Button14,
keycodeButton1: gamepad.Button15,
keycodeButton2: gamepad.Button16,
keycodeButton3: gamepad.Button17,
keycodeButton4: gamepad.Button18,
keycodeButton5: gamepad.Button19,
keycodeButton6: gamepad.Button20,
keycodeButton7: gamepad.Button21,
keycodeButton8: gamepad.Button22,
keycodeButton9: gamepad.Button23,
keycodeButton10: gamepad.Button24,
keycodeButton11: gamepad.Button25,
keycodeButton12: gamepad.Button26,
keycodeButton13: gamepad.Button27,
keycodeButton14: gamepad.Button28,
keycodeButton15: gamepad.Button29,
keycodeButton16: gamepad.Button30,
} }
// Axis constant definitions for joysticks only. // Axis constant definitions for joysticks only.
@ -143,39 +124,16 @@ const (
axisGeneric16 = 0x0000002f axisGeneric16 = 0x0000002f
) )
var androidAxisIDToAxisID = map[int]int{ var androidAxisToSDLAxis = map[int]int{
axisX: 0, axisX: gamepaddb.SDLControllerAxisLeftX,
axisY: 1, axisY: gamepaddb.SDLControllerAxisLeftY,
axisZ: 2, axisRx: gamepaddb.SDLControllerAxisRightX,
axisRx: 3, axisRy: gamepaddb.SDLControllerAxisRightY,
axisRy: 4, axisLtrigger: gamepaddb.SDLControllerAxisTriggerLeft,
axisRz: 5, axisRtrigger: gamepaddb.SDLControllerAxisTriggerRight,
axisLtrigger: 6,
axisRtrigger: 7,
axisThrottle: 8,
axisRudder: 9,
axisWheel: 10,
axisGas: 11,
axisBrake: 12,
axisGeneric1: 13,
axisGeneric2: 14,
axisGeneric3: 15,
axisGeneric4: 16,
axisGeneric5: 17,
axisGeneric6: 18,
axisGeneric7: 19,
axisGeneric8: 20,
axisGeneric9: 21,
axisGeneric10: 22,
axisGeneric11: 23,
axisGeneric12: 24,
axisGeneric13: 25,
axisGeneric14: 26,
axisGeneric15: 27,
axisGeneric16: 28,
} }
var androidAxisIDToHatID2 = map[int]int{ var androidAxisToHatID2 = map[int]int{
axisHatX: 0, axisHatX: 0,
axisHatY: 1, axisHatY: 1,
} }
@ -195,8 +153,8 @@ func OnKeyDownOnAndroid(keyCode int, unicodeChar int, source int, deviceID int)
switch { switch {
case source&sourceGamepad == sourceGamepad: case source&sourceGamepad == sourceGamepad:
// A gamepad can be detected as a keyboard. Detect the device as a gamepad first. // A gamepad can be detected as a keyboard. Detect the device as a gamepad first.
if button, ok := androidKeyToGamepadButton[keyCode]; ok { if button, ok := androidKeyToSDLButton[keyCode]; ok {
gamepad.UpdateAndroidGamepadButton(deviceID, button, true) gamepad.UpdateAndroidGamepadButton(deviceID, gamepad.Button(button), true)
} }
case source&sourceJoystick == sourceJoystick: case source&sourceJoystick == sourceJoystick:
// DPAD keys can come here, but they are also treated as an axis at a motion event. Ignore them. // DPAD keys can come here, but they are also treated as an axis at a motion event. Ignore them.
@ -215,8 +173,8 @@ func OnKeyUpOnAndroid(keyCode int, source int, deviceID int) {
switch { switch {
case source&sourceGamepad == sourceGamepad: case source&sourceGamepad == sourceGamepad:
// A gamepad can be detected as a keyboard. Detect the device as a gamepad first. // A gamepad can be detected as a keyboard. Detect the device as a gamepad first.
if button, ok := androidKeyToGamepadButton[keyCode]; ok { if button, ok := androidKeyToSDLButton[keyCode]; ok {
gamepad.UpdateAndroidGamepadButton(deviceID, button, false) gamepad.UpdateAndroidGamepadButton(deviceID, gamepad.Button(button), false)
} }
case source&sourceJoystick == sourceJoystick: case source&sourceJoystick == sourceJoystick:
// DPAD keys can come here, but they are also treated as an axis at a motion event. Ignore them. // DPAD keys can come here, but they are also treated as an axis at a motion event. Ignore them.
@ -229,12 +187,12 @@ func OnKeyUpOnAndroid(keyCode int, source int, deviceID int) {
} }
func OnGamepadAxesOrHatsChanged(deviceID int, axisID int, value float32) { func OnGamepadAxesOrHatsChanged(deviceID int, axisID int, value float32) {
if axis, ok := androidAxisIDToAxisID[axisID]; ok { if axis, ok := androidAxisToSDLAxis[axisID]; ok {
gamepad.UpdateAndroidGamepadAxis(deviceID, axis, float64(value)) gamepad.UpdateAndroidGamepadAxis(deviceID, axis, float64(value))
return return
} }
if hid2, ok := androidAxisIDToHatID2[axisID]; ok { if hid2, ok := androidAxisToHatID2[axisID]; ok {
const ( const (
hatUp = 1 hatUp = 1
hatRight = 2 hatRight = 2