refactor: Removed text typer functionality that won't be used

This commit is contained in:
Gerard Gascón 2024-01-26 21:39:52 +01:00
parent 4243b1b401
commit 1c8fbc94e0
3 changed files with 2 additions and 128 deletions

View file

@ -1,6 +1,5 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic;
using TMPro; using TMPro;
using UnityEngine; using UnityEngine;
@ -15,7 +14,7 @@ namespace Messaging {
_textBox = textBox; _textBox = textBox;
} }
public IEnumerator AnimateTextIn(List<DialogueCommand> commands, string processedMessage, Action onFinish) { public IEnumerator AnimateTextIn(string processedMessage, Action onFinish) {
_textAnimating = true; _textAnimating = true;
float secondsPerCharacter = 1f / 150f; float secondsPerCharacter = 1f / 150f;
float timeOfLastCharacter = 0; float timeOfLastCharacter = 0;
@ -53,8 +52,6 @@ namespace Messaging {
} }
if (ShouldShowNextCharacter(secondsPerCharacter, timeOfLastCharacter)) { if (ShouldShowNextCharacter(secondsPerCharacter, timeOfLastCharacter)) {
if (visibleCharacterIndex <= charCount) { if (visibleCharacterIndex <= charCount) {
ExecuteCommandsForCurrentIndex(commands, visibleCharacterIndex, ref secondsPerCharacter,
ref timeOfLastCharacter);
if (visibleCharacterIndex < charCount && if (visibleCharacterIndex < charCount &&
ShouldShowNextCharacter(secondsPerCharacter, timeOfLastCharacter)) { ShouldShowNextCharacter(secondsPerCharacter, timeOfLastCharacter)) {
charAnimStartTimes[visibleCharacterIndex] = Time.unscaledTime; charAnimStartTimes[visibleCharacterIndex] = Time.unscaledTime;
@ -77,24 +74,6 @@ namespace Messaging {
} }
} }
private static void ExecuteCommandsForCurrentIndex(List<DialogueCommand> commands, int visableCharacterIndex,
ref float secondsPerCharacter, ref float timeOfLastCharacter) {
for (int i = 0; i < commands.Count; i++) {
DialogueCommand command = commands[i];
if (command.Position != visableCharacterIndex) continue;
switch (command.Type) {
case DialogueCommandType.Pause:
timeOfLastCharacter = Time.unscaledTime + command.FloatValue;
break;
case DialogueCommandType.TextSpeedChange:
secondsPerCharacter = 1f / command.FloatValue;
break;
}
commands.RemoveAt(i);
i--;
}
}
private void FinishAnimating(Action onFinish) { private void FinishAnimating(Action onFinish) {
_textAnimating = false; _textAnimating = false;
_stopAnimating = false; _stopAnimating = false;
@ -102,7 +81,7 @@ namespace Messaging {
} }
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() { public void SkipToEndOfCurrentMessage() {

View file

@ -1,94 +0,0 @@
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;
namespace Messaging {
public class DialogueUtility : MonoBehaviour {
private const string REMAINDER_REGEX = "(.*?((?=>)|(/|$)))";
private const string PAUSE_REGEX_STRING = "<p:(?<pause>" + REMAINDER_REGEX + ")>";
private static readonly Regex PauseRegex = new(PAUSE_REGEX_STRING);
private const string SPEED_REGEX_STRING = "<sp:(?<speed>" + REMAINDER_REGEX + ")>";
private static readonly Regex SpeedRegex = new(SPEED_REGEX_STRING);
private static readonly Dictionary<string, float> PauseDictionary = new() {
{ "tiny", .1f },
{ "short", .25f },
{ "normal", 0.666f },
{ "long", 1f },
{ "read", 2f },
};
public static List<DialogueCommand> ProcessInputString(string message, out string processedMessage) {
List<DialogueCommand> result = new();
processedMessage = message;
processedMessage = HandlePauseTags(processedMessage, result);
processedMessage = HandleSpeedTags(processedMessage, result);
return result;
}
private static string HandleSpeedTags(string processedMessage, List<DialogueCommand> result) {
MatchCollection speedMatches = SpeedRegex.Matches(processedMessage);
foreach (Match match in speedMatches) {
string stringVal = match.Groups["speed"].Value;
if (!float.TryParse(stringVal, out float val)) {
val = 150f;
}
result.Add(new DialogueCommand {
Position = VisibleCharactersUpToIndex(processedMessage, match.Index),
Type = DialogueCommandType.TextSpeedChange,
FloatValue = val
});
}
processedMessage = Regex.Replace(processedMessage, SPEED_REGEX_STRING, "");
return processedMessage;
}
private static string HandlePauseTags(string processedMessage, List<DialogueCommand> result) {
MatchCollection pauseMatches = PauseRegex.Matches(processedMessage);
foreach (Match match in pauseMatches) {
string val = match.Groups["pause"].Value;
string pauseName = val;
Debug.Assert(PauseDictionary.ContainsKey(pauseName), "no pause registered for '" + pauseName + "'");
result.Add(new DialogueCommand {
Position = VisibleCharactersUpToIndex(processedMessage, match.Index),
Type = DialogueCommandType.Pause,
FloatValue = PauseDictionary[pauseName]
});
}
processedMessage = Regex.Replace(processedMessage, PAUSE_REGEX_STRING, "");
return processedMessage;
}
private static int VisibleCharactersUpToIndex(string message, int index) {
int result = 0;
bool insideBrackets = false;
for (int i = 0; i < index; i++) {
if (message[i] == '<') {
insideBrackets = true;
} else if (message[i] == '>') {
insideBrackets = false;
result--;
}
if (!insideBrackets) {
result++;
} else if (i + 6 < index && message.Substring(i, 6) == "sprite") {
result++;
}
}
return result;
}
}
public struct DialogueCommand {
public int Position;
public DialogueCommandType Type;
public float FloatValue;
}
public enum DialogueCommandType {
Pause,
TextSpeedChange,
}
}

View file

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 7cb18af2a0fb6af4abf80654e793de64
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: