35 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-11-23 09:01:01 +01:00
import { Animation, Component, _decorator } from "cc";
2022-12-16 11:49:52 +01:00
import { UIButton } from "../UI/Button/UIButton";
2022-11-17 18:28:39 +01:00
import { delay } from "../Utils/AsyncUtils";
2022-11-23 09:01:01 +01:00
const { property } = _decorator;
2022-11-17 18:28:39 +01:00
export abstract class ModalWindow<TParam, TResult> extends Component {
2022-11-23 09:01:01 +01:00
@property(Animation) private animation: Animation;
2022-12-16 11:49:52 +01:00
@property(UIButton) private closeButton: UIButton;
@property(UIButton) private backgroundCloseButton: UIButton;
2022-11-23 09:01:01 +01:00
2022-11-17 18:28:39 +01:00
private result: TResult;
private isDismissed = false;
public async runAsync(params?: TParam): Promise<TResult> {
2022-12-16 11:49:52 +01:00
this.closeButton?.InteractedEvent.on(() => this.dismiss(), this);
this.backgroundCloseButton?.InteractedEvent.on(() => this.dismiss(), this);
2022-11-17 18:28:39 +01:00
this.setup(params);
2022-11-23 09:01:01 +01:00
this.animation?.play("open");
2022-11-17 18:28:39 +01:00
while (!this.isDismissed) await delay(100);
2022-11-23 09:01:01 +01:00
this.animation?.play("close");
2022-11-17 18:28:39 +01:00
2022-11-23 09:01:01 +01:00
await delay(500);
2022-11-17 18:28:39 +01:00
return this.result;
}
protected abstract setup(params?: TParam): void;
protected dismiss(result?: TResult): void {
this.result = result;
this.isDismissed = true;
}
}