1 /****************************************************************************
  2  Copyright (c) 2010-2012 cocos2d-x.org
  3  Copyright (c) 2008-2010 Ricardo Quesada
  4  Copyright (c) 2011      Zynga Inc.
  5 
  6  http://www.cocos2d-x.org
  7 
  8  Permission is hereby granted, free of charge, to any person obtaining a copy
  9  of this software and associated documentation files (the "Software"), to deal
 10  in the Software without restriction, including without limitation the rights
 11  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12  copies of the Software, and to permit persons to whom the Software is
 13  furnished to do so, subject to the following conditions:
 14 
 15  The above copyright notice and this permission notice shall be included in
 16  all copies or substantial portions of the Software.
 17 
 18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24  THE SOFTWARE.
 25  ****************************************************************************/
 26 
 27 cc._txtLoader = {
 28     load : function(realUrl, url, res, cb){
 29         cc.loader.loadTxt(realUrl, cb);
 30     }
 31 };
 32 cc.loader.register(["txt", "xml", "vsh", "fsh"], cc._txtLoader);
 33 
 34 cc._jsonLoader = {
 35     load : function(realUrl, url, res, cb){
 36         cc.loader.loadJson(realUrl, cb);
 37     }
 38 };
 39 cc.loader.register(["json", "ExportJson"], cc._jsonLoader);
 40 
 41 cc._imgLoader = {
 42     load : function(realUrl, url, res, cb){
 43         cc.loader.cache[url] =  cc.loader.loadImg(realUrl, function(err, img){
 44             if(err)
 45                 return cb(err);
 46             cc.textureCache.handleLoadedTexture(url);
 47             cb(null, img);
 48         });
 49     }
 50 };
 51 cc.loader.register(["png", "jpg", "bmp","jpeg","gif"], cc._imgLoader);
 52 cc._serverImgLoader = {
 53     load : function(realUrl, url, res, cb){
 54         cc.loader.cache[url] =  cc.loader.loadImg(res.src, function(err, img){
 55             if(err)
 56                 return cb(err);
 57             cc.textureCache.handleLoadedTexture(url);
 58             cb(null, img);
 59         });
 60     }
 61 };
 62 cc.loader.register(["serverImg"], cc._serverImgLoader);
 63 
 64 cc._plistLoader = {
 65     load : function(realUrl, url, res, cb){
 66         cc.loader.loadTxt(realUrl, function(err, txt){
 67             if(err)
 68                 return cb(err);
 69             cb(null, cc.plistParser.parse(txt));
 70         });
 71     }
 72 };
 73 cc.loader.register(["plist"], cc._plistLoader);
 74 
 75 cc._fontLoader = {
 76     TYPE : {
 77         "eot" : "embedded-opentype",
 78         "ttf" : "truetype",
 79         "woff" : "woff",
 80         "svg" : "svg"
 81     },
 82     _loadFont : function(name, srcs, type){
 83         var doc = document, path = cc.path, TYPE = this.TYPE, fontStyle = cc.newElement("style");
 84         fontStyle.type = "text/css";
 85         doc.body.appendChild(fontStyle);
 86 
 87         var fontStr = "@font-face { font-family:" + name + "; src:";
 88         if(srcs instanceof Array){
 89             for(var i = 0, li = srcs.length; i < li; i++){
 90                 var src = srcs[i];
 91                 type = path.extname(src);
 92                 fontStr += "url('" + srcs[i] + "') format('" + TYPE[type] + "')";
 93                 fontStr += (i == li - 1) ? ";" : ",";
 94             }
 95         }else{
 96             fontStr += "url('" + srcs + "') format('" + TYPE[type] + "');";
 97         }
 98         fontStyle.textContent += fontStr + "};";
 99 
100         //<div style="font-family: PressStart;">.</div>
101         var preloadDiv = cc.newElement("div");
102         var _divStyle =  preloadDiv.style;
103         _divStyle.fontFamily = name;
104         preloadDiv.innerHTML = ".";
105         _divStyle.position = "absolute";
106         _divStyle.left = "-100px";
107         _divStyle.top = "-100px";
108         doc.body.appendChild(preloadDiv);
109     },
110     load : function(realUrl, url, res, cb){
111         var self = this;
112         var type = res.type, name = res.name, srcs = res.srcs;
113         if(typeof res == "string"){
114             type = cc.path.extname(res);
115             name = cc.path.basename(res, type);
116             self._loadFont(name, res, type);
117         }else{
118             self._loadFont(name, srcs);
119         }
120         cb(null, true);
121     }
122 };
123 cc.loader.register(["font", "eot", "ttf", "woff", "svg"], cc._fontLoader);
124 
125 cc._binaryLoader = {
126     load : function(realUrl, url, res, cb){
127         cc.loader.loadBinary(realUrl, cb);
128     }
129 };