Skip to content

Commit 1826c74

Browse files
authored
Merge pull request rpgtkoolmv#134 from rpgtkoolmv/progress-bar
Progress bar
2 parents 5a3b825 + 04403e6 commit 1826c74

File tree

6 files changed

+121
-1
lines changed

6 files changed

+121
-1
lines changed

js/rpg_core/Graphics.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Graphics.initialize = function(width, height, type) {
6262
this._disableContextMenu();
6363
this._setupEventHandlers();
6464
this._setupCssFontLoading();
65+
this._setupProgress();
6566
};
6667

6768

@@ -247,6 +248,54 @@ Graphics.setLoadingImage = function(src) {
247248
*/
248249
Graphics.startLoading = function() {
249250
this._loadingCount = 0;
251+
252+
ProgressWatcher.truncateProgress();
253+
ProgressWatcher.setProgressListener(this._updateProgressCount.bind(this));
254+
Graphics._showProgress();
255+
};
256+
257+
Graphics._setupProgress = function(){
258+
this._progressElement = document.createElement('div');
259+
this._progressElement.id = 'loading-progress';
260+
this._progressElement.style.width = '200px';
261+
this._progressElement.style.height = '30px';
262+
this._progressElement.style.backgroundColor = 'gray';
263+
264+
this._barElement = document.createElement('div');
265+
this._barElement.id = 'loading-bar';
266+
this._barElement.style.width = '1%';
267+
this._barElement.style.height = '30px';
268+
this._barElement.style.backgroundColor = 'green';
269+
270+
this._progressElement.appendChild(this._barElement);
271+
272+
document.body.appendChild(this._progressElement);
273+
};
274+
275+
Graphics._showProgress = function(){
276+
this._progressElement.value = 0;
277+
this._progressElement.style.visibility = 'visible';
278+
};
279+
280+
Graphics._hideProgress = function(){
281+
this._progressElement.style.visibility = 'hidden';
282+
};
283+
284+
Graphics._updateProgressCount = function(countLoaded, countLoading){
285+
var progressValue;
286+
if(countLoading !== 0){
287+
progressValue = (countLoaded/countLoading) * 100;
288+
}else{
289+
progressValue = 100;
290+
}
291+
292+
this._barElement.style.width = progressValue + '%';
293+
};
294+
295+
Graphics._updateProgress = function(){
296+
this._progressElement.style.zIndex = 99;
297+
this._centerElement(this._progressElement);
298+
this._progressElement.style.marginTop = '400px';
250299
};
251300

252301
/**
@@ -259,6 +308,7 @@ Graphics.updateLoading = function() {
259308
this._loadingCount++;
260309
this._paintUpperCanvas();
261310
this._upperCanvas.style.opacity = 1;
311+
this._updateProgress();
262312
};
263313

264314
/**
@@ -270,6 +320,7 @@ Graphics.updateLoading = function() {
270320
Graphics.endLoading = function() {
271321
this._clearUpperCanvas();
272322
this._upperCanvas.style.opacity = 0;
323+
this._hideProgress();
273324
};
274325

275326
/**

js/rpg_core/ProgressWatcher.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
function ProgressWatcher(){
2+
throw new Error('This is a static class');
3+
}
4+
5+
ProgressWatcher.initialize = function(){
6+
this.clearProgress();
7+
ImageManager.setCreationHook(this._bitmapListener.bind(this));
8+
AudioManager.setCreationHook(this._audioListener.bind(this));
9+
};
10+
11+
ProgressWatcher._bitmapListener = function(bitmap){
12+
this._countLoading++;
13+
bitmap.addLoadListener(function(){
14+
this._countLoaded++;
15+
this._progressListener(this._countLoaded, this._countLoading);
16+
}.bind(this));
17+
};
18+
19+
ProgressWatcher._audioListener = function(audio){
20+
this._countLoading++;
21+
audio.addLoadListener(function(){
22+
this._countLoaded++;
23+
this._progressListener(this._countLoaded, this._countLoading);
24+
}.bind(this));
25+
};
26+
27+
ProgressWatcher.setProgressListener = function(progressListener){
28+
this._progressListener = progressListener;
29+
};
30+
31+
ProgressWatcher.clearProgress = function(){
32+
this._countLoading = 0;
33+
this._countLoaded = 0;
34+
};
35+
36+
ProgressWatcher.truncateProgress = function(){
37+
if(this._countLoaded){
38+
this._countLoading -= this._countLoaded;
39+
this._countLoaded = 0;
40+
}
41+
};

js/rpg_managers/AudioManager.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,9 @@ AudioManager.createBuffer = function(folder, name) {
375375
else Html5Audio.setup(url);
376376
return Html5Audio;
377377
} else {
378-
return new WebAudio(url);
378+
var audio = new WebAudio(url);
379+
this._callCreationHook(audio);
380+
return audio;
379381
}
380382
};
381383

@@ -418,3 +420,11 @@ AudioManager.checkWebAudioError = function(webAudio) {
418420
throw new Error('Failed to load: ' + webAudio.url);
419421
}
420422
};
423+
424+
AudioManager.setCreationHook = function(hook){
425+
this._creationHook = hook;
426+
};
427+
428+
AudioManager._callCreationHook = function(audio){
429+
if(this._creationHook) this._creationHook(audio);
430+
};

js/rpg_managers/ImageManager.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ ImageManager.loadNormalBitmap = function(path, hue) {
100100
var bitmap = this._imageCache.get(key);
101101
if (!bitmap) {
102102
bitmap = Bitmap.load(path);
103+
this._callCreationHook(bitmap);
104+
103105
bitmap.addLoadListener(function() {
104106
bitmap.rotateHue(hue);
105107
});
@@ -289,6 +291,8 @@ ImageManager.requestNormalBitmap = function(path, hue){
289291
var bitmap = this._imageCache.get(key);
290292
if(!bitmap){
291293
bitmap = Bitmap.request(path);
294+
this._callCreationHook(bitmap);
295+
292296
bitmap.addLoadListener(function(){
293297
bitmap.rotateHue(hue);
294298
});
@@ -308,3 +312,11 @@ ImageManager.update = function(){
308312
ImageManager.clearRequest = function(){
309313
this._requestQueue.clear();
310314
};
315+
316+
ImageManager.setCreationHook = function(hook){
317+
this._creationHook = hook;
318+
};
319+
320+
ImageManager._callCreationHook = function(bitmap){
321+
if(this._creationHook) this._creationHook(bitmap);
322+
};

js/rpg_managers/SceneManager.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ SceneManager.run = function(sceneClass) {
4242
};
4343

4444
SceneManager.initialize = function() {
45+
this.initProgressWatcher();
4546
this.initGraphics();
4647
this.checkFileAccess();
4748
this.initAudio();
@@ -51,6 +52,10 @@ SceneManager.initialize = function() {
5152
this.setupErrorHandlers();
5253
};
5354

55+
SceneManager.initProgressWatcher = function(){
56+
ProgressWatcher.initialize();
57+
};
58+
5459
SceneManager.initGraphics = function() {
5560
var type = this.preferableRendererType();
5661
Graphics.initialize(this._screenWidth, this._screenHeight, type);

rpg_core.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[
22
"js/rpg_core/_header.js",
33
"js/rpg_core/Patch.js",
4+
"js/rpg_core/ProgressWatcher.js",
45
"js/rpg_core/JsExtensions.js",
56
"js/rpg_core/Utils.js",
67
"js/rpg_core/CacheEntry.js",

0 commit comments

Comments
 (0)