A game about forced loneliness, made by TACStudios
1using System;
2using System.Diagnostics;
3using UnityEngine;
4
5namespace TMPro
6{
7 /// <summary>
8 /// Structure used to track basic XML tags which are binary (on / off)
9 /// </summary>
10 public struct TMP_FontStyleStack
11 {
12 public byte bold;
13 public byte italic;
14 public byte underline;
15 public byte strikethrough;
16 public byte highlight;
17 public byte superscript;
18 public byte subscript;
19 public byte uppercase;
20 public byte lowercase;
21 public byte smallcaps;
22
23 /// <summary>
24 /// Clear the basic XML tag stack.
25 /// </summary>
26 public void Clear()
27 {
28 bold = 0;
29 italic = 0;
30 underline = 0;
31 strikethrough = 0;
32 highlight = 0;
33 superscript = 0;
34 subscript = 0;
35 uppercase = 0;
36 lowercase = 0;
37 smallcaps = 0;
38 }
39
40 public byte Add(FontStyles style)
41 {
42 switch (style)
43 {
44 case FontStyles.Bold:
45 bold++;
46 return bold;
47 case FontStyles.Italic:
48 italic++;
49 return italic;
50 case FontStyles.Underline:
51 underline++;
52 return underline;
53 case FontStyles.UpperCase:
54 uppercase++;
55 return uppercase;
56 case FontStyles.LowerCase:
57 lowercase++;
58 return lowercase;
59 case FontStyles.Strikethrough:
60 strikethrough++;
61 return strikethrough;
62 case FontStyles.Superscript:
63 superscript++;
64 return superscript;
65 case FontStyles.Subscript:
66 subscript++;
67 return subscript;
68 case FontStyles.Highlight:
69 highlight++;
70 return highlight;
71 }
72
73 return 0;
74 }
75
76 public byte Remove(FontStyles style)
77 {
78 switch (style)
79 {
80 case FontStyles.Bold:
81 if (bold > 1)
82 bold--;
83 else
84 bold = 0;
85 return bold;
86 case FontStyles.Italic:
87 if (italic > 1)
88 italic--;
89 else
90 italic = 0;
91 return italic;
92 case FontStyles.Underline:
93 if (underline > 1)
94 underline--;
95 else
96 underline = 0;
97 return underline;
98 case FontStyles.UpperCase:
99 if (uppercase > 1)
100 uppercase--;
101 else
102 uppercase = 0;
103 return uppercase;
104 case FontStyles.LowerCase:
105 if (lowercase > 1)
106 lowercase--;
107 else
108 lowercase = 0;
109 return lowercase;
110 case FontStyles.Strikethrough:
111 if (strikethrough > 1)
112 strikethrough--;
113 else
114 strikethrough = 0;
115 return strikethrough;
116 case FontStyles.Highlight:
117 if (highlight > 1)
118 highlight--;
119 else
120 highlight = 0;
121 return highlight;
122 case FontStyles.Superscript:
123 if (superscript > 1)
124 superscript--;
125 else
126 superscript = 0;
127 return superscript;
128 case FontStyles.Subscript:
129 if (subscript > 1)
130 subscript--;
131 else
132 subscript = 0;
133 return subscript;
134 }
135
136 return 0;
137 }
138 }
139
140
141 /// <summary>
142 /// Structure used to track XML tags of various types.
143 /// </summary>
144 /// <typeparam name="T"></typeparam>
145 [DebuggerDisplay("Item count = {m_Count}")]
146 public struct TMP_TextProcessingStack<T>
147 {
148 public T[] itemStack;
149 public int index;
150
151 T m_DefaultItem;
152 int m_Capacity;
153 int m_RolloverSize;
154 int m_Count;
155
156 const int k_DefaultCapacity = 4;
157
158
159 /// <summary>
160 /// Constructor to create a new item stack.
161 /// </summary>
162 /// <param name="stack"></param>
163 public TMP_TextProcessingStack(T[] stack)
164 {
165 itemStack = stack;
166 m_Capacity = stack.Length;
167 index = 0;
168 m_RolloverSize = 0;
169
170 m_DefaultItem = default(T);
171 m_Count = 0;
172 }
173
174
175 /// <summary>
176 /// Constructor for a new item stack with the given capacity.
177 /// </summary>
178 /// <param name="capacity"></param>
179 public TMP_TextProcessingStack(int capacity)
180 {
181 itemStack = new T[capacity];
182 m_Capacity = capacity;
183 index = 0;
184 m_RolloverSize = 0;
185
186 m_DefaultItem = default(T);
187 m_Count = 0;
188 }
189
190
191 public TMP_TextProcessingStack(int capacity, int rolloverSize)
192 {
193 itemStack = new T[capacity];
194 m_Capacity = capacity;
195 index = 0;
196 m_RolloverSize = rolloverSize;
197
198 m_DefaultItem = default(T);
199 m_Count = 0;
200 }
201
202
203 /// <summary>
204 ///
205 /// </summary>
206 public int Count
207 {
208 get { return m_Count; }
209 }
210
211
212 /// <summary>
213 /// Returns the current item on the stack.
214 /// </summary>
215 public T current
216 {
217 get
218 {
219 if (index > 0)
220 return itemStack[index - 1];
221
222 return itemStack[0];
223 }
224 }
225
226
227 /// <summary>
228 ///
229 /// </summary>
230 public int rolloverSize
231 {
232 get { return m_RolloverSize; }
233 set
234 {
235 m_RolloverSize = value;
236
237 //if (m_Capacity < m_RolloverSize)
238 // Array.Resize(ref itemStack, m_RolloverSize);
239 }
240 }
241
242
243 /// <summary>
244 /// Set stack elements to default item.
245 /// </summary>
246 /// <param name="stack">The stack of elements.</param>
247 /// <param name="item"></param>
248 internal static void SetDefault(TMP_TextProcessingStack<T>[] stack, T item)
249 {
250 for (int i = 0; i < stack.Length; i++)
251 stack[i].SetDefault(item);
252 }
253
254
255 /// <summary>
256 /// Function to clear and reset stack to first item.
257 /// </summary>
258 public void Clear()
259 {
260 index = 0;
261 m_Count = 0;
262 }
263
264
265 /// <summary>
266 /// Function to set the first item on the stack and reset index.
267 /// </summary>
268 /// <param name="item"></param>
269 public void SetDefault(T item)
270 {
271 if (itemStack == null)
272 {
273 m_Capacity = k_DefaultCapacity;
274 itemStack = new T[m_Capacity];
275 m_DefaultItem = default(T);
276 }
277
278 itemStack[0] = item;
279 index = 1;
280 m_Count = 1;
281 }
282
283
284 /// <summary>
285 /// Function to add a new item to the stack.
286 /// </summary>
287 /// <param name="item"></param>
288 public void Add(T item)
289 {
290 if (index < itemStack.Length)
291 {
292 itemStack[index] = item;
293 index += 1;
294 }
295 }
296
297
298 /// <summary>
299 /// Function to retrieve an item from the stack.
300 /// </summary>
301 /// <returns></returns>
302 public T Remove()
303 {
304 index -= 1;
305 m_Count -= 1;
306
307 if (index <= 0)
308 {
309 m_Count = 0;
310 index = 1;
311 return itemStack[0];
312
313 }
314
315 return itemStack[index - 1];
316 }
317
318 public void Push(T item)
319 {
320 if (index == m_Capacity)
321 {
322 m_Capacity *= 2;
323 if (m_Capacity == 0)
324 m_Capacity = k_DefaultCapacity;
325
326 Array.Resize(ref itemStack, m_Capacity);
327 }
328
329 itemStack[index] = item;
330
331 if (m_RolloverSize == 0)
332 {
333 index += 1;
334 m_Count += 1;
335 }
336 else
337 {
338 index = (index + 1) % m_RolloverSize;
339 m_Count = m_Count < m_RolloverSize ? m_Count + 1 : m_RolloverSize;
340 }
341
342 }
343
344 public T Pop()
345 {
346 if (index == 0 && m_RolloverSize == 0)
347 return default(T);
348
349 if (m_RolloverSize == 0)
350 index -= 1;
351 else
352 {
353 index = (index - 1) % m_RolloverSize;
354 index = index < 0 ? index + m_RolloverSize : index;
355 }
356
357 T item = itemStack[index];
358 itemStack[index] = m_DefaultItem;
359
360 m_Count = m_Count > 0 ? m_Count - 1 : 0;
361
362 return item;
363 }
364
365 /// <summary>
366 ///
367 /// </summary>
368 /// <returns></returns>
369 public T Peek()
370 {
371 if (index == 0)
372 return m_DefaultItem;
373
374 return itemStack[index - 1];
375 }
376
377
378 /// <summary>
379 /// Function to retrieve the current item from the stack.
380 /// </summary>
381 /// <returns>itemStack <T></returns>
382 public T CurrentItem()
383 {
384 if (index > 0)
385 return itemStack[index - 1];
386
387 return itemStack[0];
388 }
389
390
391 /// <summary>
392 /// Function to retrieve the previous item without affecting the stack.
393 /// </summary>
394 /// <returns></returns>
395 public T PreviousItem()
396 {
397 if (index > 1)
398 return itemStack[index - 2];
399
400 return itemStack[0];
401 }
402 }
403}