-
Notifications
You must be signed in to change notification settings - Fork 164
/
captureMenu.ts
267 lines (232 loc) · 10.6 KB
/
captureMenu.ts
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Import application Styles.
import "../styles/captureMenu.scss";
import { Observable } from "../../shared/utils/observable";
import { MVX } from "../mvx/mvx";
import { CaptureMenuComponent, ICaptureMenuComponentState } from "./captureMenuComponent";
import { CanvasListItemComponent } from "./canvasListItemComponent";
import { CaptureMenuActionsComponent } from "./captureMenuActionsComponent";
import { CanvasListComponent, ICanvasListComponentState } from "./canvasListComponent";
import { FpsCounterComponent } from "./fpsCounterComponent";
import { LogLevel } from "../../shared/utils/logger";
export interface ICanvasInformation {
id: string;
width: number;
height: number;
ref: any;
}
export interface ICaptureMenuOptions {
readonly rootPlaceHolder?: Element;
readonly canvas?: HTMLCanvasElement;
readonly hideLog?: boolean;
}
interface IArrayLike<T> {
length: number;
[index: number]: T;
}
export class CaptureMenu {
public static SelectCanvasHelpText = "Please, select a canvas in the list above.";
public static ActionsHelpText = "Record with the red button, you can also pause or continue playing the current scene.";
public static PleaseWaitHelpText = "Capturing, be patient (this can take up to 3 minutes)...";
public readonly onCanvasSelected: Observable<ICanvasInformation>;
public readonly onCaptureRequested: Observable<ICanvasInformation>;
public readonly onPauseRequested: Observable<ICanvasInformation>;
public readonly onPlayRequested: Observable<ICanvasInformation>;
public readonly onPlayNextFrameRequested: Observable<ICanvasInformation>;
private readonly rootPlaceHolder: Element;
private readonly mvx: MVX;
private readonly captureMenuComponent: CaptureMenuComponent;
private readonly canvasListItemComponent: CanvasListItemComponent;
private readonly actionsComponent: CaptureMenuActionsComponent;
private readonly canvasListComponent: CanvasListComponent;
private readonly fpsCounterComponent: FpsCounterComponent;
private readonly rootStateId: number;
private readonly fpsStateId: number;
private readonly actionsStateId: number;
private readonly canvasListStateId: number;
private isTrackingCanvas: boolean;
constructor(private readonly options: ICaptureMenuOptions = {}) {
this.rootPlaceHolder = options.rootPlaceHolder || document.body;
this.mvx = new MVX(this.rootPlaceHolder);
this.isTrackingCanvas = false;
this.onCanvasSelected = new Observable<ICanvasInformation>();
this.onCaptureRequested = new Observable<ICanvasInformation>();
this.onPauseRequested = new Observable<ICanvasInformation>();
this.onPlayRequested = new Observable<ICanvasInformation>();
this.onPlayNextFrameRequested = new Observable<ICanvasInformation>();
this.captureMenuComponent = new CaptureMenuComponent();
this.canvasListComponent = new CanvasListComponent();
this.canvasListItemComponent = new CanvasListItemComponent();
this.actionsComponent = new CaptureMenuActionsComponent();
this.fpsCounterComponent = new FpsCounterComponent();
this.rootStateId = this.mvx.addRootState({
visible: true,
logLevel: LogLevel.info,
logText: CaptureMenu.SelectCanvasHelpText,
logVisible: !this.options.hideLog,
}, this.captureMenuComponent);
this.canvasListStateId = this.mvx.addChildState(this.rootStateId, { currentCanvasInformation: null, showList: false }, this.canvasListComponent);
this.actionsStateId = this.mvx.addChildState(this.rootStateId, true, this.actionsComponent);
this.fpsStateId = this.mvx.addChildState(this.rootStateId, 0, this.fpsCounterComponent);
this.actionsComponent.onCaptureRequested.add(() => {
const currentCanvasInformation = this.getSelectedCanvasInformation();
if (currentCanvasInformation) {
this.updateMenuStateLog(LogLevel.info, CaptureMenu.PleaseWaitHelpText, true);
}
// Defer to ensure the log displays.
setTimeout(() => {
this.onCaptureRequested.trigger(currentCanvasInformation);
}, 200);
});
this.actionsComponent.onPauseRequested.add(() => {
this.onPauseRequested.trigger(this.getSelectedCanvasInformation());
this.mvx.updateState(this.actionsStateId, false);
});
this.actionsComponent.onPlayRequested.add(() => {
this.onPlayRequested.trigger(this.getSelectedCanvasInformation());
this.mvx.updateState(this.actionsStateId, true);
});
this.actionsComponent.onPlayNextFrameRequested.add(() => {
this.onPlayNextFrameRequested.trigger(this.getSelectedCanvasInformation());
});
this.canvasListComponent.onCanvasSelection.add((eventArgs) => {
this.mvx.updateState(this.canvasListStateId, {
currentCanvasInformation: null,
showList: !eventArgs.state.showList,
});
this.updateMenuStateLog(LogLevel.info, CaptureMenu.SelectCanvasHelpText);
this.onCanvasSelected.trigger(null);
if (this.isTrackingCanvas) {
this.trackPageCanvases();
}
if (eventArgs.state.showList) {
this.showMenuStateLog();
}
else {
this.hideMenuStateLog();
}
});
this.canvasListItemComponent.onCanvasSelected.add((eventArgs) => {
this.mvx.updateState(this.canvasListStateId, {
currentCanvasInformation: eventArgs.state,
showList: false,
});
this.onCanvasSelected.trigger(eventArgs.state);
this.updateMenuStateLog(LogLevel.info, CaptureMenu.ActionsHelpText);
this.showMenuStateLog();
});
}
public getSelectedCanvasInformation(): ICanvasInformation {
const canvasListState = this.mvx.getGenericState<ICanvasListComponentState>(this.canvasListStateId);
return canvasListState.currentCanvasInformation;
}
public trackPageCanvases(): void {
this.isTrackingCanvas = true;
if (document.body) {
const canvases = document.body.querySelectorAll("canvas");
this.updateCanvasesList(canvases);
}
}
public updateCanvasesList(canvases: NodeListOf<HTMLCanvasElement>): void {
this.updateCanvasesListInformationInternal(canvases, (info) => {
return {
id: info.id,
width: info.width,
height: info.height,
ref: info,
};
});
}
public updateCanvasesListInformation(canvasesInformation: ICanvasInformation[]): void {
this.updateCanvasesListInformationInternal(canvasesInformation, (info) => {
return {
id: info.id,
width: info.width,
height: info.height,
ref: info.ref,
};
});
}
public display(): void {
this.updateMenuStateVisibility(true);
}
public hide(): void {
this.updateMenuStateVisibility(false);
}
public captureComplete(errorText: string): void {
if (errorText) {
this.updateMenuStateLog(LogLevel.error, errorText);
}
else {
this.updateMenuStateLog(LogLevel.info, CaptureMenu.ActionsHelpText);
}
}
public setFPS(fps: number): void {
this.mvx.updateState(this.fpsStateId, fps);
}
private updateCanvasesListInformationInternal<T>(canvasesInformation: ArrayLike<T>, convertToListInfo: (info: T) => ICanvasInformation): void {
// Create a consumable information list for the view.
this.mvx.removeChildrenStates(this.canvasListStateId);
const canvasesInformationClone: ICanvasInformation[] = [];
for (let i = 0; i < canvasesInformation.length; i++) {
const canvas = canvasesInformation[i];
const canvasInformationClone = convertToListInfo(canvas);
canvasesInformationClone.push(canvasInformationClone);
this.mvx.addChildState(this.canvasListStateId, canvasInformationClone, this.canvasListItemComponent);
}
// Auto Select in the list if only one canvas.
const canvasesCount = canvasesInformationClone.length;
const canvasListState = this.mvx.getGenericState<ICanvasListComponentState>(this.canvasListStateId);
const visible = canvasListState.showList;
if (!visible) {
if (canvasesCount === 1) {
const canvasToSelect = canvasesInformationClone[0];
this.mvx.updateState(this.canvasListStateId, {
currentCanvasInformation: canvasToSelect,
showList: visible,
});
this.updateMenuStateLog(LogLevel.info, CaptureMenu.ActionsHelpText);
this.onCanvasSelected.trigger(canvasToSelect);
}
else {
this.updateMenuStateLog(LogLevel.info, CaptureMenu.SelectCanvasHelpText);
this.onCanvasSelected.trigger(null);
}
}
}
private hideMenuStateLog() {
const menuState = this.mvx.getGenericState<ICaptureMenuComponentState>(this.rootStateId);
this.mvx.updateState(this.rootStateId, {
visible: menuState.visible,
logLevel: menuState.logLevel,
logText: menuState.logText,
logVisible: false,
});
}
private showMenuStateLog() {
const menuState = this.mvx.getGenericState<ICaptureMenuComponentState>(this.rootStateId);
this.mvx.updateState(this.rootStateId, {
visible: menuState.visible,
logLevel: menuState.logLevel,
logText: menuState.logText,
logVisible: !this.options.hideLog,
});
}
private updateMenuStateLog(logLevel: LogLevel, logText: string, immediate = false) {
const menuState = this.mvx.getGenericState<ICaptureMenuComponentState>(this.rootStateId);
this.mvx.updateState(this.rootStateId, {
visible: menuState.visible,
logLevel,
logText,
logVisible: !this.options.hideLog,
}, immediate);
}
private updateMenuStateVisibility(visible: boolean) {
const menuState = this.mvx.getGenericState<ICaptureMenuComponentState>(this.rootStateId);
this.mvx.updateState(this.rootStateId, {
visible,
logLevel: menuState.logLevel,
logText: menuState.logText,
logVisible: menuState.logVisible,
});
}
}