2022-03-29 20:36:08 +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.
|
|
|
|
|
2022-11-13 19:49:26 +00:00
|
|
|
using System.Collections.Generic;
|
2022-03-29 20:36:08 +00:00
|
|
|
using osu.Framework.Bindables;
|
2022-11-13 19:49:26 +00:00
|
|
|
using osu.Framework.Input.Events;
|
2022-03-29 20:36:08 +00:00
|
|
|
using osu.Game.Graphics.UserInterface;
|
2022-05-27 05:23:03 +00:00
|
|
|
using osu.Game.Resources.Localisation.Web;
|
2022-11-13 19:49:26 +00:00
|
|
|
using osuTK.Input;
|
2022-03-29 20:36:08 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Chat
|
|
|
|
{
|
|
|
|
public class ChatTextBox : FocusedTextBox
|
|
|
|
{
|
2022-11-13 19:49:26 +00:00
|
|
|
private readonly List<string> messageHistory = new List<string>();
|
|
|
|
|
|
|
|
private int messageIndex = -1;
|
|
|
|
|
|
|
|
private string originalMessage = string.Empty;
|
|
|
|
|
2022-03-29 20:36:08 +00:00
|
|
|
public readonly BindableBool ShowSearch = new BindableBool();
|
|
|
|
|
|
|
|
public override bool HandleLeftRightArrows => !ShowSearch.Value;
|
|
|
|
|
2022-08-02 04:56:02 +00:00
|
|
|
protected override bool ClearTextOnBackKey => false;
|
|
|
|
|
2022-03-29 20:36:08 +00:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
ShowSearch.BindValueChanged(change =>
|
|
|
|
{
|
2022-04-02 16:14:27 +00:00
|
|
|
bool showSearch = change.NewValue;
|
|
|
|
|
2022-05-27 05:23:03 +00:00
|
|
|
PlaceholderText = showSearch ? HomeStrings.SearchPlaceholder : ChatStrings.InputPlaceholder;
|
2022-04-02 16:14:27 +00:00
|
|
|
Text = string.Empty;
|
2022-03-29 20:36:08 +00:00
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
|
2022-11-13 19:49:26 +00:00
|
|
|
protected override bool OnKeyDown(KeyDownEvent e)
|
|
|
|
{
|
|
|
|
/* Behavior:
|
|
|
|
* add when on last element -> last element stays
|
|
|
|
* subtract when on first element -> sets to original text
|
|
|
|
* reset indexing when Text is set to Empty
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (e.Key)
|
|
|
|
{
|
|
|
|
case Key.Up:
|
|
|
|
if (messageIndex == -1)
|
|
|
|
originalMessage = Text;
|
|
|
|
|
|
|
|
if (messageIndex == messageHistory.Count - 1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
Text = messageHistory[++messageIndex];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case Key.Down:
|
|
|
|
if (messageIndex == -1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (messageIndex == 0)
|
|
|
|
{
|
|
|
|
messageIndex = -1;
|
|
|
|
Text = originalMessage;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Text = messageHistory[--messageIndex];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onKeyDown = base.OnKeyDown(e);
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(Text))
|
|
|
|
messageIndex = -1;
|
|
|
|
|
|
|
|
return onKeyDown;
|
|
|
|
}
|
|
|
|
|
2022-03-29 20:36:08 +00:00
|
|
|
protected override void Commit()
|
|
|
|
{
|
|
|
|
if (ShowSearch.Value)
|
|
|
|
return;
|
|
|
|
|
2022-11-13 19:49:26 +00:00
|
|
|
messageHistory.Insert(0, Text);
|
|
|
|
messageIndex = -1;
|
|
|
|
|
2022-03-29 20:36:08 +00:00
|
|
|
base.Commit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|