25ea9496-df8d-4783-9850-f431d0c5c925.js 6.48 KB
"use strict";
cc._RF.push(module, '25ea9SW341Hg5hQ9DHQxckl', 'Typewriter');
// script/common/components/Typewriter.ts

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
const { ccclass, property, menu } = cc._decorator;
let Typewriter = /** @class */ (() => {
    let Typewriter = class Typewriter extends cc.Component {
        constructor() {
            super(...arguments);
            this.typerTimer = null; // 计时器Id
            this.showingString = "";
        }
        onLoad() {
        }
        clearParam() {
            this.showingString = "";
            this.showingNode = undefined;
            this.finishCallFunc = undefined;
        }
        onDisable() {
            this.clearParam();
            this.clearTimer();
        }
        onDestroy() {
            this.clearParam();
            this.clearTimer();
        }
        clearTimer() {
            // Destroy前确保定时器关闭
            this.typerTimer && clearInterval(this.typerTimer);
        }
        showLabelTyper(label, str) {
            label.string = '';
            this.showingString = str;
            this.showingNode = label;
            this.makeLaberTyper(label, str);
        }
        showRichTextTyper(richText, str, finishCallFunc, showIngCallFunc, playSpeed = 1) {
            richText.string = '';
            this.showingString = str;
            this.showingNode = richText;
            this.finishCallFunc = finishCallFunc;
            this._makeRichTextTyper(richText, str, finishCallFunc, showIngCallFunc, playSpeed);
        }
        stopTyperShowString() {
            this.clearTimer();
            this.showingNode.string = this.showingString;
            if (this.finishCallFunc) {
                this.finishCallFunc();
            }
            this.clearParam();
        }
        getShowingString() {
            return this.showingString;
        }
        makeLaberTyper(label, str) {
            let charArr = str.split('');
            let charIdx = 0;
            this.clearTimer();
            this.typerTimer = setInterval(() => {
                if (charIdx >= charArr.length) {
                    this.clearTimer();
                    this.showingString = "";
                    this.showingNode = undefined;
                }
                else {
                    charIdx += 1;
                    label.string = charArr.slice(0, charIdx).join('');
                }
            }, 50);
        }
        _makeRichTextTyper(richText, str, finishCallFunc, showIngCallFunc, playSpeed = 1) {
            let delimiterCharList = ['✁', '✂', '✃', '✄', '✺', '✻', '✼', '❄', '❅', '❆', '❇', '❈', '❉', '❊'];
            let regexp = /<.+?\/?>/g;
            let matchArr = str.match(regexp);
            let delimiterChar = delimiterCharList.find((item) => str.indexOf(item) == -1);
            let replaceStr = str.replace(regexp, delimiterChar);
            let tagInfoArr = [];
            let temp = [];
            let tagInfo = {};
            let num = 0;
            for (let i = 0; i < replaceStr.length; i++) {
                if (replaceStr[i] == delimiterChar) {
                    temp.push(i);
                    if (temp.length >= 2) {
                        if (matchArr)
                            tagInfo.endStr = matchArr[tagInfoArr.length * 2 + 1];
                        tagInfo.endtIdx = i - num;
                        tagInfoArr.push(tagInfo);
                        temp = [];
                        tagInfo = {};
                    }
                    else {
                        tagInfo.startIdx = i - num;
                        if (matchArr)
                            tagInfo.startStr = matchArr[tagInfoArr.length * 2];
                    }
                    num += 1;
                }
            }
            let showCharArr = str.replace(regexp, '').split('');
            let typerArr = [];
            for (let i = 1; i <= showCharArr.length; i++) {
                let temp = showCharArr.join('').slice(0, i);
                let addLen = 0;
                for (let j = 0; j < tagInfoArr.length; j++) {
                    let tagInfo = tagInfoArr[j];
                    let start = tagInfo.startIdx;
                    let end = tagInfo.endtIdx;
                    if (i > start && i <= end) {
                        temp = temp.slice(0, start + addLen) + tagInfo.startStr + temp.slice(start + addLen) + tagInfo.endStr;
                        addLen += tagInfo.startStr.length + tagInfo.endStr.length;
                    }
                    else if (i > end) {
                        temp =
                            temp.slice(0, start + addLen) +
                                tagInfo.startStr +
                                temp.slice(start + addLen, end + addLen) +
                                tagInfo.endStr +
                                temp.slice(end + addLen);
                        addLen += tagInfo.startStr.length + tagInfo.endStr.length;
                    }
                }
                typerArr.unshift(temp);
            }
            this.clearTimer();
            this.typerTimer = setInterval(() => {
                if (typerArr.length) {
                    // this.isShowTyper = true;
                    if (richText) {
                        let str = typerArr.pop();
                        richText.string = str ? str : "";
                        if (showIngCallFunc)
                            showIngCallFunc();
                    }
                }
                else {
                    this.showingString = "";
                    this.showingNode = undefined;
                    this.clearTimer();
                    if (finishCallFunc)
                        finishCallFunc();
                }
            }, 50 / playSpeed);
        }
    };
    Typewriter = __decorate([
        ccclass,
        menu("自定义UI组件/Typewriter")
    ], Typewriter);
    return Typewriter;
})();
exports.default = Typewriter;

cc._RF.pop();