1 using System.Collections.Generic;
4 using Microsoft.Xna.Framework.Input;
15 private readonly
bool loop;
18 private readonly
short maxCount = 10;
21 private readonly List<string> messageList =
new List<string> {
string.Empty };
27 private readonly List<GUITextBox> registers =
new List<GUITextBox>();
33 private string[] localChanges;
38 this.maxCount = maxCount;
39 localChanges =
new string[maxCount];
44 localChanges =
new string[maxCount];
56 if (manager.registers.Any(p => element == p)) {
return; }
66 Direction direction = key == Keys.Up ? Direction.Up : (key == Keys.Down ? Direction.Down : Direction.Other);
68 string newMessage = manager.SelectMessage(direction, element.
Text);
70 if (newMessage ==
null) {
return; }
72 element.
Text = newMessage;
80 manager.registers.Add(element);
84 public void Store(
string message)
87 var strip = StripMessage(message);
88 if (
string.IsNullOrWhiteSpace(strip)) {
return; }
90 if (messageList.Count > 1 && messageList[1] == message) {
return; }
93 messageList.Insert(1, message);
95 if (messageList.Count > maxCount)
97 messageList.RemoveAt(messageList.Count - 1);
101 static string StripMessage(
string text)
115 localChanges =
new string[maxCount];
124 private string SelectMessage(
Direction direction,
string original)
126 var originalIndex = index;
135 localChanges[index] = original;
137 var dir = (int) direction;
139 var nextIndex = (index + dir);
141 if (loop && messageList.Count > 1)
143 nextIndex = LoopAround(nextIndex);
147 if (nextIndex > messageList.Count - 1)
153 if (nextIndex >= 0 && EntryAt(nextIndex) == original && nextIndex != originalIndex && originalIndex != 0)
159 return nextIndex < 0 ? localChanges.FirstOrDefault() : EntryAt(index = nextIndex);
161 string EntryAt(
int i)
164 return localChanges[i] ?? messageList[i];
167 int LoopAround(
int next)
169 if (next > (messageList.Count - 1))
176 return messageList.Count - 1;
185 private enum Direction
A class used for handling special key actions in chat boxes. For example tab completion or up/down ar...
static void RegisterKeys(GUITextBox element, ChatManager manager)
Registers special input actions to the selected input field
void Store(string message)
void Clear()
Call this function whenever we should stop doing special stuff and return normal behavior....
ChatManager(bool loop, short maxCount)
static string GetChatMessageCommand(string message, out string messageWithoutCommand)