/
Event.purs
335 lines (290 loc) · 10.3 KB
/
Event.purs
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
module Game.Aff.Web.Event
( qSelEventTarget
, windowEventTarget
, documentEventTarget
, bodyEventTarget
, EventInfo
, EventExecIn
, _event
, eventUpdate
, UIEventRow
, _uiEvent
, reduceUIEventRow
, uiEventUpdate
, MouseEventRow
, _mouseEvent
, _posInTarget
, reduceMouseEventRow
, mouseEventUpdate
, mousemove
, click
, mousedown
, mouseup
, KeyboardEventRow
, _keyboardEvent
, reduceKeyboardEventRow
, keyboardEventUpdate
, keydown
, keyup
, keypressed
, change
, load
) where
import Prelude
import Control.Monad.Error.Class (throwError)
import Data.Either (Either(..))
import Data.Maybe (Maybe(..))
import Data.Int (round)
import Data.Traversable (for_, traverse_)
import Data.Vector.Polymorphic (Vector2, (><))
import Effect.Aff (effectCanceler, makeAff, runAff_)
import Game (GameUpdate(..), runReducer)
import Game.Aff (AffGameUpdate, _end, _env, _stateRef)
import Game.Aff.Web.Util (qSel)
import Game.Util (maybeThrow, newRef, readRef, runStateWithRef, writeRef)
import Game.Util.Maybe (liftBoth)
import Run (AFF, EFFECT, Run, SProxy(..), expand, liftAff, liftEffect, runBaseAff')
import Run.Choose (CHOOSE, runChoose)
import Run.Except (EXCEPT, FAIL, runExceptAt, throwAt)
import Run.Reader (READER, askAt, runReaderAt)
import Run.State (STATE, execState)
import Web.DOM.Element (toEventTarget) as Element
import Web.DOM.ParentNode (QuerySelector)
import Web.Event.Event (Event, EventType(..))
import Web.Event.Event (currentTarget) as Event
import Web.Event.EventTarget (EventTarget, addEventListener, eventListener, removeEventListener)
import Web.HTML (window)
import Web.HTML.Window (document)
import Web.HTML.Window (toEventTarget) as Window
import Web.HTML.HTMLElement (toEventTarget, getBoundingClientRect, fromEventTarget) as HTMLElement
import Web.HTML.HTMLDocument (body)
import Web.HTML.HTMLDocument (toEventTarget) as HTMLDocument
import Web.UIEvent.UIEvent (UIEvent)
import Web.UIEvent.UIEvent as UIEvent
import Web.UIEvent.MouseEvent (MouseEvent)
import Web.UIEvent.MouseEvent as MouseEvent
import Web.UIEvent.KeyboardEvent (KeyboardEvent)
import Web.UIEvent.KeyboardEvent as KeyboardEvent
qSelEventTarget ∷
∀ r. QuerySelector → Run (effect ∷ EFFECT, except ∷ FAIL | r) EventTarget
qSelEventTarget sel = qSel sel <#> Element.toEventTarget
windowEventTarget ∷ ∀ r. Run (effect ∷ EFFECT | r) EventTarget
windowEventTarget = liftEffect do window <#> Window.toEventTarget
documentEventTarget ∷ ∀ r. Run (effect ∷ EFFECT | r) EventTarget
documentEventTarget = liftEffect do
window >>= document <#> HTMLDocument.toEventTarget
bodyEventTarget ∷
∀ r. Run (effect ∷ EFFECT, except ∷ FAIL | r) EventTarget
bodyEventTarget = liftBoth do
window >>= document >>= body <#> map HTMLElement.toEventTarget
type EventInfo =
{ eventType ∷ EventType
, useCapture ∷ Boolean
}
type EventExecIn e s a r =
( state ∷ STATE s
, env ∷ READER e
, end ∷ EXCEPT a
, event ∷ READER Event
, effect ∷ EFFECT
, aff ∷ AFF
| r )
type EventTargetRow e s a =
( state ∷ STATE s
, env ∷ READER e
, end ∷ EXCEPT a
, effect ∷ EFFECT
, aff ∷ AFF
, choose ∷ CHOOSE
)
_event ∷ SProxy "event"
_event = SProxy
eventUpdate ∷
∀ e s a extra
. EventInfo
→ Run (EventTargetRow e s a) EventTarget
→ Run (EventExecIn e s a extra) Unit
→ AffGameUpdate extra e s a
eventUpdate { eventType, useCapture } targets update =
GameUpdate \reducer → do
stateRef ← askAt _stateRef
env ← askAt _env
targetArray ← targets
# (runChoose ∷ Run _ _ → Run _ (Array _))
# runStateWithRef stateRef
# expand
result ← liftAff $ makeAff \cb → do
listenerRef ← newRef Nothing
let canceler = readRef listenerRef >>= traverse_ \l →
for_ targetArray do removeEventListener eventType l useCapture
listener ← eventListener \event → do
state ← readRef stateRef
(runReducer reducer update ∷ Run (EventExecIn e s a ()) Unit)
# runReaderAt _event event
# runReaderAt _env env
# execState state
# runExceptAt _end
# runBaseAff'
# runAff_ case _ of
Right (Right newState) → writeRef newState stateRef
Right (Left end ) → canceler *> cb (Right end)
Left error → throwError error
writeRef (Just listener) listenerRef
for_ targetArray do addEventListener eventType listener useCapture
pure (effectCanceler canceler)
throwAt _end result
type UIEventRow r = (uiEvent ∷ READER UIEvent | r)
_uiEvent ∷ SProxy "uiEvent"
_uiEvent = SProxy
reduceUIEventRow ∷
∀ a r
. Run (UIEventRow (event ∷ READER Event, effect ∷ EFFECT | r)) a
→ Run (event ∷ READER Event, effect ∷ EFFECT | r) a
reduceUIEventRow uiEventRow = do
event ← askAt _event
uiEvent ← UIEvent.fromEvent event
# maybeThrow "This Event is not a UIEvent"
uiEventRow
# runReaderAt _uiEvent uiEvent
-- | Boolean is whether to use capture
uiEventUpdate ∷
∀ extra e s a
. EventType
→ Boolean
→ Run (EventTargetRow e s a) EventTarget
→ Run (EventExecIn e s a (UIEventRow extra)) Unit
→ AffGameUpdate extra e s a
uiEventUpdate eventType useCapture targets update = eventUpdate
{ eventType, useCapture }
targets
(reduceUIEventRow update)
type MouseEventRow r =
( uiEvent ∷ READER UIEvent
, mouseEvent ∷ READER MouseEvent
, posInTarget ∷ READER (Vector2 Int)
| r )
_mouseEvent ∷ SProxy "mouseEvent"
_mouseEvent = SProxy
_posInTarget ∷ SProxy "posInTarget"
_posInTarget = SProxy
-- | Assumes that the relevant `Event` is a `MouseEvent` (and `UIEvent`). The
-- | current event target (`event.currentTarget`) must be a `HTMLElement`.
-- | This function is not intended for use with events that do not meet these
-- | criteria.
reduceMouseEventRow ∷
∀ a r
. Run (MouseEventRow (event ∷ READER Event, effect ∷ EFFECT | r)) a
→ Run (event ∷ READER Event, effect ∷ EFFECT | r) a
reduceMouseEventRow mouseEventRow = do
event ← askAt _event
uiEvent ← UIEvent.fromEvent event
# maybeThrow "This Event is not a UIEvent"
mouseEvent ← MouseEvent.fromEvent event
# maybeThrow "This Event is not a MouseEvent"
target ← Event.currentTarget event >>= HTMLElement.fromEventTarget
# maybeThrow "This event.currentTarget is not a HTMLElement"
{ left, top } ← liftEffect $ HTMLElement.getBoundingClientRect target
let
clientPos = ((><) <$> MouseEvent.clientX <*> MouseEvent.clientY) mouseEvent
posInTarget = (\p b → p - round b) <$> clientPos <*> (left >< top)
mouseEventRow
# runReaderAt _uiEvent uiEvent
# runReaderAt _mouseEvent mouseEvent
# runReaderAt _posInTarget posInTarget
mouseEventUpdate ∷
∀ extra e s a
. EventType
→ Run (EventTargetRow e s a) EventTarget
→ Run (EventExecIn e s a (MouseEventRow extra)) Unit
→ AffGameUpdate extra e s a
mouseEventUpdate eventType targets update = eventUpdate
{ eventType, useCapture: false }
targets
(reduceMouseEventRow update)
mousemove ∷
∀ extra e s a
. Run (EventTargetRow e s a) EventTarget
→ Run (EventExecIn e s a (MouseEventRow extra)) Unit
→ AffGameUpdate extra e s a
mousemove = mouseEventUpdate (EventType "mousemove")
click ∷
∀ extra e s a
. Run (EventTargetRow e s a) EventTarget
→ Run (EventExecIn e s a (MouseEventRow extra)) Unit
→ AffGameUpdate extra e s a
click = mouseEventUpdate (EventType "click")
mousedown ∷
∀ extra e s a
. Run (EventTargetRow e s a) EventTarget
→ Run (EventExecIn e s a (MouseEventRow extra)) Unit
→ AffGameUpdate extra e s a
mousedown = mouseEventUpdate (EventType "mousedown")
mouseup ∷
∀ extra e s a
. Run (EventTargetRow e s a) EventTarget
→ Run (EventExecIn e s a (MouseEventRow extra)) Unit
→ AffGameUpdate extra e s a
mouseup = mouseEventUpdate (EventType "mouseup")
type KeyboardEventRow r =
( uiEvent ∷ READER UIEvent
, keyboardEvent ∷ READER KeyboardEvent
| r )
_keyboardEvent ∷ SProxy "keyboardEvent"
_keyboardEvent = SProxy
-- | Assumes that the relevant `Event` is a `KeyboardEvent` (and `UIEvent`).
-- | Throws an error when attempting to read a value that doesn't exist on the
-- | `Event`. This function is not intended for use with events that are not
-- | `KeyboardEvent`s.
reduceKeyboardEventRow ∷
∀ a r
. Run (KeyboardEventRow (event ∷ READER Event, effect ∷ EFFECT | r)) a
→ Run (event ∷ READER Event, effect ∷ EFFECT | r) a
reduceKeyboardEventRow keyboardEventRow = do
event ← askAt _event
uiEvent ← UIEvent.fromEvent event
# maybeThrow "This Event is not UIEvent"
keyboardEvent ← KeyboardEvent.fromEvent event
# maybeThrow "This Event is not KeyboardEvent"
keyboardEventRow
# runReaderAt _uiEvent uiEvent
# runReaderAt _keyboardEvent keyboardEvent
keyboardEventUpdate ∷
∀ extra e s a
. EventType
→ Run (EventTargetRow e s a) EventTarget
→ Run (EventExecIn e s a (KeyboardEventRow extra)) Unit
→ AffGameUpdate extra e s a
keyboardEventUpdate eventType targets update = eventUpdate
{ eventType, useCapture: false }
targets
(reduceKeyboardEventRow update)
keydown ∷
∀ extra e s a
. Run (EventTargetRow e s a) EventTarget
→ Run (EventExecIn e s a (KeyboardEventRow extra)) Unit
→ AffGameUpdate extra e s a
keydown = keyboardEventUpdate (EventType "keydown")
keyup ∷
∀ extra e s a
. Run (EventTargetRow e s a) EventTarget
→ Run (EventExecIn e s a (KeyboardEventRow extra)) Unit
→ AffGameUpdate extra e s a
keyup = keyboardEventUpdate (EventType "keyup")
keypressed ∷
∀ extra e s a
. Run (EventTargetRow e s a) EventTarget
→ Run (EventExecIn e s a (KeyboardEventRow extra)) Unit
→ AffGameUpdate extra e s a
keypressed = keyboardEventUpdate (EventType "keypressed")
change ∷
∀ extra e s a
. Run (EventTargetRow e s a) EventTarget
→ Run (EventExecIn e s a extra) Unit
→ AffGameUpdate extra e s a
change = eventUpdate { eventType: EventType "change", useCapture: false }
load ∷
∀ extra e s a
. Run (EventTargetRow e s a) EventTarget
→ Run (EventExecIn e s a extra) Unit
→ AffGameUpdate extra e s a
load = eventUpdate { eventType: EventType "load", useCapture: false }