-
Notifications
You must be signed in to change notification settings - Fork 17
/
controls.js
255 lines (244 loc) · 7.43 KB
/
controls.js
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
/**
* Copyright 2014 Josua Krause, Christopher Collins
* Copyright 2024 Josua Krause
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// @ts-check
import { COLORS, PRESETS } from './presets.js';
import { attr } from './util.js';
/** @typedef {import('./presets').Preset} Preset */
/**
* @typedef {{
* initPreset: number,
* initIx: number,
* initPad: number,
* minPad: number,
* maxPad: number,
* stepPad: number,
* onPresetUpdate: (preset: Preset) => void,
* onColorUpdate: (ix: number) => void,
* onPadUpdate: (newPad: number) => void,
* onDebugUpdate: (isDebug: boolean) => void,
* onRemoveUpdate: (isRemove: boolean) => void,
* onClear: () => void,
* }} Controls
*/
function addDivider(/** @type {HTMLElement} */ main) {
const div = document.createElement('div');
div.classList.add('divider');
main.appendChild(div);
}
function addLabel(
/** @type {HTMLElement} */ main,
/** @type {HTMLElement} */ target,
/** @type {string} */ name,
/** @type {string} */ desc,
) {
const label = document.createElement('label');
label.setAttribute('for', name);
label.textContent = desc;
main.appendChild(label);
attr(target, { id: name, name });
}
function addPresetSelect(
/** @type {HTMLElement} */ main,
/** @type {Controls} */ controls,
) {
const div = document.createElement('div');
const selectPreset = document.createElement('select');
addLabel(div, selectPreset, 'select-preset', 'Preset:');
PRESETS.forEach((preset, ix) => {
const option = document.createElement('option');
option.value = `${ix}`;
option.innerText = preset.name;
selectPreset.appendChild(option);
});
selectPreset.addEventListener('change', () => {
const newIx = +selectPreset.value;
controls.onPresetUpdate(PRESETS[newIx]);
});
selectPreset.value = `${controls.initPreset}`;
div.appendChild(selectPreset);
main.appendChild(div);
return {
updatePreset: (/** @type {number} */ ix) => {
if (selectPreset.value !== `${ix}`) {
selectPreset.value = `${ix}`;
}
},
};
}
function addColorSelect(
/** @type {HTMLElement} */ main,
/** @type {Controls} */ controls,
) {
const div = document.createElement('div');
const selectColor = document.createElement('select');
addLabel(div, selectColor, 'select-color', 'Color:');
COLORS.forEach((_, ix) => {
const option = document.createElement('option');
option.value = `${ix}`;
option.innerText = `Group ${ix + 1}`;
selectColor.appendChild(option);
});
selectColor.addEventListener('change', () => {
const newIx = +selectColor.value;
selectColor.style.backgroundColor = COLORS[newIx];
controls.onColorUpdate(newIx);
});
selectColor.value = `${controls.initIx}`;
selectColor.style.backgroundColor = COLORS[controls.initIx];
div.appendChild(selectColor);
main.appendChild(div);
return {
updateColor: (/** @type {number} */ ix) => {
if (selectColor.value !== `${ix}`) {
selectColor.value = `${ix}`;
selectColor.style.backgroundColor = COLORS[ix];
}
},
};
}
function addRemoveToggle(
/** @type {HTMLElement} */ main,
/** @type {Controls} */ controls,
) {
const div = document.createElement('div');
const toggle = document.createElement('input');
addLabel(div, toggle, 'remove', 'Remove:');
toggle.setAttribute('type', 'checkbox');
toggle.checked = false;
toggle.addEventListener('change', () => {
controls.onRemoveUpdate(toggle.checked);
});
div.appendChild(toggle);
main.appendChild(div);
return {
setRemoveMode: (/** @type {boolean} */ isRemove) => {
toggle.checked = isRemove;
},
};
}
function addPaddingSlider(
/** @type {HTMLElement} */ main,
/** @type {Controls} */ controls,
) {
const div = document.createElement('div');
const range = document.createElement('input');
addLabel(div, range, 'pad-range', 'Padding:');
const initValue = controls.initPad;
const maxValue = controls.maxPad;
const minValue = controls.minPad;
const step = controls.stepPad;
attr(range, {
type: 'range',
min: `${minValue}`,
max: `${maxValue}`,
step: `${step}`,
});
range.classList.add('range');
const edit = document.createElement('input');
attr(edit, {
id: `pad-range-edit`,
name: `pad-range-edit`,
type: 'text',
value: `${initValue}`,
});
edit.classList.add('rangeedit');
edit.value = `${initValue}`;
range.value = `${initValue}`;
edit.addEventListener('change', () => {
const evalue = +edit.value;
if (Number.isFinite(evalue)) {
controls.onPadUpdate(evalue);
range.value = `${evalue}`;
edit.classList.remove('invalid');
} else {
edit.classList.add('invalid');
}
});
range.addEventListener('input', () => {
const evalue = +edit.value;
const rvalue = +range.value;
if (evalue !== rvalue) {
controls.onPadUpdate(rvalue);
edit.value = `${rvalue}`;
edit.classList.remove('invalid');
}
});
div.appendChild(edit);
div.appendChild(range);
main.appendChild(div);
return {};
}
function addDebugToggle(
/** @type {HTMLElement} */ main,
/** @type {Controls} */ controls,
) {
const div = document.createElement('div');
const toggle = document.createElement('input');
addLabel(div, toggle, 'debug', 'Show Potential Field:');
toggle.setAttribute('type', 'checkbox');
toggle.checked = false;
toggle.addEventListener('change', () => {
controls.onDebugUpdate(toggle.checked);
});
div.appendChild(toggle);
main.appendChild(div);
return {};
}
function addRemoveAll(
/** @type {HTMLElement} */ main,
/** @type {Controls} */ controls,
) {
const div = document.createElement('div');
const removeAll = document.createElement('input');
removeAll.setAttribute('type', 'button');
removeAll.value = 'Clear';
removeAll.addEventListener('click', () => {
controls.onClear();
});
div.appendChild(removeAll);
main.appendChild(div);
return {};
}
export function addControls(/** @type {Controls} */ controls) {
const top = document.getElementById('topbar');
const presetControls = addPresetSelect(top, controls);
const colorControls = addColorSelect(top, controls);
const removeControls = addRemoveToggle(top, controls);
addDivider(top);
const paddingControls = addPaddingSlider(top, controls);
const debugControls = addDebugToggle(top, controls);
const clearControls = addRemoveAll(top, controls);
const footNormal = document.createElement('div');
footNormal.classList.add('normalonly');
footNormal.textContent =
'Add boxes by clicking and remove boxes via Shift+Click.';
const footMobile = document.createElement('div');
footMobile.classList.add('mobileonly');
footMobile.textContent =
'Add boxes by tapping. Select "Remove" to remove boxes instead.';
const footer = document.getElementById('footer');
footer.appendChild(footNormal);
footer.appendChild(footMobile);
return {
...presetControls,
...colorControls,
...removeControls,
...paddingControls,
...debugControls,
...clearControls,
};
}