瀏覽代碼

Merge pull request #1799 from nockawa/PictureAltasJSONLocal

Canvas2D: AtlasPictureInfoFactory.loadFromUrl now rely on Tools.LoadFile()
Loïc Baumann 8 年之前
父節點
當前提交
692bdfe480

+ 28 - 36
canvas2D/src/Engine/babylon.atlasPicture.ts

@@ -208,46 +208,38 @@
          * @param onError a callback that will be called in case of error
          */
         public static loadFromUrl(texture: Texture, url: string, onLoad: (api: AtlasPictureInfo) => void, onError: (msg: string, code: number) => void = null) {
-            var xhr = new XMLHttpRequest();
-            xhr.onreadystatechange = () => {
-                if (xhr.readyState === XMLHttpRequest.DONE) {
-                    if (xhr.status === 200) {
-                        let ext = url.split('.').pop().split(/\#|\?/)[0];
-                        let plugins = AtlasPictureInfoFactory.plugins.get(ext.toLocaleLowerCase());
-                        if (!plugins) {
-                            if (onError) {
-                                onError("couldn't find a plugin for this file extension", -1);
-                            }
-                            return;
-                        }
-                        for (let p of plugins) {
-                            let ret = p.loadFile(xhr.response);
-                            if (ret) {
-                                if (ret.api) {
-                                    ret.api.texture = texture;
-                                    if (onLoad) {
-                                        onLoad(ret.api);
-                                    }
-                                } else if (onError) {
-                                    onError(ret.errorMsg, ret.errorCode);
-                                }
-                                return;
+            Tools.LoadFile(url, (data) => {
+                let ext = url.split('.').pop().split(/\#|\?/)[0];
+                let plugins = AtlasPictureInfoFactory.plugins.get(ext.toLocaleLowerCase());
+                if (!plugins) {
+                    if (onError) {
+                        onError("couldn't find a plugin for this file extension", -1);
+                    }
+                    return;
+                }
+                for (let p of plugins) {
+                    let ret = p.loadFile(data);
+                    if (ret) {
+                        if (ret.api) {
+                            ret.api.texture = texture;
+                            if (onLoad) {
+                                onLoad(ret.api);
                             }
+                        } else if (onError) {
+                            onError(ret.errorMsg, ret.errorCode);
                         }
-
-                        if (onError) {
-                            onError("No plugin to load this Atlas Data file format", -1);
-                        }
-
-                    } else {
-                        if (onError) {
-                            onError("Couldn't load file through HTTP Request, HTTP Status " + xhr.status, xhr.status);
-                        }
+                        return;
                     }
                 }
-            }
-            xhr.open("GET", url, true);
-            xhr.send();
+
+                if (onError) {
+                    onError("No plugin to load this Atlas Data file format", -1);
+                }
+            }, null, null, null, () => {
+                if (onError) {
+                    onError("Couldn't load file", -1);
+                }
+            });
             return null;
         }
 

+ 28 - 36
dist/preview release/canvas2D/babylon.canvas2d.js

@@ -13987,47 +13987,39 @@ var BABYLON;
          */
         AtlasPictureInfoFactory.loadFromUrl = function (texture, url, onLoad, onError) {
             if (onError === void 0) { onError = null; }
-            var xhr = new XMLHttpRequest();
-            xhr.onreadystatechange = function () {
-                if (xhr.readyState === XMLHttpRequest.DONE) {
-                    if (xhr.status === 200) {
-                        var ext = url.split('.').pop().split(/\#|\?/)[0];
-                        var plugins = AtlasPictureInfoFactory.plugins.get(ext.toLocaleLowerCase());
-                        if (!plugins) {
-                            if (onError) {
-                                onError("couldn't find a plugin for this file extension", -1);
-                            }
-                            return;
-                        }
-                        for (var _i = 0, plugins_1 = plugins; _i < plugins_1.length; _i++) {
-                            var p = plugins_1[_i];
-                            var ret = p.loadFile(xhr.response);
-                            if (ret) {
-                                if (ret.api) {
-                                    ret.api.texture = texture;
-                                    if (onLoad) {
-                                        onLoad(ret.api);
-                                    }
-                                }
-                                else if (onError) {
-                                    onError(ret.errorMsg, ret.errorCode);
-                                }
-                                return;
+            BABYLON.Tools.LoadFile(url, function (data) {
+                var ext = url.split('.').pop().split(/\#|\?/)[0];
+                var plugins = AtlasPictureInfoFactory.plugins.get(ext.toLocaleLowerCase());
+                if (!plugins) {
+                    if (onError) {
+                        onError("couldn't find a plugin for this file extension", -1);
+                    }
+                    return;
+                }
+                for (var _i = 0, plugins_1 = plugins; _i < plugins_1.length; _i++) {
+                    var p = plugins_1[_i];
+                    var ret = p.loadFile(data);
+                    if (ret) {
+                        if (ret.api) {
+                            ret.api.texture = texture;
+                            if (onLoad) {
+                                onLoad(ret.api);
                             }
                         }
-                        if (onError) {
-                            onError("No plugin to load this Atlas Data file format", -1);
-                        }
-                    }
-                    else {
-                        if (onError) {
-                            onError("Couldn't load file through HTTP Request, HTTP Status " + xhr.status, xhr.status);
+                        else if (onError) {
+                            onError(ret.errorMsg, ret.errorCode);
                         }
+                        return;
                     }
                 }
-            };
-            xhr.open("GET", url, true);
-            xhr.send();
+                if (onError) {
+                    onError("No plugin to load this Atlas Data file format", -1);
+                }
+            }, null, null, null, function () {
+                if (onError) {
+                    onError("Couldn't load file", -1);
+                }
+            });
             return null;
         };
         return AtlasPictureInfoFactory;

文件差異過大導致無法顯示
+ 5 - 5
dist/preview release/canvas2D/babylon.canvas2d.min.js