Skip to content

Commit 2237944

Browse files
authored
add press duration to PressedActionInfo (#33)
Add key press duration to the PressedActionInfo's EventInfo. Note: includes a minOf function that can be replaced with builtin min after we switch to Go 1.21. Fixes #32
1 parent fe527f9 commit 2237944

6 files changed

Lines changed: 62 additions & 10 deletions

File tree

event.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,18 @@ type SimulatedAction struct {
4242
//
4343
// StartPos is only set for a few events where it makes sense.
4444
// A drag event, for instance, will store the "dragging from" location there.
45+
//
46+
// Duration carries the key press duration if available.
47+
// Duration specifies how long the key has been pressed in ticks same as inpututil.KeyPressDuration.
48+
// Duration for key press with modifiers it will return the lowest duration of all key presses.
49+
// Use HasDuration() predicate to know whether there is a duration associated
50+
// with the event to distinguish between 0 duration and lack of duration info.
4551
type EventInfo struct {
46-
kind keyKind
47-
hasPos bool
52+
kind keyKind
53+
hasPos bool
54+
hasDuration bool
4855

56+
Duration int
4957
Pos Vec
5058
StartPos Vec
5159
}
@@ -54,6 +62,10 @@ type EventInfo struct {
5462
// Use Pos field to get the pos value.
5563
func (e EventInfo) HasPos() bool { return e.hasPos }
5664

65+
// HasDuration reports whether this event has a press duration associated with it.
66+
// Use Duration field to get the press duration value.
67+
func (e EventInfo) HasDuration() bool { return e.hasDuration }
68+
5769
// IsTouchEvent reports whether this event was triggered by a screen touch device.
5870
func (e EventInfo) IsTouchEvent() bool { return e.kind == keyTouch }
5971

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ go 1.18
55
require (
66
github.com/hajimehoshi/ebiten/v2 v2.5.9
77
github.com/quasilyte/gmath v0.0.0-20221217210116-fba37a2e15c7
8+
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
89
)
910

1011
require (
1112
github.com/ebitengine/purego v0.4.0 // indirect
1213
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b // indirect
1314
github.com/jezek/xgb v1.1.0 // indirect
14-
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect
1515
golang.org/x/image v0.10.0 // indirect
1616
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c // indirect
1717
golang.org/x/sync v0.1.0 // indirect
18-
golang.org/x/sys v0.7.0 // indirect
18+
golang.org/x/sys v0.13.0 // indirect
1919
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8U
1515
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
1616
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 h1:estk1glOnSVeJ9tdEZZc5mAMDZk5lNJNyJ6DvrBkTEU=
1717
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4=
18+
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
19+
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
1820
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
1921
golang.org/x/image v0.10.0 h1:gXjUUtwtx5yOE0VKWq1CH4IJAClq4UGgUA3i+rpON9M=
2022
golang.org/x/image v0.10.0/go.mod h1:jtrku+n79PfroUbvDdeUWMAI+heR786BofxrbiSF+J0=
@@ -43,6 +45,8 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
4345
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4446
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
4547
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
48+
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
49+
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4650
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
4751
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
4852
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=

handler.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ func (h *Handler) PressedActionInfo(action Action) (EventInfo, bool) {
239239
info.hasPos = keyHasPos(k.kind)
240240
info.Pos = h.getKeyPos(k)
241241
info.StartPos = h.getKeyStartPos(k)
242+
info.hasDuration = keyHasDuration(k.kind)
243+
info.Duration = h.getKeyPressDuration(k)
242244
return info, true
243245
}
244246
return EventInfo{}, false
@@ -380,6 +382,27 @@ func (h *Handler) getKeyPos(k Key) Vec {
380382
return result
381383
}
382384

385+
// getKeyPressDuration returns how long the key has been pressed in ticks same as inpututil.KeyPressDuration.
386+
// When looking at a key press with modifiers it will return the lowest duration of all key presses.
387+
func (h *Handler) getKeyPressDuration(k Key) int {
388+
switch k.kind {
389+
case keyKeyboardWithShift:
390+
return minOf(inpututil.KeyPressDuration(ebiten.Key(k.code)), inpututil.KeyPressDuration(ebiten.KeyShift))
391+
case keyKeyboardWithCtrl:
392+
return minOf(inpututil.KeyPressDuration(ebiten.Key(k.code)), inpututil.KeyPressDuration(ebiten.KeyControl))
393+
case keyKeyboardWithCtrlShift:
394+
return minOf(
395+
inpututil.KeyPressDuration(ebiten.Key(k.code)),
396+
minOf(
397+
inpututil.KeyPressDuration(ebiten.KeyShift),
398+
inpututil.KeyPressDuration(ebiten.KeyControl)))
399+
case keyKeyboard:
400+
return inpututil.KeyPressDuration(ebiten.Key(k.code))
401+
}
402+
403+
return 0
404+
}
405+
383406
func (h *Handler) keyIsPressed(k Key) bool {
384407
switch k.kind {
385408
case keyTouch:

internal_key.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,21 @@ type keyKindFlag uint8
5454
const (
5555
keyFlagHasPos keyKindFlag = 1 << iota
5656
keyFlagNeedID
57+
keyFlagHasDuration
5758
)
5859

59-
func keyHasPos(k keyKind) bool { return keyKindFlagTable[k]&keyFlagHasPos != 0 }
60-
func keyNeedID(k keyKind) bool { return keyKindFlagTable[k]&keyFlagNeedID != 0 }
60+
func keyHasPos(k keyKind) bool { return keyKindFlagTable[k]&keyFlagHasPos != 0 }
61+
func keyNeedID(k keyKind) bool { return keyKindFlagTable[k]&keyFlagNeedID != 0 }
62+
func keyHasDuration(k keyKind) bool { return keyKindFlagTable[k]&keyFlagHasDuration != 0 }
6163

6264
// Using a 256-byte LUT to get a fast map-like lookup without a bound check.
6365
var keyKindFlagTable = [256]keyKindFlag{
6466
keySimulated: keyFlagHasPos | keyFlagNeedID,
6567

66-
keyKeyboard: 0,
67-
keyKeyboardWithCtrl: 0,
68-
keyKeyboardWithShift: 0,
69-
keyKeyboardWithCtrlShift: 0,
68+
keyKeyboard: keyFlagHasDuration,
69+
keyKeyboardWithCtrl: keyFlagHasDuration,
70+
keyKeyboardWithShift: keyFlagHasDuration,
71+
keyKeyboardWithCtrlShift: keyFlagHasDuration,
7072

7173
keyGamepad: keyFlagNeedID,
7274
keyGamepadLeftStick: keyFlagNeedID,

internal_min.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package input
2+
3+
import "golang.org/x/exp/constraints"
4+
5+
// minOf is a placeholder implementation until Go 1.21's builtin min implementation is available
6+
func minOf[T constraints.Ordered](a, b T) T {
7+
if a < b {
8+
return a
9+
}
10+
return b
11+
}

0 commit comments

Comments
 (0)