請參考 eui.DefaultThemeAdapter, 以下介紹 IThemeAdapter 實作類 ThemeAdapter.ts 裡的 ThemeAdapter.基本上筆者測試 if 判斷式 generateEUI, generateEUI2, generateJSON 程式都不會經過, 從程式碼看應該是兼容舊版本的 exml 路徑資源解析 (歡迎指正), ThemeAdapter 範例如下
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 {
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);
}
}
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 {
// 實際上目前版本的程式都只會經過這裡
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 }
沒有留言:
張貼留言