-
Notifications
You must be signed in to change notification settings - Fork 11
/
ui.js
399 lines (334 loc) · 12.4 KB
/
ui.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
/*
Ethereal Farm
Copyright (C) 2020-2024 Lode Vandevenne
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// tab contents
var fieldFlex;
var upgradeFlex;
var fruitFlex;
var medalDiv;
var field2Flex;
var field3Flex;
var upgrade2Flex;
var automatonFlex;
var squirrelFlex;
var amberFlex;
var mainw;
var mainh;
// The begin and end variables may be used to update positions of some dynamically sized pieces of content, such as in updateTabButtons depending on amount of tab buttons visible
var topFlex;
var infoFlex;
var tabFlexBegin;
var tabFlexEnd;
var tabFlex;
var shortcutFlexBegin;
var shortcutFlexEnd;
var shortcutFlexHeight;
var shortcutFlex;
var contentFlexBegin;
var contentFlexEnd;
var contentFlex;
var logFlexBegin;
var logFlex;
var goalFlex;
var medalFlex;
var mainFlex = null; // takes width of entire screen, not of the actual game
var gameFlex = null; // anything in this one becomes inactive during modal dialog
var topDialogFlex = null; // has exactly same size as gameFlex, but can contain elements even during modal dialogs (and the modal dialogs themselves)
var nonDialogFlex = null;
var rightFlex;
var topRightFlex;
var bottomRightFlex;
window.onresize = function() {
if(mainFlex) mainFlex.update();
};
function makeMainDivs() {
var showdebugborders = false;
var has_top_notice = !!document.getElementById('topnotice');
if(mainFlex) mainFlex.removeSelf(null);
mainFlex = new Flex(null, 0, 0, 1, 1);
nonDialogFlex = new Flex(mainFlex, 0, 0, 1, 1);
gameFlex = new Flex(nonDialogFlex, [0, 0, 0.01, 0.75], has_top_notice ? 0.03 : 0.01, [0, 0, 0.99, 0.75], 0.99);
if(showdebugborders) gameFlex.div.style.border = '2px solid green';
//gameFlex.div.style.border = '2px solid black';
//gameFlex.div.style.backgroundColor = '#0001';
topDialogFlex = new Flex(mainFlex, [0, 0, 0.01, 0.75], has_top_notice ? 0.03 : 0.01, [0, 0, 0.99, 0.75], 0.99);
topDialogFlex.div.style.visibility = 'hidden';
topFlex = new Flex(gameFlex, 0, 0, 1, 0.05);
if(showdebugborders) topFlex.div.style.border = '2px solid red';
infoFlex = new Flex(gameFlex, 0, 0.05, 1, 0.17);
if(showdebugborders) infoFlex.div.style.border = '2px solid blue';
tabFlexBegin = 0.17;
tabFlexEnd = 0.29;
tabFlex = new Flex(gameFlex, 0, 0.171, 1, tabFlexEnd);
if(showdebugborders) tabFlex.div.style.border = '2px solid green';
shortcutFlexBegin = tabFlexEnd + 0.002;
shortcutFlexEnd = 0.32;
shortcutFlexHeight = shortcutFlexEnd - shortcutFlexBegin;
shortcutFlex = new Flex(gameFlex, 0, shortcutFlexBegin, 1, shortcutFlexEnd);
if(showdebugborders) shortcutFlex.div.style.border = '2px solid gray';
contentFlexBegin = shortcutFlexEnd + 0.005;
contentFlexEnd = 0.825;
//contentDiv = makeDiv(0, 0, 0, 0, document.body);
contentFlex = new Flex(gameFlex, 0, contentFlexBegin, 1, 0.825);
if(showdebugborders) contentFlex.div.style.border = '2px solid orange';
//contentFlex.div.style.backgroundColor = '#0004';
logFlexBegin = contentFlexEnd + 0.005;
logFlex = new Flex(gameFlex, 0, logFlexBegin, 1, 1);
if(showdebugborders) logFlex.div.style.border = '2px solid gray';
goalFlex = new Flex(gameFlex, 0, logFlexBegin, 1, logFlexBegin + 0.05);
if(showdebugborders) goalFlex.div.style.border = '2px solid yellow';
goalFlex.div.style.visibility = 'hidden';
goalFlex.div.className = 'efGoal';
// have right pane follow same vertical size rules as the left part for good alignment
rightFlex = new Flex(nonDialogFlex, [0, 0, 0.99, 0.75], has_top_notice ? 0.03 : 0.01, [0, 0, 0.99, 1.1], 0.99);
rightFlex.div.style.visibility = 'hidden';
rightFlex.div.style.overflow = 'hidden'; // avoid creating unwanted global scrollbars
//rightFlex.div.style.border = '4px solid red';
topRightFlex = new Flex(rightFlex, 0.02, 0.05, 0.98, 0.25, FONT_SMALL);
bottomRightFlex = new Flex(rightFlex, 0.02, 0.25, 0.98, 1);
topRightFlex.div.className = 'efBordered';
topRightFlex.div.style.padding = '1%';
bottomRightFlex.div.className = 'efBordered';
topRightFlex.div.style.borderBottom = 'none';
bottomRightFlex.div.style.borderTop = 'none';
mainFlex.attachTo(document.body);
mainFlex.update();
}
// UI that persists even through loading of savegame, hard resets, etc...
function initUIGlobal() {
makeMainDivs();
initMessageUI();
initSettingsUI();
}
var tabindex_field;
var tabindex_upgrades;
var tabindex_fruit;
var tabindex_field2;
var tabindex_upgrades2;
var tabindex_field3;
var tabindex_automaton;
var tabindex_squirrel;
var tabindex_amber;
var tabindex_medals;
// init the UI after a reset, save load, .... Keeps log messages
// assume state is already correctly initialized
// NOTE: nothing in here may depend on prefield
function initUI() {
//topDiv.innerHTML = '';
//tabDiv.innerHTML = '';
//contentDiv.innerHTML = '';
//logDiv.innerHTML = '';
removeAllArrows();
setStyle();
document.body.className = 'efBackground';
contentFlex.clear();
pausedflex = undefined;
pausedbuttoncanvasstate = -1;
pausedFlexTextGlobal = undefined;
tabFlex.clear();
//setMainDivSizes();
tabbuttons = [];
tabs = [];
// this determines the unique id of each tab (used in savegame) but not the display order. The display order is determined in ui_tabs.js in updateTabButtons.
var tabnum = 0;
tabindex_field = tabnum++;
tabindex_upgrades = tabnum++;
tabindex_fruit = tabnum++;
tabindex_field2 = tabnum++;
tabindex_upgrades2 = tabnum++;
tabindex_field3 = tabnum++;
tabindex_medals = tabnum++;
tabindex_automaton = tabnum++;
tabindex_squirrel = tabnum++;
tabindex_amber = tabnum++;
for(var i = 0; i < tabnum; i++) tabs[i] = new Flex(contentFlex, 0, 0, 1, 1);
fieldFlex = tabs[tabindex_field];
fieldFlex.div.style.userSelect = 'none'; // prevent unwanted selections when double clicking things
upgradeFlex = tabs[tabindex_upgrades];
fruitFlex = tabs[tabindex_fruit];
medalFlex = tabs[tabindex_medals];
field2Flex = tabs[tabindex_field2];
field2Flex.div.style.userSelect = 'none'; // prevent unwanted selections when double clicking things
upgrade2Flex = tabs[tabindex_upgrades2];
field3Flex = tabs[tabindex_field3];
field3Flex.div.style.userSelect = 'none'; // prevent unwanted selections when double clicking things
automatonFlex = tabs[tabindex_automaton];
squirrelFlex = tabs[tabindex_squirrel];
amberFlex = tabs[tabindex_amber];
updateTabButtons();
initFieldUI();
initInfoUI();
initField2UI();
initField3UI();
updatePausedUI();
update_prev_state_ctor_count = -1;
if(state) setTab(state.currentTab);
else setTab(0, true);
prevGoal = -1; // causes redraw of goal chips, arrows, ... if necessary
}
function updateMainFlexPositions() {
// This does:
// -Make the field use up the entire tab space if no tab buttons visible
// -Show the shortcuts bar, or not, and if not also make the content take up that space
if(shouldRenderAutomatonShortcuts()) {
if(numtabs <= 1) {
if(contentFlex.y0 != tabFlexBegin + shortcutFlexHeight) {
contentFlex.y0 = tabFlexBegin + shortcutFlexHeight;
contentFlex.update(gameFlex.div);
}
if(shortcutFlex.y0 != tabFlexBegin) {
shortcutFlex.y0 = tabFlexBegin;
shortcutFlex.update(gameFlex.div);
}
} else {
if(contentFlex.y0 != contentFlexBegin) {
contentFlex.y0 = contentFlexBegin;
contentFlex.update(gameFlex.div);
}
if(shortcutFlex.y0 != shortcutFlexBegin) {
shortcutFlex.y0 = shortcutFlexBegin;
shortcutFlex.update(gameFlex.div);
}
}
} else {
if(numtabs <= 1) {
if(contentFlex.y0 != tabFlexBegin) {
contentFlex.y0 = tabFlexBegin;
contentFlex.update(gameFlex.div);
}
} else {
if(contentFlex.y0 != shortcutFlexBegin) {
contentFlex.y0 = shortcutFlexBegin;
contentFlex.update(gameFlex.div);
}
}
}
}
var pausedflex = undefined;
var pausedbuttoncanvasstate = -1;
// Globally remembered text because reading .innerText is slow
// This is used also for the 'Computing' text that can occur
var pausedFlexTextGlobal = undefined;
function updatePausedUI() {
var needflex = state.paused || heavy_computing;
if(needflex && !pausedflex) {
pausedflex = new Flex(contentFlex, 0, 0, 1, 1, FONT_FULL);
centerText2(pausedflex.div);
pausedflex.div.style.pointerEvents = 'none';
pausedflex.div.style.color = '#f008';
} else if(!needflex && pausedflex) {
pausedflex.removeSelf(contentFlex);
pausedflex = undefined;
pausedFlexTextGlobal = undefined;
}
var pausedFlexText = undefined;
if(state.paused) {
if(heavy_computing) {
pausedFlexText = 'Computing<br>Paused';
} else {
pausedFlexText = 'Paused';
}
} else if(heavy_computing) {
var hours = Math.floor((util.getTime() - state.time) / 3600);
if(hours > 1) {
pausedFlexText = 'Computing ' + '<br>' + hours + 'h';
} else {
pausedFlexText = 'Computing';
}
}
if(pausedFlexText != undefined && pausedFlexText != pausedFlexTextGlobal) {
pausedFlexTextGlobal = pausedFlexText;
pausedflex.div.textEl.innerHTML = pausedFlexText;
}
if(state.paused && pausedbuttoncanvasstate != 1) {
pausedbuttoncanvasstate = 1;
renderImage(image_paused, pauseButtonCanvas);
} else if(!state.paused && pausedbuttoncanvasstate != 0) {
pausedbuttoncanvasstate = 0;
renderImage(image_pause, pauseButtonCanvas);
}
}
var holiday_hats_active = 0; // used here but also for ui_field2 caching
// some parts of the UI are updated more often than just in initUI, their functions, even for initial creation, are called 'update' instead of 'init'
// this one is not called per frame, only for some more rare actions that have high likelyhood of invalidating the UI (e.g. loading a save, or undo)
function updateUI() {
updateUpgradeUI();
//updateMedalUI();
//updateResourceUI();
updateTabButtons();
updateUpgrade2UI();
updateAutomatonUI();
updateAmberUI();
updateSquirrelUI();
updatePausedUI();
updateMainFlexPositions();
}
// the one for during a game update
function updateUI2() {
renderField();
renderField2();
renderField3();
renderPond();
updateResourceUI();
updateUpgradeUIIfNeeded();
updateUpgrade2UIIfNeeded();
updateTabButtons();
updateAbilitiesUI();
updateRightPane();
updatePausedUI();
if(squirrelUINeedsFastUpdate()) updateSquirrelUI(true);
if(updatetooltipfun) {
updatetooltipfun();
}
if(globalupdatedialogfun) {
globalupdatedialogfun();
if(dialog_level == 0) globalupdatedialogfun = undefined;
}
updateSettingsAboutIcon();
if(state.holiday != holiday_hats_active) {
holiday_hats_active = state.holiday;
state.initEvolutionAndHatImages();
updateUI();
}
updateMainFlexPositions();
}
//document.body.style.fontFamily = 'Verdana, Arial, Helvetica, sans-serif';
var oldfont = false;
if(oldfont) {
document.body.style.fontFamily = 'Verdana, sans-serif';
//document.body.style.fontSize = '0.9em';
} else {
document.body.style.fontFamily = 'Arial, sans-serif';
//document.body.style.fontSize = '1em';
}
////////////////////////////////////////////////////////////////////////////////
function setCSSFile(file) {
var link = document.getElementById('csslink');
link.setAttribute("href", file);
}
var prevstyle = undefined;
function setStyle() {
if(prevstyle == state.uistyle) return; // don't load the style if the same one is already loaded: otherwise pressing undo causes it to reload the stylesheet for no reason, which can break the display when offline
prevstyle = state.uistyle;
// add version code, otherwise the CSS files get cached for a long time and if new styles are added they won't be picked up when loading a next game version
// caching within a game version is desired though
var version_code = '?v=' + formatVersion().replace(/\./g, '_');
if(state.uistyle == 2) {
setCSSFile('style_dark.css' + version_code);
} else if(state.uistyle == 3) {
setCSSFile('style_dark2.css' + version_code);
} else {
setCSSFile('style_light.css' + version_code);
}
}