1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 22:47:24 +08:00

Cleaning up

This commit is contained in:
Dragon 2022-11-17 10:18:17 +01:00 committed by Terochi
parent 0100c01b82
commit a9747d367c
2 changed files with 28 additions and 29 deletions

View File

@ -12,16 +12,18 @@ using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osuTK.Input;
namespace osu.Game.Tests.Visual.Online
namespace osu.Game.Tests.Visual.UserInterface
{
[TestFixture]
public class TestSceneChatManipulation : OsuManualInputManagerTestScene
public class TestSceneHistoryTextBox : OsuManualInputManagerTestScene
{
private HistoryTextBox box;
private OsuSpriteText text;
private const string temp = "Temp message";
private int messageCounter;
private HistoryTextBox box;
private OsuSpriteText text;
[SetUp]
public void SetUp()
{
@ -49,6 +51,9 @@ namespace osu.Game.Tests.Visual.Online
box.OnCommit += (_, __) =>
{
if (string.IsNullOrEmpty(box.Text))
return;
text.Text = $"{nameof(box.OnCommit)}: {box.Text}";
box.Text = string.Empty;
box.TakeFocus();
@ -64,7 +69,6 @@ namespace osu.Game.Tests.Visual.Online
[Test]
public void TestEmptyHistory()
{
const string temp = "Temp message";
AddStep("Set text", () => box.Text = temp);
AddStep("Move down", () => InputManager.Key(Key.Down));
@ -78,8 +82,6 @@ namespace osu.Game.Tests.Visual.Online
public void TestPartialHistory()
{
addMessages(2);
const string temp = "Temp message";
AddStep("Set text", () => box.Text = temp);
AddStep("Move down", () => InputManager.Key(Key.Down));
@ -102,21 +104,18 @@ namespace osu.Game.Tests.Visual.Online
public void TestFullHistory()
{
addMessages(5);
AddStep("Set text", () => box.Text = temp);
AddAssert("History saved as <5-1>", () =>
Enumerable.Range(0, 5).Select(number => $"Message {5 - number}").SequenceEqual(box.MessageHistory));
const string temp = "Temp message";
AddStep("Set text", () => box.Text = temp);
AddStep("Move down", () => InputManager.Key(Key.Down));
AddAssert("Text is the same", () => box.Text == temp);
addMessages(2);
AddStep("Set text", () => box.Text = temp);
AddAssert("Overrode history to <7-3>", () =>
Enumerable.Range(0, 5).Select(number => $"Message {7 - number}").SequenceEqual(box.MessageHistory));
AddStep("Set text", () => box.Text = temp);
AddRepeatStep("Move Up", () => InputManager.Key(Key.Up), 5);
AddAssert("Same as 3rd message", () => box.Text == "Message 3");

View File

@ -12,27 +12,27 @@ namespace osu.Game.Graphics.UserInterface
public class HistoryTextBox : FocusedTextBox
{
private readonly int historyLimit;
private bool everythingSelected;
public int HistoryLength => messageHistory.Count;
private readonly List<string> messageHistory;
public IReadOnlyList<string> MessageHistory =>
Enumerable.Range(0, HistoryLength).Select(GetOldMessage).ToList();
private string originalMessage = string.Empty;
private int historyIndex = -1;
public int HistoryLength => messageHistory.Count;
private int startIndex;
private int selectedIndex = -1;
private string originalMessage = string.Empty;
private bool everythingSelected;
private int getNormalizedIndex(int index) =>
(HistoryLength + startIndex - index - 1) % HistoryLength;
public HistoryTextBox(int historyLimit = 100)
{
if (historyLimit <= 0)
throw new ArgumentOutOfRangeException();
this.historyLimit = historyLimit;
messageHistory = new List<string>(historyLimit);
@ -40,7 +40,7 @@ namespace osu.Game.Graphics.UserInterface
{
if (string.IsNullOrEmpty(text.NewValue) || everythingSelected)
{
historyIndex = -1;
selectedIndex = -1;
everythingSelected = false;
}
};
@ -66,28 +66,28 @@ namespace osu.Game.Graphics.UserInterface
switch (e.Key)
{
case Key.Up:
if (historyIndex == HistoryLength - 1)
if (selectedIndex == HistoryLength - 1)
return true;
if (historyIndex == -1)
if (selectedIndex == -1)
originalMessage = Text;
Text = messageHistory[getNormalizedIndex(++historyIndex)];
Text = messageHistory[getNormalizedIndex(++selectedIndex)];
return true;
case Key.Down:
if (historyIndex == -1)
if (selectedIndex == -1)
return true;
if (historyIndex == 0)
if (selectedIndex == 0)
{
historyIndex = -1;
selectedIndex = -1;
Text = originalMessage;
return true;
}
Text = messageHistory[getNormalizedIndex(--historyIndex)];
Text = messageHistory[getNormalizedIndex(--selectedIndex)];
return true;
}
@ -110,7 +110,7 @@ namespace osu.Game.Graphics.UserInterface
}
}
historyIndex = -1;
selectedIndex = -1;
base.Commit();
}