ExtraPlotViewPresenter.ts 6.24 KB
import { ConfigManager } from "simba-config-manager";
import { SDK } from "simba-sdk";
import { DateType } from "../../../avg/EditorEnums";
import { Presenter } from "../../../common/classbase/PresenterBase";
import { UIManager } from "../../../common/gameplay/managers/UIManager";
import { dateSceneConfig } from "../../../config/DateSceneConfig";
import { channel, GameConfig } from "../../../GameConfig";
import { DatingEventStatus } from "../../model/DatingEventSceneModel";
import { ExtraStoryModelManager } from "../../model/ExtraStoryModelManager";
import { GameModelManager } from "../../model/GameModelManager";
import { RegPresenter } from "../PresenterCCViewFactory";
import { ExtraPlotViewType, ExtraPlotViewProps, ExtraPlotView, ExtraPlotViewState } from "../view/type/ExtraPlotView";

@RegPresenter(ExtraPlotViewType)
export default class ExtraPlotViewPresenter extends Presenter<undefined, ExtraPlotView>{
    static uuid = "ExtraPlotViewPresenter";
    private _viewProps: ExtraPlotViewProps;
    private _tabbarId: number = 0;
    private _markButtonsY: number[] = [];
    private _viewState: ExtraPlotViewState;

    constructor() {
        super();
        this._viewProps = {
            onMarkButtonClickCallback: this.onMarkButtonClickCallback,
            onCloseButtonClickCallback: this.onCloseButtonClickCallback,
            onGotoShopButtonClickCallback: this.onGotoShopButtonClickCallback,
        }
    }

    onOpen() {
        super.onOpen(undefined);
        this.view.setProps(this._viewProps);
        this.onShow();
    }

    onShow() {
        super.onShow();
        if (this._viewState == ExtraPlotViewState.List) {
            GameModelManager.RefreshExtraPlotByType.emit(this._tabbarId);
        } else {
            this.initViewDate();
        }
    }

    onEnterBackground() {
        super.onEnterBackground();
    }

    onEnterForeground() {
        super.onEnterForeground();
    }

    onClose() {
        super.onClose();
    }

    initViewDate() {
        this._viewState = ExtraPlotViewState.Choose;
        this.setMarkButtonsPosition();
        this.dropAction();
        this.updateRedDot();
    }

    setMarkButtonsPosition() {
        for (let i = 0; i < this.view.getMarkButtonNodes().length; i++) {
            let y = this.view.getMarkButtonNodes()[i].y;
            if (!this._markButtonsY[i]) {
                this._markButtonsY.push(y);
            }
            this.view.getMarkButtonNodes()[i].y = this._markButtonsY[i] + cc.winSize.height;
        }
    }

    dropAction() {
        this.view.getMaskNode().active = true;
        GameModelManager.RefreshExtraPlotByType.emit(0);
        for (let i = 0; i < this.view.getMarkButtonNodes().length; i++) {
            let node = this.view.getMarkButtonNodes()[i];
            cc.tween(node)
                .to(0.8 + i * 0.1, { y: this._markButtonsY[i] }, { easing: 'backOut' })
                .call(() => {
                    if (i == (this.view.getMarkButtonNodes().length - 1)) {
                        this.view.getMaskNode().active = false;
                    }
                })
                .start();
        }
    }

    risingAction() {
        this.view.getMaskNode().active = true;
        for (let i = 0; i < this.view.getMarkButtonNodes().length; i++) {
            let node = this.view.getMarkButtonNodes()[i];
            cc.tween(node)
                .to(0.8 + i * 0.1, { y: this._markButtonsY[i] + cc.winSize.height }, { easing: 'backIn' })
                .call(() => {
                    if (i == (this.view.getMarkButtonNodes().length - 1)) {
                        GameModelManager.RefreshExtraPlotByType.emit(this._tabbarId);
                        this.view.getMaskNode().active = false;
                    }
                })
                .start();
        }
    }

    async updateRedDot() {
        for (let i = 0; i < this.view.getMarkButtonNodes().length; i++) {
            let node = this.view.getMarkButtonNodes()[i].getChildByName("RedDotSprite");
            node.active = false;
        }
        let cfg = ConfigManager.getAllConfig(dateSceneConfig);
        for (const id in cfg) {
            for (let i = 0; i < this.view.getMarkButtonNodes().length; i++) {
                if (cfg[id].DateType == DateType.Date_Sp && cfg[id].chapter_index == (i + 1)) {
                    await ExtraStoryModelManager.startBranches([cfg[id].start_plot_id]);
                    let branchStatus = ExtraStoryModelManager.getBranchStatus(Number(id), cfg[id].start_plot_id);
                    if (branchStatus == DatingEventStatus.New || branchStatus == DatingEventStatus.InProgress) {
                        let node = this.view.getMarkButtonNodes()[i].getChildByName("RedDotSprite");
                        node.active = true;
                    }
                }
            }
        }
    }

    onMarkButtonClickCallback = (tabbarId: number) => {
        let cfg = ConfigManager.getAllConfig(dateSceneConfig);
        let index: number[] = [];
        for (const id in cfg) {
            if (cfg[id].DateType == DateType.Date_Sp) {
                index.push(cfg[id].chapter_index);
            }
        }
        if (index.indexOf(tabbarId) == -1) {
            UIManager.showToast("敬请期待");
            return;
        }
        this._viewState = ExtraPlotViewState.List;
        this._tabbarId = tabbarId;
        this.risingAction();
    }

    onGotoShopButtonClickCallback = () => {
        if (channel == "wechat") {
            // GameCenter.getInstance().openSubProgram(GameConfig.youZanShopAppId);
            let link = GameModelManager.getYouZanShopUrlByType(8);
            if (link) {
                SDK.openProgramV2(GameConfig.youZanShopAppId, link);
            }
        } else if (channel == "android") {
            let link = GameModelManager.getTaoBaoShopUrlByType(8);
            if (link) {
                GameModelManager.jumpToTaobaoShop(link);
            }
        } else {
            UIManager.showToast("该功能在当前平台不可使用");
        }
    }

    onCloseButtonClickCallback = async () => {
        if (this._viewState == ExtraPlotViewState.List) {
            this.initViewDate();
        } else {
            GameModelManager.CloseExtraSceneView.emit();
            GameModelManager.setIsMainPlotSceneType(false);
            this.view.close();
        }
    }
}