refactor: Removed unused text typer functionality

This commit is contained in:
Gerard Gascón 2024-01-27 09:47:45 +01:00
parent c010ef24a4
commit 5c27b5e5b6
2 changed files with 10 additions and 40 deletions

View file

@ -1,4 +1,5 @@
using System; using System;
using Messaging.Typer;
using TMPro; using TMPro;
using UnityEngine; using UnityEngine;
using Utilities; using Utilities;
@ -19,9 +20,6 @@ namespace Messaging {
} }
public void SetMessageText(string text, bool animate) { public void SetMessageText(string text, bool animate) {
if (_textTyper.IsMessageAnimating()) {
_textTyper.SkipToEndOfCurrentMessage();
}
this.EnsureCoroutineStopped(ref _typingCoroutine); this.EnsureCoroutineStopped(ref _typingCoroutine);
if (animate) if (animate)

View file

@ -3,14 +3,11 @@ using System.Collections;
using TMPro; using TMPro;
using UnityEngine; using UnityEngine;
namespace Messaging { namespace Messaging.Typer {
public class TextTyper { public class TextTyper {
private bool _textAnimating;
private bool _stopAnimating;
private readonly TMP_Text _textBox; private readonly TMP_Text _textBox;
static readonly Color32 Clear = new(0, 0, 0, 0); private static readonly Color32 Clear = new(0, 0, 0, 0);
private const float SecondsPerCharacter = 1f / 60f; private const float SecondsPerCharacter = 1f / 60f;
public TextTyper(TMP_Text textBox) { public TextTyper(TMP_Text textBox) {
@ -18,8 +15,6 @@ namespace Messaging {
} }
public IEnumerator AnimateTextIn(string processedMessage, Action onFinish) { public IEnumerator AnimateTextIn(string processedMessage, Action onFinish) {
_textAnimating = true;
_stopAnimating = false;
float timeOfLastCharacter = 0; float timeOfLastCharacter = 0;
TMP_TextInfo textInfo = _textBox.textInfo; TMP_TextInfo textInfo = _textBox.textInfo;
@ -40,28 +35,16 @@ namespace Messaging {
Array.Copy(theColors, originalColors[i], theColors.Length); Array.Copy(theColors, originalColors[i], theColors.Length);
} }
int charCount = textInfo.characterCount; int charCount = textInfo.characterCount;
float[] charAnimStartTimes = new float[charCount];
for (int i = 0; i < charCount; i++) {
charAnimStartTimes[i] = -1;
}
int visibleCharacterIndex = 0; int visibleCharacterIndex = 0;
while (true) { while (true) {
if (_stopAnimating) {
for (int i = visibleCharacterIndex; i < charCount; i++) {
charAnimStartTimes[i] = Time.unscaledTime;
}
visibleCharacterIndex = charCount;
FinishAnimating(onFinish);
}
if (ShouldShowNextCharacter(SecondsPerCharacter, timeOfLastCharacter)) { if (ShouldShowNextCharacter(SecondsPerCharacter, timeOfLastCharacter)) {
if (visibleCharacterIndex <= charCount) { if (visibleCharacterIndex <= charCount) {
if (visibleCharacterIndex < charCount && if (visibleCharacterIndex < charCount &&
ShouldShowNextCharacter(SecondsPerCharacter, timeOfLastCharacter)) { ShouldShowNextCharacter(SecondsPerCharacter, timeOfLastCharacter)) {
charAnimStartTimes[visibleCharacterIndex] = Time.unscaledTime;
visibleCharacterIndex++; visibleCharacterIndex++;
timeOfLastCharacter = Time.unscaledTime; timeOfLastCharacter = Time.unscaledTime;
if (visibleCharacterIndex == charCount) { if (visibleCharacterIndex == charCount) {
FinishAnimating(onFinish); onFinish?.Invoke();
} }
} }
} }
@ -87,28 +70,17 @@ namespace Messaging {
theInfo.mesh.vertices = theInfo.vertices; theInfo.mesh.vertices = theInfo.vertices;
_textBox.UpdateGeometry(theInfo.mesh, i); _textBox.UpdateGeometry(theInfo.mesh, i);
} }
yield return null;
} if (visibleCharacterIndex == charCount) {
break;
} }
private void FinishAnimating(Action onFinish) { yield return null;
_textAnimating = false; }
_stopAnimating = false;
onFinish?.Invoke();
} }
private static bool ShouldShowNextCharacter(float secondsPerCharacter, float timeOfLastCharacter) { private static bool ShouldShowNextCharacter(float secondsPerCharacter, float timeOfLastCharacter) {
return Time.unscaledTime - timeOfLastCharacter > secondsPerCharacter; return Time.unscaledTime - timeOfLastCharacter > secondsPerCharacter;
} }
public void SkipToEndOfCurrentMessage() {
if (_textAnimating) {
_stopAnimating = true;
}
}
public bool IsMessageAnimating() {
return _textAnimating;
}
} }
} }