-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsnapScroll.ts
More file actions
57 lines (44 loc) · 1.46 KB
/
snapScroll.ts
File metadata and controls
57 lines (44 loc) · 1.46 KB
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
import { Beat } from "../../charting";
import { RootStore } from "../../store";
import { Action } from "../action";
/**
* Arguments for the SnapScrollAction.
*/
export interface SnapScrollArgs {
direction: "forward" | "backward";
autoInvert?: boolean;
}
/**
* Action for scrolling the notefield based on the current beat snapping.
*/
export class SnapScrollAction implements Action {
args: SnapScrollArgs;
oldScroll!: Beat;
store: RootStore;
constructor(store: RootStore, args: SnapScrollArgs) {
this.args = args;
this.store = store;
if (this.args.autoInvert === undefined) {
this.args.autoInvert = true;
}
}
run(): void {
const { scrollDirection } = this.store.notefieldDisplay.data;
const { notefield } = this.store;
// Ignore scroll events if the notefield is auto scrolling
if (notefield.data.isPlaying) {
return;
}
this.oldScroll = notefield.data.scroll.beat;
const { scroll, snap } = notefield.data;
let dir = this.args.direction;
if (this.args.autoInvert === true && scrollDirection === "down") {
dir = dir === "forward" ? "backward" : "forward";
}
const beat = dir === "forward" ? snap.nextBeat(scroll.beat) : snap.prevBeat(scroll.beat);
this.store.notefield.setScroll({ beat });
}
undo(): void {
this.store.notefield.setScroll({ beat: this.oldScroll });
}
}