Skip to content

Commit 3cf8526

Browse files
author
liply
authored
Merge branch 'master' into memory-bloat
2 parents 7ea8bca + 5a95385 commit 3cf8526

26 files changed

+264
-87
lines changed

js/libs/iphone-inline-video.browser.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/rpg_core/Bitmap.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ Bitmap.snap = function(stage) {
153153
}
154154
context.drawImage(canvas, 0, 0);
155155
} else {
156-
//TODO: Ivan: what if stage is not present?
156+
157157
}
158158
renderTexture.destroy({ destroyBase: true });
159159
bitmap._setDirty();
@@ -809,7 +809,7 @@ Bitmap.prototype.decode = function(){
809809
Bitmap.prototype._callLoadListeners = function() {
810810
while (this._loadListeners.length > 0) {
811811
var listener = this._loadListeners.shift();
812-
listener();
812+
listener(this);
813813
}
814814
};
815815

js/rpg_core/CacheEntry.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ CacheEntry.prototype.touch = function () {
7373
this.touchSeconds = cache.updateSeconds;
7474
} else if (this.freedByTTL) {
7575
this.freedByTTL = false;
76-
//TODO: shall we log this event? its not normal
7776
if (!cache._inner[this.key]) {
7877
cache._inner[this.key] = this;
7978
}

js/rpg_core/Graphics.js

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
//-----------------------------------------------------------------------------
2-
3-
var waitForLoading = false;
4-
var register = false;
5-
6-
function handleiOSTouch(ev) {
7-
if (Graphics._video.paused && Graphics.isVideoPlaying())Graphics._video.play();
8-
}
9-
102
/**
113
* The static class that carries out graphics processing.
124
*
@@ -16,6 +8,10 @@ function Graphics() {
168
throw new Error('This is a static class');
179
}
1810

11+
Graphics._cssFontLoading = document.fonts && document.fonts.ready;
12+
Graphics._fontLoaded = null;
13+
14+
1915
/**
2016
* Initializes the graphics system.
2117
*
@@ -39,6 +35,8 @@ Graphics.initialize = function(width, height, type) {
3935
this._errorPrinter = null;
4036
this._canvas = null;
4137
this._video = null;
38+
this._videoUnlocked = !Utils.isMobileDevice();
39+
this._videoLoading = false;
4240
this._upperCanvas = null;
4341
this._renderer = null;
4442
this._fpsMeter = null;
@@ -62,6 +60,22 @@ Graphics.initialize = function(width, height, type) {
6260
this._disableTextSelection();
6361
this._disableContextMenu();
6462
this._setupEventHandlers();
63+
this._setupCssFontLoading();
64+
};
65+
66+
67+
Graphics._setupCssFontLoading = function(){
68+
if(Graphics._cssFontLoading){
69+
document.fonts.ready.then(function(fonts){
70+
Graphics._fontLoaded = fonts;
71+
}).catch(function(error){
72+
SceneManager.onError(error);
73+
});
74+
}
75+
};
76+
77+
Graphics.canUseCssFontLoading = function(){
78+
return !!this._cssFontLoading;
6579
};
6680

6781
/**
@@ -323,17 +337,25 @@ Graphics.loadFont = function(name, url) {
323337
* @return {Boolean} True if the font file is loaded
324338
*/
325339
Graphics.isFontLoaded = function(name) {
326-
if (!this._hiddenCanvas) {
327-
this._hiddenCanvas = document.createElement('canvas');
340+
if (Graphics._cssFontLoading) {
341+
if(Graphics._fontLoaded){
342+
return Graphics._fontLoaded.check('10px "'+name+'"');
343+
}
344+
345+
return false;
346+
} else {
347+
if (!this._hiddenCanvas) {
348+
this._hiddenCanvas = document.createElement('canvas');
349+
}
350+
var context = this._hiddenCanvas.getContext('2d');
351+
var text = 'abcdefghijklmnopqrstuvwxyz';
352+
var width1, width2;
353+
context.font = '40px ' + name + ', sans-serif';
354+
width1 = context.measureText(text).width;
355+
context.font = '40px sans-serif';
356+
width2 = context.measureText(text).width;
357+
return width1 !== width2;
328358
}
329-
var context = this._hiddenCanvas.getContext('2d');
330-
var text = 'abcdefghijklmnopqrstuvwxyz';
331-
var width1, width2;
332-
context.font = '40px ' + name + ', sans-serif';
333-
width1 = context.measureText(text).width;
334-
context.font = '40px sans-serif';
335-
width2 = context.measureText(text).width;
336-
return width1 !== width2;
337359
};
338360

339361
/**
@@ -349,14 +371,7 @@ Graphics.playVideo = function(src) {
349371
this._video.onerror = this._onVideoError.bind(this);
350372
this._video.onended = this._onVideoEnd.bind(this);
351373
this._video.load();
352-
353-
if (Utils.isMobileSafari()) {
354-
waitForLoading = true;
355-
if (!register) {
356-
register = true;
357-
document.addEventListener('touchstart', handleiOSTouch);
358-
}
359-
}
374+
this._videoLoading = true;
360375
};
361376

362377
/**
@@ -367,8 +382,7 @@ Graphics.playVideo = function(src) {
367382
* @return {Boolean} True if the video is playing
368383
*/
369384
Graphics.isVideoPlaying = function() {
370-
if (Utils.isMobileSafari()) return waitForLoading || (this._video && this._isVideoVisible());
371-
return this._video && this._isVideoVisible();
385+
return this._videoLoading || this._isVideoVisible();
372386
};
373387

374388
/**
@@ -707,7 +721,9 @@ Graphics._createVideo = function() {
707721
this._video = document.createElement('video');
708722
this._video.id = 'GameVideo';
709723
this._video.style.opacity = 0;
724+
this._video.setAttribute('playsinline', '');
710725
this._updateVideo();
726+
makeVideoPlayableInline(this._video);
711727
document.body.appendChild(this._video);
712728
};
713729

@@ -962,9 +978,7 @@ Graphics._applyCanvasFilter = function() {
962978
Graphics._onVideoLoad = function() {
963979
this._video.play();
964980
this._updateVisibility(true);
965-
if (Utils.isMobileSafari()) {
966-
waitForLoading = false;
967-
}
981+
this._videoLoading = false;
968982
};
969983

970984
/**
@@ -974,6 +988,7 @@ Graphics._onVideoLoad = function() {
974988
*/
975989
Graphics._onVideoError = function() {
976990
this._updateVisibility(false);
991+
this._videoLoading = false;
977992
};
978993

979994
/**
@@ -983,13 +998,6 @@ Graphics._onVideoError = function() {
983998
*/
984999
Graphics._onVideoEnd = function() {
9851000
this._updateVisibility(false);
986-
987-
if (Utils.isMobileSafari()) {
988-
if (register) {
989-
document.removeEventListener('touchstart', handleiOSTouch);
990-
register = false;
991-
}
992-
}
9931001
};
9941002

9951003
/**
@@ -1021,6 +1029,7 @@ Graphics._isVideoVisible = function() {
10211029
Graphics._setupEventHandlers = function() {
10221030
window.addEventListener('resize', this._onWindowResize.bind(this));
10231031
document.addEventListener('keydown', this._onKeyDown.bind(this));
1032+
document.addEventListener('touchend', this._onTouchEnd.bind(this));
10241033
};
10251034

10261035
/**
@@ -1057,6 +1066,22 @@ Graphics._onKeyDown = function(event) {
10571066
}
10581067
};
10591068

1069+
/**
1070+
* @static
1071+
* @method _onTouchEnd
1072+
* @param {TouchEvent} event
1073+
* @private
1074+
*/
1075+
Graphics._onTouchEnd = function(event) {
1076+
if (!this._videoUnlocked) {
1077+
this._video.play();
1078+
this._videoUnlocked = true;
1079+
}
1080+
if (this._isVideoVisible() && this._video.paused) {
1081+
this._video.play();
1082+
}
1083+
};
1084+
10601085
/**
10611086
* @static
10621087
* @method _switchFPSMeter

js/rpg_core/JsonEx.js

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ function JsonEx() {
1818
*/
1919
JsonEx.maxDepth = 100;
2020

21+
JsonEx._id = 1;
22+
JsonEx._generateId = function(){
23+
return JsonEx._id++;
24+
};
25+
2126
/**
2227
* Converts an object to a JSON string with object information.
2328
*
@@ -27,7 +32,23 @@ JsonEx.maxDepth = 100;
2732
* @return {String} The JSON string
2833
*/
2934
JsonEx.stringify = function(object) {
30-
return JSON.stringify(this._encode(object));
35+
var circular = [];
36+
JsonEx._id = 1;
37+
var json = JSON.stringify(this._encode(object, circular, 0));
38+
this._cleanMetadata(object);
39+
this._restoreCircularReference(circular);
40+
41+
return json;
42+
};
43+
44+
JsonEx._restoreCircularReference = function(circulars){
45+
circulars.forEach(function(circular){
46+
var key = circular[0];
47+
var value = circular[1];
48+
var content = circular[2];
49+
50+
value[key] = content;
51+
});
3152
};
3253

3354
/**
@@ -39,9 +60,43 @@ JsonEx.stringify = function(object) {
3960
* @return {Object} The reconstructed object
4061
*/
4162
JsonEx.parse = function(json) {
42-
return this._decode(JSON.parse(json));
63+
var circular = [];
64+
var registry = {};
65+
var contents = this._decode(JSON.parse(json), circular, registry);
66+
this._cleanMetadata(contents);
67+
this._linkCircularReference(contents, circular, registry);
68+
69+
return contents;
70+
};
71+
72+
JsonEx._linkCircularReference = function(contents, circulars, registry){
73+
circulars.forEach(function(circular){
74+
var key = circular[0];
75+
var value = circular[1];
76+
var id = circular[2];
77+
78+
value[key] = registry[id];
79+
});
4380
};
4481

82+
JsonEx._cleanMetadata = function(object){
83+
if(!object) return;
84+
85+
delete object['@'];
86+
delete object['@c'];
87+
delete object['@m'];
88+
89+
if(typeof object === 'object'){
90+
Object.keys(object).forEach(function(key){
91+
var value = object[key];
92+
if(typeof value === 'object'){
93+
JsonEx._cleanMetadata(value);
94+
}
95+
});
96+
}
97+
};
98+
99+
45100
/**
46101
* Makes a deep copy of the specified object.
47102
*
@@ -58,24 +113,36 @@ JsonEx.makeDeepCopy = function(object) {
58113
* @static
59114
* @method _encode
60115
* @param {Object} value
116+
* @param {Array} circular
61117
* @param {Number} depth
62118
* @return {Object}
63119
* @private
64120
*/
65-
JsonEx._encode = function(value, depth) {
121+
JsonEx._encode = function(value, circular, depth) {
66122
depth = depth || 0;
67123
if (++depth >= this.maxDepth) {
68124
throw new Error('Object too deep');
69125
}
70126
var type = Object.prototype.toString.call(value);
71127
if (type === '[object Object]' || type === '[object Array]') {
128+
value['@m'] = true;
129+
value['@c'] = JsonEx._generateId();
72130
var constructorName = this._getConstructorName(value);
73131
if (constructorName !== 'Object' && constructorName !== 'Array') {
74132
value['@'] = constructorName;
75133
}
76134
for (var key in value) {
77135
if (value.hasOwnProperty(key)) {
78-
value[key] = this._encode(value[key], depth + 1);
136+
if(value[key] && typeof value[key] === 'object'){
137+
if(!value[key]['@m']){
138+
value[key] = this._encode(value[key], circular, depth + 1);
139+
}else{
140+
circular.push([key, value, value[key]]);
141+
value[key] = {'@r': value[key]['@c']};
142+
}
143+
}else{
144+
value[key] = this._encode(value[key], circular, depth + 1);
145+
}
79146
}
80147
}
81148
}
@@ -87,12 +154,16 @@ JsonEx._encode = function(value, depth) {
87154
* @static
88155
* @method _decode
89156
* @param {Object} value
157+
* @param {Array} circular
158+
* @param {Object} registry
90159
* @return {Object}
91160
* @private
92161
*/
93-
JsonEx._decode = function(value) {
162+
JsonEx._decode = function(value, circular, registry) {
94163
var type = Object.prototype.toString.call(value);
95164
if (type === '[object Object]' || type === '[object Array]') {
165+
registry[value['@c']] = value;
166+
96167
if (value['@']) {
97168
var constructor = window[value['@']];
98169
if (constructor) {
@@ -101,7 +172,10 @@ JsonEx._decode = function(value) {
101172
}
102173
for (var key in value) {
103174
if (value.hasOwnProperty(key)) {
104-
value[key] = this._decode(value[key]);
175+
if(value[key] && value[key]['@r']){
176+
circular.push([key, value, value[key]['@r']])
177+
}
178+
value[key] = this._decode(value[key], circular, registry);
105179
}
106180
}
107181
}

0 commit comments

Comments
 (0)