Skip to content

Commit

Permalink
Added undo/redo
Browse files Browse the repository at this point in the history
  • Loading branch information
18107 committed Apr 28, 2021
1 parent 187bd7c commit cc4e1ce
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 16 deletions.
90 changes: 89 additions & 1 deletion PulsarPluginLoader/Chat/Extensions/ChatHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ public class ChatHelper
public static Tuple<string, string[][]>[] chatCommands = null;

public static LinkedList<string> chatHistory = new LinkedList<string>();
public static List<Tuple<string, int>> typingHistory = null;
public static LinkedListNode<string> currentHistory = null;

public static LinkedList<Tuple<string, int>> typingHistory = null;
private static LinkedListNode<Tuple<string, int>> node = null;

public static bool isTyping = false;
public static bool adding = false;

public static Tuple<string, string[][]>[] getCommands()
{
Expand All @@ -45,6 +51,88 @@ public class ChatHelper
return publicCommands;
}

public static void Undo(ref string chat)
{
if (node == null || typingHistory == null)
{
return;
}

if (currentHistory != null)
{
currentHistory = null;
chat = node.Value.Item1;
cursorPos = node.Value.Item2;
cursorPos2 = -1;
return;
}

if (node == typingHistory.Last && node.Value.Item1 != chat)
{
typingHistory.AddLast(new Tuple<string, int>(chat, cursorPos));
chat = node.Value.Item1;
cursorPos = node.Value.Item2;
cursorPos2 = -1;
}
else
{
if (node.Previous != null)
{
node = node.Previous;
chat = node.Value.Item1;
cursorPos = node.Value.Item2;
cursorPos2 = -1;
}
}
}

public static void Redo(ref string chat)
{
if (node == null)
{
return;
}

if (node.Next != null)
{
node = node.Next;
chat = node.Value.Item1;
cursorPos = node.Value.Item2;
cursorPos2 = -1;
}
}

public static void UpdateTypingHistory(string chat, bool add, bool forceUpdate = false)
{
if (!isTyping)
{
isTyping = true;
adding = false;
node = null;
typingHistory = new LinkedList<Tuple<string, int>>();
}

if (node != null)
{
while (node.Next != null)
{
typingHistory.Remove(node.Next);
}
}

if (add != adding || forceUpdate)
{
adding = add;
if (typingHistory.Last?.Value?.Item1 != chat)
{
typingHistory.AddLast(new Tuple<string, int>(chat, cursorPos));
}
node = typingHistory.Last;
}

currentHistory = null;
}

