1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 16:47:24 +08:00
osu-lazer/osu.Game/Online/Chat/StandAloneChatDisplay.cs

153 lines
4.5 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.
2018-12-20 15:50:38 +08:00
using System;
2018-12-20 15:50:38 +08:00
using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
2018-12-20 17:21:37 +08:00
using osu.Framework.Graphics.UserInterface;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays.Chat;
2018-12-20 15:50:38 +08:00
using osuTK.Graphics;
namespace osu.Game.Online.Chat
{
/// <summary>
/// Display a chat channel in an insolated region.
/// </summary>
public class StandAloneChatDisplay : CompositeDrawable
{
public readonly Bindable<Channel> Channel = new Bindable<Channel>();
2018-12-20 15:50:38 +08:00
2018-12-25 16:14:56 +08:00
public Action Exit;
2018-12-20 17:21:37 +08:00
private readonly FocusedTextBox textbox;
protected ChannelManager ChannelManager;
private ScrollContainer scroll;
private DrawableChannel drawableChannel;
2018-12-25 16:14:56 +08:00
private readonly bool postingTextbox;
private const float textbox_height = 30;
2018-12-20 17:21:37 +08:00
/// <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)
2018-12-20 15:50:38 +08:00
{
this.postingTextbox = postingTextbox;
2018-12-20 15:50:38 +08:00
CornerRadius = 10;
Masking = true;
InternalChildren = new Drawable[]
{
new Box
{
Colour = Color4.Black,
Alpha = 0.8f,
RelativeSizeAxes = Axes.Both
},
};
2018-12-20 17:21:37 +08:00
if (postingTextbox)
{
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,
});
2018-12-25 16:14:56 +08:00
textbox.Exit += () => Exit?.Invoke();
2018-12-20 17:21:37 +08:00
}
2018-12-20 15:50:38 +08:00
Channel.BindValueChanged(channelChanged);
}
2018-12-20 17:21:37 +08:00
[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] == '/')
2018-12-21 13:02:29 +08:00
ChannelManager?.PostCommand(text.Substring(1), Channel);
2018-12-20 17:21:37 +08:00
else
2018-12-21 13:02:29 +08:00
ChannelManager?.PostMessage(text, target: Channel);
2018-12-20 17:21:37 +08:00
textbox.Text = string.Empty;
}
2018-12-20 15:50:38 +08:00
public void Contract()
{
this.FadeIn(300);
this.MoveToY(0, 500, Easing.OutQuint);
}
public void Expand()
{
this.FadeOut(200);
this.MoveToY(100, 500, Easing.In);
}
protected virtual ChatLine CreateMessage(Message message) => new StandAloneMessage(message);
2018-12-20 15:50:38 +08:00
private void channelChanged(Channel channel)
{
drawableChannel?.Expire();
2018-12-20 15:50:38 +08:00
if (channel == null) return;
AddInternal(drawableChannel = new StandAloneDrawableChannel(channel)
{
CreateChatLineAction = CreateMessage,
Padding = new MarginPadding { Bottom = postingTextbox ? textbox_height : 0 }
});
2018-12-20 15:50:38 +08:00
}
protected class StandAloneDrawableChannel : DrawableChannel
2018-12-20 15:50:38 +08:00
{
public Func<Message,ChatLine> CreateChatLineAction;
protected override ChatLine CreateChatLine(Message m) => CreateChatLineAction(m);
2018-12-20 15:50:38 +08:00
public StandAloneDrawableChannel(Channel channel)
: base(channel)
2018-12-20 15:50:38 +08:00
{
ChatLineFlow.Padding = new MarginPadding { Horizontal = 0 };
2018-12-20 15:50:38 +08:00
}
}
protected class StandAloneMessage : ChatLine
2018-12-20 15:50:38 +08:00
{
protected override float TextSize => 15;
2018-12-20 15:50:38 +08:00
protected override float HorizontalPadding => 10;
protected override float MessagePadding => 120;
2018-12-20 15:50:38 +08:00
public StandAloneMessage(Message message) : base(message)
2018-12-20 15:50:38 +08:00
{
}
}
}
}