2022-11-13 21:34:02 +00: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 15:53:35 +00:00
|
|
|
using osu.Game.Utils;
|
2022-11-13 21:34:02 +00:00
|
|
|
using osuTK.Input;
|
|
|
|
|
2022-11-15 12:06:02 +00:00
|
|
|
namespace osu.Game.Graphics.UserInterface
|
2022-11-13 21:34:02 +00:00
|
|
|
{
|
2022-11-20 11:29:41 +00: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-15 15:12:24 +00:00
|
|
|
public partial class HistoryTextBox : FocusedTextBox
|
2022-11-13 21:34:02 +00:00
|
|
|
{
|
2022-11-17 10:35:13 +00:00
|
|
|
private readonly LimitedCapacityQueue<string> messageHistory;
|
2022-11-16 12:16:01 +00:00
|
|
|
|
2022-11-17 10:35:13 +00:00
|
|
|
public int HistoryCount => messageHistory.Count;
|
2022-11-16 12:16:01 +00:00
|
|
|
|
2022-11-17 10:35:13 +00:00
|
|
|
private int selectedIndex;
|
2022-11-17 09:18:17 +00:00
|
|
|
|
|
|
|
private string originalMessage = string.Empty;
|
2022-11-16 12:16:01 +00:00
|
|
|
|
2022-11-20 11:29:41 +00: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 10:35:13 +00:00
|
|
|
public HistoryTextBox(int capacity = 100)
|
2022-11-15 12:06:02 +00:00
|
|
|
{
|
2022-11-17 10:35:13 +00:00
|
|
|
messageHistory = new LimitedCapacityQueue<string>(capacity);
|
2022-11-21 09:11:26 +00:00
|
|
|
|
|
|
|
Current.ValueChanged += text =>
|
|
|
|
{
|
2022-11-21 09:17:28 +00:00
|
|
|
if (selectedIndex != HistoryCount && text.NewValue != messageHistory[selectedIndex])
|
2022-11-21 09:11:26 +00:00
|
|
|
{
|
|
|
|
selectedIndex = HistoryCount;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-11-13 21:34:02 +00:00
|
|
|
protected override bool OnKeyDown(KeyDownEvent e)
|
|
|
|
{
|
2023-01-03 01:44:34 +00:00
|
|
|
if (e.ControlPressed || e.AltPressed || e.SuperPressed || e.ShiftPressed)
|
|
|
|
return false;
|
|
|
|
|
2022-11-13 21:34:02 +00:00
|
|
|
switch (e.Key)
|
|
|
|
{
|
|
|
|
case Key.Up:
|
2022-11-17 10:35:13 +00:00
|
|
|
if (selectedIndex == 0)
|
2022-11-13 21:34:02 +00:00
|
|
|
return true;
|
|
|
|
|
2022-11-17 10:35:13 +00:00
|
|
|
if (selectedIndex == HistoryCount)
|
2022-11-16 12:16:01 +00:00
|
|
|
originalMessage = Text;
|
|
|
|
|
2022-11-17 10:35:13 +00:00
|
|
|
Text = messageHistory[--selectedIndex];
|
2022-11-13 21:34:02 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case Key.Down:
|
2022-11-17 10:35:13 +00:00
|
|
|
if (selectedIndex == HistoryCount)
|
2022-11-13 21:34:02 +00:00
|
|
|
return true;
|
|
|
|
|
2022-11-17 10:35:13 +00:00
|
|
|
if (selectedIndex == HistoryCount - 1)
|
2022-11-13 21:34:02 +00:00
|
|
|
{
|
2022-11-17 10:35:13 +00:00
|
|
|
selectedIndex = HistoryCount;
|
2022-11-13 21:34:02 +00:00
|
|
|
Text = originalMessage;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-17 10:35:13 +00:00
|
|
|
Text = messageHistory[++selectedIndex];
|
2022-11-13 21:34:02 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-15 12:06:02 +00:00
|
|
|
return base.OnKeyDown(e);
|
2022-11-13 21:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Commit()
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(Text))
|
2022-11-17 10:35:13 +00:00
|
|
|
messageHistory.Enqueue(Text);
|
2022-11-13 21:34:02 +00:00
|
|
|
|
2022-11-17 10:35:13 +00:00
|
|
|
selectedIndex = HistoryCount;
|
2022-11-13 21:34:02 +00:00
|
|
|
|
|
|
|
base.Commit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|