Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Elrinth http://www.elrinth.com/ @Elrinth
- */
- /**
- * The sprdef file loader is used to load in sprdef data and parse it
- * When loaded this class will dispatch a 'loaded' event
- * If loading fails this class will dispatch an 'error' event
- *
- * @class SprdefLoader
- * @uses EventTarget
- * @constructor
- * @param url {String} The url of the sprdef file
- * @param crossorigin {Boolean} Whether requests should be treated as crossorigin
- */
- PIXI.SprdefLoader = function (url, crossorigin) {
- PIXI.EventTarget.call(this);
- /**
- * The url of the bitmap font data
- *
- * @property url
- * @type String
- */
- this.url = url;
- /**
- * Whether the requests should be treated as cross origin
- *
- * @property crossorigin
- * @type Boolean
- */
- this.crossorigin = crossorigin;
- /**
- * [read-only] The base url of the bitmap font data
- *
- * @property baseUrl
- * @type String
- * @readOnly
- */
- this.baseUrl = url.replace(/[^\/]*$/, '');
- /**
- * [read-only] Whether the data has loaded yet
- *
- * @property loaded
- * @type Boolean
- * @readOnly
- */
- this.loaded = false;
- };
- // constructor
- PIXI.SprdefLoader.prototype.constructor = PIXI.SprdefLoader;
- /**
- * Loads the sprdef data
- *
- * @method load
- */
- PIXI.SprdefLoader.prototype.load = function () {
- var scope = this;
- if(window.XDomainRequest && scope.crossorigin)
- {
- this.ajaxRequest = new window.XDomainRequest();
- // XDomainRequest has a few querks. Occasionally it will abort requests
- // A way to avoid this is to make sure ALL callbacks are set even if not used
- // More info here: http://stackoverflow.com/questions/15786966/xdomainrequest-aborts-post-on-ie-9
- this.ajaxRequest.timeout = 3000;
- this.ajaxRequest.onerror = function () {
- scope.onError();
- };
- this.ajaxRequest.ontimeout = function () {
- scope.onError();
- };
- this.ajaxRequest.onprogress = function() {};
- }
- else if (window.XMLHttpRequest)
- {
- this.ajaxRequest = new window.XMLHttpRequest();
- }
- else
- {
- this.ajaxRequest = new window.ActiveXObject('Microsoft.XMLHTTP');
- }
- this.ajaxRequest.onload = function(){
- scope.onSprdefLoaded();
- };
- this.ajaxRequest.open('GET',this.url,true);
- this.ajaxRequest.send();
- };
- /**
- * Invoke when sprdef file is loaded
- *
- * @method onSprdefLoaded
- * @private
- */
- PIXI.SprdefLoader.prototype.onSprdefLoaded = function () {
- if(!this.ajaxRequest.responseText )
- {
- this.onError();
- return;
- }
- // elrinth special parser code of: this.ajaxRequest.responseText
- //read line by line
- var lines = this.ajaxRequest.responseText.split('\n');
- var scope = this;
- var textureUrl = this.url.replace(".sprdef", ".png");
- var image = new PIXI.ImageLoader(textureUrl, this.crossorigin);
- image.addEventListener('loaded', function() {
- scope.texture = image.texture.baseTexture;
- scope.onImageFinishedLoad(lines, this, textureUrl);
- });
- image.load(); // load image before we start doing stuff because image holds important width and height data we want to use...
- };
- /**
- * Invoke when image file has finished loading
- *
- * @method onImageFinishedLoad
- * @private
- */
- PIXI.SprdefLoader.prototype.onImageFinishedLoad = function (lines, image, textureUrl) {
- i = 0;
- for (lineIndex = 0; lineIndex < lines.length; lineIndex++) {
- // trim line first
- curLine = lines[lineIndex].trim();
- if (curLine.length == 0 || curLine[0] == "#")
- continue;
- splitStrings = curLine.split(',');
- if (splitStrings.length < 2) {
- continue;
- } else if (splitStrings.length == 2) {
- x = 0;
- y = 0;
- w = parseInt(splitStrings[0]);
- h = parseInt(splitStrings[1]);
- //textureSize = GetTexCoords(x,y,w,h);
- nrInX = Math.floor(this.texture.width / w);
- nrInY = Math.floor(this.texture.height / h);
- for (var iy=0; iy < nrInY; iy++) {
- for (var ix=0; ix < nrInX; ix++) {
- PIXI.TextureCache[textureUrl + i] = new PIXI.Texture(this.texture, {
- x: ix*w,
- y: iy*h,
- width: w,
- height: h
- });
- PIXI.TextureCache[textureUrl + i].crop = new PIXI.Rectangle(ix*w,iy*h,w,h);
- i++;
- }
- }
- } else if (splitStrings.length == 4 || splitStrings.length == 5) {
- x = parseInt(splitStrings[0]);
- y = parseInt(splitStrings[1]);
- w = parseInt(splitStrings[2]);
- h = parseInt(splitStrings[3]);
- PIXI.TextureCache[textureUrl + i] = new PIXI.Texture(this.texture, {
- x: ix*w,
- y: iy*h,
- width: w,
- height: h
- });
- PIXI.TextureCache[textureUrl + i].crop = new PIXI.Rectangle(ix*w,iy*h,w,h);
- i++;
- } else if (splitStrings.length == 6 || splitStrings.length == 7) {
- x = parseInt(splitStrings[0]);
- y = parseInt(splitStrings[1]);
- w = parseInt(splitStrings[2]);
- h = parseInt(splitStrings[3]);
- nrInX = Math.floor(parseInt(splitStrings[4]));
- nrInY = Math.floor(parseInt(splitStrings[5]));
- indexName = i;
- if (splitStrings.length == 7)
- indexName = splitStrings[6];
- for (iy=0; iy < nrInY; iy++) {
- for (ix=0; ix < nrInX; ix++) {
- PIXI.TextureCache[textureUrl + indexName] = new PIXI.Texture(this.texture, {
- x: ix*w,
- y: iy*h,
- width: w,
- height: h
- });
- PIXI.TextureCache[textureUrl + indexName].crop = new PIXI.Rectangle(ix*w,iy*h,w,h);
- i++;
- }
- }
- } else {
- continue;
- }
- }
- this.onLoaded();
- };
- /**
- * Invoke when sprdef file loaded
- *
- * @method onLoaded
- * @private
- */
- PIXI.SprdefLoader.prototype.onLoaded = function () {
- this.loaded = true;
- this.dispatchEvent({
- type: 'loaded',
- content: this
- });
- };
- /**
- * Invoke when error occured
- *
- * @method onError
- * @private
- */
- PIXI.SprdefLoader.prototype.onError = function () {
- this.dispatchEvent({
- type: 'error',
- content: this
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement