MainInfoSubviewImpl.ts 2.21 KB
import { DummyLabel, DummySprite } from "../../../../common/CCDummyObjects";
import { CCPureSubview } from "../../../../common/classbase/CCViewBase";
import { RegSubview } from "../../PresenterCCViewFactory";
import { MainInfoSubviewProps, MainInfoSubviewType } from "../type/MainInfoSubviewiew";

const { ccclass, property } = cc._decorator;




@ccclass
@RegSubview(MainInfoSubviewType)
export class MainInfoViewImpl extends CCPureSubview<MainInfoSubviewProps> {
    //#region  editor bindings
    @property(cc.Label)
    private timeLabel: cc.Label = DummyLabel;
    @property(cc.Sprite)
    private energySprite: cc.Sprite = DummySprite;
    @property(cc.Label)
    private energyLabel: cc.Label = DummyLabel;

    @property(cc.SpriteFrame)
    private energySprites: cc.SpriteFrame[] = [];
    @property(cc.Label)
    private coolddown: cc.Label = DummyLabel;
    //#endregion



    onLoad() {
        this.updateTime();
        this.energySprite.type = cc.Sprite.Type.FILLED;
        this.energySprite.fillType = cc.Sprite.FillType.VERTICAL;

        this.bindProp("energycontent", (value) => {
            this.energyLabel.string = value;
        });

        this.bindProp("timecontent", (value) => {
            if (value === "") {
                this.coolddown.node.active = false;
            } else {
                this.coolddown.node.active = true;
                this.coolddown.string = value;
            }
        });

        this.bindProp("energybackindex", (value) => {
            this.energySprite.spriteFrame = this.energySprites[value];
        });

        this.bindProp("energy", (value) => {
            this.energySprite.fillRange = value;
        });

        this.energySprite.node.on(cc.Node.EventType.TOUCH_END, this.onEnergyClickCallback, this);
        this.energyLabel.node.on(cc.Node.EventType.TOUCH_END, this.onEnergyClickCallback, this);
    }



    onEnergyClickCallback = () => {
        this._props.onClick();
    }

    private updateTime = () => {
        let now = new Date;
        this.timeLabel.string = `${now.getHours()}:${(now.getMinutes() < 10 ? '0' : '') + now.getMinutes()}`;
        this.scheduleOnce(() => {
            this.scheduleOnce(this.updateTime, 60 - now.getSeconds());
        });
    }


}