熱門文章

Ⓔeui.IThemeAdapter 用法

官方提供 IThemeAdapter 之實作

class ThemeAdapter implements eui.IThemeAdapter {

    /**
     * 解析主题
     * @param url 待解析的主题url
     * @param onSuccess 解析完成回调函数,示例:compFunc(e:egret.Event):void;
     * @param onError 解析失败回调函数,示例:errorFunc():void;
     * @param thisObject 回调的this引用
     */
    public getTheme(url: string, onSuccess: Function, onError: Function, thisObject: any): void {
        // 請自己加上 console.log 觀察, 當解析 default.thm.json 時觸發一次, 且解析每個 exml 都會觸發
        console.log('ThemeAdapter:', url);
        // 取得資源成功時觸發
        function onResGet(e: string): void {
            onSuccess.call(thisObject, e);
        }
        // 取得資源失敗時觸發
        function onResError(e: RES.ResourceEvent): void {
            if (e.resItem.url == url) {
                RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, onResError, null);
                onError.call(thisObject);
            }
        }
        // 網路文
        // generateEUI以及generateEUI2是在發佈為commonjs或者commonjs2模式的時候才使用的,
        // 本地調試時用不到代碼中也沒有,只有發布的時候才會根據發布策略才會添加初始化代碼
        if (typeof generateEUI !== 'undefined') {
            egret.callLater(() => {
                onSuccess.call(thisObject, generateEUI);
            }, this);
        }
        else if (typeof generateEUI2 !== 'undefined') {
            RES.getResByUrl("resource/gameEui.json", (data, url) => {
                window["JSONParseClass"]["setData"](data);
                egret.callLater(() => {
                    onSuccess.call(thisObject, generateEUI2);
                }, this);
            }, this, RES.ResourceItem.TYPE_JSON);
        }
        else if (typeof generateJSON !== 'undefined') {
            if (url.indexOf(".exml") > -1) {
                let dataPath = url.split("/");
                dataPath.pop();
                let dirPath = dataPath.join("/") + "_EUI.json";
                if (!generateJSON.paths[url]) {
                    RES.getResByUrl(dirPath, (data) => {
                        window["JSONParseClass"]["setData"](data);
                        egret.callLater(() => {
                            onSuccess.call(thisObject, generateJSON.paths[url]);
                        }, this);
                    }, this, RES.ResourceItem.TYPE_JSON);
                } else {
                    egret.callLater(() => {
                        onSuccess.call(thisObject, generateJSON.paths[url]);
                    }, this);
                }
            }
            else {
                egret.callLater(() => {
                    onSuccess.call(thisObject, generateJSON);
                }, this);
            }
        }
        else {
            // 上面的都不會經過, 一般會在這處理 json 跟 exml
            RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, onResError, null);
            RES.getResByUrl(url, onResGet, this, RES.ResourceItem.TYPE_TEXT);
        }
    }
}

declare var generateEUI: { paths: string[], skins: any }
declare var generateEUI2: { paths: string[], skins: any }
declare var generateJSON: { paths: string[], skins: any }

簡化

class ThemeAdapter implements eui.IThemeAdapter {
    public getTheme(url: string, onSuccess: Function, onError: Function, thisObject: any): void {
        let onResGet = (e) => onSuccess.call(thisObject, e);
        let onResError = (e) => e.resItem.url == url && (RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, onResError, null), onError.call(thisObject));
        RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, onResError, null);
        RES.getResByUrl(url, onResGet, this, RES.ResourceItem.TYPE_TEXT);
    }
}

沒有留言:

張貼留言