Advertisement
Guest User

Untitled

a guest
Apr 14th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @author Elrinth http://www.elrinth.com/ @Elrinth
  3.  */
  4.  
  5.  /**
  6.   * The sprdef file loader is used to load in sprdef data and parse it
  7.   * When loaded this class will dispatch a 'loaded' event
  8.   * If loading fails this class will dispatch an 'error' event
  9.   *
  10.   * @class SprdefLoader
  11.   * @uses EventTarget
  12.   * @constructor
  13.   * @param url {String} The url of the sprdef file
  14.   * @param crossorigin {Boolean} Whether requests should be treated as crossorigin
  15.   */
  16. PIXI.SprdefLoader = function (url, crossorigin) {
  17.     PIXI.EventTarget.call(this);
  18.  
  19.     /**
  20.      * The url of the bitmap font data
  21.      *
  22.      * @property url
  23.      * @type String
  24.      */
  25.     this.url = url;
  26.  
  27.     /**
  28.      * Whether the requests should be treated as cross origin
  29.      *
  30.      * @property crossorigin
  31.      * @type Boolean
  32.      */
  33.     this.crossorigin = crossorigin;
  34.  
  35.     /**
  36.      * [read-only] The base url of the bitmap font data
  37.      *
  38.      * @property baseUrl
  39.      * @type String
  40.      * @readOnly
  41.      */
  42.     this.baseUrl = url.replace(/[^\/]*$/, '');
  43.  
  44.     /**
  45.      * [read-only] Whether the data has loaded yet
  46.      *
  47.      * @property loaded
  48.      * @type Boolean
  49.      * @readOnly
  50.      */
  51.     this.loaded = false;
  52.  
  53. };
  54.  
  55. // constructor
  56. PIXI.SprdefLoader.prototype.constructor = PIXI.SprdefLoader;
  57.  
  58. /**
  59.  * Loads the sprdef data
  60.  *
  61.  * @method load
  62.  */
  63. PIXI.SprdefLoader.prototype.load = function () {
  64.  
  65.     var scope = this;
  66.  
  67.     if(window.XDomainRequest && scope.crossorigin)
  68.     {
  69.         this.ajaxRequest = new window.XDomainRequest();
  70.  
  71.         // XDomainRequest has a few querks. Occasionally it will abort requests
  72.         // A way to avoid this is to make sure ALL callbacks are set even if not used
  73.         // More info here: http://stackoverflow.com/questions/15786966/xdomainrequest-aborts-post-on-ie-9
  74.         this.ajaxRequest.timeout = 3000;
  75.  
  76.         this.ajaxRequest.onerror = function () {
  77.             scope.onError();
  78.         };
  79.            
  80.         this.ajaxRequest.ontimeout = function () {
  81.             scope.onError();
  82.         };
  83.  
  84.         this.ajaxRequest.onprogress = function() {};
  85.  
  86.     }
  87.     else if (window.XMLHttpRequest)
  88.     {
  89.         this.ajaxRequest = new window.XMLHttpRequest();
  90.     }
  91.     else
  92.     {
  93.         this.ajaxRequest = new window.ActiveXObject('Microsoft.XMLHTTP');
  94.     }
  95.  
  96.    
  97.  
  98.     this.ajaxRequest.onload = function(){
  99.  
  100.         scope.onSprdefLoaded();
  101.     };
  102.  
  103.     this.ajaxRequest.open('GET',this.url,true);
  104.  
  105.     this.ajaxRequest.send();
  106. };
  107.  
  108. /**
  109.  * Invoke when sprdef file is loaded
  110.  *
  111.  * @method onSprdefLoaded
  112.  * @private
  113.  */
  114. PIXI.SprdefLoader.prototype.onSprdefLoaded = function () {
  115.    
  116.     if(!this.ajaxRequest.responseText )
  117.     {
  118.         this.onError();
  119.         return;
  120.     }
  121.    
  122.     // elrinth special parser code of: this.ajaxRequest.responseText
  123.     //read line by line
  124.     var lines = this.ajaxRequest.responseText.split('\n');
  125.    
  126.     var scope = this;
  127.     var textureUrl = this.url.replace(".sprdef", ".png");
  128.     var image = new PIXI.ImageLoader(textureUrl, this.crossorigin);
  129.    
  130.     image.addEventListener('loaded', function() {
  131.         scope.texture = image.texture.baseTexture;
  132.         scope.onImageFinishedLoad(lines, this, textureUrl);
  133.     });
  134.  
  135.     image.load(); // load image before we start doing stuff because image holds important width and height data we want to use...
  136.  
  137. };
  138.  
  139. /**
  140.  * Invoke when image file has finished loading
  141.  *
  142.  * @method onImageFinishedLoad
  143.  * @private
  144.  */
  145. PIXI.SprdefLoader.prototype.onImageFinishedLoad = function (lines, image, textureUrl) {
  146.     i = 0;
  147.     for (lineIndex = 0; lineIndex < lines.length; lineIndex++) {
  148.         // trim line first
  149.         curLine = lines[lineIndex].trim();
  150.         if (curLine.length == 0 || curLine[0] == "#")
  151.             continue;
  152.            
  153.         splitStrings = curLine.split(',');
  154.        
  155.         if (splitStrings.length < 2) {
  156.             continue;
  157.         } else if (splitStrings.length == 2) {
  158.            
  159.            
  160.             x = 0;
  161.             y = 0;
  162.             w = parseInt(splitStrings[0]);
  163.             h = parseInt(splitStrings[1]);
  164.            
  165.            
  166.             //textureSize = GetTexCoords(x,y,w,h);
  167.             nrInX = Math.floor(this.texture.width / w);
  168.             nrInY = Math.floor(this.texture.height / h);
  169.            
  170.            
  171.             for (var iy=0; iy < nrInY; iy++) {
  172.                 for (var ix=0; ix < nrInX; ix++) {
  173.                     PIXI.TextureCache[textureUrl + i] = new PIXI.Texture(this.texture, {
  174.                         x: ix*w,
  175.                         y: iy*h,
  176.                         width: w,
  177.                         height: h
  178.                     });
  179.                     PIXI.TextureCache[textureUrl + i].crop = new PIXI.Rectangle(ix*w,iy*h,w,h);
  180.                    
  181.                     i++;
  182.                 }
  183.             }
  184.         } else if (splitStrings.length == 4 || splitStrings.length == 5) {
  185.             x = parseInt(splitStrings[0]);
  186.             y = parseInt(splitStrings[1]);
  187.             w = parseInt(splitStrings[2]);
  188.             h = parseInt(splitStrings[3]);
  189.             PIXI.TextureCache[textureUrl + i] = new PIXI.Texture(this.texture, {
  190.                 x: ix*w,
  191.                 y: iy*h,
  192.                 width: w,
  193.                 height: h
  194.             });
  195.             PIXI.TextureCache[textureUrl + i].crop = new PIXI.Rectangle(ix*w,iy*h,w,h);
  196.            
  197.             i++;
  198.         } else if (splitStrings.length == 6 || splitStrings.length == 7) {
  199.             x = parseInt(splitStrings[0]);
  200.             y = parseInt(splitStrings[1]);
  201.             w = parseInt(splitStrings[2]);
  202.             h = parseInt(splitStrings[3]);
  203.  
  204.             nrInX = Math.floor(parseInt(splitStrings[4]));
  205.             nrInY = Math.floor(parseInt(splitStrings[5]));
  206.  
  207.             indexName = i;
  208.             if (splitStrings.length == 7)
  209.                 indexName = splitStrings[6];
  210.            
  211.             for (iy=0; iy < nrInY; iy++) {
  212.                 for (ix=0; ix < nrInX; ix++) {
  213.                     PIXI.TextureCache[textureUrl + indexName] = new PIXI.Texture(this.texture, {
  214.                         x: ix*w,
  215.                         y: iy*h,
  216.                         width: w,
  217.                         height: h
  218.                     });
  219.                     PIXI.TextureCache[textureUrl + indexName].crop = new PIXI.Rectangle(ix*w,iy*h,w,h);
  220.                    
  221.                     i++;
  222.                 }
  223.             }
  224.         } else {
  225.             continue;
  226.         }
  227.     }
  228.  
  229.     this.onLoaded();
  230. };
  231.  
  232. /**
  233.  * Invoke when sprdef file loaded
  234.  *
  235.  * @method onLoaded
  236.  * @private
  237.  */
  238. PIXI.SprdefLoader.prototype.onLoaded = function () {
  239.     this.loaded = true;
  240.     this.dispatchEvent({
  241.         type: 'loaded',
  242.         content: this
  243.     });
  244. };
  245.  
  246. /**
  247.  * Invoke when error occured
  248.  *
  249.  * @method onError
  250.  * @private
  251.  */
  252. PIXI.SprdefLoader.prototype.onError = function () {
  253.  
  254.     this.dispatchEvent({
  255.         type: 'error',
  256.         content: this
  257.     });
  258. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement