diff --git a/internal/uidriver/mobile/gamepad_android.go b/internal/uidriver/mobile/gamepad_android.go new file mode 100644 index 000000000..55e8b4694 --- /dev/null +++ b/internal/uidriver/mobile/gamepad_android.go @@ -0,0 +1,33 @@ +// Copyright 2021 The Ebiten 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 mobile + +import ( + "github.com/hajimehoshi/ebiten/v2/internal/driver" +) + +// UpdateGamepads updates the gamepad states. +// UpdateGamepads is called when the gamepad states are given from outside like Android. +func (u *UserInterface) UpdateGamepads(gamepads []Gamepad) { + u.input.updateGamepads(gamepads) + if u.fpsMode == driver.FPSModeVsyncOffMinimum { + u.renderRequester.RequestRenderIfNeeded() + } +} + +func (i *Input) updateGamepads(gamepads []Gamepad) { + i.gamepads = i.gamepads[:0] + i.gamepads = append(i.gamepads, gamepads...) +} diff --git a/internal/uidriver/mobile/gamepad_ios.go b/internal/uidriver/mobile/gamepad_ios.go new file mode 100644 index 000000000..ae01b6c42 --- /dev/null +++ b/internal/uidriver/mobile/gamepad_ios.go @@ -0,0 +1,20 @@ +// Copyright 2021 The Ebiten 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. + +//go:build ios +// +build ios + +package mobile + +// TODO: Implement gamepad detection for iOS (#1105). diff --git a/internal/uidriver/mobile/input.go b/internal/uidriver/mobile/input.go index 5eab36b2d..bbca8d7f7 100644 --- a/internal/uidriver/mobile/input.go +++ b/internal/uidriver/mobile/input.go @@ -212,11 +212,6 @@ func (i *Input) update(keys map[driver.Key]struct{}, runes []rune, touches []Tou i.touches = append(i.touches, touches...) } -func (i *Input) updateGamepads(gamepads []Gamepad) { - i.gamepads = i.gamepads[:0] - i.gamepads = append(i.gamepads, gamepads...) -} - func (i *Input) resetForFrame() { i.runes = nil } diff --git a/internal/uidriver/mobile/ui.go b/internal/uidriver/mobile/ui.go index b534a4aed..fac5b7acf 100644 --- a/internal/uidriver/mobile/ui.go +++ b/internal/uidriver/mobile/ui.go @@ -475,15 +475,6 @@ func (u *UserInterface) UpdateInput(keys map[driver.Key]struct{}, runes []rune, } } -// UpdateGamepads updates the gamepad states. -// UpdateGamepads is called when the gamepad states are given from outside like Android. -func (u *UserInterface) UpdateGamepads(gamepads []Gamepad) { - u.input.updateGamepads(gamepads) - if u.fpsMode == driver.FPSModeVsyncOffMinimum { - u.renderRequester.RequestRenderIfNeeded() - } -} - type RenderRequester interface { SetExplicitRenderingMode(explicitRendering bool) RequestRenderIfNeeded() diff --git a/mobile/ebitenmobileview/input.go b/mobile/ebitenmobileview/input.go index fd8ae4baa..8523762c0 100644 --- a/mobile/ebitenmobileview/input.go +++ b/mobile/ebitenmobileview/input.go @@ -18,8 +18,6 @@ package ebitenmobileview import ( - "runtime" - "github.com/hajimehoshi/ebiten/v2/internal/driver" "github.com/hajimehoshi/ebiten/v2/internal/uidriver/mobile" ) @@ -33,14 +31,10 @@ var ( keys = map[driver.Key]struct{}{} runes []rune touches = map[driver.TouchID]position{} - - // gamepads is updated only at Android. - gamepads = map[driver.GamepadID]mobile.Gamepad{} ) var ( - touchSlice []mobile.Touch - gamepadSlice []mobile.Gamepad + touchSlice []mobile.Touch ) func updateInput() { @@ -54,12 +48,4 @@ func updateInput() { } mobile.Get().UpdateInput(keys, runes, touchSlice) - - if runtime.GOOS == "android" { - gamepadSlice = gamepadSlice[:0] - for _, g := range gamepads { - gamepadSlice = append(gamepadSlice, g) - } - mobile.Get().UpdateGamepads(gamepadSlice) - } } diff --git a/mobile/ebitenmobileview/input_android.go b/mobile/ebitenmobileview/input_android.go index 752265997..e0ae1686d 100644 --- a/mobile/ebitenmobileview/input_android.go +++ b/mobile/ebitenmobileview/input_android.go @@ -222,7 +222,7 @@ func OnKeyDownOnAndroid(keyCode int, unicodeChar int, source int, deviceID int) return } g.Buttons[button] = true - updateInput() + updateGamepads() } case source&sourceJoystick == sourceJoystick: // DPAD keys can come here, but they are also treated as an axis at a motion event. Ignore them. @@ -248,7 +248,7 @@ func OnKeyUpOnAndroid(keyCode int, source int, deviceID int) { return } g.Buttons[button] = false - updateInput() + updateGamepads() } case source&sourceJoystick == sourceJoystick: // DPAD keys can come here, but they are also treated as an axis at a motion event. Ignore them. @@ -272,7 +272,7 @@ func OnGamepadAxesChanged(deviceID int, axisID int, value float32) { return } g.Axes[aid] = value - updateInput() + updateGamepads() } func OnGamepadAdded(deviceID int, name string, buttonNum int, axisNum int, descriptor string, vendorID int, productID int, buttonMask int, axisMask int) { @@ -309,6 +309,7 @@ func OnGamepadAdded(deviceID int, name string, buttonNum int, axisNum int, descr ButtonNum: buttonNum, AxisNum: axisNum, } + updateGamepads() } func OnInputDeviceRemoved(deviceID int) { @@ -316,4 +317,18 @@ func OnInputDeviceRemoved(deviceID int) { delete(gamepads, id) delete(deviceIDToGamepadID, deviceID) } + updateGamepads() +} + +var ( + gamepads = map[driver.GamepadID]mobile.Gamepad{} + gamepadSlice []mobile.Gamepad +) + +func updateGamepads() { + gamepadSlice = gamepadSlice[:0] + for _, g := range gamepads { + gamepadSlice = append(gamepadSlice, g) + } + mobile.Get().UpdateGamepads(gamepadSlice) }