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;
}
boolean[] keyExistences = inputDevice.hasKeys(gamepadButtons);
int nbuttons = 0;
for (int i = 0; i < gamepadButtons.length; i++) {
if (!keyExistences[i]) {
break;
}
nbuttons++;
}
// Use the same number of SDL buttons. See internal/gamepaddb/sdl.go.
int nbuttons = 15;
int naxes = 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 {
// 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
const faceButtonMask = ((1 << SDLControllerButtonA) |
(1 << SDLControllerButtonB) |
(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"
"github.com/hajimehoshi/ebiten/v2/internal/gamepad"
"github.com/hajimehoshi/ebiten/v2/internal/gamepaddb"
"github.com/hajimehoshi/ebiten/v2/internal/ui"
)
@ -73,38 +74,18 @@ const (
// TODO: Can we map these values to the standard gamepad buttons?
var androidKeyToGamepadButton = map[int]gamepad.Button{
keycodeButtonA: gamepad.Button0,
keycodeButtonB: gamepad.Button1,
keycodeButtonC: gamepad.Button2,
keycodeButtonX: gamepad.Button3,
keycodeButtonY: gamepad.Button4,
keycodeButtonZ: gamepad.Button5,
keycodeButtonL1: gamepad.Button6,
keycodeButtonR1: gamepad.Button7,
keycodeButtonL2: gamepad.Button8,
keycodeButtonR2: gamepad.Button9,
keycodeButtonThumbl: gamepad.Button10,
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,
var androidKeyToSDLButton = map[int]int{
keycodeButtonA: gamepaddb.SDLControllerButtonA,
keycodeButtonB: gamepaddb.SDLControllerButtonB,
keycodeButtonX: gamepaddb.SDLControllerButtonX,
keycodeButtonY: gamepaddb.SDLControllerButtonY,
keycodeButtonL1: gamepaddb.SDLControllerButtonLeftShoulder,
keycodeButtonR1: gamepaddb.SDLControllerButtonRightShoulder,
keycodeButtonThumbl: gamepaddb.SDLControllerButtonLeftStick,
keycodeButtonThumbr: gamepaddb.SDLControllerButtonRightStick,
keycodeButtonStart: gamepaddb.SDLControllerButtonStart,
keycodeButtonSelect: gamepaddb.SDLControllerButtonBack,
keycodeButtonMode: gamepaddb.SDLControllerButtonGuide,
}
// Axis constant definitions for joysticks only.
@ -143,39 +124,16 @@ const (
axisGeneric16 = 0x0000002f
)
var androidAxisIDToAxisID = map[int]int{
axisX: 0,
axisY: 1,
axisZ: 2,
axisRx: 3,
axisRy: 4,
axisRz: 5,
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 androidAxisToSDLAxis = map[int]int{
axisX: gamepaddb.SDLControllerAxisLeftX,
axisY: gamepaddb.SDLControllerAxisLeftY,
axisRx: gamepaddb.SDLControllerAxisRightX,
axisRy: gamepaddb.SDLControllerAxisRightY,
axisLtrigger: gamepaddb.SDLControllerAxisTriggerLeft,
axisRtrigger: gamepaddb.SDLControllerAxisTriggerRight,
}
var androidAxisIDToHatID2 = map[int]int{
var androidAxisToHatID2 = map[int]int{
axisHatX: 0,
axisHatY: 1,
}
@ -195,8 +153,8 @@ func OnKeyDownOnAndroid(keyCode int, unicodeChar int, source int, deviceID int)
switch {
case source&sourceGamepad == sourceGamepad:
// A gamepad can be detected as a keyboard. Detect the device as a gamepad first.
if button, ok := androidKeyToGamepadButton[keyCode]; ok {
gamepad.UpdateAndroidGamepadButton(deviceID, button, true)
if button, ok := androidKeyToSDLButton[keyCode]; ok {
gamepad.UpdateAndroidGamepadButton(deviceID, gamepad.Button(button), true)
}
case source&sourceJoystick == sourceJoystick:
// 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 {
case source&sourceGamepad == sourceGamepad:
// A gamepad can be detected as a keyboard. Detect the device as a gamepad first.
if button, ok := androidKeyToGamepadButton[keyCode]; ok {
gamepad.UpdateAndroidGamepadButton(deviceID, button, false)
if button, ok := androidKeyToSDLButton[keyCode]; ok {
gamepad.UpdateAndroidGamepadButton(deviceID, gamepad.Button(button), false)
}
case source&sourceJoystick == sourceJoystick:
// 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) {
if axis, ok := androidAxisIDToAxisID[axisID]; ok {
if axis, ok := androidAxisToSDLAxis[axisID]; ok {
gamepad.UpdateAndroidGamepadAxis(deviceID, axis, float64(value))
return
}
if hid2, ok := androidAxisIDToHatID2[axisID]; ok {
if hid2, ok := androidAxisToHatID2[axisID]; ok {
const (
hatUp = 1
hatRight = 2