-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
116 lines (92 loc) · 2.21 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//go:build example
package main
import (
"fmt"
"log"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
input "github.com/quasilyte/ebitengine-input"
)
const (
ActionUnknown input.Action = iota
ActionPing
ActionRemap
)
func main() {
ebiten.SetWindowSize(640, 480)
if err := ebiten.RunGame(newExampleGame()); err != nil {
log.Fatal(err)
}
}
type exampleGame struct {
started bool
k input.Key
prevK input.Key
scanning bool
keyScanner input.KeyScanner
inputHandler *input.Handler
inputSystem input.System
}
func newExampleGame() *exampleGame {
g := &exampleGame{}
g.inputSystem.Init(input.SystemConfig{
DevicesEnabled: input.AnyDevice,
})
return g
}
func (g *exampleGame) Layout(_, _ int) (int, int) {
return 640, 480
}
func (g *exampleGame) Draw(screen *ebiten.Image) {
if g.scanning {
ebitenutil.DebugPrint(screen, fmt.Sprintf("keybind: %s\n<scanning the new keybing>", g.k))
} else {
ebitenutil.DebugPrint(screen, fmt.Sprintf("keybind: %s\npress ctrl+enter to remap", g.k))
}
}
func (g *exampleGame) Update() error {
g.inputSystem.Update()
if !g.started {
g.Init()
g.started = true
}
g.handleRemap()
if !g.scanning && g.inputHandler.ActionIsJustPressed(ActionPing) {
fmt.Printf("ping! (activated with %s keybind)\n", g.k)
}
return nil
}
func (g *exampleGame) handleRemap() {
if !g.scanning {
if g.inputHandler.ActionIsJustPressed(ActionRemap) {
g.prevK = g.k // Save it for an easier fallback
g.scanning = true
}
return
}
k, status := g.keyScanner.Scan()
if status != input.KeyScanUnchanged {
g.k = k
}
if status == input.KeyScanCompleted {
g.scanning = false
// Check for the new key to be available.
// Resolve the conflicts here; I'll just reject
// the combination that is already in use.
if g.k == input.KeyWithModifier(input.KeyEnter, input.ModControl) {
g.k = g.prevK
} else {
g.inputHandler.Remap(g.makeKeymap())
}
}
}
func (g *exampleGame) makeKeymap() input.Keymap {
return input.Keymap{
ActionPing: {g.k},
ActionRemap: {input.KeyWithModifier(input.KeyEnter, input.ModControl)},
}
}
func (g *exampleGame) Init() {
g.k = input.KeyQ
g.inputHandler = g.inputSystem.NewHandler(0, g.makeKeymap())
}