Files
RobustToolbox/Robust.Client/UserInterface/Controls/HistoryLineEdit.cs
T
AcruidandGitHub 2183cd7ca1 Massive Namespace Cleanup (#1544)
* Removed the Interfaces folder.
* All objects inside the GameObjects subfolders are now in the GameObjects namespace.
* Added a Resharper DotSettings file to mark the GameObjects subfolders as not providing namespaces.
* Simplified Robust.client.Graphics namespace.
* Automated remove redundant using statements.
2021-02-10 23:27:19 -08:00

99 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using Robust.Shared.Input;
namespace Robust.Client.UserInterface.Controls
{
public class HistoryLineEdit : LineEdit
{
private const int MaxHistorySize = 100;
private string? _historyTemp;
public List<string> History { get; } = new();
public int HistoryIndex { get; set; } = 0;
public event Action? OnHistoryChanged;
public HistoryLineEdit()
{
OnTextEntered += OnSubmit;
}
public void ClearHistory()
{
History.Clear();
HistoryIndex = 0;
}
private void OnSubmit(LineEditEventArgs args)
{
if (string.IsNullOrWhiteSpace(args.Text))
{
return;
}
if (History.Count == 0 || History[History.Count - 1] != args.Text)
{
History.Add(args.Text);
if (History.Count > MaxHistorySize)
{
History.RemoveAt(0);
}
}
HistoryIndex = History.Count;
OnHistoryChanged?.Invoke();
}
protected internal override void KeyBindDown(GUIBoundKeyEventArgs args)
{
base.KeyBindDown(args);
if (!HasKeyboardFocus())
{
return;
}
if (args.Function == EngineKeyFunctions.TextHistoryPrev)
{
if (HistoryIndex <= 0)
{
return;
}
if (HistoryIndex == History.Count)
{
_historyTemp = Text;
}
HistoryIndex--;
Text = History[HistoryIndex];
CursorPosition = Text.Length;
args.Handle();
}
else if (args.Function == EngineKeyFunctions.TextHistoryNext)
{
if (HistoryIndex >= History.Count)
{
return;
}
HistoryIndex++;
if (HistoryIndex == History.Count)
{
Text = _historyTemp!;
}
else
{
Text = History[HistoryIndex];
}
CursorPosition = Text.Length;
args.Handle();
}
}
}
}