public static string AutoComplete(string text, int cursorPos)
{
if (text[0] != '!' && text[0] != '/')
Expand Down
6 changes: 6 additions & 0 deletions PulsarPluginLoader/Chat/Extensions/HarmonyHandleChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ static void Prefix(PLInGameUI __instance, ref bool ___evenChatString, ref string
}
else
{
if (ChatHelper.isTyping)
{
ChatHelper.isTyping = false;
ChatHelper.typingHistory = null;
}

cursorPos = 0;
cursorPos2 = -1;
__instance.ChatLabel.supportRichText = true;
Expand Down
87 changes: 72 additions & 15 deletions PulsarPluginLoader/Chat/Extensions/HarmonyNetworkUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,30 @@ namespace PulsarPluginLoader.Chat.Extensions
[HarmonyPatch(typeof(PLNetworkManager), "Update")]
class HarmonyNetworkUpdate
{
private static LinkedListNode<string> curentHistory = null;

private static string currentChatText;

private static bool textModified = false;

private static long lastTimePaste = long.MaxValue;
private static long lastTimeDelete = long.MaxValue;
private static long lastTimeUndo = long.MaxValue;
private static long lastTimeRedo = long.MaxValue;

private static void SetChat(PLNetworkManager instance)
{
if (curentHistory == null)
if (currentHistory == null)
{
instance.CurrentChatText = "";
}
else
{
instance.CurrentChatText = curentHistory.Value;
instance.CurrentChatText = currentHistory.Value;
}
}

private static void DeleteSelected()
{
ChatHelper.UpdateTypingHistory(currentChatText, false, true);
int pos;
int length;
if (cursorPos < cursorPos2)
Expand All @@ -52,7 +53,7 @@ private static void DeleteSelected()
static void Prefix(PLNetworkManager __instance)
{
currentChatText = __instance.CurrentChatText;
if (__instance.IsTyping && (cursorPos > 0 || cursorPos2 > 0))
if (__instance.IsTyping)
{
foreach (char c in Input.inputString)
{
Expand All @@ -61,11 +62,13 @@ static void Prefix(PLNetworkManager __instance)
if (cursorPos2 != -1 && cursorPos2 != cursorPos)
{
DeleteSelected();
ChatHelper.UpdateTypingHistory(currentChatText, false, true);
}
else
{
if (cursorPos != currentChatText.Length)
{
ChatHelper.UpdateTypingHistory(currentChatText, false);
currentChatText = currentChatText.Remove(currentChatText.Length - cursorPos - 1, 1);
}
}
Expand All @@ -81,6 +84,7 @@ static void Prefix(PLNetworkManager __instance)
{
DeleteSelected();
}
ChatHelper.UpdateTypingHistory(currentChatText, true);
currentChatText = currentChatText.Insert(currentChatText.Length - cursorPos, c.ToString());
textModified = true;
}
Expand All @@ -91,31 +95,33 @@ static void Prefix(PLNetworkManager __instance)
if (cursorPos2 != -1 && cursorPos2 != cursorPos)
{
DeleteSelected();
ChatHelper.UpdateTypingHistory(currentChatText, false, true);
}
else
{
ChatHelper.UpdateTypingHistory(currentChatText, false);
currentChatText = currentChatText.Remove(currentChatText.Length - cursorPos, 1);
cursorPos--;
}
textModified = true;
}
if (Input.GetKey(KeyCode.Delete) && DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond > lastTimeDelete)
{
ChatHelper.UpdateTypingHistory(currentChatText, false);
lastTimeDelete += 30 /*(long)(1 / ((SystemInformation.KeyboardSpeed + 1) * 0.859375))*/;
currentChatText = currentChatText.Remove(currentChatText.Length - cursorPos, 1);
cursorPos--;
textModified = true;
}
}
if (__instance.IsTyping)
{

if (Input.GetKeyDown(KeyCode.Tab))
{
if (currentChatText.StartsWith("/"))
{
string chatText = AutoComplete(currentChatText, cursorPos);
if (chatText != currentChatText)
{
ChatHelper.UpdateTypingHistory(currentChatText, true, true);
textModified = true;
currentChatText = chatText;
cursorPos2 = -1;
Expand All @@ -128,6 +134,7 @@ static void Prefix(PLNetworkManager __instance)
string chatText = AutoComplete(currentChatText, cursorPos);
if (chatText != currentChatText)
{
ChatHelper.UpdateTypingHistory(currentChatText, true, true);
textModified = true;
currentChatText = chatText;
cursorPos2 = -1;
Expand All @@ -146,27 +153,74 @@ static void Postfix(PLNetworkManager __instance)
{
if (!__instance.IsTyping)
{
curentHistory = null;
currentHistory = null;
return;
}

if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
{
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
{
if (Input.GetKeyDown(KeyCode.Z))
{
lastTimeRedo = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond + /*(SystemInformation.KeyboardDelay + 1) **/ 250;
ChatHelper.Redo(ref currentChatText);
textModified = true;
}
if (Input.GetKey(KeyCode.Z) && DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond > lastTimeRedo)
{
lastTimeRedo += 30 /*(long)(1 / ((SystemInformation.KeyboardSpeed + 1) * 0.859375))*/;
ChatHelper.Redo(ref currentChatText);
textModified = true;
}
}
else
{
if (Input.GetKeyDown(KeyCode.Z))
{
lastTimeUndo = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond + /*(SystemInformation.KeyboardDelay + 1) **/ 250;
ChatHelper.Undo(ref currentChatText);
textModified = true;
}
if (Input.GetKey(KeyCode.Z) && DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond > lastTimeUndo)
{
lastTimeUndo += 30 /*(long)(1 / ((SystemInformation.KeyboardSpeed + 1) * 0.859375))*/;
ChatHelper.Undo(ref currentChatText);
textModified = true;
}
}
if (Input.GetKeyDown(KeyCode.Y))
{
lastTimeRedo = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond + /*(SystemInformation.KeyboardDelay + 1) **/ 250;
ChatHelper.Redo(ref currentChatText);
textModified = true;
}
if (Input.GetKey(KeyCode.Y) && DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond > lastTimeRedo)
{
lastTimeRedo += 30 /*(long)(1 / ((SystemInformation.KeyboardSpeed + 1) * 0.859375))*/;
ChatHelper.Redo(ref currentChatText);
textModified = true;
}

if (Input.GetKeyDown(KeyCode.V))
{
ChatHelper.UpdateTypingHistory(currentChatText, true, true);
lastTimePaste = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond + /*(SystemInformation.KeyboardDelay + 1) **/ 250;
if (cursorPos2 != -1 && cursorPos2 != cursorPos)
{
DeleteSelected();
}
currentChatText = currentChatText.Insert(currentChatText.Length - cursorPos, GUIUtility.systemCopyBuffer);
textModified = true;
ChatHelper.UpdateTypingHistory(currentChatText, true, true);
}
if (Input.GetKey(KeyCode.V) && DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond > lastTimePaste)
{
ChatHelper.UpdateTypingHistory(currentChatText, true, true);
lastTimePaste += 30 /*(long)(1 / ((SystemInformation.KeyboardSpeed + 1) * 0.859375))*/;
currentChatText = currentChatText.Insert(currentChatText.Length - cursorPos, GUIUtility.systemCopyBuffer);
textModified = true;
ChatHelper.UpdateTypingHistory(currentChatText, true, true);
}
if (Input.GetKeyDown(KeyCode.C) && cursorPos2 != -1 && cursorPos2 != cursorPos)
{
Expand All @@ -186,6 +240,7 @@ static void Postfix(PLNetworkManager __instance)
}
if (Input.GetKeyDown(KeyCode.X) && cursorPos2 != -1 && cursorPos2 != cursorPos)
{
ChatHelper.UpdateTypingHistory(currentChatText, false, true);
int pos;
int length;
if (cursorPos < cursorPos2)
Expand All @@ -201,6 +256,7 @@ static void Postfix(PLNetworkManager __instance)
GUIUtility.systemCopyBuffer = currentChatText.Substring(pos, length);
DeleteSelected();
textModified = true;
ChatHelper.UpdateTypingHistory(currentChatText, false, true);
}
if (Input.GetKeyDown(KeyCode.A))
{
Expand All @@ -219,27 +275,28 @@ static void Postfix(PLNetworkManager __instance)
{
cursorPos = 0;
cursorPos2 = -1;
if (curentHistory == null)
if (currentHistory == null)
{
curentHistory = chatHistory.Last;
ChatHelper.UpdateTypingHistory(currentChatText, true, true);
currentHistory = chatHistory.Last;
}
else
{
curentHistory = curentHistory.Previous;
currentHistory = currentHistory.Previous;
}
SetChat(__instance);
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
cursorPos = 0;
cursorPos2 = -1;
if (curentHistory == null)
if (currentHistory == null)
{
curentHistory = chatHistory.First;
currentHistory = chatHistory.First;
}
else
{
curentHistory = curentHistory.Next;
currentHistory = currentHistory.Next;
}
SetChat(__instance);
}
Expand Down

0 comments on commit cc4e1ce

Please sign in to comment.