浏览代码

Update StatsPanel. Add fullscreen toggle

customisations
alemart 1年前
父节点
当前提交
a195398ae3
共有 2 个文件被更改,包括 73 次插入26 次删除
  1. 1
    1
      src/core/session.ts
  2. 72
    25
      src/core/stats-panel.ts

+ 1
- 1
src/core/session.ts 查看文件

174
         this._setupRenderLoop();
174
         this._setupRenderLoop();
175
 
175
 
176
         // setup the stats panel
176
         // setup the stats panel
177
-        this._statsPanel = new StatsPanel(this._viewport.hud.container);
177
+        this._statsPanel = new StatsPanel(this._viewport);
178
         this._statsPanel.visible = stats;
178
         this._statsPanel.visible = stats;
179
 
179
 
180
         // done!
180
         // done!

+ 72
- 25
src/core/stats-panel.ts 查看文件

22
 
22
 
23
 import { SpeedySize } from 'speedy-vision/types/core/speedy-size';
23
 import { SpeedySize } from 'speedy-vision/types/core/speedy-size';
24
 import { Settings, PowerPreference } from './settings';
24
 import { Settings, PowerPreference } from './settings';
25
+import { Viewport } from './viewport';
25
 import { Tracker } from '../trackers/tracker';
26
 import { Tracker } from '../trackers/tracker';
26
 import { Source } from '../sources/source';
27
 import { Source } from '../sources/source';
28
+import { Utils, Nullable } from '../utils/utils';
27
 import Martins from '../main';
29
 import Martins from '../main';
28
 
30
 
29
 
31
 
44
  */
46
  */
45
 export class StatsPanel
47
 export class StatsPanel
