summaryrefslogtreecommitdiff
path: root/WorldlineKeepers/Assets/ThirdParty/StringUtil/VTypingString.cs
blob: 50b63e6c65b7a67fe50f754e14b4cec78c1c6087 (plain)
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
164
165
166
167
168
169
170
171
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 优化打字功能的字符串
/// </summary>
public class VTypingString
{
    enum Tags
    {
        Color = 0,
        Size,
        B,
        I
    }


    private VString allContentString;
    private DoubleVString partialDisplayString;
    private VString willShowString;

    private int _timerMS;
    private int _factor;
    private int _miniSecondPerWord;

    private static readonly string[] endTags = new string[] { "</color>", "</size>", "</b>", "</i>" };
    private static readonly string[] startTags = new string[] { "<color", "<size", "<b", "<i" };
    
    List<int> endTagCaches = new List<int>();


    /// <summary>
    /// 
    /// </summary>
    /// <param name="text"></param>
    /// <param name="miniSecondPerWord">出一个字符需要的毫秒时间</param>
    public VTypingString(string text, int miniSecondPerWord = 240)
    {
        if(string.IsNullOrEmpty(text))
        {
            throw new ArgumentException("text is null or empty");
        }

        _miniSecondPerWord = Mathf.Max(miniSecondPerWord, 10);
        allContentString = new VString(text.Length);
        allContentString.Push(text);
        partialDisplayString = new DoubleVString(text.Length);
        willShowString = new VString(text.Length);
        JumpToBegin();
    }

    public bool IsEnd()
    {
        return _factor > allContentString.GetString().Length;
    }
        
    public string GetString()
    {
        return partialDisplayString.GetCurrentVString().GetString();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="deltaTimeMS"></param>
    /// <returns>true表示触发了一次打字变化</returns>
    public bool OnUpdate(int deltaTimeMS)
    {
        _timerMS += deltaTimeMS;
        if(_timerMS >= _miniSecondPerWord)
        {
            _timerMS = 0;
            OnTyping();
            return true;
        }
        return false;
    }

    private void OnTyping()
    {
        if (CheckStart(Tags.Color)) { }
        else if (CheckStart(Tags.Size)) { }
        else if (CheckStart(Tags.B)) { }
        else if (CheckStart(Tags.I)) { }
        else
        {
            partialDisplayString.GetCurrentVString().Clear();
            partialDisplayString.SwapVString();
            partialDisplayString.GetCurrentVString().CopyFrom(allContentString, 0, Mathf.Min(_factor, allContentString.GetString().Length));
            for (int i = endTagCaches.Count - 1; i >= 0; --i)
            {
                partialDisplayString.GetCurrentVString().Push(endTags[endTagCaches[i]]);
            }
            _factor++;
        }
    }

    public void JumpToBegin()
    {
        _factor = 0;
        _timerMS = -_miniSecondPerWord;
        endTagCaches.Clear();
        partialDisplayString.GetCurrentVString().Clear();
        partialDisplayString.GetNextVString().Clear();
    }

    public void JumpToEnd()
    {
        _factor = allContentString.GetString().Length;
        endTagCaches.Clear();
        OnTyping();
    }


    bool CheckStart(Tags tag)
    {
        if (_factor >= allContentString.GetString().Length)
        {
            return false;
        }

        int iTag = (int)tag;
        willShowString.CopyFrom(allContentString, _factor, allContentString.GetString().Length - _factor);
        string willShow = willShowString.GetString();
        string endTag = endTags[iTag];
        if (willShow.StartsWith(startTags[iTag]))
        {
            int tagLeng = willShow.IndexOf(">") + 1;
            _factor += tagLeng;
            endTagCaches.Add(iTag);//倒叙

            if (CheckStart(Tags.Color)) { }
            else if (CheckStart(Tags.Size)) { }
            else if (CheckStart(Tags.B)) { }
            else if (CheckStart(Tags.I)) { }
            else
            {
                return false;
            }
            return true;
        }
        else if (willShow.StartsWith(endTag))
        {
            int endleng = endTag.Length;//"</color>"的长度
            _factor += endleng;
            for (int i = endTagCaches.Count - 1; i >= 0; --i)
            {
                if(iTag == endTagCaches[i])
                {
                    endTagCaches.RemoveAt(i);
                }
            }

            if (CheckStart(Tags.Color)) { }
            else if (CheckStart(Tags.Size)) { }
            else if (CheckStart(Tags.B)) { }
            else if (CheckStart(Tags.I)) { }
            else
            {
                return false;
            }
            return true;
        }
        return false;
    }


}