25ea9496-df8d-4783-9850-f431d0c5c925.js
6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"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();