-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUI.ts
192 lines (158 loc) · 6.2 KB
/
UI.ts
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
const panelWrapper = document.getElementById("panel-wrapper")!!;
const panel = document.getElementById("main-panel")!!;
const showPanel = document.getElementById("show-panel")!!;
const paramsContainer = document.getElementById("params")!!;
const renderSelect = document.getElementById("render-select")!! as HTMLSelectElement;
const colorInputA = document.getElementById("color-a")!! as HTMLInputElement;
const colorInputB = document.getElementById("color-b")!! as HTMLInputElement;
const colorInputC = document.getElementById("color-c")!! as HTMLInputElement;
const colorThreshold = document.getElementById("color-treshold")!! as HTMLInputElement;
const exportSizeTypeSelect = document.getElementById("export-size-type") as HTMLSelectElement;
const exportSizeXInput = document.getElementById("export-size-x") as HTMLInputElement;
const exportSizeYInput = document.getElementById("export-size-y") as HTMLInputElement;
const fps = document.getElementById("fps") as HTMLInputElement;
const slowMessage = document.getElementById("slow-message") as HTMLElement;
let selectedRenderer = 0;
let currentRenderer: Renderer;
let currentRendererID: number;
const addBtnSpeed = 0.5; // each second
const addBtnShiftSpeed = 2; // each second
const addBtnCtrlSpeed = 0.05; // each second
let shift: boolean;
let control: boolean;
const innerPanels : { [id: string] : HTMLElement; } = {};
let currentSubPanel : HTMLElement | null = null;
let showFPS = false;
let isShowingSlowMessage = false;
let discardedSlowMessage = false;
enum ExportSizeType {
screenSize,
windowSize,
customSize
}
function initUI() {
for (let i = 0; i < renderers.length; i++) {
let option = document.createElement("option");
option.setAttribute("value", i.toString());
option.innerHTML = renderers[i].name;
renderSelect.appendChild(option);
}
renderSelect.value = currentRendererID.toString();
document.addEventListener("keyup", UpdateKeys);
document.addEventListener("keydown", UpdateKeys);
for (let child of panel.children) {
innerPanels[child.id] = child as HTMLElement;
innerPanels[child.id].style.opacity = "0";
innerPanels[child.id].style.left = "100%";
SetFocus(innerPanels[child.id], false);
}
OpenSubPanel("props-panel");
HideExportCustomSize();
}
function UpdateKeys(ev: KeyboardEvent) {
shift = ev.shiftKey;
control = ev.ctrlKey;
}
function togglePanel() {
panelWrapper.classList.toggle("hidden")
showPanel.classList.toggle("hidden")
}
function changeRendererEvent(ev: Event) {
changeRenderer(+(ev!!.target!! as HTMLSelectElement).value);
propsChangedSinceLastFrame = true;
}
function changeRenderer(id: number) {
currentRenderer = renderers[id];
currentRendererID = id;
paramsContainer.innerHTML = "";
renderers[id].CreateUI();
}
function UpdateUI() {
if (shaderLoaded("main")){
let shader = getShader("main");
currentRenderer.SetUniforms(shader);
GL.uniform3fv(getShaderUniform(shader, "colorA"), hexToRgb(colorInputA.value));
GL.uniform3fv(getShaderUniform(shader, "colorB"), hexToRgb(colorInputB.value));
GL.uniform3fv(getShaderUniform(shader, "colorC"), hexToRgb(colorInputC.value));
GL.uniform1f(getShaderUniform(shader, "treshold"), +colorThreshold.value);
document.body.style.setProperty('--prim', colorInputB.value);
}
panel.style.setProperty("height", currentSubPanel?.clientHeight + "px");
}
function AddButton(ev: Event, direction: number, int: boolean) {
let func = setInterval(() => {
let parent = (ev.target as HTMLElement).parentElement;
let input = parent?.querySelector("input")!!;
let value = parseFloat(input.value);
if (int) {
value = Math.round(value);
value += direction;
}
else if (shift) {
value += addBtnShiftSpeed * deltaTime * direction;
value = Math.round(value * 1000) / 1000;
}
else if (control) {
value += addBtnCtrlSpeed * deltaTime * direction;
value = Math.round(value * 1000) / 1000;
}
else{
value += addBtnSpeed * deltaTime * direction;
value = Math.round(value * 1000) / 1000;
}
propsChangedSinceLastFrame = true;
input.value = value.toString();
input.dispatchEvent(new Event('change'));
}
, 0);
(ev.target as HTMLElement).onmouseup = () => clearInterval(func);
(ev.target as HTMLElement).onmouseleave = () => clearInterval(func);
(ev.target as HTMLElement).ontouchend = () => clearInterval(func);
}
function OpenSubPanel(id : string) {
if (currentSubPanel != null) {
currentSubPanel.style.opacity = "0";
if (currentSubPanel.id === "props-panel") {
currentSubPanel.style.left = "-100%";
currentSubPanel.style.right = "100%";
}
else {
currentSubPanel.style.left = "100%";
currentSubPanel.style.right = "-100%";
}
SetFocus(currentSubPanel, false);
}
innerPanels[id].style.opacity = "1";
innerPanels[id].style.left = "0";
innerPanels[id].style.right = "0";
SetFocus(innerPanels[id], true);
currentSubPanel = innerPanels[id];
}
function HideExportCustomSize() {
const exportSizeType = +exportSizeTypeSelect.value;
if (exportSizeType === ExportSizeType.customSize) {
if (exportSizeXInput.parentElement)
exportSizeXInput.parentElement.style.display = "flex";
if (exportSizeYInput.parentElement)
exportSizeYInput.parentElement.style.display = "flex";
}
else {
if (exportSizeXInput.parentElement)
exportSizeXInput.parentElement.style.display = "none";
if (exportSizeYInput.parentElement)
exportSizeYInput.parentElement.style.display = "none";
}
}
function ToggleFPS() {
showFPS = !showFPS;
fps.classList.toggle("hidden");
}
function SetFocus(element: Element, focusEnabled: boolean) {
if (focusEnabled)
element.removeAttribute("tabindex");
else
element.setAttribute("tabindex", "-1");
for (let child of element.children) {
SetFocus(child, focusEnabled);
}
}