1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 23:23:04 +08:00

Add textbox/posting support

This commit is contained in:
Dean Herbert 2018-12-20 18:21:37 +09:00
parent 65447d6f4a
commit bc8b0485d8
2 changed files with 76 additions and 5 deletions

View File

@ -1,6 +1,7 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>. // Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Allocation;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Game.Online.Chat; using osu.Game.Online.Chat;
using osu.Game.Users; using osu.Game.Users;
@ -31,24 +32,42 @@ namespace osu.Game.Tests.Visual
Id = 4, Id = 4,
}; };
[Cached]
private ChannelManager channelManager = new ChannelManager();
private readonly StandAloneChatDisplay chatDisplay;
private readonly StandAloneChatDisplay chatDisplay2;
public TestCaseStandAloneChatDisplay() public TestCaseStandAloneChatDisplay()
{ {
StandAloneChatDisplay chatDisplay; Add(channelManager);
Add(chatDisplay = new StandAloneChatDisplay Add(chatDisplay = new StandAloneChatDisplay
{ {
Anchor = Anchor.Centre, Anchor = Anchor.CentreLeft,
Origin = Anchor.Centre, Origin = Anchor.CentreLeft,
Margin = new MarginPadding(20),
Size = new Vector2(400, 80) Size = new Vector2(400, 80)
}); });
chatDisplay.Channel.Value = testChannel; Add(chatDisplay2 = new StandAloneChatDisplay(true)
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Margin = new MarginPadding(20),
Size = new Vector2(400, 150)
});
} }
protected override void LoadComplete() protected override void LoadComplete()
{ {
base.LoadComplete(); base.LoadComplete();
channelManager.CurrentChannel.Value = testChannel;
chatDisplay.Channel.Value = testChannel;
chatDisplay2.Channel.Value = testChannel;
AddStep("message from admin", () => testChannel.AddLocalEcho(new LocalEchoMessage AddStep("message from admin", () => testChannel.AddLocalEcho(new LocalEchoMessage
{ {
Sender = admin, Sender = admin,

View File

@ -8,9 +8,11 @@ using osu.Framework.Configuration;
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.UserInterface;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites; using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osuTK; using osuTK;
using osuTK.Graphics; using osuTK.Graphics;
@ -22,10 +24,20 @@ namespace osu.Game.Online.Chat
public class StandAloneChatDisplay : CompositeDrawable public class StandAloneChatDisplay : CompositeDrawable
{ {
public readonly Bindable<Channel> Channel = new Bindable<Channel>(); public readonly Bindable<Channel> Channel = new Bindable<Channel>();
private readonly FillFlowContainer messagesFlow; private readonly FillFlowContainer messagesFlow;
private Channel lastChannel; private Channel lastChannel;
public StandAloneChatDisplay() private readonly FocusedTextBox textbox;
protected ChannelManager ChannelManager;
/// <summary>
/// Construct a new instance.
/// </summary>
/// <param name="postingTextbox">Whether a textbox for posting new messages should be displayed.</param>
public StandAloneChatDisplay(bool postingTextbox = false)
{ {
CornerRadius = 10; CornerRadius = 10;
Masking = true; Masking = true;
@ -50,9 +62,49 @@ namespace osu.Game.Online.Chat
} }
}; };
const float textbox_height = 30;
if (postingTextbox)
{
messagesFlow.Y -= textbox_height;
AddInternal(textbox = new FocusedTextBox
{
RelativeSizeAxes = Axes.X,
Height = textbox_height,
PlaceholderText = "type your message",
OnCommit = postMessage,
ReleaseFocusOnCommit = false,
HoldFocus = true,
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
});
}
Channel.BindValueChanged(channelChanged); Channel.BindValueChanged(channelChanged);
} }
[BackgroundDependencyLoader(true)]
private void load(ChannelManager manager)
{
if (ChannelManager == null)
ChannelManager = manager;
}
private void postMessage(TextBox sender, bool newtext)
{
var text = textbox.Text.Trim();
if (string.IsNullOrWhiteSpace(text))
return;
if (text[0] == '/')
ChannelManager?.PostCommand(text.Substring(1));
else
ChannelManager?.PostMessage(text);
textbox.Text = string.Empty;
}
public void Contract() public void Contract()
{ {
this.FadeIn(300); this.FadeIn(300);