2022-11-14 05:34:02 +08:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
using osu.Framework.Input.Events;
|
2022-11-19 23:53:35 +08:00
|
|
|
using osu.Game.Utils;
|
2022-11-14 05:34:02 +08:00
|
|
|
using osuTK.Input;
|
|
|
|
|
2022-11-15 20:06:02 +08:00
|
|
|
namespace osu.Game.Graphics.UserInterface
|
2022-11-14 05:34:02 +08:00
|
|
|
{
|
2022-11-15 23:12:24 +08:00
|
|
|
public class HistoryTextBox : FocusedTextBox
|
2022-11-14 05:34:02 +08:00
|
|
|
{
|
2022-11-17 18:35:13 +08:00
|
|
|
private readonly LimitedCapacityQueue<string> messageHistory;
|
2022-11-16 20:16:01 +08:00
|
|
|
|
2022-11-17 18:35:13 +08:00
|
|
|
public int HistoryCount => messageHistory.Count;
|
2022-11-16 20:16:01 +08:00
|
|
|
|
2022-11-17 18:35:13 +08:00
|
|
|
private int selectedIndex;
|
2022-11-17 17:18:17 +08:00
|
|
|
|
|
|
|
private string originalMessage = string.Empty;
|
|
|
|
private bool everythingSelected;
|
2022-11-16 20:16:01 +08:00
|
|
|
|
2022-11-17 18:35:13 +08:00
|
|
|
public HistoryTextBox(int capacity = 100)
|
2022-11-15 20:06:02 +08:00
|
|
|
{
|
2022-11-17 18:35:13 +08:00
|
|
|
messageHistory = new LimitedCapacityQueue<string>(capacity);
|
2022-11-16 06:51:57 +08:00
|
|
|
|
2022-11-15 20:06:02 +08:00
|
|
|
Current.ValueChanged += text =>
|
|
|
|
{
|
2022-11-16 20:16:01 +08:00
|
|
|
if (string.IsNullOrEmpty(text.NewValue) || everythingSelected)
|
|
|
|
{
|
2022-11-17 18:35:13 +08:00
|
|
|
selectedIndex = HistoryCount;
|
2022-11-16 20:16:01 +08:00
|
|
|
everythingSelected = false;
|
|
|
|
}
|
2022-11-15 20:06:02 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-11-16 20:16:01 +08:00
|
|
|
protected override void OnTextSelectionChanged(TextSelectionType selectionType)
|
|
|
|
{
|
|
|
|
everythingSelected = SelectedText == Text;
|
|
|
|
|
|
|
|
base.OnTextSelectionChanged(selectionType);
|
|
|
|
}
|
|
|
|
|
2022-11-14 05:34:02 +08:00
|
|
|
protected override bool OnKeyDown(KeyDownEvent e)
|
|
|
|
{
|
|
|
|
switch (e.Key)
|
|
|
|
{
|
|
|
|
case Key.Up:
|
2022-11-17 18:35:13 +08:00
|
|
|
if (selectedIndex == 0)
|
2022-11-14 05:34:02 +08:00
|
|
|
return true;
|
|
|
|
|
2022-11-17 18:35:13 +08:00
|
|
|
if (selectedIndex == HistoryCount)
|
2022-11-16 20:16:01 +08:00
|
|
|
originalMessage = Text;
|
|
|
|
|
2022-11-17 18:35:13 +08:00
|
|
|
Text = messageHistory[--selectedIndex];
|
2022-11-14 05:34:02 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case Key.Down:
|
2022-11-17 18:35:13 +08:00
|
|
|
if (selectedIndex == HistoryCount)
|
2022-11-14 05:34:02 +08:00
|
|
|
return true;
|
|
|
|
|
2022-11-17 18:35:13 +08:00
|
|
|
if (selectedIndex == HistoryCount - 1)
|
2022-11-14 05:34:02 +08:00
|
|
|
{
|
2022-11-17 18:35:13 +08:00
|
|
|
selectedIndex = HistoryCount;
|
2022-11-14 05:34:02 +08:00
|
|
|
Text = originalMessage;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-17 18:35:13 +08:00
|
|
|
Text = messageHistory[++selectedIndex];
|
2022-11-14 05:34:02 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-15 20:06:02 +08:00
|
|
|
return base.OnKeyDown(e);
|
2022-11-14 05:34:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Commit()
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(Text))
|
2022-11-17 18:35:13 +08:00
|
|
|
messageHistory.Enqueue(Text);
|
2022-11-14 05:34:02 +08:00
|
|
|
|
2022-11-17 18:35:13 +08:00
|
|
|
selectedIndex = HistoryCount;
|
2022-11-14 05:34:02 +08:00
|
|
|
|
|
|
|
base.Commit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|