-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweak-ui.html
255 lines (221 loc) · 5.82 KB
/
tweak-ui.html
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
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin:0;
padding:0;
display: flex;
flex-flow:column;
justify-content:start;
align-items:center;
background:#222;
}
#controls {
display: flex;
flex-flow:column;
margin: 10px;
background:#444;
box-shadow: 0px 2px 3px #000;
}
#controls > div {
display: flex;
flex-flow: row;
color:#fff;
background:#444;
border-bottom: solid 1px #dd0;
padding:5px;
}
#controls > div:hover {
background:#555;
}
#controls > div > .label {
width:20vw;
}
#controls > .type-float > .editor {
display:flex;
flex-flow:column;
}
</style>
</head>
<body>
<div id="controls">
</div>
</body>
<script>
function makeTweakControl(name, hint, value) {
let control = {
name:name,
hint:hint,
div:document.createElement('DIV'),
//these functions will be replaced per-control below:
getEditorValue:function(){ return this.value; }, //get user-edited value
showValue:function(value){ } //update UI with new value
};
let div = control.div;
let label = document.createElement('LABEL');
label.classList.add("label");
div.appendChild(label);
label.innerText = name;
//read first word in hint string as type:
let hints = hint.split(/\s+/);
let type = hints.shift();
div.classList.add("type-" + type);
let editor = document.createElement('DIV');
editor.classList.add("editor");
div.appendChild(editor);
if (type === "float" || type === "double") {
//parse remaining hits as range:
let min = NaN;
let max = NaN;
if (hints.length) min = parseFloat(hints.shift());
if (hints.length) max = parseFloat(hints.shift());
//float/double get a number input and a slider (if min/max is given):
let range = null;
let number = document.createElement('INPUT');
editor.appendChild(number);
number.type = "number";
number.value = value;
number.oninput = function(){
//clamp to range:
if (min === min) number.value = Math.max(min, number.value);
if (max === max) number.value = Math.min(max, number.value);
//copy to range:
if (range) range.value = number.value;
queuePushTweaks();
};
if (min === min && max === max) {
range = document.createElement('INPUT');
editor.appendChild(range);
range.type = "range";
range.min = min;
range.max = max;
range.step = "any";
range.value = value;
range.oninput = function() {
//copy to number:
number.value = range.value;
queuePushTweaks();
};
}
control.showValue = function(value){
//TODO
};
control.getEditorValue = function(){
return number.value;
};
} else {
let current = document.createElement('DIV');
editor.appendChild(current);
let input = document.createElement('INPUT');
editor.appendChild(input);
input.type = "text";
input.value = value;
input.oninput = function(){ queuePushTweaks(); };
control.showValue = function(value){
current.innerText = value;
};
control.getEditorValue = function(){ return input.value; };
}
control.showValue(value);
return control;
}
var lastSerial = 0;
var lastState = {};
var controls = {};
var controlsDiv = document.getElementById("controls");
function syncState() {
for (var name in controls) {
if (!(name in lastState)) {
controls[name].elt.parentNode.removeChild(elt);
delete controls[name];
}
}
for (var name in lastState) {
if (!(name in controls)) {
var control = makeTweakControl(name, lastState[name].hint, lastState[name].value);
controls[name] = control;
controls[name].setValue = lastState[name].value;
controlsDiv.appendChild(controls[name].div);
} else if (controls[name].hint !== lastState[name].hint) {
var control = makeTweakControl(name, lastState[name].hint, lastState[name].value);
controlsDiv.replaceChild(controls[name].div, control.div);
controls[name] = control;
controls[name].setValue = lastState[name].value;
} else if (controls[name].value !== lastState[name].value) {
controls[name].showValue(lastState[name].value);
}
console.log(name);
}
}
function pollTweaks() {
if ('polling' in pollTweaks) {
console.log("Note: trying to pollTweaks when already polling.");
return;
}
pollTweaks.polling = true;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState !== XMLHttpRequest.DONE) return;
delete pollTweaks.polling;
try {
if (xhr.status !== 200) {
console.log("Request failed somehow.");
window.setTimeout(pollTweaks, 5000); //poll again in a longer bit
return;
}
console.log(xhr.responseText);
var response = JSON.parse(xhr.responseText);
lastSerial = response.serial;
lastState = response.state;
syncState();
} catch ( e ) {
console.log("Request raised an exception:",e);
}
window.setTimeout(pollTweaks, 100); //poll again in a little bit
};
xhr.open('GET', 'tweaks?' + lastSerial);
xhr.send();
}
pollTweaks();
function queuePushTweaks() {
//TODO: could use this function to rate-limit tweaks pushes
pushTweaks();
}
function pushTweaks() {
//wait for previous push to finish:
if (pushTweaks.pending) return;
//see if a new push is needed:
var message = { };
var haveNew = false;
for (var name in controls) {
let value = controls[name].getEditorValue();
if (value !== controls[name].sentValue) {
message[name] = value;
controls[name].sentValue = value;
haveNew = true;
}
}
//no need to push if nothing new:
if (!haveNew) return;
//otherwise, mark push as happening and send a request:
pushTweaks.pending = true;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState !== XMLHttpRequest.DONE) return;
try {
if (xhr.status !== 200) {
console.log("Push failed somehow.");
}
} catch ( e ) {
console.log("Request raised an exception:",e);
}
//clear the pending state and poll for any new tweaks:
delete pushTweaks.pending;
pushTweaks();
};
xhr.open('POST', 'tweaks');
xhr.send(JSON.stringify(message));
}
</script>
</html>