@kairotic/app 0.0.0-stage → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -2
- package/index.html +1101 -0
- package/kairotic/api.cljs +362 -0
- package/kairotic/assistant.cljs +505 -0
- package/kairotic/backends/gun-backend.js +371 -0
- package/kairotic/canvas.cljs +751 -0
- package/kairotic/code.cljs +108 -0
- package/kairotic/components.cljs +838 -0
- package/kairotic/core.cljs +96 -0
- package/kairotic/corefns.cljs +473 -0
- package/kairotic/docs.mjs +2 -0
- package/kairotic/engine.cljs +482 -0
- package/kairotic/examples.mjs +96 -0
- package/kairotic/faust.cljs +225 -0
- package/kairotic/help.cljs +1036 -0
- package/kairotic/hiccup.cljs +566 -0
- package/kairotic/input.cljs +271 -0
- package/kairotic/kfile.mjs +138 -0
- package/kairotic/library.cljs +214 -0
- package/kairotic/links.cljs +41 -0
- package/kairotic/mt.cljs +1604 -0
- package/kairotic/mtboxes.cljs +276 -0
- package/kairotic/packages/analysis.mjs +64 -0
- package/kairotic/packages/controls.mjs +28 -0
- package/kairotic/packages/data.mjs +22 -0
- package/kairotic/packages/faust-units/cmaes.mjs +82 -0
- package/kairotic/packages/faust-units/graph.mjs +212 -0
- package/kairotic/packages/faust-units/listen.mjs +125 -0
- package/kairotic/packages/faust-units/notation.mjs +139 -0
- package/kairotic/packages/faust-units/parse.mjs +439 -0
- package/kairotic/packages/faust-units/recipe.mjs +195 -0
- package/kairotic/packages/faust-units/render.mjs +69 -0
- package/kairotic/packages/faust-units/units.mjs +130 -0
- package/kairotic/packages/faust-units/view.mjs +353 -0
- package/kairotic/packages/faust-units.mjs +9 -0
- package/kairotic/packages/io.mjs +58 -0
- package/kairotic/packages/net.mjs +28 -0
- package/kairotic/packages/patterns.mjs +34 -0
- package/kairotic/packages/spatial.mjs +22 -0
- package/kairotic/packages/spectral/shift.mjs +169 -0
- package/kairotic/packages/spectral.mjs +16 -0
- package/kairotic/packages/synth.mjs +28 -0
- package/kairotic/packages/theory/counterpoint.mjs +798 -0
- package/kairotic/packages/theory/mirror.mjs +221 -0
- package/kairotic/packages/theory.mjs +46 -0
- package/kairotic/patterns.cljs +538 -0
- package/kairotic/presets.cljs +227 -0
- package/kairotic/quantize.cljs +782 -0
- package/kairotic/record.cljs +169 -0
- package/kairotic/release.cljs +90 -0
- package/kairotic/ring.cljs +129 -0
- package/kairotic/ringaudio.cljs +232 -0
- package/kairotic/ringcore.mjs +270 -0
- package/kairotic/score.cljs +176 -0
- package/kairotic/shapes.cljs +283 -0
- package/kairotic/signal.cljs +327 -0
- package/kairotic/sounds.cljs +565 -0
- package/kairotic/sounds.mjs +108 -0
- package/kairotic/span.cljs +129 -0
- package/kairotic/spell.cljs +281 -0
- package/kairotic/structure.cljs +145 -0
- package/kairotic/talk.cljs +115 -0
- package/kairotic/tempo.cljs +226 -0
- package/kairotic/treecanvas.cljs +299 -0
- package/kairotic/tutorials.mjs +1283 -0
- package/kairotic/vendor/codemirror.mjs +23 -0
- package/package.json +23 -4
- package/page/00-work.cljs +862 -0
- package/page/01-commands.cljs +1219 -0
- package/page/02-patch-view.cljs +1051 -0
- package/page/03-history-view.cljs +481 -0
- package/page/04-panels.cljs +448 -0
- package/page/05-gestures.cljs +1247 -0
- package/page/06-connection.cljs +219 -0
- package/page/07-claude.cljs +430 -0
- package/page/08-sessions.cljs +423 -0
- package/page/09-learn.cljs +602 -0
- package/page/10-embed.cljs +469 -0
- package/page/11-startup.cljs +61 -0
- package/page/12-feedback.cljs +172 -0
- package/page/13-perform.cljs +325 -0
- package/page/14-editor.cljs +656 -0
- package/page/15-presets.cljs +230 -0
- package/page/16-release.cljs +242 -0
- package/page/17-sounds.cljs +147 -0
- package/page/18-lines.cljs +594 -0
- package/page/19-files.cljs +247 -0
- package/page/19-html.cljs +152 -0
- package/page/20-talk.cljs +527 -0
- package/page/21-net.cljs +110 -0
- package/page/22-learn.cljs +303 -0
- package/page/23-ids.cljs +233 -0
- package/page/24-clipboard.cljs +136 -0
- package/page/25-tidy.cljs +74 -0
- package/page/26-probes.cljs +111 -0
- package/sources.mjs +2 -0
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
(in-ns 'user)
|
|
2
|
+
;; ───────────────────────── learn: move a controller, and it moves a box (discussion #135) ─────────────────────────
|
|
3
|
+
;; (learn :b5), or Learn with a box selected: the next controller that moves — a MIDI controller or key, a
|
|
4
|
+
;; gamepad's stick or button, a phone's tilt, an OSC address — is wired to that box, as gestures of the
|
|
5
|
+
;; work anyone can read and change: the source (midiin, gamepad, motion or oscin; one already in the patch
|
|
6
|
+
;; if there is one) → control (that one control) → scale (from the controller's range to the box's: a
|
|
7
|
+
;; knob's min and max, logarithmic if it is) → the box. While it waits, this page listens to the page's MIDI,
|
|
8
|
+
;; the gamepads, the motion of the device and the OSC bridge if one is open; ▶ is not needed.
|
|
9
|
+
;;
|
|
10
|
+
;; Two scopes, asked each time (the last one is remembered):
|
|
11
|
+
;; for the work the chain above, in the patch: the same for everyone in a room (each page reads its own
|
|
12
|
+
;; devices), part of the piece
|
|
13
|
+
;; only for me nothing in the work: the mapping is kept in this browser, for this work, and the page
|
|
14
|
+
;; applies it as your own gesture — so in a jam the others hear it too (13-perform.cljs).
|
|
15
|
+
;; Each player brings their own instrument without touching the work of the others.
|
|
16
|
+
|
|
17
|
+
(defonce learn-state (atom nil)) ; {:target :bN :off [fns] :base {...}} while it waits
|
|
18
|
+
|
|
19
|
+
(defn learn-banner! [html]
|
|
20
|
+
(let [el (floating "learn-pop" "perform-pop")]
|
|
21
|
+
(if html
|
|
22
|
+
(do (set! (.-innerHTML el) html) (set! (.-hidden el) false))
|
|
23
|
+
(set! (.-hidden el) true))))
|
|
24
|
+
|
|
25
|
+
(defn learn-stop! []
|
|
26
|
+
(when-let [s @learn-state] (doseq [f (:off s)] (try (f) (catch :default _ nil))))
|
|
27
|
+
(reset! learn-state nil)
|
|
28
|
+
(when-let [b ($ "learn-toggle")] (.remove (.-classList b) "on"))
|
|
29
|
+
(learn-banner! nil))
|
|
30
|
+
|
|
31
|
+
;; ── what a source is, and how to reach its events ──
|
|
32
|
+
;; kind → [the source class, its event outlet, the controller's range]
|
|
33
|
+
(def learn-sources
|
|
34
|
+
{"cc" ["midiin" 2 [0 1]] "note" ["midiin" 2 [0 1]] "pad" ["gamepad" 0 [-1 1]]
|
|
35
|
+
"tilt" ["motion" 2 [0 1]] "osc" ["oscin" 0 [0 1]]})
|
|
36
|
+
|
|
37
|
+
(defn learn-range
|
|
38
|
+
"The range of a box's input: a knob's (or any class with :min and :max) [min max log?], else [0 1 false]."
|
|
39
|
+
[b]
|
|
40
|
+
(let [d (comp/lookup (comp/klass (:text b))) a (when d (comp/args-for d (:text b)))]
|
|
41
|
+
(if (and (map? a) (number? (:min a)) (number? (:max a)))
|
|
42
|
+
[(:min a) (:max a) (boolean (:log a))]
|
|
43
|
+
[0 1 false])))
|
|
44
|
+
|
|
45
|
+
(defn learn-ranges
|
|
46
|
+
"[in-lo in-hi lo hi log?]: the controller's range and the box's, for spec (see learn-wire!)."
|
|
47
|
+
[target spec]
|
|
48
|
+
(let [[_ _ [in-lo in-hi]] (get learn-sources (first spec))
|
|
49
|
+
;; a gamepad's buttons go 0…1, its sticks −1…1
|
|
50
|
+
[in-lo in-hi] (if (and (= "pad" (first spec)) (not (re-matches #"lx|ly|rx|ry|a\d+" (second spec)))) [0 1] [in-lo in-hi])
|
|
51
|
+
[lo hi lg] (learn-range (get-in (current) [:boxes target]))]
|
|
52
|
+
[in-lo in-hi lo hi lg]))
|
|
53
|
+
|
|
54
|
+
(defn learn-wire!
|
|
55
|
+
"Wires the control found (spec: [kind which & more], as the words of a control box) to the box learning."
|
|
56
|
+
[target spec]
|
|
57
|
+
(let [p (current) tb (get-in p [:boxes target])
|
|
58
|
+
[cls outlet] (get learn-sources (first spec))
|
|
59
|
+
[in-lo in-hi lo hi lg] (learn-ranges target spec)
|
|
60
|
+
existing (some (fn [[id b]] (when (= cls (comp/klass (:text b))) id)) (sort-by key (:boxes p)))
|
|
61
|
+
x (:x tb) y (:y tb)
|
|
62
|
+
src (or existing (next-id))]
|
|
63
|
+
(when-not existing (gesture! (str "(box " src " " (pr-str cls) " " (- x 0) " " (max 0 (- y 150)) ")")))
|
|
64
|
+
(let [ctl (next-id)]
|
|
65
|
+
(gesture! (str "(box " ctl " " (pr-str (str "control " (str/join " " spec))) " " x " " (max 0 (- y 105)) ")"))
|
|
66
|
+
(let [sc (next-id)]
|
|
67
|
+
(gesture! (str "(box " sc " " (pr-str (str "scale " (api/nice in-lo) " " (api/nice in-hi) " " (api/nice lo) " " (api/nice hi) (when lg " log"))) " " x " " (max 0 (- y 60)) ")"))
|
|
68
|
+
(gesture! (str "(connect " src " " outlet " " ctl " 0)"))
|
|
69
|
+
(gesture! (str "(connect " ctl " 0 " sc " 0)"))
|
|
70
|
+
(gesture! (str "(connect " sc " 0 " target " 0)"))
|
|
71
|
+
;; an OSC value's range is unknown: the scale learns it from the next values
|
|
72
|
+
(when (= "osc" (first spec)) (send! sc :learn))
|
|
73
|
+
[src ctl sc]))))
|
|
74
|
+
|
|
75
|
+
(declare mine-add!)
|
|
76
|
+
|
|
77
|
+
(defn learn-found! [spec]
|
|
78
|
+
(when-let [{:keys [target scope]} @learn-state]
|
|
79
|
+
(learn-stop!)
|
|
80
|
+
(if (= scope :me)
|
|
81
|
+
(do (mine-add! {:spec spec :box target :range (learn-ranges target spec)})
|
|
82
|
+
(repl-print! "out" (esc (str "; learnt for you only: " (str/join " " spec) " → " target " (in this browser, not in the work: (my-controls))")))
|
|
83
|
+
spec)
|
|
84
|
+
(let [made (learn-wire! target spec)]
|
|
85
|
+
(repl-print! "out" (esc (str "; learnt for the work: " (str/join " " spec) " → " target (when (= "osc" (first spec)) " (move it end to end: the scale learns its range)"))))
|
|
86
|
+
made))))
|
|
87
|
+
|
|
88
|
+
;; ── listening, while it waits ──
|
|
89
|
+
|
|
90
|
+
(defn learn-listen-midi! []
|
|
91
|
+
(let [on (fn [e] (let [[s a b] (js/Array.from (.-data e)) hi (bit-and s 0xF0) ch (inc (bit-and s 0x0F))]
|
|
92
|
+
(cond (and (= hi 0xB0) (not (#{0 6 32 38 98 99 100 101} a))) (learn-found! ["cc" (str a) (str ch)])
|
|
93
|
+
(and (= hi 0x90) (pos? b)) (learn-found! ["note" (str a)]))))
|
|
94
|
+
ins (atom [])]
|
|
95
|
+
(when-let [p (or (.-kairoticMidi js/window) (when (.-requestMIDIAccess js/navigator)
|
|
96
|
+
(let [p (.requestMIDIAccess js/navigator #js {:sysex false})]
|
|
97
|
+
(set! (.-kairoticMidi js/window) p) p)))]
|
|
98
|
+
(.then p (fn [m] (when @learn-state
|
|
99
|
+
(reset! ins (vec (js/Array.from (.values (.-inputs m)))))
|
|
100
|
+
(doseq [i @ins] (.addEventListener i "midimessage" on)))))
|
|
101
|
+
(.catch p (fn [_] nil)))
|
|
102
|
+
(fn [] (doseq [i @ins] (.removeEventListener i "midimessage" on)))))
|
|
103
|
+
|
|
104
|
+
(defn learn-pads []
|
|
105
|
+
(let [raw (or (.-kairoticFakePads js/window) (when (.-getGamepads js/navigator) (.getGamepads js/navigator)) #js [])]
|
|
106
|
+
(vec (for [p (js/Array.from raw) :when p]
|
|
107
|
+
{:index (.-index p) :std (= "standard" (.-mapping p)) :axes (vec (js/Array.from (.-axes p)))
|
|
108
|
+
:buttons (mapv (fn [b] (.-pressed b)) (js/Array.from (.-buttons p)))}))))
|
|
109
|
+
|
|
110
|
+
(def learn-pad-buttons ["a" "b" "x" "y" "lb" "rb" "lt" "rt" "back" "start" "ls" "rs" "up" "down" "left" "right" "home"])
|
|
111
|
+
|
|
112
|
+
(defn learn-listen-pads! []
|
|
113
|
+
(let [base (atom (learn-pads)) alive (atom true)]
|
|
114
|
+
(letfn [(poll [& _]
|
|
115
|
+
(when (and @alive @learn-state)
|
|
116
|
+
(let [now (learn-pads)
|
|
117
|
+
found (first (for [p now :let [b (first (filter #(= (:index %) (:index p)) @base))]
|
|
118
|
+
x (concat
|
|
119
|
+
(for [[k v] (map-indexed vector (:axes p))
|
|
120
|
+
:when (> (js/Math.abs (- v (get-in b [:axes k] 0))) 0.5)]
|
|
121
|
+
(if (and (:std p) (< k 4)) (nth ["lx" "ly" "rx" "ry"] k) (str "a" k)))
|
|
122
|
+
(for [[k on] (map-indexed vector (:buttons p))
|
|
123
|
+
:when (and on (not (get-in b [:buttons k])))]
|
|
124
|
+
(if (and (:std p) (< k 17)) (nth learn-pad-buttons k) (str "b" k))))]
|
|
125
|
+
x))]
|
|
126
|
+
(if found (learn-found! ["pad" found]) (js/requestAnimationFrame poll)))))]
|
|
127
|
+
(poll))
|
|
128
|
+
(fn [] (reset! alive false))))
|
|
129
|
+
|
|
130
|
+
(defn learn-listen-motion! []
|
|
131
|
+
(let [base (atom nil)
|
|
132
|
+
on (fn [e] (when (number? (.-gamma e))
|
|
133
|
+
(if-let [[g b] @base]
|
|
134
|
+
(cond (> (js/Math.abs (- (.-gamma e) g)) 20) (learn-found! ["tilt" "x"])
|
|
135
|
+
(> (js/Math.abs (- (.-beta e) b)) 20) (learn-found! ["tilt" "y"]))
|
|
136
|
+
(reset! base [(.-gamma e) (.-beta e)]))))]
|
|
137
|
+
(.addEventListener js/window "deviceorientation" on)
|
|
138
|
+
(fn [] (.removeEventListener js/window "deviceorientation" on))))
|
|
139
|
+
|
|
140
|
+
(defn learn-listen-osc! []
|
|
141
|
+
;; only through a bridge an oscin or oscout of this page has already opened
|
|
142
|
+
(let [socks (when-let [all (.-kairoticOsc js/window)] (vec (js/Object.values all)))
|
|
143
|
+
on (fn [f] (when (and (.-address f) (number? (aget (or (.-args f) #js []) 0))) (learn-found! ["osc" (.-address f)])))]
|
|
144
|
+
(doseq [s socks] (.push (.-listeners s) on))
|
|
145
|
+
(fn [] (doseq [s socks] (set! (.-listeners s) (.filter (.-listeners s) (fn [x & _] (not= x on))))))))
|
|
146
|
+
|
|
147
|
+
;; ── only for me: mappings kept in this browser, for this work, applied as your own gestures ──
|
|
148
|
+
|
|
149
|
+
(defonce learn-scope (atom (if (= "me" (try (.getItem js/localStorage "kairotic-learn-scope") (catch :default _ nil))) :me :work)))
|
|
150
|
+
(defonce mine (atom [])) ; [{:spec [...] :box :bN :range [in-lo in-hi lo hi log?]} …] of the open work
|
|
151
|
+
(defonce mine-off (atom [])) ; what stops the listeners
|
|
152
|
+
(defonce mine-last (atom {})) ; [spec box] → last value sent (a moving stick or tilt sends only changes)
|
|
153
|
+
(defonce mine-raw (atom {})) ; spec → last reading of a polled controller (only a move plays)
|
|
154
|
+
|
|
155
|
+
(declare mine-apply!)
|
|
156
|
+
|
|
157
|
+
(defn mine-moved!
|
|
158
|
+
"A reading of a controller that is read all the time (a stick, a tilt): it plays only when it moves, not
|
|
159
|
+
the first time it is read (the box keeps its value until the controller is touched)."
|
|
160
|
+
[spec v]
|
|
161
|
+
(let [old (get @mine-raw spec)]
|
|
162
|
+
(swap! mine-raw assoc spec v)
|
|
163
|
+
(when (and (some? old) (> (js/Math.abs (- v old)) 0.003)) (mine-apply! spec v))))
|
|
164
|
+
|
|
165
|
+
(defn mine-key [] (str "kairotic-controls:" (name (or (:kind @work) :local)) ":" (or (:id @work) "local")))
|
|
166
|
+
|
|
167
|
+
(defn mine-save! []
|
|
168
|
+
(try (.setItem js/localStorage (mine-key) (pr-str @mine)) (catch :default _ nil)))
|
|
169
|
+
|
|
170
|
+
(defn mine-load! []
|
|
171
|
+
(reset! mine (or (try (some-> (.getItem js/localStorage (mine-key)) clojure.edn/read-string vec) (catch :default _ nil)) [])))
|
|
172
|
+
|
|
173
|
+
(defn mine-value
|
|
174
|
+
"A controller's value through a mapping's ranges: what goes to the box."
|
|
175
|
+
[[in-lo in-hi lo hi lg] v]
|
|
176
|
+
(let [u (api/clamp (if (== in-lo in-hi) 0 (/ (- v in-lo) (- in-hi in-lo))) 0 1)]
|
|
177
|
+
(api/nice (if (and lg (pos? lo) (pos? hi)) (* lo (js/Math.pow (/ hi lo) u)) (+ lo (* u (- hi lo)))))))
|
|
178
|
+
|
|
179
|
+
(defn mine-apply!
|
|
180
|
+
"A controller (spec as Learn writes it) moved to v: every mapping of it plays its box, as a gesture of yours."
|
|
181
|
+
[spec v]
|
|
182
|
+
(doseq [{s :spec box :box r :range} @mine
|
|
183
|
+
:when (and (= s spec) (get-in (current) [:boxes box]))]
|
|
184
|
+
(let [out (mine-value r v)]
|
|
185
|
+
(when (not= out (get @mine-last [spec box]))
|
|
186
|
+
(swap! mine-last assoc [spec box] out)
|
|
187
|
+
(inject-local! box 0 out)))))
|
|
188
|
+
|
|
189
|
+
(defn mine-listen! []
|
|
190
|
+
(doseq [f @mine-off] (try (f) (catch :default _ nil)))
|
|
191
|
+
(reset! mine-off [])
|
|
192
|
+
(let [kinds (set (map (comp first :spec) @mine))]
|
|
193
|
+
(when (or (kinds "cc") (kinds "note"))
|
|
194
|
+
(let [on (fn [e] (let [[s a b] (js/Array.from (.-data e)) hi (bit-and s 0xF0) ch (inc (bit-and s 0x0F))]
|
|
195
|
+
(cond (= hi 0xB0) (do (mine-apply! ["cc" (str a) (str ch)] (/ b 127)) (mine-apply! ["cc" (str a)] (/ b 127)))
|
|
196
|
+
(= hi 0x90) (mine-apply! ["note" (str a)] (/ b 127))
|
|
197
|
+
(= hi 0x80) (mine-apply! ["note" (str a)] 0))))
|
|
198
|
+
ins (atom [])]
|
|
199
|
+
(when-let [p (or (.-kairoticMidi js/window) (when (.-requestMIDIAccess js/navigator)
|
|
200
|
+
(let [p (.requestMIDIAccess js/navigator #js {:sysex false})]
|
|
201
|
+
(set! (.-kairoticMidi js/window) p) p)))]
|
|
202
|
+
(.catch (.then p (fn [m] (reset! ins (vec (js/Array.from (.values (.-inputs m)))))
|
|
203
|
+
(doseq [i @ins] (.addEventListener i "midimessage" on))))
|
|
204
|
+
(fn [_] nil)))
|
|
205
|
+
(swap! mine-off conj (fn [] (doseq [i @ins] (.removeEventListener i "midimessage" on))))))
|
|
206
|
+
(when (kinds "pad")
|
|
207
|
+
(let [alive (atom true)]
|
|
208
|
+
(letfn [(poll [& _]
|
|
209
|
+
(when @alive
|
|
210
|
+
(doseq [p (learn-pads)
|
|
211
|
+
[k v] (map-indexed vector (:axes p))]
|
|
212
|
+
(mine-moved! ["pad" (if (and (:std p) (< k 4)) (nth ["lx" "ly" "rx" "ry"] k) (str "a" k))] v))
|
|
213
|
+
(doseq [p (learn-pads)
|
|
214
|
+
[k on] (map-indexed vector (:buttons p))]
|
|
215
|
+
(mine-moved! ["pad" (if (and (:std p) (< k 17)) (nth learn-pad-buttons k) (str "b" k))] (if on 1 0)))
|
|
216
|
+
(js/requestAnimationFrame poll)))]
|
|
217
|
+
(poll))
|
|
218
|
+
(swap! mine-off conj (fn [] (reset! alive false)))))
|
|
219
|
+
(when (kinds "tilt")
|
|
220
|
+
(let [on (fn [e] (when (number? (.-gamma e))
|
|
221
|
+
(mine-moved! ["tilt" "x"] (api/clamp (/ (+ (.-gamma e) 90) 180) 0 1))
|
|
222
|
+
(mine-moved! ["tilt" "y"] (api/clamp (/ (+ (api/clamp (.-beta e) -90 90) 90) 180) 0 1))))]
|
|
223
|
+
(.addEventListener js/window "deviceorientation" on)
|
|
224
|
+
(swap! mine-off conj (fn [] (.removeEventListener js/window "deviceorientation" on)))))
|
|
225
|
+
(when (kinds "osc")
|
|
226
|
+
(let [socks (when-let [all (.-kairoticOsc js/window)] (vec (js/Object.values all)))
|
|
227
|
+
on (fn [f] (let [v (aget (or (.-args f) #js []) 0)] (when (and (.-address f) (number? v)) (mine-apply! ["osc" (.-address f)] v))))]
|
|
228
|
+
(doseq [s socks] (.push (.-listeners s) on))
|
|
229
|
+
(swap! mine-off conj (fn [] (doseq [s socks] (set! (.-listeners s) (.filter (.-listeners s) (fn [x & _] (not= x on)))))))))))
|
|
230
|
+
|
|
231
|
+
(defn mine-add! [m]
|
|
232
|
+
(swap! mine (fn [ms] (conj (vec (remove #(and (= (:spec %) (:spec m)) (= (:box %) (:box m))) ms)) m)))
|
|
233
|
+
(mine-save!) (mine-listen!))
|
|
234
|
+
|
|
235
|
+
(defn my-controls
|
|
236
|
+
"Your own controller mappings for this work (Learn · only for me), kept in this browser: [[n spec box] …]."
|
|
237
|
+
[] (vec (map-indexed (fn [i {:keys [spec box]}] [i (str/join " " spec) box]) @mine)))
|
|
238
|
+
|
|
239
|
+
(defn forget-control
|
|
240
|
+
"Forgets one of your own controller mappings, by its number in (my-controls); (forget-control :all) all of them."
|
|
241
|
+
[n]
|
|
242
|
+
(swap! mine (fn [ms] (if (= n :all) [] (vec (keep-indexed (fn [i m] (when (not= i n) m)) ms)))))
|
|
243
|
+
(mine-save!) (mine-listen!)
|
|
244
|
+
(my-controls))
|
|
245
|
+
|
|
246
|
+
;; another work has its own mappings
|
|
247
|
+
(add-watch work :mine (fn [_ _ o n] (when (not= [(:kind o) (:id o)] [(:kind n) (:id n)]) (mine-load!) (mine-listen!))))
|
|
248
|
+
|
|
249
|
+
;; ── the banner: the box, the scope, your mappings ──
|
|
250
|
+
|
|
251
|
+
(defn learn-banner-html [id b]
|
|
252
|
+
(let [me? (= :me (:scope @learn-state))]
|
|
253
|
+
(str "<p><b>Learn</b> · move a controller for <code>" (esc (name id)) " " (esc (:text b)) "</code>: a MIDI controller or key, a gamepad's stick or button, the phone's tilt, an OSC address.</p>"
|
|
254
|
+
"<div class=\"learn-scope\" role=\"radiogroup\" aria-label=\"For whom\">"
|
|
255
|
+
"<label><input type=\"radio\" name=\"learn-scope\" value=\"work\"" (when-not me? " checked") "> <b>for the work</b> — boxes in the patch, the same for everyone</label>"
|
|
256
|
+
"<label><input type=\"radio\" name=\"learn-scope\" value=\"me\"" (when me? " checked") "> <b>only for me</b> — in this browser, played as my gesture</label>"
|
|
257
|
+
"</div>"
|
|
258
|
+
(when (seq @mine)
|
|
259
|
+
(str "<p class=\"learn-mine\">Yours here: "
|
|
260
|
+
(str/join " · " (map-indexed (fn [i {:keys [spec box]}] (str (esc (str/join " " spec)) " → " (esc (name box)) " <button class=\"act small\" data-mine-forget=\"" i "\" title=\"Forget it\">✕</button>")) @mine))
|
|
261
|
+
"</p>"))
|
|
262
|
+
"<div class=\"ask-bar\"><span class=\"spacer\"></span><button class=\"act\" data-learn-cancel=\"1\">Cancel · Esc</button></div>")))
|
|
263
|
+
|
|
264
|
+
(defn learn
|
|
265
|
+
"Wires the next controller that moves to a box: (learn :b5) for the work (boxes in the patch), (learn :b5 :me)
|
|
266
|
+
only for you (kept in this browser, played as your gesture). (learn false) stops waiting."
|
|
267
|
+
([] (learn false))
|
|
268
|
+
([id] (learn id @learn-scope))
|
|
269
|
+
([id scope]
|
|
270
|
+
(learn-stop!)
|
|
271
|
+
(if (and id (not= id false))
|
|
272
|
+
(let [b (or (get-in (current) [:boxes id]) (fail (str "There is no box " id)))
|
|
273
|
+
scope (if (= scope :me) :me :work)]
|
|
274
|
+
(reset! learn-state {:target id :scope scope})
|
|
275
|
+
(swap! learn-state assoc :off [(learn-listen-midi!) (learn-listen-pads!) (learn-listen-motion!) (learn-listen-osc!)])
|
|
276
|
+
(when-let [t ($ "learn-toggle")] (.add (.-classList t) "on"))
|
|
277
|
+
(learn-banner! (learn-banner-html id b))
|
|
278
|
+
id)
|
|
279
|
+
nil)))
|
|
280
|
+
|
|
281
|
+
(when ($ "learn-toggle")
|
|
282
|
+
(.addEventListener ($ "learn-toggle") "click"
|
|
283
|
+
(fn [_] (if @learn-state
|
|
284
|
+
(learn false)
|
|
285
|
+
(let [[kind v] (:sel @ui)]
|
|
286
|
+
(if (= kind :box)
|
|
287
|
+
(learn v)
|
|
288
|
+
(repl-print! "err" "Learn: select the box a controller should move (a knob, a number, any box), then Learn."))))))
|
|
289
|
+
(.addEventListener js/document "click"
|
|
290
|
+
(fn [e] (let [t (.-target e)]
|
|
291
|
+
(when (.getAttribute t "data-learn-cancel") (learn false))
|
|
292
|
+
(when-let [n (.getAttribute t "data-mine-forget")]
|
|
293
|
+
(forget-control (js/parseInt n))
|
|
294
|
+
(when-let [{:keys [target]} @learn-state] (learn-banner! (learn-banner-html target (get-in (current) [:boxes target]))))))))
|
|
295
|
+
(.addEventListener js/document "change"
|
|
296
|
+
(fn [e] (let [t (.-target e)]
|
|
297
|
+
(when (and (= "learn-scope" (.-name t)) @learn-state)
|
|
298
|
+
(let [sc (if (= "me" (.-value t)) :me :work)]
|
|
299
|
+
(swap! learn-state assoc :scope sc)
|
|
300
|
+
(reset! learn-scope sc)
|
|
301
|
+
(try (.setItem js/localStorage "kairotic-learn-scope" (name sc)) (catch :default _ nil)))))))
|
|
302
|
+
(mine-load!) (mine-listen!)
|
|
303
|
+
(.addEventListener js/document "keydown" (fn [e] (when (and @learn-state (= "Escape" (.-key e))) (learn false)))))
|
package/page/23-ids.cljs
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
;; SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
;; Kairotic · page · box ids (issue #163)
|
|
3
|
+
;; Boxes are named by their ids everywhere ((connect :b1 0 :b2 0), (send! :b7 …), @b3 in the Room tab), so:
|
|
4
|
+
;; - each box shows its id over its top right corner when it is hovered or selected; I shows them all
|
|
5
|
+
;; (02-patch-view.cljs draws it, the page's CSS shows it);
|
|
6
|
+
;; - ( on the patch opens a line of the REPL there, in the field of a new box (05-gestures.cljs);
|
|
7
|
+
;; - while a line of code waits for a box — the REPL, the Code tab, the field opened with (, the Room tab's
|
|
8
|
+
;; message — a click on a box writes its id there (:b7, or @b7 in a message), as a click on a cell does in
|
|
9
|
+
;; a spreadsheet's formula. It waits for one when what is before the cursor ends with a space, ( [ {, or the
|
|
10
|
+
;; : or @ that begins an id; otherwise a click on a box does what it always does.
|
|
11
|
+
|
|
12
|
+
(defn cm-of
|
|
13
|
+
"The CodeMirror view of a wrapper made by 14-editor.cljs, if its text is where the focus is (asked of the
|
|
14
|
+
document's active element: the view's own hasFocus is false while the window itself is not in front)."
|
|
15
|
+
[ed] (when-let [v (some-> ed .-view)] (when (identical? (.-activeElement js/document) (.-contentDOM v)) v)))
|
|
16
|
+
|
|
17
|
+
(declare mark-picking!)
|
|
18
|
+
|
|
19
|
+
;; ───── the line of the REPL on the patch, as an editor (the same as the REPL's) ─────
|
|
20
|
+
;; The field of a new box becomes a line of the REPL when what is written in it starts with ( (05-gestures.cljs);
|
|
21
|
+
;; once the code editor is loaded, that line is one, as the REPL at the bottom is: completion of Kairotic's
|
|
22
|
+
;; commands and clojure.core, brackets closed and matched, Enter runs it once its brackets are balanced
|
|
23
|
+
;; (Shift+Enter: a new line), ↑ ↓ the REPL's history, Esc closes it. What it gave shows below it.
|
|
24
|
+
|
|
25
|
+
(defonce line-ed (atom nil)) ; {:ed editor :el the line :words what it completes} while the line is open
|
|
26
|
+
|
|
27
|
+
(defn close-repl-line!
|
|
28
|
+
"Closes the line of the REPL on the patch, without running anything."
|
|
29
|
+
[]
|
|
30
|
+
(when-let [{:keys [ed el]} @line-ed]
|
|
31
|
+
(reset! line-ed nil)
|
|
32
|
+
(try (.destroy ed) (catch :default _ nil))
|
|
33
|
+
(.remove el)
|
|
34
|
+
(mark-picking!)))
|
|
35
|
+
|
|
36
|
+
(defn repl-line-text "What is written in the line of the REPL on the patch (nil if it is not open)." []
|
|
37
|
+
(some-> @line-ed :ed .getValue))
|
|
38
|
+
|
|
39
|
+
(defn open-repl-cm!
|
|
40
|
+
"Opens the line of the REPL on the patch as a code editor (cm, the editor's module), at x y, with text."
|
|
41
|
+
[cm x y text]
|
|
42
|
+
(close-repl-line!)
|
|
43
|
+
(let [el (js/document.createElement "div") host (js/document.createElement "div")
|
|
44
|
+
out (js/document.createElement "div")
|
|
45
|
+
ed (atom nil) hidx (atom nil)
|
|
46
|
+
free? (fn [] (not (.completing @ed)))
|
|
47
|
+
start (if (= "(" (str/trim (str text))) "()" (str text))
|
|
48
|
+
words (vec (concat (command-words) (ns-names 'clojure.core "")))
|
|
49
|
+
[px py] (wrap-px x (- y 4))
|
|
50
|
+
run (fn []
|
|
51
|
+
(let [code (str/trim (.getValue @ed))]
|
|
52
|
+
(if (str/blank? code)
|
|
53
|
+
(close-repl-line!)
|
|
54
|
+
(do (reset! hidx nil)
|
|
55
|
+
(repl-line-run! code out)
|
|
56
|
+
(.setValue @ed "")
|
|
57
|
+
(.focus @ed)))
|
|
58
|
+
true))
|
|
59
|
+
recall (fn [up?]
|
|
60
|
+
(let [h (:hist @ui) n (count h)]
|
|
61
|
+
(when (pos? n)
|
|
62
|
+
(let [i (if up? (max 0 (dec (or @hidx n))) (min n (inc (or @hidx n))))]
|
|
63
|
+
(reset! hidx i)
|
|
64
|
+
(.setValue @ed (if (< i n) (nth h i) ""))
|
|
65
|
+
(.cursorToEnd @ed)
|
|
66
|
+
true))))]
|
|
67
|
+
(set! (.-id el) "repl-line")
|
|
68
|
+
(set! (.-className el) "edit as-repl repl-line")
|
|
69
|
+
(set! (.-className host) "repl-line-host")
|
|
70
|
+
(set! (.-id out) "box-edit-out")
|
|
71
|
+
(set! (.-className out) "edit-out")
|
|
72
|
+
(set! (.. el -style -left) (str px "px"))
|
|
73
|
+
(set! (.. el -style -top) (str py "px"))
|
|
74
|
+
(.appendChild el host)
|
|
75
|
+
(.appendChild el out)
|
|
76
|
+
;; keys typed in it are its own: a space is not ▶, Delete does not delete a box
|
|
77
|
+
(.addEventListener el "keydown" (fn [e] (.stopPropagation e)))
|
|
78
|
+
(.appendChild ($ "canvas-wrap") el)
|
|
79
|
+
(reset! ed ((.-createEditor cm)
|
|
80
|
+
#js {:parent host :doc start :language "clojurescript" :gutter false
|
|
81
|
+
:placeholder "(command) · Enter runs it · Esc closes"
|
|
82
|
+
:words (clj->js words)
|
|
83
|
+
:onCancel (fn [] (close-repl-line!))
|
|
84
|
+
:keys #js [#js {:key "Enter" :run (fn [] (when (and (free?) (balanced? (.getValue @ed))) (run)))}
|
|
85
|
+
#js {:key "Shift-Enter" :run (fn [] (.newline @ed) true)}
|
|
86
|
+
#js {:key "ArrowUp" :run (fn [] (boolean (and (free?) (= 1 (.cursorLine @ed)) (recall true))))}
|
|
87
|
+
#js {:key "ArrowDown" :run (fn [] (boolean (and (free?) (= (.cursorLine @ed) (.lineCount @ed)) (recall false))))}]}))
|
|
88
|
+
(.setAttribute (.-contentDOM (.-view @ed)) "aria-label" "A line of the REPL")
|
|
89
|
+
(reset! line-ed {:ed @ed :el el :words words})
|
|
90
|
+
;; the cursor inside the brackets of a new line, or at the end of what was written
|
|
91
|
+
(let [v (.-view @ed)]
|
|
92
|
+
(.dispatch v #js {:selection #js {:anchor (if (= start "()") 1 (count start))}}))
|
|
93
|
+
(.focus @ed)
|
|
94
|
+
;; leaving it closes it (a click on a box that writes its id keeps the focus here)
|
|
95
|
+
(.addEventListener el "focusout"
|
|
96
|
+
(fn [_] (js/setTimeout (fn [] (when (and (= el (:el @line-ed)) (not (.contains el (.-activeElement js/document))))
|
|
97
|
+
(close-repl-line!)))
|
|
98
|
+
0)))
|
|
99
|
+
@ed))
|
|
100
|
+
|
|
101
|
+
(defn upgrade-repl-line!
|
|
102
|
+
"The field of a new box that became a line of the REPL (input inp, at x y), as a code editor once it is loaded;
|
|
103
|
+
until then, or without it, the field itself is the line. close-field! removes the field without running it."
|
|
104
|
+
[inp x y close-field!]
|
|
105
|
+
(.then (load-cm)
|
|
106
|
+
(fn [cm]
|
|
107
|
+
(when (and cm (.-createEditor cm) (.-isConnected inp))
|
|
108
|
+
(let [text (.-value inp)]
|
|
109
|
+
(close-field!)
|
|
110
|
+
(open-repl-cm! cm x y text))))))
|
|
111
|
+
|
|
112
|
+
(defn id-target
|
|
113
|
+
"Where an id would be written now: {:cm view :mark \":\"}, {:el input :mark \":\" or \"@\"}, or nil."
|
|
114
|
+
[]
|
|
115
|
+
(let [a (.-activeElement js/document) id (some-> a .-id)]
|
|
116
|
+
(cond
|
|
117
|
+
(cm-of (:ed @line-ed)) {:cm (cm-of (:ed @line-ed)) :mark ":"}
|
|
118
|
+
(cm-of @repl-ed) {:cm (cm-of @repl-ed) :mark ":"}
|
|
119
|
+
(cm-of @code-ed) {:cm (cm-of @code-ed) :mark ":"}
|
|
120
|
+
(#{"repl-in" "code"} id) {:el a :mark ":"}
|
|
121
|
+
(and (= id "box-edit") (.contains (.-classList a) "as-repl")) {:el a :mark ":"}
|
|
122
|
+
(= id "talk-say") {:el a :mark "@"}
|
|
123
|
+
:else nil)))
|
|
124
|
+
|
|
125
|
+
(defn before-cursor
|
|
126
|
+
"The text before the cursor of a target."
|
|
127
|
+
[{:keys [cm el]}]
|
|
128
|
+
(cond cm (let [h (.. cm -state -selection -main -from)] (.sliceDoc (.-state cm) 0 h))
|
|
129
|
+
el (subs (str (.-value el)) 0 (or (.-selectionStart el) (count (.-value el))))
|
|
130
|
+
:else ""))
|
|
131
|
+
|
|
132
|
+
(defn waits-for-id?
|
|
133
|
+
"Does the text before the cursor wait for a box: not empty, and ending with a space, ( [ {, or the mark."
|
|
134
|
+
[t before]
|
|
135
|
+
(and (seq (str/trim before))
|
|
136
|
+
(boolean (re-find (re-pattern (str "[\\s(\\[{" (if (= "@" (:mark t)) "@" ":") "]$")) before))))
|
|
137
|
+
|
|
138
|
+
(defn picking-target
|
|
139
|
+
"The target, when a click on a box would write its id there; nil otherwise."
|
|
140
|
+
[]
|
|
141
|
+
(when-not (locked?)
|
|
142
|
+
(when-let [t (id-target)]
|
|
143
|
+
(when (waits-for-id? t (before-cursor t)) t))))
|
|
144
|
+
|
|
145
|
+
(defn write-id!
|
|
146
|
+
"Writes the id of box id at the cursor of target t (after its mark, if it is already there), then a space."
|
|
147
|
+
[t id]
|
|
148
|
+
(let [before (before-cursor t)
|
|
149
|
+
mark (:mark t)
|
|
150
|
+
text (str (if (str/ends-with? before mark) "" mark) (name id) " ")]
|
|
151
|
+
(if-let [cm (:cm t)]
|
|
152
|
+
;; as typed (input.type): the Clojure editor keeps the space after it, before a closing bracket too
|
|
153
|
+
(let [sel (.. cm -state -selection -main) from (.-from sel)]
|
|
154
|
+
(.dispatch cm #js {:changes #js {:from from :to (.-to sel) :insert text}
|
|
155
|
+
:selection #js {:anchor (+ from (count text))}
|
|
156
|
+
:userEvent "input.type"}))
|
|
157
|
+
(let [el (:el t) s (.-selectionStart el) e (.-selectionEnd el)]
|
|
158
|
+
(.setRangeText el text s e "end")
|
|
159
|
+
(.dispatchEvent el (js/Event. "input" #js {:bubbles true}))))))
|
|
160
|
+
|
|
161
|
+
(defn mark-picking!
|
|
162
|
+
"The patch shows (a copy pointer, a box marked under it) whether a click on a box would write its id."
|
|
163
|
+
[]
|
|
164
|
+
(.toggle (.-classList svg) "picking" (some? (picking-target))))
|
|
165
|
+
|
|
166
|
+
(doseq [ev ["selectionchange" "focusin" "focusout" "keyup" "input"]]
|
|
167
|
+
(.addEventListener js/document ev (fn [_] (mark-picking!)) true))
|
|
168
|
+
|
|
169
|
+
;; the click: before the patch's own handler (capture), and without taking the focus from the line
|
|
170
|
+
(defonce id-click (atom false))
|
|
171
|
+
|
|
172
|
+
(.addEventListener svg "pointerdown"
|
|
173
|
+
(fn [e]
|
|
174
|
+
(reset! id-click false)
|
|
175
|
+
(when-let [t (picking-target)]
|
|
176
|
+
(when-let [el (when-not (closest e ".port") (closest e ".box"))]
|
|
177
|
+
(let [id (keyword (.. el -dataset -box))]
|
|
178
|
+
(when (get-in (current) [:boxes id])
|
|
179
|
+
(reset! id-click true)
|
|
180
|
+
(.preventDefault e)
|
|
181
|
+
(.stopImmediatePropagation e)
|
|
182
|
+
(write-id! t id)
|
|
183
|
+
;; the box named lights up a moment
|
|
184
|
+
(.add (.-classList el) "picked")
|
|
185
|
+
(js/setTimeout (fn [] (some-> (.querySelector ($ "scene") (str "g.box[data-box=\"" (name id) "\"]"))
|
|
186
|
+
.-classList (.remove "picked")))
|
|
187
|
+
400)
|
|
188
|
+
(mark-picking!))))))
|
|
189
|
+
true)
|
|
190
|
+
|
|
191
|
+
;; the press that wrote an id keeps the focus where it was
|
|
192
|
+
(.addEventListener svg "mousedown" (fn [e] (when @id-click (.preventDefault e))) true)
|
|
193
|
+
(.addEventListener svg "click" (fn [e] (when @id-click (.stopImmediatePropagation e) (reset! id-click false))) true)
|
|
194
|
+
|
|
195
|
+
;; ───── keys on the patch: ( opens a line of the REPL, I shows every id ─────
|
|
196
|
+
|
|
197
|
+
(defonce last-patch-pt (atom nil)) ; where the pointer last was over the patch, in patch coordinates
|
|
198
|
+
(.addEventListener svg "pointermove" (fn [e] (reset! last-patch-pt (pt e))))
|
|
199
|
+
(.addEventListener svg "pointerleave" (fn [_] (reset! last-patch-pt nil)))
|
|
200
|
+
|
|
201
|
+
(defn patch-middle
|
|
202
|
+
"The middle of the part of the patch on screen, in patch coordinates."
|
|
203
|
+
[]
|
|
204
|
+
(let [w ($ "canvas-wrap")]
|
|
205
|
+
[(+ (.-scrollLeft w) (* 0.3 (.-clientWidth w))) (+ (.-scrollTop w) (* 0.4 (.-clientHeight w)))]))
|
|
206
|
+
|
|
207
|
+
(defn open-repl-line!
|
|
208
|
+
"Opens a line of the REPL on the patch, where the pointer is (or in the middle of what is on screen)."
|
|
209
|
+
[]
|
|
210
|
+
(let [[x y] (or @last-patch-pt (patch-middle))]
|
|
211
|
+
(open-editor! {:x x :y y :text "("})))
|
|
212
|
+
|
|
213
|
+
(defn show-ids!
|
|
214
|
+
"Shows the id of every box (true), only of the one hovered or selected (false), or switches."
|
|
215
|
+
([] (show-ids! (not (.contains (.-classList svg) "show-ids"))))
|
|
216
|
+
([on]
|
|
217
|
+
(.toggle (.-classList svg) "show-ids" (boolean on))
|
|
218
|
+
(try (.setItem js/localStorage "kairotic.show-ids" (if on "1" "0")) (catch :default _ nil))
|
|
219
|
+
(boolean on)))
|
|
220
|
+
|
|
221
|
+
(try (when (= "1" (.getItem js/localStorage "kairotic.show-ids")) (show-ids! true)) (catch :default _ nil))
|
|
222
|
+
|
|
223
|
+
(.addEventListener js/document "keydown"
|
|
224
|
+
(fn [e]
|
|
225
|
+
(let [a (.-activeElement js/document)
|
|
226
|
+
typing? (or (#{"INPUT" "TEXTAREA" "SELECT"} (some-> a .-tagName)) (some-> a .-isContentEditable))
|
|
227
|
+
k (.-key e)]
|
|
228
|
+
(when (and (not typing?) (not (.-ctrlKey e)) (not (.-metaKey e)) (not (.-altKey e))
|
|
229
|
+
(.-hidden ($ "manager")) (not (locked?)) (not embed-mode))
|
|
230
|
+
(cond
|
|
231
|
+
(= k "(") (do (.preventDefault e) (open-repl-line!))
|
|
232
|
+
(and (#{"i" "I"} k) (not (:tsel @ui)) (not (:nsel @ui))) (do (.preventDefault e) (show-ids!))
|
|
233
|
+
:else nil)))))
|