-
-
Notifications
You must be signed in to change notification settings - Fork 622
/
input.go
219 lines (201 loc) · 4.47 KB
/
input.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2016 The Ebiten 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.
package twenty48
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
// Dir represents a direction.
type Dir int
const (
DirUp Dir = iota
DirRight
DirDown
DirLeft
)
type mouseState int
const (
mouseStateNone mouseState = iota
mouseStatePressing
mouseStateSettled
)
type touchState int
const (
touchStateNone touchState = iota
touchStatePressing
touchStateSettled
touchStateInvalid
)
// String returns a string representing the direction.
func (d Dir) String() string {
switch d {
case DirUp:
return "Up"
case DirRight:
return "Right"
case DirDown:
return "Down"
case DirLeft:
return "Left"
}
panic("not reach")
}
// Vector returns a [-1, 1] value for each axis.
func (d Dir) Vector() (x, y int) {
switch d {
case DirUp:
return 0, -1
case DirRight:
return 1, 0
case DirDown:
return 0, 1
case DirLeft:
return -1, 0
}
panic("not reach")
}
// Input represents the current key states.
type Input struct {
mouseState mouseState
mouseInitPosX int
mouseInitPosY int
mouseDir Dir
touches []ebiten.TouchID
touchState touchState
touchID ebiten.TouchID
touchInitPosX int
touchInitPosY int
touchLastPosX int
touchLastPosY int
touchDir Dir
}
// NewInput generates a new Input object.
func NewInput() *Input {
return &Input{}
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
func vecToDir(dx, dy int) (Dir, bool) {
if abs(dx) < 4 && abs(dy) < 4 {
return 0, false
}
if abs(dx) < abs(dy) {
if dy < 0 {
return DirUp, true
}
return DirDown, true
}
if dx < 0 {
return DirLeft, true
}
return DirRight, true
}
// Update updates the current input states.
func (i *Input) Update() {
switch i.mouseState {
case mouseStateNone:
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
x, y := ebiten.CursorPosition()
i.mouseInitPosX = x
i.mouseInitPosY = y
i.mouseState = mouseStatePressing
}
case mouseStatePressing:
if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
x, y := ebiten.CursorPosition()
dx := x - i.mouseInitPosX
dy := y - i.mouseInitPosY
d, ok := vecToDir(dx, dy)
if !ok {
i.mouseState = mouseStateNone
break
}
i.mouseDir = d
i.mouseState = mouseStateSettled
}
case mouseStateSettled:
i.mouseState = mouseStateNone
}
i.touches = ebiten.AppendTouchIDs(i.touches[:0])
switch i.touchState {
case touchStateNone:
if len(i.touches) == 1 {
i.touchID = i.touches[0]
x, y := ebiten.TouchPosition(i.touches[0])
i.touchInitPosX = x
i.touchInitPosY = y
i.touchLastPosX = x
i.touchLastPosX = y
i.touchState = touchStatePressing
}
case touchStatePressing:
if len(i.touches) >= 2 {
break
}
if len(i.touches) == 1 {
if i.touches[0] != i.touchID {
i.touchState = touchStateInvalid
} else {
x, y := ebiten.TouchPosition(i.touches[0])
i.touchLastPosX = x
i.touchLastPosY = y
}
break
}
if len(i.touches) == 0 {
dx := i.touchLastPosX - i.touchInitPosX
dy := i.touchLastPosY - i.touchInitPosY
d, ok := vecToDir(dx, dy)
if !ok {
i.touchState = touchStateNone
break
}
i.touchDir = d
i.touchState = touchStateSettled
}
case touchStateSettled:
i.touchState = touchStateNone
case touchStateInvalid:
if len(i.touches) == 0 {
i.touchState = touchStateNone
}
}
}
// Dir returns a currently pressed direction.
// Dir returns false if no direction key is pressed.
func (i *Input) Dir() (Dir, bool) {
if inpututil.IsKeyJustPressed(ebiten.KeyArrowUp) {
return DirUp, true
}
if inpututil.IsKeyJustPressed(ebiten.KeyArrowLeft) {
return DirLeft, true
}
if inpututil.IsKeyJustPressed(ebiten.KeyArrowRight) {
return DirRight, true
}
if inpututil.IsKeyJustPressed(ebiten.KeyArrowDown) {
return DirDown, true
}
if i.mouseState == mouseStateSettled {
return i.mouseDir, true
}
if i.touchState == touchStateSettled {
return i.touchDir, true
}
return 0, false
}