Replacing structure to use LimitedCapacityQueue.cs

This commit is contained in:
Dragon 2022-11-17 11:35:13 +01:00 committed by Terochi
parent a9747d367c
commit a25c94d567
2 changed files with 31 additions and 50 deletions

View File

@ -49,7 +49,7 @@ namespace osu.Game.Tests.Visual.UserInterface
} }
}; };
box.OnCommit += (_, __) => box.OnCommit += (_, _) =>
{ {
if (string.IsNullOrEmpty(box.Text)) if (string.IsNullOrEmpty(box.Text))
return; return;
@ -70,6 +70,7 @@ namespace osu.Game.Tests.Visual.UserInterface
public void TestEmptyHistory() public void TestEmptyHistory()
{ {
AddStep("Set text", () => box.Text = temp); AddStep("Set text", () => box.Text = temp);
AddAssert("History is empty", () => !box.GetMessageHistory().Any());
AddStep("Move down", () => InputManager.Key(Key.Down)); AddStep("Move down", () => InputManager.Key(Key.Down));
AddAssert("Text is the same", () => box.Text == temp); AddAssert("Text is the same", () => box.Text == temp);
@ -106,7 +107,7 @@ namespace osu.Game.Tests.Visual.UserInterface
addMessages(5); addMessages(5);
AddStep("Set text", () => box.Text = temp); AddStep("Set text", () => box.Text = temp);
AddAssert("History saved as <5-1>", () => AddAssert("History saved as <5-1>", () =>
Enumerable.Range(0, 5).Select(number => $"Message {5 - number}").SequenceEqual(box.MessageHistory)); Enumerable.Range(0, 5).Select(number => $"Message {5 - number}").SequenceEqual(box.GetMessageHistory()));
AddStep("Move down", () => InputManager.Key(Key.Down)); AddStep("Move down", () => InputManager.Key(Key.Down));
AddAssert("Text is the same", () => box.Text == temp); AddAssert("Text is the same", () => box.Text == temp);
@ -114,7 +115,7 @@ namespace osu.Game.Tests.Visual.UserInterface
addMessages(2); addMessages(2);
AddStep("Set text", () => box.Text = temp); AddStep("Set text", () => box.Text = temp);
AddAssert("Overrode history to <7-3>", () => AddAssert("Overrode history to <7-3>", () =>
Enumerable.Range(0, 5).Select(number => $"Message {7 - number}").SequenceEqual(box.MessageHistory)); Enumerable.Range(0, 5).Select(number => $"Message {7 - number}").SequenceEqual(box.GetMessageHistory()));
AddRepeatStep("Move Up", () => InputManager.Key(Key.Up), 5); AddRepeatStep("Move Up", () => InputManager.Key(Key.Up), 5);
AddAssert("Same as 3rd message", () => box.Text == "Message 3"); AddAssert("Same as 3rd message", () => box.Text == "Message 3");
@ -134,11 +135,11 @@ namespace osu.Game.Tests.Visual.UserInterface
{ {
addMessages(5); addMessages(5);
AddAssert("History saved as <5-1>", () => AddAssert("History saved as <5-1>", () =>
Enumerable.Range(0, 5).Select(number => $"Message {5 - number}").SequenceEqual(box.MessageHistory)); Enumerable.Range(0, 5).Select(number => $"Message {5 - number}").SequenceEqual(box.GetMessageHistory()));
addMessages(6); addMessages(6);
AddAssert("Overrode history to <11-7>", () => AddAssert("Overrode history to <11-7>", () =>
Enumerable.Range(0, 5).Select(number => $"Message {11 - number}").SequenceEqual(box.MessageHistory)); Enumerable.Range(0, 5).Select(number => $"Message {11 - number}").SequenceEqual(box.GetMessageHistory()));
} }
[Test] [Test]

View File

@ -1,46 +1,44 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using osu.Framework.Input.Events; using osu.Framework.Input.Events;
using osu.Game.Rulesets.Difficulty.Utils;
using osuTK.Input; using osuTK.Input;
namespace osu.Game.Graphics.UserInterface namespace osu.Game.Graphics.UserInterface
{ {
public class HistoryTextBox : FocusedTextBox public class HistoryTextBox : FocusedTextBox
{ {
private readonly int historyLimit; private readonly LimitedCapacityQueue<string> messageHistory;
private readonly List<string> messageHistory;
public IReadOnlyList<string> MessageHistory => public IEnumerable<string> GetMessageHistory()
Enumerable.Range(0, HistoryLength).Select(GetOldMessage).ToList(); {
if (HistoryCount == 0)
yield break;
public int HistoryLength => messageHistory.Count; for (int i = HistoryCount - 1; i >= 0; i--)
yield return messageHistory[i];
}
private int startIndex; public int HistoryCount => messageHistory.Count;
private int selectedIndex = -1;
private int selectedIndex;
private string originalMessage = string.Empty; private string originalMessage = string.Empty;
private bool everythingSelected; private bool everythingSelected;
private int getNormalizedIndex(int index) => public string GetOldMessage(int index) => messageHistory[HistoryCount - index - 1];
(HistoryLength + startIndex - index - 1) % HistoryLength;
public HistoryTextBox(int historyLimit = 100) public HistoryTextBox(int capacity = 100)
{ {
if (historyLimit <= 0) messageHistory = new LimitedCapacityQueue<string>(capacity);
throw new ArgumentOutOfRangeException();
this.historyLimit = historyLimit;
messageHistory = new List<string>(historyLimit);
Current.ValueChanged += text => Current.ValueChanged += text =>
{ {
if (string.IsNullOrEmpty(text.NewValue) || everythingSelected) if (string.IsNullOrEmpty(text.NewValue) || everythingSelected)
{ {
selectedIndex = -1; selectedIndex = HistoryCount;
everythingSelected = false; everythingSelected = false;
} }
}; };
@ -53,41 +51,33 @@ namespace osu.Game.Graphics.UserInterface
base.OnTextSelectionChanged(selectionType); base.OnTextSelectionChanged(selectionType);
} }
public string GetOldMessage(int index)
{
if (index < 0 || index >= HistoryLength)
throw new ArgumentOutOfRangeException();
return HistoryLength == 0 ? string.Empty : messageHistory[getNormalizedIndex(index)];
}
protected override bool OnKeyDown(KeyDownEvent e) protected override bool OnKeyDown(KeyDownEvent e)
{ {
switch (e.Key) switch (e.Key)
{ {
case Key.Up: case Key.Up:
if (selectedIndex == HistoryLength - 1) if (selectedIndex == 0)
return true; return true;
if (selectedIndex == -1) if (selectedIndex == HistoryCount)
originalMessage = Text; originalMessage = Text;
Text = messageHistory[getNormalizedIndex(++selectedIndex)]; Text = messageHistory[--selectedIndex];
return true; return true;
case Key.Down: case Key.Down:
if (selectedIndex == -1) if (selectedIndex == HistoryCount)
return true; return true;
if (selectedIndex == 0) if (selectedIndex == HistoryCount - 1)
{ {
selectedIndex = -1; selectedIndex = HistoryCount;
Text = originalMessage; Text = originalMessage;
return true; return true;
} }
Text = messageHistory[getNormalizedIndex(--selectedIndex)]; Text = messageHistory[++selectedIndex];
return true; return true;
} }
@ -98,19 +88,9 @@ namespace osu.Game.Graphics.UserInterface
protected override void Commit() protected override void Commit()
{ {
if (!string.IsNullOrEmpty(Text)) if (!string.IsNullOrEmpty(Text))
{ messageHistory.Enqueue(Text);
if (HistoryLength == historyLimit)
{
messageHistory[startIndex++] = Text;
startIndex %= historyLimit;
}
else
{
messageHistory.Add(Text);
}
}
selectedIndex = -1; selectedIndex = HistoryCount;
base.Commit(); base.Commit();
} }