1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-08 12:07:51 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/HistoryTextBox.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

74 lines
2.0 KiB
C#
Raw Normal View History

// 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;
namespace osu.Game.Graphics.UserInterface
{
2022-11-15 23:12:24 +08:00
public class HistoryTextBox : FocusedTextBox
{
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;
private string originalMessage = string.Empty;
2022-11-15 23:12:24 +08:00
public HistoryTextBox()
{
Current.ValueChanged += text =>
{
if (string.IsNullOrEmpty(text.NewValue))
2022-11-16 01:10:43 +08:00
historyIndex = messageHistory.Count;
};
}
protected override bool OnKeyDown(KeyDownEvent e)
{
switch (e.Key)
{
case Key.Up:
2022-11-16 01:10:43 +08:00
if (historyIndex == messageHistory.Count)
originalMessage = Text;
2022-11-16 01:10:43 +08:00
if (historyIndex == 0)
return true;
2022-11-16 01:10:43 +08:00
Text = messageHistory[--historyIndex];
return true;
case Key.Down:
2022-11-16 01:10:43 +08:00
if (historyIndex == messageHistory.Count)
return true;
2022-11-16 01:10:43 +08:00
if (historyIndex == messageHistory.Count - 1)
{
2022-11-16 01:10:43 +08:00
historyIndex = messageHistory.Count;
Text = originalMessage;
return true;
}
2022-11-16 01:10:43 +08:00
Text = messageHistory[++historyIndex];
return true;
}
return base.OnKeyDown(e);
}
protected override void Commit()
{
if (!string.IsNullOrEmpty(Text))
2022-11-16 01:10:43 +08:00
messageHistory.Add(Text);
2022-11-16 01:10:43 +08:00
historyIndex = messageHistory.Count;
base.Commit();
}
}
}