inpututil: add AppendJustReleasedTouchIDs and TouchPreviousPosition

Closes #2057
This commit is contained in:
Hajime Hoshi 2022-08-16 00:53:53 +09:00
parent e21c881644
commit bcba362e7e
2 changed files with 150 additions and 6 deletions

View File

@ -0,0 +1,87 @@
// 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.
//go:build example
// +build example
package main
import (
"fmt"
"log"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
const (
screenWidth = 640
screenHeight = 480
)
type pos struct {
x int
y int
}
type Game struct {
releasedTouchIDs []ebiten.TouchID
lastTouchPositions []pos
}
func (g *Game) Update() error {
// In general, Append* function allocates a new slice if the given slice doesn't have enough capacity,
// or otherwise, just extends the length of the given slice.
//
// This example passes an empty slice that might have a capacity in order to reduce the chances of slice allocations.
// You can also pass 'nil' to AppendJustReleasedTouchIDs if you don't care the cost of creating slices.
// In this case, AppendJustReleasedTouchIDs would always create a new slice.
g.releasedTouchIDs = inpututil.AppendJustReleasedTouchIDs(g.releasedTouchIDs[:0])
for _, id := range g.releasedTouchIDs {
// Get the last position of the touch.
// ebiten.TouchPosition would not work as the touch has already gone in this tick.
x, y := inpututil.TouchPositionInPreviousTick(id)
g.lastTouchPositions = append(g.lastTouchPositions, pos{x: x, y: y})
}
const n = 10
if len(g.lastTouchPositions) > n {
copy(g.lastTouchPositions, g.lastTouchPositions[len(g.lastTouchPositions)-n:])
g.lastTouchPositions = g.lastTouchPositions[:n]
}
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
msg := "Touch the screen and release your finger from it.\n\nLast Positions:\n"
for _, p := range g.lastTouchPositions {
msg += fmt.Sprintf(" (%d, %d)\n", p.x, p.y)
}
ebitenutil.DebugPrint(screen, msg)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Last Touch Positions (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err)
}
}

View File

