Explorar el Código

Update StatsPanel. Add fullscreen toggle

customisations
alemart hace 1 año
padre
commit
a195398ae3
Se han modificado 2 ficheros con 73 adiciones y 26 borrados
  1. 1
    1
      src/core/session.ts
  2. 72
    25
      src/core/stats-panel.ts

+ 1
- 1
src/core/session.ts Ver fichero

@@ -174,7 +174,7 @@ export class Session extends AREventTarget<SessionEventType>
174 174
         this._setupRenderLoop();
175 175
 
176 176
         // setup the stats panel
177
-        this._statsPanel = new StatsPanel(this._viewport.hud.container);
177
+        this._statsPanel = new StatsPanel(this._viewport);
178 178
         this._statsPanel.visible = stats;
179 179
 
180 180
         // done!

+ 72
- 25
src/core/stats-panel.ts Ver fichero

@@ -22,8 +22,10 @@
22 22
 
23 23
 import { SpeedySize } from 'speedy-vision/types/core/speedy-size';
24 24
 import { Settings, PowerPreference } from './settings';
25
+import { Viewport } from './viewport';
25 26
 import { Tracker } from '../trackers/tracker';
26 27
 import { Source } from '../sources/source';
28
+import { Utils, Nullable } from '../utils/utils';
27 29
 import Martins from '../main';
28 30
 
29 31
 
@@ -44,6 +46,9 @@ const POWER_ICON: { readonly [P in PowerPreference]: string } = Object.freeze({
44 46
  */
45 47
 export class StatsPanel
46 48
 {
49
+    /** The viewport associated to this panel */
50
+    private readonly _viewport: Viewport;
51
+
47 52
     /** A container for the panel */
48 53
     private readonly _container: HTMLDivElement;
49 54
 
@@ -57,10 +62,13 @@ export class StatsPanel
57 62
      * Constructor
58 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 68
         this._lastUpdate = 0;
69
+
70
+        this._container = this._createContainer();
71
+        viewport.hud.container.appendChild(this._container);
64 72
     }
65 73
 
66 74
     /**
@@ -112,39 +120,47 @@ export class StatsPanel
112 120
      */
113 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 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 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 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 164
      * Associate a color to a frequency number
149 165
      * @param f frequency given in cycles per second
150 166
      * @returns colorized number (HTML)
@@ -161,10 +177,9 @@ export class StatsPanel
161 177
 
162 178
     /**
163 179
      * Create the container for the panel
164
-     * @param parent parent element
165 180
      * @returns a container
166 181
      */
167
-    private _createContainer(parent: HTMLElement): HTMLDivElement
182
+    private _createContainer(): HTMLDivElement
168 183
     {
169 184
         const container = document.createElement('div') as HTMLDivElement;
170 185
         const print = (html: string) => container.insertAdjacentHTML('beforeend', html);
@@ -175,7 +190,7 @@ export class StatsPanel
175 190
         container.style.padding = '4px';
176 191
         container.style.whiteSpace = 'pre-line';
177 192
         container.style.backgroundColor = 'rgba(0,0,0,0.5)';
178
-        container.style.color = '#fff';
193
+        container.style.color = 'white';
179 194
         container.style.fontFamily = 'monospace';
180 195
         container.style.fontSize = '14px';
181 196
 
@@ -193,7 +208,39 @@ export class StatsPanel
193 208
         print('<br>');
194 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 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
 }

Loading…
Cancelar
Guardar