Implemented finite limit of stored history.

This commit is contained in:
Terochi 2022-11-16 13:16:01 +01:00 committed by Terochi
parent 19dc31c7ae
commit 0100c01b82

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using osu.Framework.Input.Events; using osu.Framework.Input.Events;
using osuTK.Input; using osuTK.Input;
@ -12,16 +13,23 @@ namespace osu.Game.Graphics.UserInterface
{ {
private readonly int historyLimit; private readonly int historyLimit;
private bool everythingSelected;
public int HistoryLength => messageHistory.Count;
private readonly List<string> messageHistory; private readonly List<string> messageHistory;
public IReadOnlyList<string> MessageHistory => messageHistory; public IReadOnlyList<string> MessageHistory =>
Enumerable.Range(0, HistoryLength).Select(GetOldMessage).ToList();
private string originalMessage = string.Empty;
private int historyIndex = -1;
private int startIndex; private int startIndex;
private string originalMessage = string.Empty; private int getNormalizedIndex(int index) =>
private int nullIndex => -1; (HistoryLength + startIndex - index - 1) % HistoryLength;
private int historyIndex = -1;
private int endIndex => (messageHistory.Count + startIndex - 1) % Math.Max(1, messageHistory.Count);
public HistoryTextBox(int historyLimit = 100) public HistoryTextBox(int historyLimit = 100)
{ {
@ -30,17 +38,27 @@ namespace osu.Game.Graphics.UserInterface
Current.ValueChanged += text => Current.ValueChanged += text =>
{ {
if (string.IsNullOrEmpty(text.NewValue)) if (string.IsNullOrEmpty(text.NewValue) || everythingSelected)
historyIndex = nullIndex; {
historyIndex = -1;
everythingSelected = false;
}
}; };
} }
protected override void OnTextSelectionChanged(TextSelectionType selectionType)
{
everythingSelected = SelectedText == Text;
base.OnTextSelectionChanged(selectionType);
}
public string GetOldMessage(int index) public string GetOldMessage(int index)
{ {
if (index < 0 || index >= messageHistory.Count) if (index < 0 || index >= HistoryLength)
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
return messageHistory[(startIndex + index) % messageHistory.Count]; return HistoryLength == 0 ? string.Empty : messageHistory[getNormalizedIndex(index)];
} }
protected override bool OnKeyDown(KeyDownEvent e) protected override bool OnKeyDown(KeyDownEvent e)
@ -48,33 +66,28 @@ namespace osu.Game.Graphics.UserInterface
switch (e.Key) switch (e.Key)
{ {
case Key.Up: case Key.Up:
if (historyIndex == nullIndex) if (historyIndex == HistoryLength - 1)
{
historyIndex = endIndex;
originalMessage = Text;
}
if (historyIndex == startIndex)
return true; return true;
historyIndex = (historyLimit + historyIndex - 1) % historyLimit; if (historyIndex == -1)
Text = messageHistory[historyIndex]; originalMessage = Text;
Text = messageHistory[getNormalizedIndex(++historyIndex)];
return true; return true;
case Key.Down: case Key.Down:
if (historyIndex == nullIndex) if (historyIndex == -1)
return true; return true;
if (historyIndex == endIndex) if (historyIndex == 0)
{ {
historyIndex = nullIndex; historyIndex = -1;
Text = originalMessage; Text = originalMessage;
return true; return true;
} }
historyIndex = (historyIndex + 1) % historyLimit; Text = messageHistory[getNormalizedIndex(--historyIndex)];
Text = messageHistory[historyIndex];
return true; return true;
} }
@ -86,9 +99,10 @@ namespace osu.Game.Graphics.UserInterface
{ {
if (!string.IsNullOrEmpty(Text)) if (!string.IsNullOrEmpty(Text))
{ {
if (messageHistory.Count == historyLimit) if (HistoryLength == historyLimit)
{ {
messageHistory[startIndex++] = Text; messageHistory[startIndex++] = Text;
startIndex %= historyLimit;
} }
else else
{ {
@ -96,7 +110,7 @@ namespace osu.Game.Graphics.UserInterface
} }
} }
historyIndex = nullIndex; historyIndex = -1;
base.Commit(); base.Commit();
} }