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-20 19:29:41 +08:00
|
|
|
/// <summary>
|
|
|
|
/// A <see cref="FocusedTextBox"/> which additionally retains a history of text committed, up to a limit
|
|
|
|
/// (100 by default, specified in constructor).
|
|
|
|
/// The history of committed text can be navigated using up/down arrows.
|
|
|
|
/// This resembles the operation of command-line terminals.
|
|
|
|
/// </summary>
|
2022-11-24 13:32:20 +08:00
|
|
|
public partial 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;
|
2022-11-16 20:16:01 +08:00
|
|
|
|
2022-11-20 19:29:41 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new <see cref="HistoryTextBox"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="capacity">
|
|
|
|
/// The maximum number of committed lines to keep in history.
|
|
|
|
/// When exceeded, the oldest lines in history will be dropped to make space for new ones.
|
|
|
|
/// </param>
|
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-21 17:11:26 +08:00
|
|
|
|
|
|
|
Current.ValueChanged += text =>
|
|
|
|
{
|
2022-11-21 17:17:28 +08:00
|
|
|
if (selectedIndex != HistoryCount && text.NewValue != messageHistory[selectedIndex])
|
2022-11-21 17:11:26 +08:00
|
|
|
{
|
|
|
|
selectedIndex = HistoryCount;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-11-14 05:34:02 +08:00
|
|
|
protected override bool OnKeyDown(KeyDownEvent e)
|
|
|
|
{
|
2023-01-03 09:44:34 +08:00
|
|
|
if (e.ControlPressed || e.AltPressed || e.SuperPressed || e.ShiftPressed)
|
|
|
|
return false;
|
|
|
|
|
2022-11-14 05:34:02 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|