Skip to content

Commit 089ab9e

Browse files
committed
Set note keys when letter is pressed on keyboard
1 parent 16fdde2 commit 089ab9e

File tree

4 files changed

+26
-20
lines changed

4 files changed

+26
-20
lines changed

src/App.ml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,11 @@ let update model = function
103103
| None -> Cmd.msg (SelectNote (Some default))
104104
in
105105
(match key with
106-
| Keyboard.Up -> model, maybe_update_note Direction.Next
107-
| Keyboard.Down -> model, maybe_update_note Direction.Prev
108-
| Keyboard.Left -> model, maybe_update_index Tune.Index.max Tune.Index.prev
109-
| Keyboard.Right -> model, maybe_update_index Tune.Index.min Tune.Index.next)
106+
| Keyboard.Up -> model, maybe_update_note Direction.Next
107+
| Keyboard.Down -> model, maybe_update_note Direction.Prev
108+
| Keyboard.Left -> model, maybe_update_index Tune.Index.max Tune.Index.prev
109+
| Keyboard.Right -> model, maybe_update_index Tune.Index.min Tune.Index.next
110+
| Keyboard.ChangeNote note -> model, maybe_update_note (Direction.Set note))
110111
| PlayingNote maybe_index -> { model with playing_index = maybe_index }, Cmd.none
111112
| ExportImage -> { model with awaiting_frame = true; selected_index = None }, Cmd.none
112113
| UrlChange location ->
@@ -131,10 +132,10 @@ let update model = function
131132
in
132133
let maybe_new_note = model.tune |> Tune.get index |> f in
133134
(match maybe_new_note with
134-
| None -> model, Cmd.none
135-
| Some new_note ->
136-
let new_tune = model.tune |> Tune.update index new_note in
137-
{ model with tune = new_tune }, Cmd.msg (PlayNote new_note))
135+
| None -> model, Cmd.none
136+
| Some new_note ->
137+
let new_tune = model.tune |> Tune.update index new_note in
138+
{ model with tune = new_tune }, Cmd.msg (PlayNote new_note))
138139
| PlayNote note ->
139140
let play_note _ =
140141
match note with
@@ -270,8 +271,8 @@ let view model =
270271
let subscriptions model =
271272
Sub.batch
272273
[ (if model.awaiting_frame
273-
then AnimationFrame.every (fun _ -> FrameRendered)
274-
else Sub.none)
274+
then AnimationFrame.every (fun _ -> FrameRendered)
275+
else Sub.none)
275276
; Sub.map keyPressed Keyboard.pressed
276277
]
277278
;;

src/Keyboard.ml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@ type key =
55
| Down
66
| Left
77
| Right
8+
| ChangeNote of Note.note
89

9-
let key_of_int = function
10-
| 37 -> Some Left
11-
| 38 -> Some Up
12-
| 39 -> Some Right
13-
| 40 -> Some Down
10+
let of_note note = ChangeNote note
11+
12+
let key_of_string = function
13+
| "ArrowLeft" -> Some Left
14+
| "ArrowUp" -> Some Up
15+
| "ArrowRight" -> Some Right
16+
| "ArrowDown" -> Some Down
17+
| other when String.length other == 1 -> Note.of_char other |. Belt.Option.map of_note
1418
| _ -> None
1519
;;
1620

21+
(* TODO: Ignore key when ctrl or cmd are pressed *)
1722
let decode_event =
1823
let open Tea.Json in
19-
Decoder.field "keyCode" Decoder.int |> Decoder.map key_of_int
24+
Decoder.field "key" Decoder.string |> Decoder.map key_of_string
2025
;;
2126

2227
let pressed =

src/Note.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ let prev note = (meta note).prev
5656
let color note = (meta note).color
5757
let string_of_note note = (meta note).as_str
5858

59-
let from_char = function
59+
let of_char = function
6060
| "z" -> Some Rest
6161
| "-" -> Some Hold
6262
| "g" -> Some G
@@ -72,12 +72,12 @@ let from_char = function
7272
| "C" -> Some C'
7373
| "D" -> Some D'
7474
| "E" -> Some E'
75-
| "q" -> Some Random
75+
| "q" | "?" -> Some Random
7676
| _ -> None
7777
;;
7878

7979
let notes_of_string str =
80-
let get_or_rest c = from_char c |. Belt.Option.getWithDefault Rest in
80+
let get_or_rest c = of_char c |. Belt.Option.getWithDefault Rest in
8181
Js.String.split "" str |> Array.map get_or_rest |> Array.to_list
8282
;;
8383

src/Tune.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ let random () : t = List.init length (fun _ -> Note.random ())
2121

2222
let from_string (str : string) : t =
2323
let right_pad = length - String.length str |> max 0 in
24-
let get_or_rest c = Note.from_char c |. Belt.Option.getWithDefault Note.Rest in
24+
let get_or_rest c = Note.of_char c |. Belt.Option.getWithDefault Note.Rest in
2525
str
2626
|> Js.String.slice ~from:0 ~to_:length
2727
|> Js.String.split ""

0 commit comments

Comments
 (0)