mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
inpututil: add AppendJustReleasedTouchIDs and TouchPreviousPosition
Closes #2057
This commit is contained in:
parent
e21c881644
commit
bcba362e7e
87
examples/lasttouch/main.go
Normal file
87
examples/lasttouch/main.go
Normal 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)
|
||||
}
|
||||
}
|
@ -23,6 +23,11 @@ import (
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/hooks"
|
||||
)
|
||||
|
||||
type pos struct {
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
type inputState struct {
|
||||
keyDurations []int
|
||||
prevKeyDurations []int
|
||||
@ -41,7 +46,9 @@ type inputState struct {
|
||||
|
||||
touchIDs map[ebiten.TouchID]struct{}
|
||||
touchDurations map[ebiten.TouchID]int
|
||||
touchPositions map[ebiten.TouchID]pos
|
||||
prevTouchDurations map[ebiten.TouchID]int
|
||||
prevTouchPositions map[ebiten.TouchID]pos
|
||||
|
||||
gamepadIDsBuf []ebiten.GamepadID
|
||||
touchIDsBuf []ebiten.TouchID
|
||||
@ -67,7 +74,9 @@ var theInputState = &inputState{
|
||||
|
||||
touchIDs: map[ebiten.TouchID]struct{}{},
|
||||
touchDurations: map[ebiten.TouchID]int{},
|
||||
touchPositions: map[ebiten.TouchID]pos{},
|
||||
prevTouchDurations: map[ebiten.TouchID]int{},
|
||||
prevTouchPositions: map[ebiten.TouchID]pos{},
|
||||
}
|
||||
|
||||
func init() {
|
||||
@ -172,13 +181,19 @@ func (i *inputState) update() {
|
||||
|
||||
// Touches
|
||||
|
||||
// Copy the touch durations.
|
||||
// Copy the touch durations and positions.
|
||||
for id := range i.prevTouchDurations {
|
||||
delete(i.prevTouchDurations, id)
|
||||
}
|
||||
for id := range i.touchDurations {
|
||||
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 {
|
||||
delete(i.touchIDs, id)
|
||||
@ -187,10 +202,13 @@ func (i *inputState) update() {
|
||||
for _, id := range i.touchIDsBuf {
|
||||
i.touchIDs[id] = struct{}{}
|
||||
i.touchDurations[id]++
|
||||
x, y := ebiten.TouchPosition(id)
|
||||
i.touchPositions[id] = pos{x: x, y: y}
|
||||
}
|
||||
for id := range i.touchDurations {
|
||||
if _, ok := i.touchIDs[id]; !ok {
|
||||
delete(i.touchDurations, id)
|
||||
delete(i.touchPositions, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -403,18 +421,21 @@ func StandardGamepadButtonPressDuration(id ebiten.GamepadID, button ebiten.Stand
|
||||
//
|
||||
// AppendJustPressedTouchIDs is concurrent safe.
|
||||
func AppendJustPressedTouchIDs(touchIDs []ebiten.TouchID) []ebiten.TouchID {
|
||||
origLen := len(touchIDs)
|
||||
theInputState.m.RLock()
|
||||
defer theInputState.m.RUnlock()
|
||||
|
||||
origLen := len(touchIDs)
|
||||
for id, s := range theInputState.touchDurations {
|
||||
if s == 1 {
|
||||
touchIDs = append(touchIDs, id)
|
||||
}
|
||||
}
|
||||
theInputState.m.RUnlock()
|
||||
|
||||
s := touchIDs[origLen:]
|
||||
sort.Slice(s, func(a, b int) bool {
|
||||
return s[a] < s[b]
|
||||
})
|
||||
|
||||
return touchIDs
|
||||
}
|
||||
|
||||
@ -425,15 +446,39 @@ func JustPressedTouchIDs() []ebiten.TouchID {
|
||||
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
|
||||
// whether the given touch is released just in the current tick.
|
||||
//
|
||||
// IsTouchJustReleased is concurrent safe.
|
||||
func IsTouchJustReleased(id ebiten.TouchID) bool {
|
||||
theInputState.m.RLock()
|
||||
r := theInputState.touchDurations[id] == 0 && theInputState.prevTouchDurations[id] > 0
|
||||
theInputState.m.RUnlock()
|
||||
return r
|
||||
defer theInputState.m.RUnlock()
|
||||
|
||||
return theInputState.touchDurations[id] == 0 && theInputState.prevTouchDurations[id] > 0
|
||||
}
|
||||
|
||||
// TouchPressDuration returns how long the touch remains in ticks (Update).
|
||||
@ -445,3 +490,15 @@ func TouchPressDuration(id ebiten.TouchID) int {
|
||||
theInputState.m.RUnlock()
|
||||
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user