183 lines
4.8 KiB
TypeScript
Raw Normal View History

2022-08-26 16:48:17 +08:00
import { CoroutineV2 } from "../../CatanEngine/CoroutineV2/CoroutineV2";
import ScreenResize from "./ScreenResize";
import SwitchActiveGroup from "./SwitchShowUI/SwitchActiveGroup";
import SwitchImgGroup from "./SwitchShowUI/SwitchImgGroup";
import SwitchPositionGroup from "./SwitchShowUI/SwitchPositionGroup";
import SwitchRotationGroup from "./SwitchShowUI/SwitchRotationGroup";
import SwitchScaleGroup from "./SwitchShowUI/SwitchScaleGroup";
import SwitchWHGroup from "./SwitchShowUI/SwitchWHGroup";
import { UIManager } from "./UIManager";
const { ccclass } = cc._decorator;
@ccclass
export default class UIPanel extends cc.Component {
/**[基底UIPanel]等待跳出旗標*/
private _isWaiting: boolean = false;
/**
* [UIPanel](UI每次創建時呼叫, )
* @param initData
*/
protected ImplementInitial(...initData: any[]): void {
}
/**
* [UIPanel](UI顯示前呼叫)
* @param param
*/
protected *ImplementReadyShow(...param: any[]): IterableIterator<any> {
}
/**
* [UIPanel](UI顯示呼叫)
* @param param
*/
protected *ImplementShow(): IterableIterator<any> {
}
/**
* [UIPanel]()
* @param param
*/
protected *ImplementHide(...param: any[]): IterableIterator<any> {
}
/**
* [UIPanel]
*/
protected ImplementDestroy() {
}
/**
* [UIPanel]
* @param param
*/
public ChangeDire(param: any[] = null): void {
if (!cc.isValid(this)) {
return;
}
cc.log("ChangeDire:" + this.name);
if (this.getComponent(SwitchScaleGroup)) {
this.getComponent(SwitchScaleGroup).Run();
}
if (this.getComponent(SwitchPositionGroup)) {
this.getComponent(SwitchPositionGroup).Run();
}
if (this.getComponent(SwitchWHGroup)) {
this.getComponent(SwitchWHGroup).Run();
}
if (this.getComponent(SwitchImgGroup)) {
this.getComponent(SwitchImgGroup).Run();
}
if (this.getComponent(SwitchActiveGroup)) {
this.getComponent(SwitchActiveGroup).Run();
}
if (this.getComponent(SwitchRotationGroup)) {
this.getComponent(SwitchRotationGroup).Run();
}
this._uiMaskHandler();
}
private _uiMaskHandler(): void {
let mask: cc.Node = this.node.getChildByName("Mask");
if (mask && this.node.parent.name == "ElementContent") {
let size: cc.Vec2 = ScreenResize.CanvasSize[ScreenResize.IsPortrait];
mask.SetSizeDelta(cc.v2(size.x / UIManager.ScreenScale, size.y / UIManager.ScreenScale));
}
}
//=======================================================================================
/**
* []UI
* @param source
* @param parent
* @param data
*/
public static CreateUI(source: cc.Prefab, parent: cc.Node, ...data: any[]): UIPanel {
let node = parent.ExAddChild(source);
let script = node.getComponent(UIPanel);
script.Initial(...data);
return script;
}
/**
* []
* @param initData
*/
public Initial(...initData: any[]): void {
UIManager.DireEvent.RemoveByBindTarget(this);
UIManager.DireEvent.AddCallback(this.ChangeDire, this);
this.node.active = false;
this._uiMaskHandler();
this.ImplementInitial(...initData);
}
/**
* []UI
* @param param
*/
public *Show(...param: any[]): IterableIterator<any> {
yield* this.ImplementReadyShow(...param);
this.node.active = true;
yield* this.ImplementShow();
}
/**
* []UI
* @param param
*/
public *Hide(...param: any[]): IterableIterator<any> {
if (this._isWaiting) {
cc.warn(this.node.name, "_isWaiting = true無法關閉");
}
else {
yield* this.ImplementHide(...param);
if (this && this.node) {
this.node.active = false;
}
}
}
/**
* []UI
* @param showData
* @param hideData
*/
public *Wait(showData: any[] = [], hideData: any[] = []): IterableIterator<any> {
yield* this.Show(...showData);
this._isWaiting = true;
while (this._isWaiting) {
yield null;
}
yield* this.Hide(...hideData);
}
/**
* []UI
* @param param
*/
public Close(...param: any[]): void {
if (this._isWaiting) {
this._isWaiting = false;
}
else {
CoroutineV2.Single(this.Hide(...param)).Start();
}
}
//=======================================================================================
onLoad() {
UIManager.DireEvent.RemoveByBindTarget(this);
UIManager.DireEvent.AddCallback(this.ChangeDire, this);
}
//COCOS引擎內建生命週期
//onLoad->start->update->lateUpdate->onDestroy->onEnable->onDisable
onDestroy() {
UIManager.DireEvent.RemoveByBindTarget(this);
this.ImplementDestroy();
}
//=======================================================================================
}