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 System.Collections.Generic;
|
|
|
|
using osu.Framework.Input.Events;
|
|
|
|
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
|
|
|
{
|
|
|
|
private readonly List<string> messageHistory = new List<string>();
|
|
|
|
|
2022-11-15 23:12:24 +08:00
|
|
|
public IReadOnlyList<string> MessageHistory => messageHistory;
|
|
|
|
|
2022-11-16 01:10:43 +08:00
|
|
|
private int historyIndex;
|
2022-11-14 05:34:02 +08:00
|
|
|
|
|
|
|
private string originalMessage = string.Empty;
|
|
|
|
|
2022-11-15 23:12:24 +08:00
|
|
|
public HistoryTextBox()
|
2022-11-15 20:06:02 +08:00
|
|
|
{
|
|
|
|
Current.ValueChanged += text =>
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(text.NewValue))
|
2022-11-16 01:10:43 +08:00
|
|
|
historyIndex = messageHistory.Count;
|
2022-11-15 20:06:02 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-11-14 05:34:02 +08:00
|
|
|
protected override bool OnKeyDown(KeyDownEvent e)
|
|
|
|
{
|
|
|
|
switch (e.Key)
|
|
|
|
{
|
|
|
|
case Key.Up:
|
2022-11-16 01:10:43 +08:00
|
|
|
if (historyIndex == messageHistory.Count)
|
2022-11-14 05:34:02 +08:00
|
|
|
originalMessage = Text;
|
|
|
|
|
2022-11-16 01:10:43 +08:00
|
|
|
if (historyIndex == 0)
|
2022-11-14 05:34:02 +08:00
|
|
|
return true;
|
|
|
|
|
2022-11-16 01:10:43 +08:00
|
|
|
Text = messageHistory[--historyIndex];
|
2022-11-14 05:34:02 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case Key.Down:
|
2022-11-16 01:10:43 +08:00
|
|
|
if (historyIndex == messageHistory.Count)
|
2022-11-14 05:34:02 +08:00
|
|
|
return true;
|
|
|
|
|
2022-11-16 01:10:43 +08:00
|
|
|
if (historyIndex == messageHistory.Count - 1)
|
2022-11-14 05:34:02 +08:00
|
|
|
{
|
2022-11-16 01:10:43 +08:00
|
|
|
historyIndex = messageHistory.Count;
|
2022-11-14 05:34:02 +08:00
|
|
|
Text = originalMessage;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-16 01:10:43 +08:00
|
|
|
Text = messageHistory[++historyIndex];
|
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-16 01:10:43 +08:00
|
|
|
messageHistory.Add(Text);
|
2022-11-14 05:34:02 +08:00
|
|
|
|
2022-11-16 01:10:43 +08:00
|
|
|
historyIndex = messageHistory.Count;
|
2022-11-14 05:34:02 +08:00
|
|
|
|
|
|
|
base.Commit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|