/
input.clj
102 lines (86 loc) · 3.37 KB
/
input.clj
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
(ns quip.input
(:require [quil.core :as q]))
;;; Default handlers for key events.
;;; We use these to manage the set of currently held keys, which
;;; allows us to make decisions during scene update functions, useful
;;; for character control etc.
(defn default-key-pressed
"Prevent the default behaviour of esc closing the sketch and add the
pressed key to the list of currently held keys."
[state e]
(if (= 27 (q/key-code))
(set! (.key (quil.applet/current-applet)) (char 0)))
(update state :held-keys #(conj % (:key e))))
(defn default-key-released
"Remove the released key from the list of currently held keys."
[state e]
(update state :held-keys #(disj % (:key e))))
;;; Default handlers for mouse events.
;;; Currently haven't needed these to do anything, but they could
;;; similarly be used to keep track of the currently held mouse
;;; buttons.
(defn default-mouse-pressed
[state e]
state)
(defn default-mouse-released
[state e]
state)
;;; We allow scenes to define a collection of their own handlers for
;;; each event type.
;;; We can then reduce accross this collection using an anonymous
;;; apply-handler function (which is a closure over the event `e`) as
;;; our reducing function.
;;; Most of the time only one of the handlers will modifiy the state
;;; as it goes through, but its totally fine for multiple to.
(defn key-pressed
"Reduce applying a handler function:
(f state e)
accross the collection of `:key-pressed-fns` in the current scene."
[{:keys [input-enabled? scenes current-scene] :as state} e]
(if input-enabled?
(let [default-handled-state (default-key-pressed state e)
scene-handlers (get-in scenes [current-scene :key-pressed-fns])]
(reduce (fn [acc-state f]
(f acc-state e))
default-handled-state
scene-handlers))
state))
(defn key-released
"Reduce applying a handler function:
(f state e)
accross the collection of `:key-released-fns` in the current scene."
[{:keys [input-enabled? scenes current-scene] :as state} e]
(if input-enabled?
(let [default-handled-state (default-key-released state e)
scene-handlers (get-in scenes [current-scene :key-released-fns])]
(reduce (fn [acc-state f]
(f acc-state e))
default-handled-state
scene-handlers))
state))
(defn mouse-pressed
"Reduce applying a handler function:
(f state e)
accross the collection of `:mouse-pressed-fns` in the current scene."
[{:keys [input-enabled? scenes current-scene] :as state} e]
(if input-enabled?
(let [default-handled-state (default-mouse-pressed state e)
scene-handlers (get-in scenes [current-scene :mouse-pressed-fns])]
(reduce (fn [acc-state f]
(f acc-state e))
default-handled-state
scene-handlers))
state))
(defn mouse-released
"Reduce applying a handler function:
(f state e)
accross the collection of `:mouse-released-fns` in the current scene."
[{:keys [input-enabled? scenes current-scene] :as state} e]
(if input-enabled?
(let [default-handled-state (default-mouse-released state e)
scene-handlers (get-in scenes [current-scene :mouse-released-fns])]
(reduce (fn [acc-state f]
(f acc-state e))
default-handled-state
scene-handlers))
state))