﻿/** 
 * @Author       : yuanqi.bai
 * @Date         : 2023-04-17 15:26:42
 * @LastEditors  : yuanqi.bai
 * @LastEditTime : 2023-04-17 16:23:58
 * @FilePath     : \布娃娃和冲量\JavaScripts\GuideRollTxtUI.ts
 * @Description  : 修改描述
 */


import GuideRollTxt_Generate from "./ui-generate/GuideRollTxt_generate";

export default class GuideRollTxtUI extends GuideRollTxt_Generate {
    //缓存的string
    private _cacheTxt: string = "";
    //缓存的string的下标
    private txtIndex: number = 0;
    //期望大小
    private desieredSize: Vector2;
    //期望位置
    private desierdePose: Vector2 = new Vector2();

    onStart() {
        this.mButtonBack.onClicked.add(() => {
            UIService.hideUI(this)
        })
    }
    onHide() {
        //这里隐藏，防止再次打开的时候露底
        this.imgBack.visibility = mw.SlateVisibility.Collapsed;

    }
    onShow(txt: string) {

        this._cacheTxt = txt;
        this.txtContext.text = "";
        this.txtIndex = 0;
        this.calSize();
    }
    /**
     * 预估尺寸和位置
     */
    private calSize() {
        this.txtPreCal.text = this._cacheTxt;
        //期望大小是在渲染之后计算出来的数据，做一个缓存，不然获得的数据不准。
        setTimeout(() => {
            this.desieredSize = this.txtPreCal.desiredSize;
            let piovtPose = this.txtContext.position;
            let pivotSize = this.txtContext.size;
            let offx = piovtPose.x - (this.desieredSize.x - pivotSize.x) / 2;
            let offy = piovtPose.y;
            this.desierdePose.set(offx, offy);
            this.imgBack.size = this.desieredSize;
            this.imgBack.position = this.desierdePose;
            this.canUpdate = true;
            this.imgBack.visibility = mw.SlateVisibility.Visible;

        }, 100);
    }
    /**
     * 每帧更新一个文字内容
     * @param dt 
     * @returns 
     */
    onUpdate(dt: number) {
        for (; this.txtIndex < this._cacheTxt.length;) {
            this.txtContext.text += this._cacheTxt[this.txtIndex];
            ++this.txtIndex
            return;
        }
        this.canUpdate = false;
    }
}