Revert "internal/gamepaddb: fix button assignments on Android"

This reverts commit e161b28bff.

Reason: this would break backward compatibility

Updates #2309
This commit is contained in:
Hajime Hoshi 2022-09-07 15:36:53 +09:00
parent e161b28bff
commit b5d755b07a
5 changed files with 108 additions and 74 deletions

View File

@ -545,8 +545,14 @@ public class EbitenView extends ViewGroup implements InputManager.InputDeviceLis
return; return;
} }
// Use the same number of SDL buttons. See internal/gamepaddb/sdl.go. boolean[] keyExistences = inputDevice.hasKeys(gamepadButtons);
int nbuttons = 15; int nbuttons = 0;
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,7 +581,37 @@ 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) |

View File

@ -1,44 +0,0 @@
// 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,7 +21,6 @@ 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"
) )
@ -74,18 +73,38 @@ const (
// TODO: Can we map these values to the standard gamepad buttons? // TODO: Can we map these values to the standard gamepad buttons?
var androidKeyToSDLButton = map[int]int{ var androidKeyToGamepadButton = map[int]gamepad.Button{
keycodeButtonA: gamepaddb.SDLControllerButtonA, keycodeButtonA: gamepad.Button0,
keycodeButtonB: gamepaddb.SDLControllerButtonB, keycodeButtonB: gamepad.Button1,
keycodeButtonX: gamepaddb.SDLControllerButtonX, keycodeButtonC: gamepad.Button2,
keycodeButtonY: gamepaddb.SDLControllerButtonY, keycodeButtonX: gamepad.Button3,
keycodeButtonL1: gamepaddb.SDLControllerButtonLeftShoulder, keycodeButtonY: gamepad.Button4,
keycodeButtonR1: gamepaddb.SDLControllerButtonRightShoulder, keycodeButtonZ: gamepad.Button5,
keycodeButtonThumbl: gamepaddb.SDLControllerButtonLeftStick, keycodeButtonL1: gamepad.Button6,
keycodeButtonThumbr: gamepaddb.SDLControllerButtonRightStick, keycodeButtonR1: gamepad.Button7,
keycodeButtonStart: gamepaddb.SDLControllerButtonStart, keycodeButtonL2: gamepad.Button8,
keycodeButtonSelect: gamepaddb.SDLControllerButtonBack, keycodeButtonR2: gamepad.Button9,
keycodeButtonMode: gamepaddb.SDLControllerButtonGuide, 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,
} }
// Axis constant definitions for joysticks only. // Axis constant definitions for joysticks only.
@ -124,16 +143,39 @@ const (
axisGeneric16 = 0x0000002f axisGeneric16 = 0x0000002f
) )
var androidAxisToSDLAxis = map[int]int{ var androidAxisIDToAxisID = map[int]int{
axisX: gamepaddb.SDLControllerAxisLeftX, axisX: 0,
axisY: gamepaddb.SDLControllerAxisLeftY, axisY: 1,
axisRx: gamepaddb.SDLControllerAxisRightX, axisZ: 2,
axisRy: gamepaddb.SDLControllerAxisRightY, axisRx: 3,
axisLtrigger: gamepaddb.SDLControllerAxisTriggerLeft, axisRy: 4,
axisRtrigger: gamepaddb.SDLControllerAxisTriggerRight, 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 androidAxisToHatID2 = map[int]int{ var androidAxisIDToHatID2 = map[int]int{
axisHatX: 0, axisHatX: 0,
axisHatY: 1, axisHatY: 1,
} }
@ -153,8 +195,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 := androidKeyToSDLButton[keyCode]; ok { if button, ok := androidKeyToGamepadButton[keyCode]; ok {
gamepad.UpdateAndroidGamepadButton(deviceID, gamepad.Button(button), true) gamepad.UpdateAndroidGamepadButton(deviceID, 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.
@ -173,8 +215,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 := androidKeyToSDLButton[keyCode]; ok { if button, ok := androidKeyToGamepadButton[keyCode]; ok {
gamepad.UpdateAndroidGamepadButton(deviceID, gamepad.Button(button), false) gamepad.UpdateAndroidGamepadButton(deviceID, 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.
@ -187,12 +229,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 := androidAxisToSDLAxis[axisID]; ok { if axis, ok := androidAxisIDToAxisID[axisID]; ok {
gamepad.UpdateAndroidGamepadAxis(deviceID, axis, float64(value)) gamepad.UpdateAndroidGamepadAxis(deviceID, axis, float64(value))
return return
} }
if hid2, ok := androidAxisToHatID2[axisID]; ok { if hid2, ok := androidAxisIDToHatID2[axisID]; ok {
const ( const (
hatUp = 1 hatUp = 1
hatRight = 2 hatRight = 2