基础界面组件

基础界面组件

Eui中一个界面的创建,预加载资源和皮肤后,首先会在舞台上创建这个界面,随后再给这个界面绑定皮肤。这个步骤需要严格监听相对应的事件,顺序不能颠倒,故每写一个界面文件时,都需要处理监听这些事件,操作统一且固定,造成冗余。故现在抽离出一个基础界面类BasicUI,进行一些统一的操作,只要界面统一继承自这个BasicUI即可。

代码实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
export class BasicUI extends eui.Component {
public adata;//protected adata;
public constructor(cd ?: any) {
super();
if(cd){
this.adata = cd;
}

let c_class = this["__class__"] as string;
let c_arr = c_class.split(".");
this.name = c_arr[1];//每个界面独有name 此方法可获取

this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);//添加到舞台
this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.remove_self, this);//从舞台上移除
this.addEventListener(eui.UIEvent.COMPLETE, this.mx_created, this);//绑定批复后,所有元素加载完成
}

private onAddToStage(event:egret.Event) {
this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
//舞台上创建完毕后,绑定对应皮肤文件
this.set_skinname();
}

private mx_created(event:eui.UIEvent):void{
this.removeEventListener(eui.UIEvent.COMPLETE, this.mx_created, this);
//所有元素加载完成 后续操作自定义实现
this.pre_init();
}

protected remove_self() : void{
let view = this;
view.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
view.removeEventListener(egret.Event.REMOVED_FROM_STAGE, this.remove_self, this);
view.removeEventListener(eui.UIEvent.COMPLETE, this.mx_created, this);
view.on_remove();
view.removeChildren();
}

//通用的绑定皮肤方法,子类可覆盖
protected set_skinname() : void{
let cname = this.name + "Skin";
let skins = window && window["skins"];
if(skins && skins[cname]){
this.skinName = skins[cname];
}else{
this.skinName = RES.getRes(cname + "_exml");//默认使用立刻获取的方式
}
}
//子类初始化操作
protected pre_init() : void{}
protected on_remove() : void{}//仅处理特殊逻辑操作

public set_scale(s: number): void {
this.scaleX = this.scaleY = s;
}
}

egret.Event.ADDED_TO_STAGEeui.UIEvent.COMPLETE这两个事件需要在所有文件界面中处理,故提取到基类处理,减少代码量,也不用暴露给开发。