-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.js
174 lines (162 loc) · 4.16 KB
/
Utils.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
//-----------------------------------------------------------------------------
/**
* The static class that defines utility methods.
*
* @class Utils
*/
function Utils() {
throw new Error('This is a static class');
}
/**
* The name of the RPG Maker. 'MV' in the current version.
*
* @static
* @property RPGMAKER_NAME
* @type String
* @final
*/
Utils.RPGMAKER_NAME = 'MV';
/**
* The version of the RPG Maker.
*
* @static
* @property RPGMAKER_VERSION
* @type String
* @final
*/
Utils.RPGMAKER_VERSION = "2.0.0";
/**
* The name of the generator engine.
*
* @static
* @property RPGMAKER_ENGINE
* @type String
* @final
*/
Utils.RPGMAKER_ENGINE = "rmmv-core-custom";
/**
* Checks whether the option is in the query string.
*
* @static
* @method isOptionValid
* @param {String} name The option name
* @return {Boolean} True if the option is in the query string
*/
Utils.isOptionValid = function(name) {
if (location.search.slice(1).split('&').contains(name)) {
return true;
}
if (typeof nw !== "undefined" &&
nw.App.argv.length > 0 &&
nw.App.argv[0].split('&').contains(name)
) {
return true;
}
return false;
};
/**
* Checks whether the platform is NW.js.
*
* @static
* @method isNwjs
* @return {Boolean} True if the platform is NW.js
*/
Utils.isNwjs = function() {
return typeof require === 'function' && typeof process === 'object';
};
/**
* Checks whether the platform is a mobile device.
*
* @static
* @method isMobileDevice
* @return {Boolean} True if the platform is a mobile device
*/
Utils.isMobileDevice = function() {
var r = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i;
return !!navigator.userAgent.match(r);
};
/**
* Checks whether the browser is Mobile Safari.
*
* @static
* @method isMobileSafari
* @return {Boolean} True if the browser is Mobile Safari
*/
Utils.isMobileSafari = function() {
var agent = navigator.userAgent;
return !!(agent.match(/iPhone|iPad|iPod/) && agent.match(/AppleWebKit/) &&
!agent.match('CriOS'));
};
/**
* Checks whether the browser is Android Chrome.
*
* @static
* @method isAndroidChrome
* @return {Boolean} True if the browser is Android Chrome
*/
Utils.isAndroidChrome = function() {
var agent = navigator.userAgent;
return !!(agent.match(/Android/) && agent.match(/Chrome/));
};
/**
* Checks whether the browser can read files in the game folder.
*
* @static
* @method canReadGameFiles
* @return {Boolean} True if the browser can read files in the game folder
*/
Utils.canReadGameFiles = function() {
var scripts = document.getElementsByTagName('script');
var lastScript = scripts[scripts.length - 1];
var xhr = new XMLHttpRequest();
try {
xhr.open('GET', lastScript.src);
xhr.overrideMimeType('text/javascript');
xhr.send();
return true;
} catch (e) {
return false;
}
};
/**
* Makes a CSS color string from RGB values.
*
* @static
* @method rgbToCssColor
* @param {Number} r The red value in the range (0, 255)
* @param {Number} g The green value in the range (0, 255)
* @param {Number} b The blue value in the range (0, 255)
* @return {String} CSS color string
*/
Utils.rgbToCssColor = function(r, g, b) {
r = Math.round(r);
g = Math.round(g);
b = Math.round(b);
return 'rgb(' + r + ',' + g + ',' + b + ')';
};
Utils._id = 1;
Utils.generateRuntimeId = function(){
return Utils._id++;
};
Utils._supportPassiveEvent = null;
/**
* Test this browser support passive event feature
*
* @static
* @method isSupportPassiveEvent
* @return {Boolean} this browser support passive event or not
*/
Utils.isSupportPassiveEvent = function() {
if (typeof Utils._supportPassiveEvent === "boolean") {
return Utils._supportPassiveEvent;
}
// test support passive event
// https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md#feature-detection
var passive = false;
var options = Object.defineProperty({}, "passive", {
get: function() { passive = true; }
});
window.addEventListener("test", null, options);
Utils._supportPassiveEvent = passive;
return passive;
}