annotate web/timeline/brick_layout.ts @ 2439:06da5db2fafe

rewrite ascoltami to use the graph for more playback data
author drewp@bigasterisk.com
date Thu, 30 May 2024 01:08:07 -0700
parents 4556eebe5d73
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
1 import { debug } from "debug";
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
2 import { sortBy } from "underscore";
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
3 import { ViewState } from "viewstate";
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
4 const log = debug("brick");
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
5
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
6 interface Placement {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
7 row?: number;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
8 prev?: number;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
9 t0: number;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
10 t1: number;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
11 onRowChange: () => void;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
12 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
13
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
14 export class BrickLayout {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
15 viewState: ViewState;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
16 numRows: number;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
17 noteRow: { [uri: string]: Placement };
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
18 constructor(viewState: ViewState, numRows: number) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
19 this.viewState = viewState;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
20 this.numRows = numRows;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
21 this.noteRow = {}; // uristr: row, t0, t1, onRowChange
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
22 }
1771
792bf30de608 brick mode works. fix some refresh issues.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
23
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
24 addNote(n: { uri: { value: string } }, onRowChange: any) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
25 this.noteRow[n.uri.value] = { row: 0, t0: 0, t1: 0, onRowChange };
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
26 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
27
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
28 setNoteSpan(n: { uri: { value: string } }, t0: any, t1: any) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
29 this.noteRow[n.uri.value].t0 = t0;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
30 this.noteRow[n.uri.value].t1 = t1;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
31 this._recompute();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
32 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
33
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
34 delNote(n: { uri: { value: string } }) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
35 delete this.noteRow[n.uri.value];
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
36 this._recompute();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
37 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
38
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
39 _recompute() {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
40 for (let u in this.noteRow) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
41 const row = this.noteRow[u];
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
42 row.prev = row.row;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
43 row.row = undefined;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
44 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
45 const overlap = (a: Placement, b: Placement) => a.t0 < b.t1 && a.t1 > b.t0;
1771
792bf30de608 brick mode works. fix some refresh issues.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
46
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
47 const result = [];
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
48 for (let u in this.noteRow) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
49 const row = this.noteRow[u];
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
50 result.push({ dur: row.t1 - row.t0 + row.t0 * 0.0001, uri: u });
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
51 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
52 const notesByWidth = sortBy(result, "dur");
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
53 notesByWidth.reverse();
1771
792bf30de608 brick mode works. fix some refresh issues.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
54
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
55 for (let n of Array.from(notesByWidth)) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
56 const blockedRows = new Set();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
57 for (let u in this.noteRow) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
58 const other = this.noteRow[u];
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
59 if (other.row !== null) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
60 if (overlap(other, this.noteRow[n.uri])) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
61 blockedRows.add(other.row);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
62 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
63 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
64 }
1771
792bf30de608 brick mode works. fix some refresh issues.
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
65
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
66 for (let r = 0; r < this.numRows; r++) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
67 if (!blockedRows.has(r)) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
68 this.noteRow[n.uri].row = r;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
69 break;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
70 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
71 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
72 if (this.noteRow[n.uri].row === null) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
73 log(`warning: couldn't place ${n.uri}`);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
74 this.noteRow[n.uri].row = 0;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
75 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
76 if (this.noteRow[n.uri].row !== this.noteRow[n.uri].prev) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
77 this.noteRow[n.uri].onRowChange();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
78 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
79 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
80 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
81
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
82 rowBottom(row: number) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
83 return this.viewState.rowsY() + 20 + 150 * row + 140;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
84 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
85
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
86 yForVFor(n: { uri: { value: string } }) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
87 const row = this.noteRow[n.uri.value].row;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
88 if (row === undefined) {
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
89 throw new Error();
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
90 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
91 const rowBottom = this.rowBottom(row);
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
92 const rowTop = rowBottom - 140;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
93 return (v: number) => rowBottom + (rowTop - rowBottom) * v;
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
94 }
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 1901
diff changeset
95 }