internal/uidriver/mobile: Refactoring

Updates #1105
This commit is contained in:
Hajime Hoshi 2021-10-16 23:37:57 +09:00
parent 2d9349824f
commit a70be6e2f8
6 changed files with 72 additions and 32 deletions

View File

@ -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...)
}

View File

@ -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).

View File

@ -212,11 +212,6 @@ func (i *Input) update(keys map[driver.Key]struct{}, runes []rune, touches []Tou
i.touches = append(i.touches, touches...) 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() { func (i *Input) resetForFrame() {
i.runes = nil i.runes = nil
} }

View File

@ -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 { type RenderRequester interface {
SetExplicitRenderingMode(explicitRendering bool) SetExplicitRenderingMode(explicitRendering bool)
RequestRenderIfNeeded() RequestRenderIfNeeded()

View File

@ -18,8 +18,6 @@
package ebitenmobileview package ebitenmobileview
import ( import (
"runtime"
"github.com/hajimehoshi/ebiten/v2/internal/driver" "github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/uidriver/mobile" "github.com/hajimehoshi/ebiten/v2/internal/uidriver/mobile"
) )
@ -33,14 +31,10 @@ var (
keys = map[driver.Key]struct{}{} keys = map[driver.Key]struct{}{}
runes []rune runes []rune
touches = map[driver.TouchID]position{} touches = map[driver.TouchID]position{}
// gamepads is updated only at Android.
gamepads = map[driver.GamepadID]mobile.Gamepad{}
) )
var ( var (
touchSlice []mobile.Touch touchSlice []mobile.Touch
gamepadSlice []mobile.Gamepad
) )
func updateInput() { func updateInput() {
@ -54,12 +48,4 @@ func updateInput() {
} }
mobile.Get().UpdateInput(keys, runes, touchSlice) 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)
}
} }

View File

@ -222,7 +222,7 @@ func OnKeyDownOnAndroid(keyCode int, unicodeChar int, source int, deviceID int)
return return
} }
g.Buttons[button] = true g.Buttons[button] = true
updateInput() updateGamepads()
} }
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.
@ -248,7 +248,7 @@ func OnKeyUpOnAndroid(keyCode int, source int, deviceID int) {
return return
} }
g.Buttons[button] = false g.Buttons[button] = false
updateInput() updateGamepads()
} }
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.
@ -272,7 +272,7 @@ func OnGamepadAxesChanged(deviceID int, axisID int, value float32) {
return return
} }
g.Axes[aid] = value 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) { 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, ButtonNum: buttonNum,
AxisNum: axisNum, AxisNum: axisNum,
} }
updateGamepads()
} }
func OnInputDeviceRemoved(deviceID int) { func OnInputDeviceRemoved(deviceID int) {
@ -316,4 +317,18 @@ func OnInputDeviceRemoved(deviceID int) {
delete(gamepads, id) delete(gamepads, id)
delete(deviceIDToGamepadID, deviceID) 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)
} }