Files
RobustToolbox/Robust.Client/UserInterface/Controls/HistoryLineEdit.cs
T
Pieter-Jan Briers 51b87513ed Improve LineEdit
Fixes #748, Closes #1010

Can select text now, can move by words, and all the other stuff you'd expect from a control like this.
2020-05-04 17:13:29 +02:00

100 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
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 List<string>();
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 (!this.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();
}
}
}
}