2022-11-23 08:01:01 +00:00
|
|
|
import { Animation, Component, _decorator } from "cc";
|
2022-12-16 10:49:52 +00:00
|
|
|
import { UIButton } from "../UI/Button/UIButton";
|
2022-11-17 17:28:39 +00:00
|
|
|
import { delay } from "../Utils/AsyncUtils";
|
|
|
|
|
2022-11-23 08:01:01 +00:00
|
|
|
const { property } = _decorator;
|
|
|
|
|
2022-11-17 17:28:39 +00:00
|
|
|
export abstract class ModalWindow<TParam, TResult> extends Component {
|
2022-11-23 08:01:01 +00:00
|
|
|
@property(Animation) private animation: Animation;
|
2022-12-16 10:49:52 +00:00
|
|
|
@property(UIButton) private closeButton: UIButton;
|
|
|
|
@property(UIButton) private backgroundCloseButton: UIButton;
|
2022-11-23 08:01:01 +00:00
|
|
|
|
2022-11-17 17:28:39 +00:00
|
|
|
private result: TResult;
|
|
|
|
private isDismissed = false;
|
|
|
|
|
2023-01-30 13:25:43 +00:00
|
|
|
private openAnimationName = "open";
|
|
|
|
private closeAnimationName = "close";
|
|
|
|
|
2022-11-17 17:28:39 +00:00
|
|
|
public async runAsync(params?: TParam): Promise<TResult> {
|
2022-12-16 10:49:52 +00:00
|
|
|
this.closeButton?.InteractedEvent.on(() => this.dismiss(), this);
|
|
|
|
this.backgroundCloseButton?.InteractedEvent.on(() => this.dismiss(), this);
|
|
|
|
|
2022-11-17 17:28:39 +00:00
|
|
|
this.setup(params);
|
2023-01-30 13:25:43 +00:00
|
|
|
this.animation?.play(this.openAnimationName);
|
2022-11-17 17:28:39 +00:00
|
|
|
while (!this.isDismissed) await delay(100);
|
2023-01-30 13:25:43 +00:00
|
|
|
this.animation?.play(this.closeAnimationName);
|
2022-11-17 17:28:39 +00:00
|
|
|
|
2023-01-30 13:25:43 +00:00
|
|
|
await delay(this.getCloseAnimationTime() * 1000);
|
2022-11-17 17:28:39 +00:00
|
|
|
return this.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract setup(params?: TParam): void;
|
|
|
|
|
|
|
|
protected dismiss(result?: TResult): void {
|
|
|
|
this.result = result;
|
|
|
|
this.isDismissed = true;
|
|
|
|
}
|
2023-01-30 13:25:43 +00:00
|
|
|
|
|
|
|
private getCloseAnimationTime(): number {
|
|
|
|
const state = this.animation?.getState(this.closeAnimationName);
|
|
|
|
if (state != null) {
|
|
|
|
return state.duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2022-11-17 17:28:39 +00:00
|
|
|
}
|