Sphinx-Polyfill
Comme tous mes plugins, ce script est publié sous licence CC BY 4.0.
Ce plugin implémente simplement quelques fonctions basiques utilisées par d’autres plugins. L’intérêt de les intégrer à un plugin séparé est de pouvoir les réutiliser à loisir dans l’ensemble des plugins qui en ont besoin. Ce script s’enrichira au fur et à mesure de mes besoins.
//=============================================================================
// Sphinx-Polyfill.js
//=============================================================================
/*:
* @plugindesc Import de fonctions JS non disponibles dans le moteur de RMMV
* @author Sphinx
*
* @help
* //==========================================================================
* // Plugin : Sphinx-Polyfill
* // Date : 27 décembre 2016
* // Auteur : Sphinx
* //==========================================================================
* Polyfill de fonctions JS non disponibles dans le moteur de RMMV
* - object.isNumeric : vérifie si l'objet est un nombre
* - Math.randomIntBetween : retourne un entier aléatoire compris entre min et max
* - string.capitalize : Convertit la première lettre d'une chaine en
* majuscules
* - Bitmap.measureTextHeight : mesure la hauteur du texte en pixel
*
* Dépendances :
* Aucune
*/
// Vérifie si une valeur est numérique
if(!Object.prototype.isNumeric) {
Object.prototype.isNumeric = function() {
return !isNaN(parseFloat(this)) && isFinite(this);
};
}
// Retourne un entier pseudo-aléatoire entre deux bornes
if(!Math.randomIntBetween) {
Math.randomIntBetween = function(min, max) {
return min + Math.floor(Math.random() * Math.floor(max - min));
};
}
// Mélange les valeurs d'un tableau de facon pseudo-aléatoire
if(!Array.prototype.shuffle) {
Array.prototype.shuffle = function() {
var i = this.length, j, temp;
if ( i == 0 ) return this;
while ( --i ) {
j = Math.floor( Math.random() * ( i + 1 ) );
temp = this[i];
this[i] = this[j];
this[j] = temp;
}
return this;
};
}
// Bascule la première lettre de la chaine en majuscule
if(!String.prototype.capitalize) {
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
};
}
// Retourne un tableau contenant toutes les valeurs de this à limit
if(!Number.prototype.to) {
Number.prototype.to = function(limit, excludeLimit = true) {
from = this;
to = limit;
if(!excludeLimit) ++to;
return [...Array(to - from).keys()].map(value => value + from);
};
}
// Retourne un tableau contenant toutes les valeurs de this à limit avec un pas défini
if(!Number.prototype.toWithStep) {
Number.prototype.toWithStep = function(limit, step = 1, excludeLimit = true) {
from = this;
to = limit;
if(!excludeLimit) ++to;
return [...Array(to - from).keys()].filter(value => value % step == 0).map(value => value + from);
};
}
// Retourne la hauteur en px d'une taille de police en pt
if(!Bitmap.prototype.measureTextHeight) {
Bitmap.prototype.measureTextHeight = function(text) {
return Math.round(this.fontSize * 1.3281472327365);
};
}
// Retourne la couleur CSS passée en paramètre au format #hexadécimal
if(!Game_System.prototype.getColorToHexa) {
Game_System.prototype.getColorToHexa = function(color) {
d = document.createElement("div");
d.style.color = color;
document.body.appendChild(d);
color = window.getComputedStyle(d).color;
document.body.removeChild(d);
color = color.replace(/^rgb\((\d+), (\d+), (\d+)\)$/, "$1-$2-$3").split("-");
for(i = 0; i < color.length; ++i) {
color[i] = parseInt(color[i], 10).toString(16);
if(color[i].length == 0) color[i] = "00";
else if(color[i].length == 1) color[i] = "0" + color[i];
}
return color.join("");
};
}
// Retourne la couleur CSS passée en paramètre au format rgba()
if(!window.getColorToRGBA) {
window.getColorToRGBA = function(color, alpha = 1) {
d = document.createElement("div");
d.style.color = color;
document.body.appendChild(d);
color = window.getComputedStyle(d).color;
document.body.removeChild(d);
color = color.replace(/^rgb\((\d+), (\d+), (\d+)\)$/, "$1-$2-$3").split("-");
color = "rgba(" + color[0] + "," + color[1] + "," + color[2] + "," + alpha + ")";
return color;
};
}