GObject
GObject是FGUI中UI組件的基類。GComponent,GImage,GGraph等都繼承自GObject。
但GObject雖是FGUI的組件基類,但是FGUI的所有組件還是在Laya的基礎(chǔ)上進(jìn)行封裝的咧叭。例如每個(gè)GObject對(duì)象或者GObject子類的對(duì)象中都包含一個(gè)Laya.Sprite對(duì)象
handleXYChanged() {
var xv = this._x;
var yv = this._y + this._yOffset;
if (this._pivotAsAnchor) {
xv -= this._pivotX * this._width;
yv -= this._pivotY * this._height;
}
if (this._pixelSnapping) {
xv = Math.round(xv);
yv = Math.round(yv);
}
this._displayObject.pos(xv + this._pivotOffsetX, yv + this._pivotOffsetY);
}
關(guān)于軸心點(diǎn)
設(shè)置軸心點(diǎn)的源碼:
setPivot(xv, yv = 0, asAnchor = false) {
if (this._pivotX != xv || this._pivotY != yv || this._pivotAsAnchor != asAnchor) {
this._pivotX = xv;
this._pivotY = yv;
this._pivotAsAnchor = asAnchor;
this.updatePivotOffset();
this.handleXYChanged();
}
}
updatePivotOffset() {
if (this._displayObject != null) {
if (this._displayObject.transform && (this._pivotX != 0 || this._pivotY != 0)) {
GObject.sHelperPoint.x = this._pivotX * this._width;
GObject.sHelperPoint.y = this._pivotY * this._height;
var pt = this._displayObject.transform.transformPoint(GObject.sHelperPoint);
this._pivotOffsetX = this._pivotX * this._width - pt.x;
this._pivotOffsetY = this._pivotY * this._height - pt.y;
}
else {
this._pivotOffsetX = 0;
this._pivotOffsetY = 0;
}
}
}
FGUI的軸心點(diǎn)和Laya的有一點(diǎn)區(qū)別,如果設(shè)置一個(gè)120*120的圖片的軸心點(diǎn)為中心時(shí)Laya應(yīng)設(shè)置為(60烁竭,60)菲茬,F(xiàn)GUI應(yīng)設(shè)置為(0.5,0.5)颖变。并且FGUI中不允許不設(shè)置軸心點(diǎn)的情況下直接設(shè)置錨點(diǎn)生均。
GComponent
FGUI中GComponent是非常常用的基礎(chǔ)容器,可以包含多個(gè)基礎(chǔ)顯示對(duì)象或者包含其他GComponent腥刹。例如按鈕马胧,列表,進(jìn)度條等UI組件也都是繼承自GComponent衔峰。GComponent是一個(gè)容器佩脊,所以包含_children屬性,可以被當(dāng)成父節(jié)點(diǎn)垫卤,GObject則不可以威彰。
GComponent的使用
- 創(chuàng)建
const testCom = fairygui.UIPackage.createObject('包名','組件名').asCom
- 銷毀
testCom.dispose();
-
操作子節(jié)點(diǎn)(子節(jié)點(diǎn)相關(guān)的方法有很多,看下源碼吧)
GComponent.png
其中包括按名稱,按zIndex,按ID等獲取子節(jié)點(diǎn)穴肘,添加子節(jié)點(diǎn)歇盼,刪除子節(jié)點(diǎn),交換子節(jié)點(diǎn)的zIndex等方法评抚。
實(shí)例運(yùn)用中我習(xí)慣將每個(gè)獨(dú)立的頁面創(chuàng)建成GComponent豹缀。項(xiàng)目開發(fā)中很多UI是以非全屏彈框的形式展示,F(xiàn)GUI中的窗口系統(tǒng)可以實(shí)現(xiàn)彈框慨代,但是個(gè)人覺得使用的限制比較多邢笙,在美術(shù)人員把非全屏改全屏或者全屏改非全屏的時(shí)候修改的地方比較多。