SpecialPlotViewPresenter.ts 6.4 KB
import { ConfigManager } from "simba-config-manager";
import { SDK } from "simba-sdk";
import { DeepReadonlyObject } from "simba-utils";
import { Presenter } from "../../../common/classbase/PresenterBase";
import { UIManager } from "../../../common/gameplay/managers/UIManager";
import { IStoryTbl, storyTbl } from "../../../config/StoryTbl";
import GameCenter from "../../../cooperation/script/GameCenter";
import { channel, GameConfig } from "../../../GameConfig";
import GameDotMgr from "../../GameDotMgr";
import { GameModelManager } from "../../model/GameModelManager";
import UnlockSpecialPlotEventManager from "../../model/UnlockSpecialPlotEventManager";
import { UnlockSpecialPlotModelManager } from "../../model/UnlockSpecialPlotModelManager";
import { RegPresenter } from "../PresenterCCViewFactory";
import { SpecialPlotViewType, SpecialPlotViewProps, SpecialPlotView } from "../view/type/SpecialPlotView";

@RegPresenter(SpecialPlotViewType)
export default class SpecialPlotViewPresenter extends Presenter<SpecialPlotViewProps, SpecialPlotView>
{
    static uuid = "SpecialPlotViewPresenter";

    private _viewProps: SpecialPlotViewProps;

    private _value: number = 0;
    private _cfg: DeepReadonlyObject<IStoryTbl> | undefined = undefined;
    constructor() {
        super();
    }

    onOpen(props: SpecialPlotViewProps) {
        super.onOpen(props);
        this._viewProps = props;
        this._viewProps.onLeftBtnClick = this.onLeftBtnClickCallBack;
        this._viewProps.onRightBtnClick = this.onRightBtnClickCallBack;
        this._viewProps.createGameClubBtn = this.createGameClubBtn;
        this._viewProps.onShopBtnClick = this.onShopBtnClickCallBack;
        this._viewProps.onCloseBtnClick = this.onCloseBtnClick;
        this.view.setProps(this._viewProps);
        this.onShow();
    }

    onShow() {
        super.onShow();
        this.showSpecialPlot(this._viewProps.specialPlotIds[this._value]);
        this.createGameClubBtn();
        this.showGameClubBtn();
    }

    onClose() {
        super.onClose();
        SDK.destroyGameClubButton();
    }

    onEnterBackground() {
        super.onEnterBackground();
        this.hideGameClubBtn();
    }

    onEnterForeground() {
        super.onEnterForeground();
        this.showGameClubBtn();
    }

    showGameClubBtn() {
        SDK.showGameClubButton();
    }

    hideGameClubBtn() {
        SDK.hideGameClubButton();
    }

    showSpecialPlot(specialPlotId: number) {
        try {
            if (specialPlotId) {
                this._cfg = ConfigManager.getConfig(storyTbl, specialPlotId);
                if (this._cfg) {
                    this.view.getSpecialPlotTitleNode()!.getComponent(cc.Label).string = this._cfg.title;
                    let cfgStr: string[] = this._cfg.content.split("%%");
                    let specialPlotNodeStr = "";
                    for (let i = 0; i < cfgStr.length; i++) {
                        specialPlotNodeStr = specialPlotNodeStr + cfgStr[i] + "\n";
                    }
                    this.view.getSpecialPlotNode()!.getComponent(cc.Label).string = specialPlotNodeStr;
                    UnlockSpecialPlotModelManager.saveClickedSpecialPlotIdByItemIdToGameRecord(specialPlotId);
                }
                this.updateLeftOrRightBtnStatus();
                this.updateRightBtnRedDotStatus();
                this.view.getSpecialPlotScrollView().scrollToTop();
            }
        } catch (error) {
            console.error(error);
        }
    }

    updateLeftOrRightBtnStatus() {
        //处理左按钮显隐,右按钮暂不处理
        if (this._value <= 0) {
            this.view.showLeftBtn(false);
        } else {
            this.view.showLeftBtn(true);
        }
    }

    onLeftBtnClickCallBack = () => {
        if (this._value <= 0) {
            return;
        } else {
            this._value -= 1;
            this.showSpecialPlot(this._viewProps.specialPlotIds[this._value]);
        }
    }

    onRightBtnClickCallBack = () => {
        if (this._value >= this._viewProps.specialPlotIds.length - 1) {
            UIManager.showToast(this._cfg!.toastMsg);
            return;
        } else {
            this._value += 1;
            this.showSpecialPlot(this._viewProps.specialPlotIds[this._value]);
        }
    }

    createGameClubBtn = () => {
        this.view.getViewNode()!.getComponent(cc.Widget).updateAlignment();
        let info = SDK.systemInfo.displayInfo;
        let aspect = info.windowSize.height / cc.view.getVisibleSize().height;
        this.view.getViewNode()!.getComponent(cc.Widget).updateAlignment();

        let pos = this.view.getForumBtnNode()!.convertToWorldSpaceAR(cc.v2(0, 0));
        pos.x += cc.view.getViewportRect().x / cc.view.getScaleX();
        let xPos = pos.x * aspect;
        let yPos = info.windowSize.height - pos.y * aspect;
        let width = this.view.getForumBtnNode()!.width * aspect;
        let height = this.view.getForumBtnNode()!.height * aspect;
        let left = xPos - width / 2;
        let top = yPos - height / 2;

        SDK.createGameClubButton({
            type: "text",
            text: "",
            style: {
                left: left,
                top: top,
                width: width,
                height: height
            },
            icon: "green"
        });
    }

    onShopBtnClickCallBack = () => {
        GameDotMgr.getInstance().dotClickUI("shop_btn_specoalPlot");
        let type = GameModelManager.getTypeByBedroomItemId(this._viewProps.itemId);
        if (channel == "wechat") {
            // GameCenter.getInstance().openSubProgram(GameConfig.youZanShopAppId);
            if (type) {
                let link = GameModelManager.getYouZanShopUrlByType(type);
                if (link) {
                    SDK.openProgramV2(GameConfig.youZanShopAppId, link);
                }
            }
        } else if (channel == "android") {
            if (type) {
                let link = GameModelManager.getTaoBaoShopUrlByType(type);
                if (link) {
                    GameModelManager.jumpToTaobaoShop(link);
                }
            }
        } else {
            UIManager.showToast("该功能在当前平台不可使用");
        }
    }

    onCloseBtnClick = () => {
        this.view.close();
        GameModelManager.BackToBedRoom.emit();
    }

    updateRightBtnRedDotStatus() {
        this.view.showRedDot(UnlockSpecialPlotEventManager.getInstance().isShowBedRoomRedDotStatus(this._viewProps.itemId));
    }
}