mirror of
https://github.com/ppy/osu.git
synced 2025-02-16 03:02:56 +08:00
Merge pull request #17545 from jai-x/new-chat-textbox
Implement `ChatTextBox` for new chat design
This commit is contained in:
commit
969f7e121a
122
osu.Game.Tests/Visual/Online/TestSceneChatTextBox.cs
Normal file
122
osu.Game.Tests/Visual/Online/TestSceneChatTextBox.cs
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
// 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 NUnit.Framework;
|
||||||
|
using osu.Framework.Allocation;
|
||||||
|
using osu.Framework.Bindables;
|
||||||
|
using osu.Framework.Graphics;
|
||||||
|
using osu.Framework.Graphics.Containers;
|
||||||
|
using osu.Game.Graphics;
|
||||||
|
using osu.Game.Graphics.Sprites;
|
||||||
|
using osu.Game.Online.API.Requests.Responses;
|
||||||
|
using osu.Game.Online.Chat;
|
||||||
|
using osu.Game.Overlays;
|
||||||
|
using osu.Game.Overlays.Chat;
|
||||||
|
|
||||||
|
namespace osu.Game.Tests.Visual.Online
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public class TestSceneChatTextBox : OsuTestScene
|
||||||
|
{
|
||||||
|
[Cached]
|
||||||
|
private readonly OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Pink);
|
||||||
|
|
||||||
|
[Cached]
|
||||||
|
private readonly Bindable<Channel> currentChannel = new Bindable<Channel>();
|
||||||
|
|
||||||
|
private OsuSpriteText commitText;
|
||||||
|
private OsuSpriteText searchText;
|
||||||
|
private ChatTextBar bar;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
Schedule(() =>
|
||||||
|
{
|
||||||
|
Child = new GridContainer
|
||||||
|
{
|
||||||
|
Anchor = Anchor.Centre,
|
||||||
|
Origin = Anchor.Centre,
|
||||||
|
RelativeSizeAxes = Axes.X,
|
||||||
|
AutoSizeAxes = Axes.Y,
|
||||||
|
RowDimensions = new[]
|
||||||
|
{
|
||||||
|
new Dimension(GridSizeMode.Absolute, 30),
|
||||||
|
new Dimension(GridSizeMode.AutoSize),
|
||||||
|
},
|
||||||
|
Content = new[]
|
||||||
|
{
|
||||||
|
new Drawable[]
|
||||||
|
{
|
||||||
|
new GridContainer
|
||||||
|
{
|
||||||
|
RelativeSizeAxes = Axes.Both,
|
||||||
|
ColumnDimensions = new[]
|
||||||
|
{
|
||||||
|
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[]
|
||||||
|
{
|
||||||
|
bar = new ChatTextBar
|
||||||
|
{
|
||||||
|
Anchor = Anchor.TopCentre,
|
||||||
|
Origin = Anchor.TopCentre,
|
||||||
|
Width = 0.99f,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
bar.OnChatMessageCommitted += text =>
|
||||||
|
{
|
||||||
|
commitText.Text = $"{nameof(bar.OnChatMessageCommitted)}: {text}";
|
||||||
|
commitText.FadeOutFromOne(1000, Easing.InQuint);
|
||||||
|
};
|
||||||
|
|
||||||
|
bar.OnSearchTermsChanged += text =>
|
||||||
|
{
|
||||||
|
searchText.Text = $"{nameof(bar.OnSearchTermsChanged)}: {text}";
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestVisual()
|
||||||
|
{
|
||||||
|
AddStep("Public Channel", () => currentChannel.Value = createPublicChannel("#osu"));
|
||||||
|
AddStep("Public Channel Long Name", () => currentChannel.Value = createPublicChannel("#public-channel-long-name"));
|
||||||
|
AddStep("Private Channel", () => currentChannel.Value = createPrivateChannel("peppy", 2));
|
||||||
|
AddStep("Private Long Name", () => currentChannel.Value = createPrivateChannel("test user long name", 3));
|
||||||
|
|
||||||
|
AddStep("Chat Mode Channel", () => bar.ShowSearch.Value = false);
|
||||||
|
AddStep("Chat Mode Search", () => bar.ShowSearch.Value = true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Channel createPublicChannel(string name)
|
||||||
|
=> new Channel { Name = name, Type = ChannelType.Public, Id = 1234 };
|
||||||
|
|
||||||
|
private static Channel createPrivateChannel(string username, int id)
|
||||||
|
=> new Channel(new APIUser { Id = id, Username = username });
|
||||||
|
}
|
||||||
|
}
|
163
osu.Game/Overlays/Chat/ChatTextBar.cs
Normal file
163
osu.Game/Overlays/Chat/ChatTextBar.cs
Normal file
@ -0,0 +1,163 @@
|
|||||||
|
// 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.
|
||||||
|
|
||||||
|
#nullable enable
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using osu.Framework.Allocation;
|
||||||
|
using osu.Framework.Bindables;
|
||||||
|
using osu.Framework.Graphics;
|
||||||
|
using osu.Framework.Graphics.Containers;
|
||||||
|
using osu.Framework.Graphics.Shapes;
|
||||||
|
using osu.Framework.Graphics.Sprites;
|
||||||
|
using osu.Framework.Graphics.UserInterface;
|
||||||
|
using osu.Game.Graphics.Containers;
|
||||||
|
using osu.Game.Online.Chat;
|
||||||
|
using osuTK;
|
||||||
|
|
||||||
|
namespace osu.Game.Overlays.Chat
|
||||||
|
{
|
||||||
|
public class ChatTextBar : Container
|
||||||
|
{
|
||||||
|
public readonly BindableBool ShowSearch = new BindableBool();
|
||||||
|
|
||||||
|
public event Action<string>? OnChatMessageCommitted;
|
||||||
|
|
||||||
|
public event Action<string>? OnSearchTermsChanged;
|
||||||
|
|
||||||
|
[Resolved]
|
||||||
|
private Bindable<Channel> currentChannel { get; set; } = null!;
|
||||||
|
|
||||||
|
private OsuTextFlowContainer chattingTextContainer = null!;
|
||||||
|
private Container searchIconContainer = null!;
|
||||||
|
private ChatTextBox chatTextBox = null!;
|
||||||
|
|
||||||
|
private const float chatting_text_width = 180;
|
||||||
|
private const float search_icon_width = 40;
|
||||||
|
|
||||||
|
[BackgroundDependencyLoader]
|
||||||
|
private void load(OverlayColourProvider colourProvider)
|
||||||
|
{
|
||||||
|
RelativeSizeAxes = Axes.X;
|
||||||
|
Height = 60;
|
||||||
|
|
||||||
|
Children = new Drawable[]
|
||||||
|
{
|
||||||
|
new Box
|
||||||
|
{
|
||||||
|
RelativeSizeAxes = Axes.Both,
|
||||||
|
Colour = colourProvider.Background5,
|
||||||
|
},
|
||||||
|
new GridContainer
|
||||||
|
{
|
||||||
|
RelativeSizeAxes = Axes.Both,
|
||||||
|
ColumnDimensions = new[]
|
||||||
|
{
|
||||||
|
new Dimension(GridSizeMode.AutoSize),
|
||||||
|
new Dimension(GridSizeMode.AutoSize),
|
||||||
|
new Dimension(),
|
||||||
|
},
|
||||||
|
Content = new[]
|
||||||
|
{
|
||||||
|
new Drawable[]
|
||||||
|
{
|
||||||
|
chattingTextContainer = new OsuTextFlowContainer(t => t.Font = t.Font.With(size: 20))
|
||||||
|
{
|
||||||
|
Masking = true,
|
||||||
|
Width = chatting_text_width,
|
||||||
|
Padding = new MarginPadding { Left = 10 },
|
||||||
|
RelativeSizeAxes = Axes.Y,
|
||||||
|
TextAnchor = Anchor.CentreRight,
|
||||||
|
Anchor = Anchor.CentreLeft,
|
||||||
|
Origin = Anchor.CentreLeft,
|
||||||
|
Colour = colourProvider.Background1,
|
||||||
|
},
|
||||||
|
searchIconContainer = new Container
|
||||||
|
{
|
||||||
|
RelativeSizeAxes = Axes.Y,
|
||||||
|
Width = search_icon_width,
|
||||||
|
Child = new SpriteIcon
|
||||||
|
{
|
||||||
|
Icon = FontAwesome.Solid.Search,
|
||||||
|
Origin = Anchor.CentreRight,
|
||||||
|
Anchor = Anchor.CentreRight,
|
||||||
|
Size = new Vector2(20),
|
||||||
|
Margin = new MarginPadding { Right = 2 },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
new Container
|
||||||
|
{
|
||||||
|
RelativeSizeAxes = Axes.Both,
|
||||||
|
Padding = new MarginPadding { Right = 5 },
|
||||||
|
Child = chatTextBox = new ChatTextBox
|
||||||
|
{
|
||||||
|
Anchor = Anchor.CentreLeft,
|
||||||
|
Origin = Anchor.CentreLeft,
|
||||||
|
RelativeSizeAxes = Axes.X,
|
||||||
|
ShowSearch = { BindTarget = ShowSearch },
|
||||||
|
HoldFocus = true,
|
||||||
|
ReleaseFocusOnCommit = false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void LoadComplete()
|
||||||
|
{
|
||||||
|
base.LoadComplete();
|
||||||
|
|
||||||
|
chatTextBox.Current.ValueChanged += chatTextBoxChange;
|
||||||
|
chatTextBox.OnCommit += chatTextBoxCommit;
|
||||||
|
|
||||||
|
ShowSearch.BindValueChanged(change =>
|
||||||
|
{
|
||||||
|
bool showSearch = change.NewValue;
|
||||||
|
|
||||||
|
chattingTextContainer.FadeTo(showSearch ? 0 : 1);
|
||||||
|
searchIconContainer.FadeTo(showSearch ? 1 : 0);
|
||||||
|
|
||||||
|
// Clear search terms if any exist when switching back to chat mode
|
||||||
|
if (!showSearch)
|
||||||
|
OnSearchTermsChanged?.Invoke(string.Empty);
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
currentChannel.BindValueChanged(change =>
|
||||||
|
{
|
||||||
|
Channel newChannel = change.NewValue;
|
||||||
|
|
||||||
|
switch (newChannel?.Type)
|
||||||
|
{
|
||||||
|
case ChannelType.Public:
|
||||||
|
chattingTextContainer.Text = $"chatting in {newChannel.Name}";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ChannelType.PM:
|
||||||
|
chattingTextContainer.Text = $"chatting with {newChannel.Name}";
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
chattingTextContainer.Text = string.Empty;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void chatTextBoxChange(ValueChangedEvent<string> change)
|
||||||
|
{
|
||||||
|
if (ShowSearch.Value)
|
||||||
|
OnSearchTermsChanged?.Invoke(change.NewValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void chatTextBoxCommit(TextBox sender, bool newText)
|
||||||
|
{
|
||||||
|
if (ShowSearch.Value)
|
||||||
|
return;
|
||||||
|
|
||||||
|
OnChatMessageCommitted?.Invoke(sender.Text);
|
||||||
|
sender.Text = string.Empty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
38
osu.Game/Overlays/Chat/ChatTextBox.cs
Normal file
38
osu.Game/Overlays/Chat/ChatTextBox.cs
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
// 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.
|
||||||
|
|
||||||
|
#nullable enable
|
||||||
|
|
||||||
|
using osu.Framework.Bindables;
|
||||||
|
using osu.Game.Graphics.UserInterface;
|
||||||
|
|
||||||
|
namespace osu.Game.Overlays.Chat
|
||||||
|
{
|
||||||
|
public class ChatTextBox : FocusedTextBox
|
||||||
|
{
|
||||||
|
public readonly BindableBool ShowSearch = new BindableBool();
|
||||||
|
|
||||||
|
public override bool HandleLeftRightArrows => !ShowSearch.Value;
|
||||||
|
|
||||||
|
protected override void LoadComplete()
|
||||||
|
{
|
||||||
|
base.LoadComplete();
|
||||||
|
|
||||||
|
ShowSearch.BindValueChanged(change =>
|
||||||
|
{
|
||||||
|
bool showSearch = change.NewValue;
|
||||||
|
|
||||||
|
PlaceholderText = showSearch ? "type here to search" : "type here";
|
||||||
|
Text = string.Empty;
|
||||||
|
}, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Commit()
|
||||||
|
{
|
||||||
|
if (ShowSearch.Value)
|
||||||
|
return;
|
||||||
|
|
||||||
|
base.Commit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user