46
 {
48
 {
49
+    /** The viewport associated to this panel */
50
+    private readonly _viewport: Viewport;
51
+
47
     /** A container for the panel */
52
     /** A container for the panel */
48
     private readonly _container: HTMLDivElement;
53
     private readonly _container: HTMLDivElement;
49
 
54
 
57
      * Constructor
62
      * Constructor
58
      * @param parent parent element of the panel
63
      * @param parent parent element of the panel
59
      */
64
      */
60
-    constructor(parent: HTMLElement)
65
+    constructor(viewport: Viewport)
61
     {
66
     {
62
-        this._container = this._createContainer(parent);
67
+        this._viewport = viewport;
63
         this._lastUpdate = 0;
68
         this._lastUpdate = 0;
69
+
70
+        this._container = this._createContainer();
71
+        viewport.hud.container.appendChild(this._container);
64
     }
72
     }
65
 
73
 
66
     /**
74
     /**
112
      */
120
      */
113
     private _update(trackers: Tracker[], sources: Source[], fps: number, gpu: number): void
121
     private _update(trackers: Tracker[], sources: Source[], fps: number, gpu: number): void
114
     {
122
     {
115
-        const get = (className: string) => this._container.getElementsByClassName(className) as HTMLCollectionOf<HTMLElement>;
116
-
117
         // all sanitized
123
         // all sanitized
118
-        const lfps = get('_ar_fps');
119
-        if(lfps.length > 0) {
120
-            lfps[0].style.color = this._color(fps);
121
-            lfps[0].innerText = String(fps);
124
+        const lfps = this._label('_ar_fps');
125
+        if(lfps !== null) {
126
+            lfps.style.color = this._color(fps);
127
+            lfps.innerText = String(fps);
122
         }
128
         }
123
 
129
 
124
-        const lgpu = get('_ar_gpu');
125
-        if(lgpu.length > 0) {
126
-            lgpu[0].style.color = this._color(gpu);
127
-            lgpu[0].innerText = String(gpu);
130
+        const lgpu = this._label('_ar_gpu');
131
+        if(lgpu !== null) {
132
+            lgpu.style.color = this._color(gpu);
133
+            lgpu.innerText = String(gpu);
128
         }
134
         }
129
 
135
 
130
-        const lpower = get('_ar_power');
131
-        if(lpower.length > 0)
132
-            lpower[0].innerHTML = POWER_ICON[Settings.powerPreference];
136
+        const lpower = this._label('_ar_power');
137
+        if(lpower !== null)
138
+            lpower.innerHTML = POWER_ICON[Settings.powerPreference];
133
 
139
 
134
-        const lin = get('_ar_in');
135
-        if(lin.length > 0) {
140
+        const lin = this._label('_ar_in');
141
+        if(lin !== null) {
136
             const sourceStats = sources.map(source => source._stats).join(', ');
142
             const sourceStats = sources.map(source => source._stats).join(', ');
137
-            lin[0].innerText = sourceStats;
143
+            lin.innerText = sourceStats;
138
         }
144
         }
139
 
145
 
140
-        const lout = get('_ar_out');
141
-        if(lout.length > 0) {
146
+        const lout = this._label('_ar_out');
147
+        if(lout !== null) {
142
             const trackerStats = trackers.map(tracker => tracker._stats).join(', ');
148
             const trackerStats = trackers.map(tracker => tracker._stats).join(', ');
143
-            lout[0].innerText = trackerStats;
149
+            lout.innerText = trackerStats;
144
         }
150
         }
145
     }
151
     }
146
 
152
 
147
     /**
153
     /**
154
+     * Get a label of the panel
155
+     * @param className
156
+     * @returns the HTML element, or null if it doesn't exist
157
+     */
158
+    private _label(className: string): Nullable<HTMLElement>
159
+    {
160
+        return this._container.getElementsByClassName(className).item(0) as Nullable<HTMLElement>;
161
+    }
162
+
163
+    /**
148
      * Associate a color to a frequency number
164
      * Associate a color to a frequency number
149
      * @param f frequency given in cycles per second
165
      * @param f frequency given in cycles per second
150
      * @returns colorized number (HTML)
166
      * @returns colorized number (HTML)
161
 
177
 
162
     /**
178
     /**
163
      * Create the container for the panel
179
      * Create the container for the panel
164
-     * @param parent parent element
165
      * @returns a container
180
      * @returns a container
166
      */
181
      */
167
-    private _createContainer(parent: HTMLElement): HTMLDivElement
182
+    private _createContainer(): HTMLDivElement
168
     {
183
     {
169
         const container = document.createElement('div') as HTMLDivElement;
184
         const container = document.createElement('div') as HTMLDivElement;
170
         const print = (html: string) => container.insertAdjacentHTML('beforeend', html);
185
         const print = (html: string) => container.insertAdjacentHTML('beforeend', html);
175
         container.style.padding = '4px';
190
         container.style.padding = '4px';
176
         container.style.whiteSpace = 'pre-line';
191
         container.style.whiteSpace = 'pre-line';
177
         container.style.backgroundColor = 'rgba(0,0,0,0.5)';
192
         container.style.backgroundColor = 'rgba(0,0,0,0.5)';
178
-        container.style.color = '#fff';
193
+        container.style.color = 'white';
179
         container.style.fontFamily = 'monospace';
194
         container.style.fontFamily = 'monospace';
180
         container.style.fontSize = '14px';
195
         container.style.fontSize = '14px';
181
 
196
 
193
         print('<br>');
208
         print('<br>');
194
         print('OUT: <span class="_ar_out"></span>');
209
         print('OUT: <span class="_ar_out"></span>');
195
 
210
 
196
-        parent.appendChild(container);
211
+        if(this._viewport.isFullscreenAvailable()) {
212
+            print('<br>');
213
+            container.appendChild(this._createFullscreenToggle());
214
+        }
215
+
197
         return container;
216
         return container;
198
     }
217
     }
218
+
219
+    /**
220
+     * Create a fullscreen toggle
221
+     * @returns a fullscreen toggle
222
+     */
223
+    private _createFullscreenToggle(): HTMLElement
224
+    {
225
+        const toggle = document.createElement('a') as HTMLAnchorElement;
226
+
227
+        Utils.assert(this._viewport != null);
228
+
229
+        toggle.href = 'javascript:void(0)';
230
+        toggle.innerText = 'Toggle fullscreen';
231
+        toggle.style.color = 'white';
232
+        toggle.setAttribute('role', 'button');
233
+        toggle.addEventListener('click', () => {
234
+            if(!this._viewport.fullscreen) {
235
+                this._viewport.requestFullscreen().catch(err => {
236
+                    alert(`Can't enable fullscreen mode. ` + err.toString());
237
+                });
238
+            }
239
+            else {
240
+                this._viewport.exitFullscreen();
241
+            }
242
+        });
243
+
244
+        return toggle;
245
+    }
199
 }
246
 }

正在加载...
取消
保存