mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
ui: Avoid creating empty slice at Touches
This commit is contained in:
parent
7d9b901ab3
commit
cc423c276d
@ -37,6 +37,8 @@ func (i *Input) GamepadIDs() []int {
|
|||||||
i.m.RLock()
|
i.m.RLock()
|
||||||
defer i.m.RUnlock()
|
defer i.m.RUnlock()
|
||||||
if len(i.gamepads) == 0 {
|
if len(i.gamepads) == 0 {
|
||||||
|
// Avoid creating a slice if possible.
|
||||||
|
// This is a performance optimization for browsers.
|
||||||
return emptyIDs
|
return emptyIDs
|
||||||
}
|
}
|
||||||
r := []int{}
|
r := []int{}
|
||||||
@ -84,9 +86,18 @@ func (i *Input) IsGamepadButtonPressed(id int, button GamepadButton) bool {
|
|||||||
return i.gamepads[id].buttonPressed[button]
|
return i.gamepads[id].buttonPressed[button]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var emptyTouches = []Touch{}
|
||||||
|
|
||||||
func (in *Input) Touches() []Touch {
|
func (in *Input) Touches() []Touch {
|
||||||
in.m.RLock()
|
in.m.RLock()
|
||||||
defer in.m.RUnlock()
|
defer in.m.RUnlock()
|
||||||
|
|
||||||
|
if len(in.touches) == 0 {
|
||||||
|
// Avoid creating a slice if possible.
|
||||||
|
// This is a performance optimization for browsers.
|
||||||
|
return emptyTouches
|
||||||
|
}
|
||||||
|
|
||||||
t := make([]Touch, len(in.touches))
|
t := make([]Touch, len(in.touches))
|
||||||
for i := 0; i < len(t); i++ {
|
for i := 0; i < len(t); i++ {
|
||||||
t[i] = &in.touches[i]
|
t[i] = &in.touches[i]
|
||||||
|
Loading…
Reference in New Issue
Block a user