Skip to content

Commit

Permalink
input: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sago35 committed Nov 8, 2024
1 parent 4364293 commit f6daf9a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
42 changes: 35 additions & 7 deletions inpututil.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package koebiten

import "sync"
import (
"sync"
)

type Key int

Expand Down Expand Up @@ -39,18 +41,32 @@ type pos struct {
type inputState struct {
keyDurations []int
prevKeyDurations []int
state []bool

m sync.RWMutex
}

var theInputState = &inputState{
keyDurations: make([]int, KeyMax+1),
prevKeyDurations: make([]int, KeyMax+1),
state: make([]bool, KeyMax+1),
}

var isKeyPressed = make([]bool, KeyMax)

func (i *inputState) update() {
i.m.Lock()
defer i.m.Unlock()

// Keyboard
copy(i.prevKeyDurations, i.keyDurations)
for k := Key(0); k <= KeyMax; k++ {
if i.state[k] {
i.keyDurations[k]++
} else {
i.keyDurations[k] = 0
}
}
}

// AppendPressedKeys append currently pressed keyboard keys to keys and returns the extended buffer.
Expand All @@ -60,8 +76,12 @@ func (i *inputState) update() {
//
// AppendPressedKeys is concurrent safe.
func AppendPressedKeys(keys []Key) []Key {
theInputState.m.RLock()
defer theInputState.m.RUnlock()
theInputState.m.Lock()
defer theInputState.m.Unlock()

for _, k := range keys {
theInputState.state[k] = true
}

for i, d := range theInputState.keyDurations {
if d == 0 {
Expand All @@ -88,8 +108,12 @@ func PressedKeys() []Key {
//
// AppendJustPressedKeys is concurrent safe.
func AppendJustPressedKeys(keys []Key) []Key {
theInputState.m.RLock()
defer theInputState.m.RUnlock()
theInputState.m.Lock()
defer theInputState.m.Unlock()

for _, k := range keys {
theInputState.state[k] = true
}

for i, d := range theInputState.keyDurations {
if d != 1 {
Expand All @@ -107,8 +131,12 @@ func AppendJustPressedKeys(keys []Key) []Key {
//
// AppendJustReleasedKeys is concurrent safe.
func AppendJustReleasedKeys(keys []Key) []Key {
theInputState.m.RLock()
defer theInputState.m.RUnlock()
theInputState.m.Lock()
defer theInputState.m.Unlock()

for _, k := range keys {
theInputState.state[k] = false
}

for k := Key(0); k <= KeyMax; k++ {
if theInputState.keyDurations[k] != 0 {
Expand Down
2 changes: 2 additions & 0 deletions koebiten.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func Run(d func()) error {
for {
<-tick
keyUpdate()
theInputState.update()
textY = 0
display.ClearBuffer()
d()
Expand All @@ -79,6 +80,7 @@ func RunGame(game Game) error {
for {
<-tick
keyUpdate()
theInputState.update()
textY = 0
display.ClearBuffer()
err := game.Update()
Expand Down
12 changes: 0 additions & 12 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,9 @@ func keyGpioUpdate() {
}
case NoneToPress:
state[idx] = Press
theInputState.keyDurations[idx]++
AppendJustPressedKeys([]Key{Key(idx)})
case Press:
AppendPressedKeys([]Key{Key(idx)})
theInputState.keyDurations[idx]++
if current {
cycle[idx] = 0
duration[idx]++
Expand All @@ -177,7 +175,6 @@ func keyGpioUpdate() {
case PressToRelease:
state[idx] = None
AppendJustReleasedKeys([]Key{Key(idx)})
theInputState.keyDurations[idx] = 0
}
}
}
Expand Down Expand Up @@ -207,11 +204,9 @@ func keyRotaryUpdate() {
} else {
state[idx] = PressToRelease
}
theInputState.keyDurations[idx]++
AppendJustPressedKeys([]Key{Key(idx)})
case Press:
AppendPressedKeys([]Key{Key(idx)})
theInputState.keyDurations[idx]++
if current {
} else {
state[idx] = PressToRelease
Expand All @@ -223,7 +218,6 @@ func keyRotaryUpdate() {
state[idx] = None
}
AppendJustReleasedKeys([]Key{Key(idx)})
theInputState.keyDurations[idx] = 0
}
}
}
Expand All @@ -250,11 +244,9 @@ func keyMatrixUpdate() {
}
case NoneToPress:
state[idx] = Press
theInputState.keyDurations[idx]++
AppendJustPressedKeys([]Key{Key(idx)})
case Press:
AppendPressedKeys([]Key{Key(idx)})
theInputState.keyDurations[idx]++
if current {
cycle[idx] = 0
duration[idx]++
Expand All @@ -270,7 +262,6 @@ func keyMatrixUpdate() {
case PressToRelease:
state[idx] = None
AppendJustReleasedKeys([]Key{Key(idx)})
theInputState.keyDurations[idx] = 0
}

colPins[c].Low()
Expand Down Expand Up @@ -298,11 +289,9 @@ func keyJoystickUpdate() {
}
case NoneToPress:
state[idx] = Press
theInputState.keyDurations[idx]++
AppendJustPressedKeys([]Key{Key(idx)})
case Press:
AppendPressedKeys([]Key{Key(idx)})
theInputState.keyDurations[idx]++
if current {
cycle[idx] = 0
duration[idx]++
Expand All @@ -318,7 +307,6 @@ func keyJoystickUpdate() {
case PressToRelease:
state[idx] = None
AppendJustReleasedKeys([]Key{Key(idx)})
theInputState.keyDurations[idx] = 0
}
}
}

0 comments on commit f6daf9a

Please sign in to comment.