MainViewImpl.ts 9.55 KB
import { DummyLabel, DummyNode, DummySprite } from "../../../../common/CCDummyObjects";
import { CCPureView, CCView } from "../../../../common/classbase/CCViewBase";
import { View } from "../../../../common/classbase/ViewBase";
import CCGameCenterNodeFactory from "../../../../cooperation/script/CCGameCenterNodeFactory";
import { channel, GameConfig } from "../../../../GameConfig";
import { DirectionType, MainTabs } from "../../../Enums";
import { UnlockItemModelManager } from "../../../model/UnlockItemModelManager";
import UnlockSpecialPlotEventManager from "../../../model/UnlockSpecialPlotEventManager";
import { GuideViewParamModel } from "../../presenter/GuideViewPresenter";
import { RegView } from "../../PresenterCCViewFactory";
import { MainView, MainViewProps, MainViewType } from "../type/MainView";
const { ccclass, property } = cc._decorator;

@ccclass
@RegView(MainViewType, "prefab/ui/MainView")
export class MainViewImpl extends CCPureView<MainViewProps> implements MainView {

    @property({ type: cc.Node })
    private contentParent: cc.Node = DummyNode;
    @property({ type: cc.Sprite })
    private titleSprite: cc.Sprite = DummySprite;

    @property({ type: cc.Node })
    private buttonLayout: cc.Node = DummyNode;
    @property({ type: [cc.Button], displayName: "Tabbar Buttons" })
    private tabbarButtons: cc.Button[] = [];
    @property({ type: [cc.SpriteFrame], displayName: "Tabbar Spriteframes" })
    private tabbarSpriteFrames: cc.SpriteFrame[] = [];
    @property({ type: [cc.SpriteFrame], displayName: "Title Spriteframes" })
    private titleSpriteFrames: cc.SpriteFrame[] = [];

    @property({ type: [sp.Skeleton], displayName: "Click skeleton ani array" })
    private clickAniArr: sp.Skeleton[] = [];

    @property(cc.Node)
    debugbtn: cc.Node = DummyNode;

    @property(cc.Node)
    gameCenterBtn: cc.Node = DummyNode;
    @property(cc.Node)
    urgentNoticeBtn: cc.Node = DummyNode;
    @property(cc.Node)
    goToShopBtn: cc.Node = DummyNode;
    @property(cc.Node)
    commonProblemBtn: cc.Node = DummyNode;
    @property(cc.Node)
    rewardBtn: cc.Node = DummyNode;
    @property(cc.Node)
    rewardRedDotNode: cc.Node = DummyNode;
    @property(cc.Node)
    waitNode: cc.Node = DummyNode;

    @property(cc.Node)
    subGameNode: cc.Node = DummyNode;

    @property(cc.Label)
    loadingLabel: cc.Label = DummyLabel;
    @property(cc.Node)
    downButton: cc.Node = DummyNode;
    @property(cc.Node)
    buttonsNode: cc.Node = DummyNode;

    onTabButtonClick(event, index: string) {
        // this.showTabBar(false);
        this._props.onTabClick(parseInt(index), () => {
            // this.showTabBar(true);
        });
        this.hideClickAniByIndex(parseInt(index));
    }

    private _tabContentContainer: View;
    private _dotNumber = 1;
    private _isdown = true;


    onLoad() {
        this.changeLoadingLabel();
        this._tabContentContainer = this.contentParent.addComponent(CCView);

        this.bindProp("messageRedDot", (value) => {
            this.tabbarButtons[MainTabs.messageTab].node.getChildByName("RedDot").active = value;
            if (this._props.selectedTab !== 0 && value) {
                this.clickAniArr[MainTabs.messageTab].node.active = true;
            } else {
                this.clickAniArr[MainTabs.messageTab].node.active = false;
            }
        });
        this.bindProp("datingEventRedDot", (value) => {
            this.tabbarButtons[MainTabs.datingEventTab].node.getChildByName("RedDot").active = value;
            if (this._props.selectedTab !== MainTabs.datingEventTab && value) {
                this.clickAniArr[MainTabs.datingEventTab].node.active = true;
            } else {
                this.clickAniArr[MainTabs.datingEventTab].node.active = false;
            }
        });

        this.bindProp("waitRedDot", (value) => {
            this.tabbarButtons[MainTabs.bedroomTab].node.getChildByName("RedDot").active = value;
        });

        //修改主界面发现按钮为永久显示红点
        //this.bindProp("discoverRedDot", this.tabbarButtons[MainTabs.discoverTab].node.getChildByName("RedDot"), "active");
        this.bindProp("discoverRedDot", (value) => {
            this.tabbarButtons[MainTabs.discoverTab].node.getChildByName("RedDot").active = true;
        });

        this.debugbtn.active = GameConfig.debug;

        if (channel !== "android") {
            this.gameCenterBtn.active = true;
            this.subGameNode.active = true;
            CCGameCenterNodeFactory.getInstance().registerMainUISubGameNode(this.subGameNode);
        } else {
            this.gameCenterBtn.active = false;
            this.subGameNode.active = false;
        }
        this.urgentNoticeBtn.active = false;
    }

