forked from ynoproject/forest-orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.js
146 lines (126 loc) · 4.59 KB
/
loader.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
let playerLoaderSprite = gameDefaultSprite.sprite || gameDefaultSprite;
let playerLoaderSpriteIdx = gameDefaultSprite.idx || 0;
let loaderSpriteCache = {};
let activeLoaders = new WeakMap;
let loadingCounter = 0;
function addLoader(target, instant) {
if (activeLoaders.has(target))
return;
activeLoaders.set(target, true);
const getLoaderSprites = [
getLoaderSpriteImg(playerLoaderSprite, playerLoaderSpriteIdx, 0),
getLoaderSpriteImg(playerLoaderSprite, playerLoaderSpriteIdx, 1),
getLoaderSpriteImg(playerLoaderSprite, playerLoaderSpriteIdx, 2)
];
const frameIndexes = [ 1, 0, 1, 2 ];
Promise.allSettled(getLoaderSprites)
.then(() => {
if (!activeLoaders.has(target) || activeLoaders.get(target) !== true)
return;
const el = document.createElement('div');
el.classList.add('loader');
const targetPosition = getComputedStyle(target).position;
switch (targetPosition) {
case 'fixed':
case 'relative':
el.style.position = 'fixed';
break;
}
const updateLoaderFrame = () => {
getLoaderSpriteImg(playerLoaderSprite, playerLoaderSpriteIdx, frameIndexes[loader.frame])
.then(url => loader.element.children[0].src = url);
if (loader.frame < 3)
loader.frame++;
else
loader.frame = 0;
};
const loader = {
element: el,
timer: setInterval(updateLoaderFrame, 150),
frame: 0
};
activeLoaders.set(target, loader);
const img = document.createElement('img');
el.appendChild(img);
const isIframe = target.nodeName === 'IFRAME';
if (isIframe)
target.parentElement.appendChild(el);
else
target.appendChild(el);
if (instant)
el.classList.add('visible');
else
setTimeout(() => el.classList.add('visible'), 0);
// Adds instructions after loading for a while
if (loadingCounter == 0) {
loader.longTimer = setTimeout(() => {
const loader = activeLoaders.get(target);
if (loader?.element) {
let loadText = document.createElement('div');
loadText.style.cssText = "text-align: center; color: white; font-size: 1vw; padding: 1%;";
loadText.innerHTML = localizedMessages.loadingInstruct;
loader.element.appendChild(loadText);
}
}, 30000);
loadingCounter = 1;
}
updateLoader(target);
updateLoaderFrame();
});
}
function updateLoader(target) {
if (activeLoaders.has(target)) {
const el = activeLoaders.get(target).element;
el.style.top = `${target.offsetTop}px`;
el.style.left = `${target.offsetLeft}px`;
el.style.width = `${target.offsetWidth}px`;
el.style.height = `${target.offsetHeight}px`;
const scaleX = Math.max(Math.min(Math.floor(target.offsetWidth / 48), 10), 1);
const scaleY = Math.max(Math.min(Math.floor(target.offsetHeight / 64), 10), 1);
const scale = Math.min(scaleX, scaleY);
el.children[0].style.transform = `scale(${scale})`;
}
}
function removeLoader(target) {
if (activeLoaders.has(target) && activeLoaders.get(target) !== true) {
const { timer, longTimer, element: el } = activeLoaders.get(target);
el.classList.remove('visible');
setTimeout(() => el.remove(), 500);
clearInterval(timer);
clearTimeout(longTimer);
}
activeLoaders.delete(target);
if (typeof loadingMessageTimer !== 'undefined')
clearTimeout(loadingMessageTimer);
}
async function getLoaderSpriteImg(sprite, idx, frameIdx, dir) {
const isBrave = ((navigator.brave && await navigator.brave.isBrave()) || false);
return new Promise(resolve => {
const spriteData = loaderSpriteCache;
if (!spriteData[sprite])
spriteData[sprite] = {};
if (!spriteData[sprite][idx])
spriteData[sprite][idx] = [null, null, null];
const spriteUrl = spriteData[sprite][idx][frameIdx];
if (spriteUrl)
return resolve(spriteUrl);
const img = new Image();
img.onload = function () {
getSpriteImg(img, spriteData, sprite, idx, frameIdx, 24, 32, 0, false, isBrave)
.then(url => resolve(url));
};
if (!dir) {
dir = `../data/${ynoGameId}/CharSet/`;
img.onerror = () => getLoaderSpriteImg(sprite, idx, frameIdx, `images/charsets/${ynoGameId}/`).then(url => resolve(url));
} else {
img.onerror = () => {
console.error(`Charset '${sprite}' not found`);
resolve(null);
};
}
img.src = !sprite?.startsWith('#') ? `${dir}${sprite}.png` : '';
});
}
(function() {
addLoader(document.getElementById('loadingOverlay'), true);
})();