Use OnChatMessageCommit & OnSearchTermsChanged events in ChatTextBar

This commit is contained in:
Jai Sharma
2022-04-02 17:15:19 +01:00
parent b9421d1415
commit 2297073b7e
2 changed files with 60 additions and 10 deletions

View File

@ -25,6 +25,7 @@ namespace osu.Game.Tests.Visual.Online
private readonly Bindable<Channel> currentChannel = new Bindable<Channel>(); private readonly Bindable<Channel> currentChannel = new Bindable<Channel>();
private OsuSpriteText commitText; private OsuSpriteText commitText;
private OsuSpriteText searchText;
private ChatTextBar bar; private ChatTextBar bar;
[SetUp] [SetUp]
@ -47,11 +48,32 @@ namespace osu.Game.Tests.Visual.Online
{ {
new Drawable[] new Drawable[]
{ {
commitText = new OsuSpriteText new GridContainer
{ {
Anchor = Anchor.TopCentre, RelativeSizeAxes = Axes.Both,
Origin = Anchor.TopCentre, ColumnDimensions = new[]
Font = OsuFont.Default.With(size: 20), {
new Dimension(),
new Dimension(),
},
Content = new[]
{
new Drawable[]
{
commitText = new OsuSpriteText
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Font = OsuFont.Default.With(size: 20),
},
searchText = new OsuSpriteText
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Font = OsuFont.Default.With(size: 20),
},
},
},
}, },
}, },
new Drawable[] new Drawable[]
@ -66,12 +88,17 @@ namespace osu.Game.Tests.Visual.Online
}, },
}; };
bar.TextBox.OnCommit += (sender, newText) => bar.OnChatMessageCommit += (sender, newText) =>
{ {
commitText.Text = $"Commit: {sender.Text}"; commitText.Text = $"OnChatMessageCommit: {sender.Text}";
commitText.FadeOutFromOne(1000, Easing.InQuint); commitText.FadeOutFromOne(1000, Easing.InQuint);
sender.Text = string.Empty; sender.Text = string.Empty;
}; };
bar.OnSearchTermsChanged += (text) =>
{
searchText.Text = $"OnSearchTermsChanged: {text}";
};
}); });
} }

View File

@ -3,15 +3,15 @@
#nullable enable #nullable enable
using System;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics; using osu.Framework.Graphics.UserInterface;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Online.Chat; using osu.Game.Online.Chat;
using osuTK; using osuTK;
@ -21,13 +21,16 @@ namespace osu.Game.Overlays.Chat
{ {
public readonly BindableBool ShowSearch = new BindableBool(); public readonly BindableBool ShowSearch = new BindableBool();
public ChatTextBox TextBox { get; private set; } = null!; public event TextBox.OnCommitHandler? OnChatMessageCommit;
public event Action<string>? OnSearchTermsChanged;
[Resolved] [Resolved]
private Bindable<Channel> currentChannel { get; set; } = null!; private Bindable<Channel> currentChannel { get; set; } = null!;
private OsuTextFlowContainer chattingTextContainer = null!; private OsuTextFlowContainer chattingTextContainer = null!;
private Container searchIconContainer = null!; private Container searchIconContainer = null!;
private ChatTextBox chatTextBox = null!;
private const float chatting_text_width = 180; private const float chatting_text_width = 180;
private const float search_icon_width = 40; private const float search_icon_width = 40;
@ -86,7 +89,7 @@ namespace osu.Game.Overlays.Chat
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Right = 5 }, Padding = new MarginPadding { Right = 5 },
Child = TextBox = new ChatTextBox Child = chatTextBox = new ChatTextBox
{ {
Anchor = Anchor.CentreLeft, Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft, Origin = Anchor.CentreLeft,
@ -106,12 +109,20 @@ namespace osu.Game.Overlays.Chat
{ {
base.LoadComplete(); base.LoadComplete();
chatTextBox.Current.ValueChanged += chatTextBoxChange;
chatTextBox.OnCommit += chatTextBoxCommit;
ShowSearch.BindValueChanged(change => ShowSearch.BindValueChanged(change =>
{ {
bool showSearch = change.NewValue; bool showSearch = change.NewValue;
chattingTextContainer.FadeTo(showSearch ? 0 : 1); chattingTextContainer.FadeTo(showSearch ? 0 : 1);
searchIconContainer.FadeTo(showSearch ? 1 : 0); searchIconContainer.FadeTo(showSearch ? 1 : 0);
// Clear search terms if any exist when switching back to chat mode
if (!showSearch)
OnSearchTermsChanged?.Invoke(string.Empty);
}, true); }, true);
currentChannel.BindValueChanged(change => currentChannel.BindValueChanged(change =>
@ -134,5 +145,17 @@ namespace osu.Game.Overlays.Chat
} }
}, true); }, true);
} }
private void chatTextBoxChange(ValueChangedEvent<string> change)
{
if (ShowSearch.Value)
OnSearchTermsChanged?.Invoke(change.NewValue);
}
private void chatTextBoxCommit(TextBox sender, bool newText)
{
if (!ShowSearch.Value)
OnChatMessageCommit?.Invoke(sender, newText);
}
} }
} }