    getTabContentContainer(): View {
        return this._tabContentContainer;
    }

    onPropsLoad(props: Readonly<MainViewProps>) {
        super.onPropsLoad(props);
        let currTab = this._props.selectedTab;
        this.tabbarButtons[currTab].normalSprite = this.tabbarSpriteFrames[currTab * 2 + 1];
        this.titleSprite.spriteFrame = this.titleSpriteFrames[currTab];
    }

    onPropChange(key: Extract<keyof MainViewProps, string>) {
        super.onPropChange(key);
        if (key === "selectedTab") {
            let prevTab = this._prevProps.selectedTab;
            this.tabbarButtons[prevTab].normalSprite = this.tabbarSpriteFrames[prevTab * 2];
            let currTab = this._props.selectedTab;
            this.tabbarButtons[currTab].normalSprite = this.tabbarSpriteFrames[currTab * 2 + 1];
            this.titleSprite.spriteFrame = this.titleSpriteFrames[currTab];
        }
    }


    onDebugClickCallback(event) {
        this._props.onDebugClick();
    }

    getGuideTabParam(index: number): GuideViewParamModel | undefined {
        if (index < 0 || this._props.selectedTab === index) {
            return undefined;
        }
        let node = this.tabbarButtons[index].node;
        let wp = node.convertToWorldSpaceAR(cc.v2(0, 0));
        let m: GuideViewParamModel = {
            x: wp.x,
            y: wp.y,
            w: node.width,
            h: node.height,
            d: DirectionType.DOWM,
        }
        return m;
    }

    hideClickAniByIndex = (index: number) => {
        if (index < 0 || index > this.clickAniArr.length) {
            return;
        }
        let limitArr: MainTabs[] = [MainTabs.messageTab, MainTabs.datingEventTab, MainTabs.bedroomTab];
        let contrlArr: boolean[] = [this._props.messageRedDot, this._props.datingEventRedDot];
        let exist = limitArr.findIndex((v) => v === index) !== (-1);
        if (exist) {
            for (let i = 0; i < limitArr.length; i++) {
                this.clickAniArr[limitArr[i]].node.active = (limitArr[i] !== index) && contrlArr[i];
            }
        } else {
            for (let i = 0; i < limitArr.length; i++) {
                this.clickAniArr[limitArr[i]].node.active = contrlArr[i];
            }
        }
    }

    showRewardBtn = (isShow: boolean) => {
        // this.urgentNoticeBtn.active = isShow;
        this.goToShopBtn.active = isShow;
        if (!this._isdown) {
            this.downButton.active = isShow;
        }
    }

    showBottonsNode = (isShow: boolean) => {
        this.buttonsNode.active = isShow;
        if (this._isdown) {
            this.buttonsNode.active = isShow;
        }
    }

    showSubGameNode = (isShow: boolean) => {
        if (channel !== "android") {
            this.subGameNode.active = isShow;
        } else {
            this.subGameNode.active = false;
        }
    }

    getRewardRedDotNode(): cc.Node {
        return this.rewardRedDotNode;
    }

    onUrgentNoticeClickCallback(event) {
        this._props.onUrgentNoticeClick();
    }

    onGoToShopClickCallback(event) {
        this._props.onGoToShopClick();
    }

    onCommonProblemClickCallback(event) {
        this._props.onCommonProblemClick();
    }

    onRewardBtnClickCallback(event) {
        this._props.onRewardBtnClickCallback();
    }

    updateBedRoomRedDot() {
        let showRedDot = false;
        let allItemIds: Array<number> = [];
        allItemIds = UnlockItemModelManager.unlockItems;
        for (let i = 0; i < allItemIds.length; i++) {
            if (UnlockSpecialPlotEventManager.getInstance().isShowBedRoomRedDotStatus(allItemIds[i])) {
                showRedDot = true;
                break;
            }
        }
        this.tabbarButtons[MainTabs.bedroomTab].node.getChildByName("RedDot").active = showRedDot;
    }

    showTabBar(isShow: boolean) {
        this.node.getChildByName("TabBar").active = isShow;
    }

    showWaitingView(isShow: boolean) {
        this.waitNode.active = isShow;
    }

    changeLoadingLabel() {
        this.loadingLabel.schedule(() => {
            if (this._dotNumber >= 3) {
                this._dotNumber = 1;
            } else {
                this._dotNumber++;
            }
            let dot = "";
            for (let i = 0; i < this._dotNumber; i++) {
                dot += "."
            }
            this.loadingLabel.string = "正在努力加载(ง ˙o˙)ว!请耐心等待" + dot;
        }, 0.5);
    }

    onDownButtonClick() {
        this._isdown = true;
        this.downButton.active = false;
        this.buttonsNode.active = true;
        this.buttonsNode.scaleY = 0;
        cc.tween(this.buttonsNode)
            .to(0.2, { scaleY: 1 })
            .call(() => {
            })
            .start();
    }

    onUpButtonClick() {
        this._isdown = false;
        cc.tween(this.buttonsNode)
            .to(0.2, { scaleY: 0 })
            .call(() => {
                this.downButton.active = true;
            })
            .start();
    }
}