internal/driver: Change some functions to Append*

Updates #1692
This commit is contained in:
Hajime Hoshi 2021-07-10 01:15:47 +09:00
parent 5d2c8ad9be
commit 99a6b1b03e
5 changed files with 32 additions and 60 deletions

View File

@ -32,7 +32,7 @@ import (
//
// Keyboards don't work on iOS yet (#1090).
func InputChars() []rune {
return uiDriver().Input().RuneBuffer()
return uiDriver().Input().AppendInputChars(nil)
}
// IsKeyPressed returns a boolean indicating whether key is pressed.
@ -140,7 +140,7 @@ func GamepadName(id GamepadID) string {
//
// GamepadIDs always returns an empty slice on iOS.
func GamepadIDs() []GamepadID {
return uiDriver().Input().GamepadIDs()
return uiDriver().Input().AppendGamepadIDs(nil)
}
// GamepadAxisNum returns the number of axes of the gamepad (id).
@ -198,7 +198,7 @@ type TouchID = driver.TouchID
//
// TouchIDs is concurrent-safe.
func TouchIDs() []TouchID {
return uiDriver().Input().TouchIDs()
return uiDriver().Input().AppendTouchIDs(nil)
}
// TouchPosition returns the position for the touch of the specified ID.

View File

@ -19,18 +19,18 @@ type GamepadID int
type TouchID int
type Input interface {
AppendInputChars(runes []rune) []rune
AppendGamepadIDs(gamepadIDs []GamepadID) []GamepadID
AppendTouchIDs(touchIDs []TouchID) []TouchID
CursorPosition() (x, y int)
GamepadSDLID(id GamepadID) string
GamepadName(id GamepadID) string
GamepadAxis(id GamepadID, axis int) float64
GamepadAxisNum(id GamepadID) int
GamepadButtonNum(id GamepadID) int
GamepadIDs() []GamepadID
IsGamepadButtonPressed(id GamepadID, button GamepadButton) bool
IsKeyPressed(key Key) bool
IsMouseButtonPressed(button MouseButton) bool
RuneBuffer() []rune
TouchIDs() []TouchID
TouchPosition(id TouchID) (x, y int)
Wheel() (xoff, yoff float64)
}

View File

@ -67,20 +67,19 @@ func (i *Input) CursorPosition() (x, y int) {
return i.cursorX, i.cursorY
}
func (i *Input) GamepadIDs() []driver.GamepadID {
func (i *Input) AppendGamepadIDs(gamepadIDs []driver.GamepadID) []driver.GamepadID {
if !i.ui.isRunning() {
return nil
}
i.ui.m.RLock()
defer i.ui.m.RUnlock()
r := make([]driver.GamepadID, 0, len(i.gamepads))
for id, g := range i.gamepads {
if g.valid {
r = append(r, driver.GamepadID(id))
gamepadIDs = append(gamepadIDs, driver.GamepadID(id))
}
}
return r
return gamepadIDs
}
func (i *Input) GamepadSDLID(id driver.GamepadID) string {
@ -161,21 +160,17 @@ func (i *Input) IsGamepadButtonPressed(id driver.GamepadID, button driver.Gamepa
return i.gamepads[id].buttonPressed[button]
}
func (i *Input) TouchIDs() []driver.TouchID {
func (i *Input) AppendTouchIDs(touchIDs []driver.TouchID) []driver.TouchID {
if !i.ui.isRunning() {
return nil
}
i.ui.m.RLock()
defer i.ui.m.RUnlock()
if len(i.touches) == 0 {
return nil
}
ids := make([]driver.TouchID, 0, len(i.touches))
for id := range i.touches {
ids = append(ids, id)
touchIDs = append(touchIDs, id)
}
return ids
return touchIDs
}
func (i *Input) TouchPosition(id driver.TouchID) (x, y int) {
@ -193,16 +188,14 @@ func (i *Input) TouchPosition(id driver.TouchID) (x, y int) {
return 0, 0
}
func (i *Input) RuneBuffer() []rune {
func (i *Input) AppendInputChars(runes []rune) []rune {
if !i.ui.isRunning() {
return nil
}
i.ui.m.RLock()
defer i.ui.m.RUnlock()
rs := make([]rune, len(i.runeBuffer))
copy(rs, i.runeBuffer)
return rs
return append(runes, i.runeBuffer...)
}
func (i *Input) resetForFrame() {

View File

@ -114,16 +114,11 @@ func (i *Input) GamepadName(id driver.GamepadID) string {
return g.name
}
func (i *Input) GamepadIDs() []driver.GamepadID {
if len(i.gamepads) == 0 {
return nil
}
r := make([]driver.GamepadID, 0, len(i.gamepads))
func (i *Input) AppendGamepadIDs(gamepadIDs []driver.GamepadID) []driver.GamepadID {
for id := range i.gamepads {
r = append(r, id)
gamepadIDs = append(gamepadIDs, id)
}
return r
return gamepadIDs
}
func (i *Input) GamepadAxisNum(id driver.GamepadID) int {
@ -164,16 +159,11 @@ func (i *Input) IsGamepadButtonPressed(id driver.GamepadID, button driver.Gamepa
return g.buttonPressed[button]
}
func (i *Input) TouchIDs() []driver.TouchID {
if len(i.touches) == 0 {
return nil
}
ids := make([]driver.TouchID, 0, len(i.touches))
func (i *Input) AppendTouchIDs(touchIDs []driver.TouchID) []driver.TouchID {
for id := range i.touches {
ids = append(ids, id)
touchIDs = append(touchIDs, id)
}
return ids
return touchIDs
}
func (i *Input) TouchPosition(id driver.TouchID) (x, y int) {
@ -187,10 +177,8 @@ func (i *Input) TouchPosition(id driver.TouchID) (x, y int) {
return 0, 0
}
func (i *Input) RuneBuffer() []rune {
rs := make([]rune, len(i.runeBuffer))
copy(rs, i.runeBuffer)
return rs
func (i *Input) AppendInputChars(runes []rune) []rune {
return append(runes, i.runeBuffer...)
}
func (i *Input) resetForFrame() {

View File

@ -38,15 +38,14 @@ func (i *Input) CursorPosition() (x, y int) {
return 0, 0
}
func (i *Input) GamepadIDs() []driver.GamepadID {
func (i *Input) AppendGamepadIDs(gamepadIDs []driver.GamepadID) []driver.GamepadID {
i.ui.m.RLock()
defer i.ui.m.RUnlock()
ids := make([]driver.GamepadID, 0, len(i.gamepads))
for _, g := range i.gamepads {
ids = append(ids, g.ID)
gamepadIDs = append(gamepadIDs, g.ID)
}
return ids
return gamepadIDs
}
func (i *Input) GamepadSDLID(id driver.GamepadID) string {
@ -133,19 +132,14 @@ func (i *Input) IsGamepadButtonPressed(id driver.GamepadID, button driver.Gamepa
return false
}
func (i *Input) TouchIDs() []driver.TouchID {
func (i *Input) AppendTouchIDs(touchIDs []driver.TouchID) []driver.TouchID {
i.ui.m.RLock()
defer i.ui.m.RUnlock()
if len(i.touches) == 0 {
return nil
}
ids := make([]driver.TouchID, 0, len(i.touches))
for id := range i.touches {
ids = append(ids, id)
touchIDs = append(touchIDs, id)
}
return ids
return touchIDs
}
func (i *Input) TouchPosition(id driver.TouchID) (x, y int) {
@ -160,13 +154,10 @@ func (i *Input) TouchPosition(id driver.TouchID) (x, y int) {
return 0, 0
}
func (i *Input) RuneBuffer() []rune {
i.ui.m.RLock()
defer i.ui.m.RUnlock()
rs := make([]rune, len(i.runes))
copy(rs, i.runes)
return rs
func (i *Input) AppendInputChars(runes []rune) []rune {
i.ui.m.Lock()
defer i.ui.m.Unlock()
return append(runes, i.runes...)
}
func (i *Input) IsKeyPressed(key driver.Key) bool {