@ -23,6 +23,11 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/hooks" "github.com/hajimehoshi/ebiten/v2/internal/hooks"
) )
type pos struct {
x int
y int
}
type inputState struct { type inputState struct {
keyDurations []int keyDurations []int
prevKeyDurations []int prevKeyDurations []int
@ -41,7 +46,9 @@ type inputState struct {
touchIDs map[ebiten.TouchID]struct{} touchIDs map[ebiten.TouchID]struct{}
touchDurations map[ebiten.TouchID]int touchDurations map[ebiten.TouchID]int
touchPositions map[ebiten.TouchID]pos
prevTouchDurations map[ebiten.TouchID]int prevTouchDurations map[ebiten.TouchID]int
prevTouchPositions map[ebiten.TouchID]pos
gamepadIDsBuf []ebiten.GamepadID gamepadIDsBuf []ebiten.GamepadID
touchIDsBuf []ebiten.TouchID touchIDsBuf []ebiten.TouchID
@ -67,7 +74,9 @@ var theInputState = &inputState{
touchIDs: map[ebiten.TouchID]struct{}{}, touchIDs: map[ebiten.TouchID]struct{}{},
touchDurations: map[ebiten.TouchID]int{}, touchDurations: map[ebiten.TouchID]int{},
touchPositions: map[ebiten.TouchID]pos{},
prevTouchDurations: map[ebiten.TouchID]int{}, prevTouchDurations: map[ebiten.TouchID]int{},
prevTouchPositions: map[ebiten.TouchID]pos{},
} }
func init() { func init() {
@ -172,13 +181,19 @@ func (i *inputState) update() {
// Touches // Touches
// Copy the touch durations. // Copy the touch durations and positions.
for id := range i.prevTouchDurations { for id := range i.prevTouchDurations {
delete(i.prevTouchDurations, id) delete(i.prevTouchDurations, id)
} }
for id := range i.touchDurations { for id := range i.touchDurations {
i.prevTouchDurations[id] = i.touchDurations[id] i.prevTouchDurations[id] = i.touchDurations[id]
} }
for id := range i.prevTouchPositions {
delete(i.prevTouchPositions, id)
}
for id := range i.touchPositions {
i.prevTouchPositions[id] = i.touchPositions[id]
}
for id := range i.touchIDs { for id := range i.touchIDs {
delete(i.touchIDs, id) delete(i.touchIDs, id)
@ -187,10 +202,13 @@ func (i *inputState) update() {
for _, id := range i.touchIDsBuf { for _, id := range i.touchIDsBuf {
i.touchIDs[id] = struct{}{} i.touchIDs[id] = struct{}{}
i.touchDurations[id]++ i.touchDurations[id]++
x, y := ebiten.TouchPosition(id)
i.touchPositions[id] = pos{x: x, y: y}
} }
for id := range i.touchDurations { for id := range i.touchDurations {
if _, ok := i.touchIDs[id]; !ok { if _, ok := i.touchIDs[id]; !ok {
delete(i.touchDurations, id) delete(i.touchDurations, id)
delete(i.touchPositions, id)
} }
} }
} }
@ -403,18 +421,21 @@ func StandardGamepadButtonPressDuration(id ebiten.GamepadID, button ebiten.Stand
// //
// AppendJustPressedTouchIDs is concurrent safe. // AppendJustPressedTouchIDs is concurrent safe.
func AppendJustPressedTouchIDs(touchIDs []ebiten.TouchID) []ebiten.TouchID { func AppendJustPressedTouchIDs(touchIDs []ebiten.TouchID) []ebiten.TouchID {
origLen := len(touchIDs)
theInputState.m.RLock() theInputState.m.RLock()
defer theInputState.m.RUnlock()
origLen := len(touchIDs)
for id, s := range theInputState.touchDurations { for id, s := range theInputState.touchDurations {
if s == 1 { if s == 1 {
touchIDs = append(touchIDs, id) touchIDs = append(touchIDs, id)
} }
} }
theInputState.m.RUnlock()
s := touchIDs[origLen:] s := touchIDs[origLen:]
sort.Slice(s, func(a, b int) bool { sort.Slice(s, func(a, b int) bool {
return s[a] < s[b] return s[a] < s[b]
}) })
return touchIDs return touchIDs
} }
@ -425,15 +446,39 @@ func JustPressedTouchIDs() []ebiten.TouchID {
return AppendJustPressedTouchIDs(nil) return AppendJustPressedTouchIDs(nil)
} }
// AppendJustReleasedTouchIDs append touch IDs that are released just in the current tick to touchIDs,
// and returns the extended buffer.
// Giving a slice that already has enough capacity works efficiently.
//
// AppendJustReleasedTouchIDs is concurrent safe.
func AppendJustReleasedTouchIDs(touchIDs []ebiten.TouchID) []ebiten.TouchID {
theInputState.m.RLock()
defer theInputState.m.RUnlock()
origLen := len(touchIDs)
for id := range theInputState.prevTouchDurations {
if theInputState.touchDurations[id] == 0 && theInputState.prevTouchDurations[id] > 0 {
touchIDs = append(touchIDs, id)
}
}
s := touchIDs[origLen:]
sort.Slice(s, func(a, b int) bool {
return s[a] < s[b]
})
return touchIDs
}
// IsTouchJustReleased returns a boolean value indicating // IsTouchJustReleased returns a boolean value indicating
// whether the given touch is released just in the current tick. // whether the given touch is released just in the current tick.
// //
// IsTouchJustReleased is concurrent safe. // IsTouchJustReleased is concurrent safe.
func IsTouchJustReleased(id ebiten.TouchID) bool { func IsTouchJustReleased(id ebiten.TouchID) bool {
theInputState.m.RLock() theInputState.m.RLock()
r := theInputState.touchDurations[id] == 0 && theInputState.prevTouchDurations[id] > 0 defer theInputState.m.RUnlock()
theInputState.m.RUnlock()
return r return theInputState.touchDurations[id] == 0 && theInputState.prevTouchDurations[id] > 0
} }
// TouchPressDuration returns how long the touch remains in ticks (Update). // TouchPressDuration returns how long the touch remains in ticks (Update).
@ -445,3 +490,15 @@ func TouchPressDuration(id ebiten.TouchID) int {
theInputState.m.RUnlock() theInputState.m.RUnlock()
return s return s
} }
// TouchPositionInPreviousTick returns the position in the previous tick.
// If the touch is a just-released touch, TouchPositionInPreviousTick returns the last position of the touch.
//
// TouchJustReleasedPosition is concurrent safe.
func TouchPositionInPreviousTick(id ebiten.TouchID) (int, int) {
theInputState.m.RLock()
defer theInputState.m.RUnlock()
p := theInputState.prevTouchPositions[id]
return p.x, p.y
}