-
Notifications
You must be signed in to change notification settings - Fork 0
/
pad.rkt
80 lines (67 loc) · 2.02 KB
/
pad.rkt
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
#lang racket
(require csfml
posn)
(provide pad-update
pad-find-first-connected
pad-stick
pad-button-press
pad-button-release
pad-button-pressed
pad-stick-posn)
(define (pad-find-first-connected)
(sfJoystick_update)
(for/first ([i 8]
#:when (sfJoystick_isConnected i))
i))
(define PAD-IDX
(pad-find-first-connected))
(define BUTTON-KEYS '(w s e n l1 r1 l2 r2 share options ls rs ps pad))
(define BUTTON-PREVIOUS-STATE
(map (λ (k) (cons k #f))
BUTTON-KEYS))
;; Needs to be run to query new data from driver
(define (pad-update)
(set! BUTTON-PREVIOUS-STATE
(map (λ (k) (cons k (pad-button-pressed k)))
BUTTON-KEYS))
(sfJoystick_update))
;; True while button is pressed
(define (pad-button-pressed button)
(sfJoystick_isButtonPressed PAD-IDX (button-map button)))
;; True if button is newly pressed
(define (pad-button-press button)
(and (not (dict-ref BUTTON-PREVIOUS-STATE button))
(pad-button-pressed button)))
;; True if button is newly released
(define (pad-button-release button)
(and (dict-ref BUTTON-PREVIOUS-STATE button)
(not (pad-button-pressed button))))
(define (pad-map side axis)
(case (system-type 'os)
['unix
(case (list side axis)
['(left x) 'sfJoystickX]
['(left y) 'sfJoystickY]
['(left z) 'sfJoystickZ]
['(right x) 'sfJoystickU]
['(right y) 'sfJoystickV]
['(right z) 'sfJoystickR])]
['macosx
(case (list side axis)
['(left x) 'sfJoystickX]
['(left y) 'sfJoystickY]
['(right x) 'sfJoystickZ]
['(right y) 'sfJoystickR])]))
(define (pad-stick side axis)
(if PAD-IDX
(sfJoystick_getAxisPosition PAD-IDX (pad-map side axis))
origin))
(define (pad-stick-posn side)
(posn (pad-stick side 'x) (pad-stick side 'y)))
(define (button-map button)
(case button
['w 0] ['s 1] ['e 2] ['n 3]
['l1 4] ['r1 5] ['l2 6] ['r2 7]
['share 8] ['options 9]
['ls 10] ['rs 11]
['ps 12] ['pad 13]))