-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathHorizontalScreenLayout.ts
More file actions
234 lines (199 loc) · 8.61 KB
/
HorizontalScreenLayout.ts
File metadata and controls
234 lines (199 loc) · 8.61 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { Logger } from '@coderline/alphatab/Logger';
import type { MasterBar } from '@coderline/alphatab/model/MasterBar';
import type { Score } from '@coderline/alphatab/model/Score';
import { TextAlign } from '@coderline/alphatab/platform/ICanvas';
import type { RenderHints } from '@coderline/alphatab/rendering/IScoreRenderer';
import { ScoreLayout } from '@coderline/alphatab/rendering/layout/ScoreLayout';
import { RenderFinishedEventArgs } from '@coderline/alphatab/rendering/RenderFinishedEventArgs';
import type { MasterBarsRenderers } from '@coderline/alphatab/rendering/staves/MasterBarsRenderers';
import type { StaffSystem } from '@coderline/alphatab/rendering/staves/StaffSystem';
/**
* @internal
*/
export class HorizontalScreenLayoutPartialInfo {
public x: number = 0;
public width: number = 0;
public masterBars: MasterBar[] = [];
public results: MasterBarsRenderers[] = [];
}
/**
* This layout arranges the bars all horizontally
* @internal
*/
export class HorizontalScreenLayout extends ScoreLayout {
private _system: StaffSystem | null = null;
public get name(): string {
return 'HorizontalScreen';
}
public get supportsResize(): boolean {
return false;
}
public get firstBarX(): number {
let x = this.pagePadding![0];
if (this._system) {
x += this._system.accoladeWidth;
}
return x;
}
public doResize(): void {
// not supported
}
public override doUpdateForBars(_renderHints: RenderHints): boolean {
// not supported yet, modifications likely cause anyhow full updates
// as we do not optimize effect bands yet. with effect bands being more
// isolated in bars we could try updating dynamically
return false;
}
protected doLayoutAndRender(renderHints: RenderHints | undefined): void {
const score: Score = this.renderer.score!;
let startIndex: number = this.renderer.settings.display.startBar;
startIndex--; // map to array index
startIndex = Math.min(score.masterBars.length - 1, Math.max(0, startIndex));
let currentBarIndex: number = startIndex;
let endBarIndex: number = this.renderer.settings.display.barCount;
if (endBarIndex <= 0) {
endBarIndex = score.masterBars.length;
}
endBarIndex = startIndex + endBarIndex - 1; // map count to array index
endBarIndex = Math.min(score.masterBars.length - 1, Math.max(0, endBarIndex));
this._system = this.createEmptyStaffSystem(0);
this._system.isLast = true;
this._system.x = this.pagePadding![0];
this._system.y = this.pagePadding![1];
const countPerPartial: number = this.renderer.settings.display.barCountPerPartial;
const partials: HorizontalScreenLayoutPartialInfo[] = [];
let currentPartial: HorizontalScreenLayoutPartialInfo = new HorizontalScreenLayoutPartialInfo();
while (currentBarIndex <= endBarIndex) {
const multiBarRestInfo = this.multiBarRestInfo;
const additionalMultiBarsRestBarIndices: number[] | null =
multiBarRestInfo !== null && multiBarRestInfo.has(currentBarIndex)
? multiBarRestInfo.get(currentBarIndex)!
: null;
const result = this._system.addBars(
this.renderer.tracks!,
currentBarIndex,
additionalMultiBarsRestBarIndices
);
// complete partial if its full and we are not linked
if (currentPartial.masterBars.length >= countPerPartial && !result.isLinkedToPrevious) {
currentPartial = this._completePartial(partials, currentPartial);
}
this._scaleBars(result);
currentPartial.results.push(result);
currentPartial.masterBars.push(score.masterBars[currentBarIndex]);
currentPartial.width += result.width;
currentBarIndex++;
}
// don't miss the last partial if not empty
if (currentPartial.masterBars.length > 0) {
this._completePartial(partials, currentPartial);
}
this._finalizeStaffSystem();
this.height = Math.floor(this._system.y + this._system.height);
this.width = this._system.x + this._system.width + this.pagePadding![2];
currentBarIndex = 0;
let x = 0;
for (let i: number = 0; i < partials.length; i++) {
const partial: HorizontalScreenLayoutPartialInfo = partials[i];
const e = new RenderFinishedEventArgs();
e.reuseViewport = renderHints?.reuseViewport ?? false;
e.x = x;
e.y = 0;
e.totalWidth = this.width;
e.totalHeight = this.height;
e.width = partial.width;
e.height = this.height;
e.firstMasterBarIndex = partial.masterBars[0].index;
e.lastMasterBarIndex = partial.masterBars[partial.masterBars.length - 1].index;
x += partial.width;
// pull to local scope for lambda
const partialBarIndex = currentBarIndex;
const partialIndex = i;
this._system.buildBoundingsLookup(0, 0);
this.registerPartial(e, canvas => {
let renderX: number = this._system!.getBarX(partial.masterBars[0].index) + this._system!.accoladeWidth;
if (partialIndex === 0) {
renderX -= this._system!.x + this._system!.accoladeWidth;
}
canvas.color = this.renderer.settings.display.resources.mainGlyphColor;
canvas.textAlign = TextAlign.Left;
Logger.debug(
this.name,
`Rendering partial from bar ${partial.masterBars[0].index} to ${partial.masterBars[partial.masterBars.length - 1].index}`,
null
);
this._system!.paintPartial(
-renderX,
this._system!.y,
canvas,
partialBarIndex,
partial.masterBars.length
);
});
currentBarIndex += partial.masterBars.length;
}
this.height = this.layoutAndRenderBottomScoreInfo(this.height);
this.height = this._layoutAndRenderAnnotation(this.height);
this.height += this.pagePadding![3];
this.height *= this.renderer.settings.display.scale;
}
private _scaleBars(result: MasterBarsRenderers) {
result.width = 0;
this._system!.width -= result.width;
for (const r of result.renderers) {
const barDisplayWidth =
r.staff!.system.staves.length > 1 ? r.bar.masterBar.displayWidth : r.bar.displayWidth;
if (barDisplayWidth > 0) {
r.scaleToWidth(barDisplayWidth);
}
const w = r.x + r.width;
if (w > result.width) {
result.width = w;
}
}
this._system!.width += result.width;
}
private _completePartial(
partials: HorizontalScreenLayoutPartialInfo[],
currentPartial: HorizontalScreenLayoutPartialInfo
) {
if (partials.length === 0) {
// respect accolade and on first partial
currentPartial.width += this._system!.accoladeWidth + this.pagePadding![0];
}
partials.push(currentPartial);
Logger.debug(
this.name,
`Finished partial from bar ${currentPartial.masterBars[0].index} to ${currentPartial.masterBars[currentPartial.masterBars.length - 1].index}`,
null
);
// start new partial
const newPartial = new HorizontalScreenLayoutPartialInfo();
newPartial.x = currentPartial.x + currentPartial.width;
return newPartial;
}
private _finalizeStaffSystem() {
this._alignRenderers();
this._system!.finalizeSystem();
}
private _alignRenderers(): void {
this.width = 0;
const system = this._system!;
for (const s of system.allStaves) {
s.resetSharedLayoutData();
let w = 0;
for (const renderer of s.barRenderers) {
renderer.x = w;
renderer.y = s.topPadding + s.topOverflow;
// note: this will ensure aspects like beaming helpers
// and overflows are prepared for finalization
renderer.scaleToWidth(renderer.width);
w += renderer.width;
}
if (w > this.width) {
system.width = w;
}
}
system.width += system.accoladeWidth;
}
}