Kaynağa Gözat

Refactor StatsPanel

customisations
alemart 1 yıl önce
ebeveyn
işleme
35ffe6ed76
1 değiştirilmiş dosya ile 50 ekleme ve 22 silme
  1. 50
    22
      src/core/stats-panel.ts

+ 50
- 22
src/core/stats-panel.ts Dosyayı Görüntüle

@@ -33,8 +33,8 @@ const UPDATE_INTERVAL = 500;
33 33
 /** Icons for different power profiles */
34 34
 const POWER_ICON: { readonly [P in PowerPreference]: string } = Object.freeze({
35 35
     'default': '',
36
-    'low-power': '<span style="color:#0f0">&#x1F50B</span>',
37
-    'high-performance': '<span style="color:#ff0">&#x26A1</span>'
36
+    'low-power': '&#x1F50B',
37
+    'high-performance': '&#x26A1'
38 38
 });
39 39
 
40 40
 
@@ -112,38 +112,51 @@ export class StatsPanel
112 112
      */
113 113
     private _update(trackers: Tracker[], sources: Source[], fps: number, gpu: number): void
114 114
     {
115
-        const trackerStats = trackers.map(tracker => tracker._stats).join(', ');
116
-        const sourceStats = sources.map(source => source._stats).join(', ');
117
-        const param = { // sanitzed
118
-            fps: this._colorize(fps),
119
-            gpu: this._colorize(gpu),
120
-            powerIcon: POWER_ICON[Settings.powerPreference]
121
-        };
122
-
123
-        this._container.textContent = (
124
-            `MARTINS.js v${Martins.version}
125
-            FPS: [fps] | GPU: [gpu] [powerIcon]
126
-            IN : ${sourceStats}
127
-            OUT: ${trackerStats}`
128
-        );
129
-
130
-        const fn = (_: string, x: 'fps' | 'gpu' | 'powerIcon'): string => param[x];
131
-        this._container.innerHTML = this._container.innerHTML.replace(/\[(\w+)\]/g, fn);
115
+        const get = (className: string) => this._container.getElementsByClassName(className) as HTMLCollectionOf<HTMLElement>;
116
+
117
+        // 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);
122
+        }
123
+
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);
128
+        }
129
+
130
+        const lpower = get('_ar_power');
131
+        if(lpower.length > 0)
132
+            lpower[0].innerHTML = POWER_ICON[Settings.powerPreference];
133
+
134
+        const lin = get('_ar_in');
135
+        if(lin.length > 0) {
136
+            const sourceStats = sources.map(source => source._stats).join(', ');
137
+            lin[0].innerText = sourceStats;
138
+        }
139
+
140
+        const lout = get('_ar_out');
141
+        if(lout.length > 0) {
142
+            const trackerStats = trackers.map(tracker => tracker._stats).join(', ');
143
+            lout[0].innerText = trackerStats;
144
+        }
132 145
     }
133 146
 
134 147
     /**
135
-     * Colorize a frequency number
148
+     * Associate a color to a frequency number
136 149
      * @param f frequency given in cycles per second
137 150
      * @returns colorized number (HTML)
138 151
      */
139
-    private _colorize(f: number): string
152
+    private _color(f: number): string
140 153
     {
141 154
         const GREEN = '#0f0', YELLOW = '#ff0', RED = '#f33';
142 155
         const color3 = f >= 50 ? GREEN : (f >= 30 ? YELLOW : RED);
143 156
         const color2 = f >= 30 ? GREEN : RED;
144 157
         const color = Settings.powerPreference != 'low-power' ? color3 : color2;
145 158
 
146
-        return `<span style="color:${color}">${Number(f)}</span>`;
159
+        return color;
147 160
     }
148 161
 
149 162
     /**
@@ -154,6 +167,7 @@ export class StatsPanel
154 167
     private _createContainer(parent: HTMLElement): HTMLDivElement
155 168
     {
156 169
         const container = document.createElement('div') as HTMLDivElement;
170
+        const print = (html: string) => container.insertAdjacentHTML('beforeend', html);
157 171
 
158 172
         container.style.position = 'absolute';
159 173
         container.style.left = container.style.top = '0px';
@@ -165,6 +179,20 @@ export class StatsPanel
165 179
         container.style.fontFamily = 'monospace';
166 180
         container.style.fontSize = '14px';
167 181
 
182
+        // all sanitized
183
+        container.innerText = 'MARTINS.js ' + Martins.version;
184
+
185
+        print('<br>');
186
+        print('FPS: <span class="_ar_fps"></span> | ');
187
+        print('GPU: <span class="_ar_gpu"></span> ');
188
+        print('<span class="_ar_power"></span>');
189
+
190
+        print('<br>');
191
+        print('IN: <span class="_ar_in"></span>');
192
+
193
+        print('<br>');
194
+        print('OUT: <span class="_ar_out"></span>');
195
+
168 196
         parent.appendChild(container);
169 197
         return container;
170 198
     }

Loading…
İptal
Kaydet