forked from KirkMcDonald/kirkmcdonald.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.js
505 lines (434 loc) · 13 KB
/
events.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*Copyright 2015-2019 Kirk McDonald
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.*/
"use strict"
// build target events
// The "+" button to add a new target.
function plusHandler() {
addTarget()
itemUpdate()
}
// Triggered when the item dropdown box opens.
function resetSearch(dropdown) {
dropdown.getElementsByClassName("search")[0].value = ""
// unhide all child nodes
let elems = dropdown.querySelectorAll("label, hr")
for (let elem of elems) {
elem.style.display = ""
}
}
// Triggered when user is searching target
function searchTargets() {
let ev = d3.event
let search = this
let search_text = search.value.toLowerCase().replace(/[^a-z0-9]+/g, "")
let dropdown = d3.select(search.parentNode)
if (!search_text) {
resetSearch(search.parentNode)
return
}
// handle enter key press (select target if only one is visible)
if (ev.keyCode === 13) {
let labels = dropdown.selectAll("label")
.filter(function() {
return this.style.display !== "none"
})
// don't do anything if more than one icon is visible
if (labels.size() === 1) {
let input = document.getElementById(labels.attr("for"))
input.checked = true
input.dispatchEvent(new Event("change"))
}
return
}
// hide non-matching labels & icons
let currentHrHasContent = false
let lastHrWithContent = null
dropdown.selectAll("hr, label").each(function(item) {
if (this.tagName === "HR") {
if (currentHrHasContent) {
this.style.display = ""
lastHrWithContent = this
} else {
this.style.display = "none"
}
currentHrHasContent = false
} else {
let title = item.name.replace(/-/g, "")
if (title.indexOf(search_text) === -1) {
this.style.display = "none"
} else {
this.style.display = ""
currentHrHasContent = true
}
}
})
if (!currentHrHasContent && lastHrWithContent !== null) {
lastHrWithContent.style.display = "none"
}
}
// Triggered when a build target's item is changed.
function ItemHandler(target) {
return function(item) {
target.itemName = item.name
target.recipeIndex = 0
target.displayRecipes()
itemUpdate()
}
}
// Triggered when a build target's recipe selector is changed.
function RecipeSelectorHandler(target, i) {
target.recipeIndex = i
itemUpdate()
}
// The "x" button to remove a target.
function RemoveHandler(target) {
this.handleEvent = function(event) {
build_targets.splice(target.index, 1)
for (var i=target.index; i < build_targets.length; i++) {
build_targets[i].index--
}
target.element.remove()
itemUpdate()
}
}
// Triggered when a "Factories:" text box is changed.
function FactoryHandler(target) {
this.handleEvent = function(event) {
target.factoriesChanged()
itemUpdate()
}
}
// Triggered when a "Rate:" text box is changed.
function RateHandler(target) {
this.handleEvent = function(event) {
target.rateChanged()
itemUpdate()
}
}
// settings events
// Obtains current data set from UI element, and resets the world with the new
// data.
function changeMod() {
var modName = currentMod()
reset()
loadData(modName)
}
function changeColor(event) {
setColorScheme(event.target.value)
display()
}
// Triggered when the display rate is changed.
function displayRateHandler(event) {
var value = event.target.value
displayRateFactor = displayRates[value]
rateName = value
display()
}
function changeRPrec(event) {
ratePrecision = Number(event.target.value)
display()
}
function changeFPrec(event) {
countPrecision = Number(event.target.value)
display()
}
// Triggered when the "minimum assembling machine" setting is changed.
function changeMin(min) {
setMinimumAssembler(min)
itemUpdate()
}
// Triggered when the furnace is changed.
function changeFurnace(furnace) {
spec.setFurnace(furnace.name)
solver.findSubgraphs(spec)
itemUpdate()
}
// Triggered when the preferred fuel is changed.
function changeFuel(fuel) {
setPreferredFuel(fuel.name)
solver.findSubgraphs(spec)
itemUpdate()
}
// Triggered when the preferred oil recipe is changed.
function changeOil(oil) {
setOilRecipe(oil.priority)
itemUpdate()
}
// Triggered when the Kovarex checkbox is toggled.
function changeKovarex(event) {
setKovarex(event.target.checked)
itemUpdate()
}
// Triggered when the preferred belt is changed.
function changeBelt(belt) {
setPreferredBelt(belt.name)
display()
}
// Triggered when the minimum pipe length is changed.
function changePipeLength(event) {
setMinPipe(event.target.value)
display()
}
// Triggered when the mining productivity bonus is changed.
function changeMprod() {
spec.miningProd = getMprod()
itemUpdate()
}
// Triggered when the default module is changed.
function changeDefaultModule(module) {
spec.setDefaultModule(module)
recipeTable.updateDisplayedModules()
itemUpdate()
}
// Triggered when the default beacon module is changed.
function changeDefaultBeacon(module) {
spec.setDefaultBeacon(module, spec.defaultBeaconCount)
recipeTable.updateDisplayedModules()
itemUpdate()
}
// Triggered when the default beacon count is changed.
function changeDefaultBeaconCount(event) {
var count = RationalFromString(event.target.value)
spec.setDefaultBeacon(spec.defaultBeacon, count)
recipeTable.updateDisplayedModules()
itemUpdate()
}
// Triggered when the visualizer setting box is toggled.
function toggleVisualizerSettings() {
let classes = document.getElementById("graph-wrapper").classList
if (classes.contains("open")) {
classes.remove("open")
} else {
classes.add("open")
}
}
// Triggered when the visualizer type is changed.
function changeVisualizerType(event) {
visualizer = event.target.value
display()
}
// Triggered when the visualizer direction is changed.
function changeVisualizerDirection(event) {
visDirection = event.target.value
display()
}
// Triggered when the max node breadth is changed.
function changeNodeBreadth(event) {
maxNodeHeight = Number(event.target.value)
display()
}
// Triggered when the link length is changed.
function changeLinkLength(event) {
linkLength = Number(event.target.value)
display()
}
// Triggered when the recipe sort order is changed.
function changeSortOrder(event) {
sortOrder = event.target.value
display()
}
// Triggered when the value format (decimal vs. rational) is changed.
function changeFormat(event) {
displayFormat = event.target.value
display()
}
// Triggered when fancy tooltip box is toggled.
function changeTooltip(event) {
tooltipsEnabled = event.target.checked
display()
}
// recipe row events
function IgnoreHandler(row) {
this.handleEvent = function(event) {
if (spec.ignore[row.name]) {
delete spec.ignore[row.name]
row.setIgnore(false)
} else {
spec.ignore[row.name] = true
row.setIgnore(true)
}
itemUpdate()
}
}
// Triggered when a factory module is changed.
function ModuleHandler(row, index) {
return function(module) {
if (spec.setModule(row.recipe, index, module) || isFactoryTarget(row.recipe.name)) {
itemUpdate()
} else {
display()
}
}
}
// Triggered when the right-arrow "copy module" button is pressed.
function ModuleCopyHandler(row) {
this.handleEvent = function(event) {
var moduleCount = spec.moduleCount(row.recipe)
var module = spec.getModule(row.recipe, 0)
var needRecalc = false
for (var i = 0; i < moduleCount; i++) {
needRecalc = spec.setModule(row.recipe, i, module) || needRecalc
row.setDisplayedModule(i, module)
}
if (needRecalc || isFactoryTarget(row.recipe.name)) {
itemUpdate()
} else {
display()
}
}
}
// Gets Factory object for a corresponding recipe name.
function getFactory(recipeName) {
var recipe = solver.recipes[recipeName]
return spec.getFactory(recipe)
}
// Triggered when a beacon module is changed.
function BeaconHandler(recipeName) {
return function(module) {
var factory = getFactory(recipeName)
factory.beaconModule = module
if (isFactoryTarget(recipeName) && !factory.beaconCount.isZero()) {
itemUpdate()
} else {
display()
}
}
}
// Triggered when a beacon module count is changed.
function BeaconCountHandler(recipeName) {
this.handleEvent = function(event) {
var moduleCount = RationalFromString(event.target.value)
var factory = getFactory(recipeName)
factory.beaconCount = moduleCount
if (isFactoryTarget(recipeName) && factory.beaconModule) {
itemUpdate()
} else {
display()
}
}
}
// Triggered when the up/down arrow "copy to all recipes" button is pressed.
function CopyAllHandler(name) {
this.handleEvent = function(event) {
var factory = spec.spec[name]
var needRecalc = false
for (var recipeName in spec.spec) {
if (recipeName == name) {
continue
}
var f = spec.spec[recipeName]
if (!f) {
continue
}
var recipe = solver.recipes[recipeName]
needRecalc = factory.copyModules(f, recipe) || needRecalc || isFactoryTarget(recipeName)
}
recipeTable.updateDisplayedModules()
if (needRecalc) {
itemUpdate()
} else {
display()
}
}
}
// breakdown events
function ToggleBreakdownHandler(itemRow, breakdown) {
this.handleEvent = function(event) {
if (itemRow.arrowCell.classList.contains("breakdown-open")) {
itemRow.arrowCell.classList.remove("breakdown-open")
breakdown.row.classList.remove("breakdown-open")
} else {
itemRow.arrowCell.classList.add("breakdown-open")
breakdown.row.classList.add("breakdown-open")
}
}
}
// items tab events
function PipeCountHandler(config) {
this.handleEvent = function(event) {
config.setPipes(event.target.value)
}
}
function PipeLengthHandler(config) {
this.handleEvent = function(event) {
config.setLength(event.target.value)
}
}
// graph hover events
function GraphMouseOverHandler(node) {
node.highlight()
}
function GraphMouseLeaveHandler(node) {
if (node !== clickedNode) {
node.unhighlight()
}
}
var clickedNode = null
function GraphClickHandler(node) {
if (node === clickedNode) {
node.unhighlight()
clickedNode = null
} else if (clickedNode) {
clickedNode.unhighlight()
clickedNode = node
} else {
clickedNode = node
}
}
// tab events
var DEFAULT_TAB = "totals_tab"
var currentTab = DEFAULT_TAB
var tabMap = {
"totals_tab": "totals_button",
"steps_tab": "steps_button",
"graph_tab": "graph_button",
"settings_tab": "settings_button",
"about_tab": "about_button",
"faq_tab": "faq_button",
"debug_tab": "debug_button",
}
// Triggered when a tab is clicked on.
function clickTab(tabName) {
currentTab = tabName
var tabs = document.getElementsByClassName("tab")
for (var i=0; i < tabs.length; i++) {
tabs[i].style.display = "none"
}
var buttons = document.getElementsByClassName("tab_button")
for (var i=0; i < buttons.length; i++) {
buttons[i].classList.remove("active")
}
document.getElementById(tabName).style.display = "block"
var button = document.getElementById(tabMap[tabName])
button.classList.add("active")
if (initDone) {
window.location.hash = "#" + formatSettings()
}
}
// Triggered when the "Visualize" tab is clicked on.
function clickVisualize(event, tabName) {
clickTab(event, tabName)
renderGraph(globalTotals, spec.ignore)
}
// debug event
function toggleDebug(event) {
showDebug = event.target.checked
display()
}
// utility events
function toggleVisible(targetID) {
var target = document.getElementById(targetID)
if (target.style.display == "none") {
target.style.display = "block"
} else {
target.style.display = "none"
}
}