2019-01-24 16:43:03 +08: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.
|
2018-12-20 15:50:38 +08:00
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2018-12-21 16:54:12 +08:00
|
|
|
using System;
|
2018-12-20 15:50:38 +08:00
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 18:04:31 +08:00
|
|
|
using osu.Framework.Bindables;
|
2018-12-20 15:50:38 +08:00
|
|
|
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;
|
2021-08-18 17:25:21 +08:00
|
|
|
using osu.Framework.Input.Events;
|
2019-10-22 23:24:08 +08:00
|
|
|
using osu.Game.Graphics;
|
2018-12-20 17:21:37 +08:00
|
|
|
using osu.Game.Graphics.UserInterface;
|
2018-12-21 16:54:12 +08:00
|
|
|
using osu.Game.Overlays.Chat;
|
2022-01-28 12:53:48 +08:00
|
|
|
using osu.Game.Resources.Localisation.Web;
|
2018-12-20 15:50:38 +08:00
|
|
|
using osuTK.Graphics;
|
2022-05-04 18:09:22 +08:00
|
|
|
using osuTK.Input;
|
2018-12-20 15:50:38 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Online.Chat
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Display a chat channel in an insolated region.
|
|
|
|
/// </summary>
|
|
|
|
public partial class StandAloneChatDisplay : CompositeDrawable
|
|
|
|
{
|
2018-12-21 16:54:12 +08:00
|
|
|
public readonly Bindable<Channel> Channel = new Bindable<Channel>();
|
2018-12-20 15:50:38 +08:00
|
|
|
|
2021-12-10 13:15:00 +08:00
|
|
|
protected readonly ChatTextBox TextBox;
|
2018-12-20 17:21:37 +08:00
|
|
|
|
2022-01-13 16:10:48 +08:00
|
|
|
private ChannelManager channelManager;
|
2018-12-20 17:21:37 +08:00
|
|
|
|
2020-03-23 11:03:33 +08:00
|
|
|
private StandAloneDrawableChannel drawableChannel;
|
2018-12-21 16:54:12 +08:00
|
|
|
|
2021-12-10 13:15:00 +08:00
|
|
|
private readonly bool postingTextBox;
|
2018-12-25 16:14:56 +08:00
|
|
|
|
2021-08-17 13:57:38 +08:00
|
|
|
protected readonly Box Background;
|
|
|
|
|
2021-12-10 13:15:00 +08:00
|
|
|
private const float text_box_height = 30;
|
2018-12-21 16:54:12 +08:00
|
|
|
|
2018-12-20 17:21:37 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Construct a new instance.
|
|
|
|
/// </summary>
|
2021-12-10 13:15:00 +08:00
|
|
|
/// <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
|
|
|
{
|
2021-11-16 16:50:51 +08:00
|
|
|
const float corner_radius = 10;
|
|
|
|
|
2021-12-10 13:15:00 +08:00
|
|
|
this.postingTextBox = postingTextBox;
|
2021-11-16 16:50:51 +08:00
|
|
|
CornerRadius = corner_radius;
|
2018-12-20 15:50:38 +08:00
|
|
|
Masking = true;
|
|
|
|
|
|
|
|
InternalChildren = new Drawable[]
|
|
|
|
{
|
2021-08-17 13:57:38 +08:00
|
|
|
Background = new Box
|
2018-12-20 15:50:38 +08:00
|
|
|
{
|
|
|
|
Colour = Color4.Black,
|
|
|
|
Alpha = 0.8f,
|
|
|
|
RelativeSizeAxes = Axes.Both
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-12-10 13:15:00 +08:00
|
|
|
if (postingTextBox)
|
2018-12-20 17:21:37 +08:00
|
|
|
{
|
2021-12-10 13:15:00 +08:00
|
|
|
AddInternal(TextBox = new ChatTextBox
|
2018-12-20 17:21:37 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
2021-12-10 13:15:00 +08:00
|
|
|
Height = text_box_height,
|
2022-01-28 12:53:48 +08:00
|
|
|
PlaceholderText = ChatStrings.InputPlaceholder,
|
2021-11-16 16:50:51 +08:00
|
|
|
CornerRadius = corner_radius,
|
2018-12-20 17:21:37 +08:00
|
|
|
ReleaseFocusOnCommit = false,
|
|
|
|
HoldFocus = true,
|
|
|
|
Anchor = Anchor.BottomLeft,
|
|
|
|
Origin = Anchor.BottomLeft,
|
|
|
|
});
|
2020-10-01 11:51:33 +08:00
|
|
|
|
2021-12-10 13:15:00 +08:00
|
|
|
TextBox.OnCommit += postMessage;
|
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)
|
|
|
|
{
|
2022-01-13 16:10:48 +08:00
|
|
|
channelManager ??= manager;
|
2018-12-20 17:21:37 +08:00
|
|
|
}
|
|
|
|
|
2020-03-23 11:03:33 +08:00
|
|
|
protected virtual StandAloneDrawableChannel CreateDrawableChannel(Channel channel) =>
|
|
|
|
new StandAloneDrawableChannel(channel);
|
|
|
|
|
2021-12-10 13:15:00 +08:00
|
|
|
private void postMessage(TextBox sender, bool newText)
|
2018-12-20 17:21:37 +08:00
|
|
|
{
|
2021-12-10 13:15:00 +08:00
|
|
|
string text = TextBox.Text.Trim();
|
2018-12-20 17:21:37 +08:00
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(text))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (text[0] == '/')
|
2022-01-13 16:10:48 +08:00
|
|
|
channelManager?.PostCommand(text.Substring(1), Channel.Value);
|
2018-12-20 17:21:37 +08:00
|
|
|
else
|
2022-01-13 16:10:48 +08:00
|
|
|
channelManager?.PostMessage(text, target: Channel.Value);
|
2018-12-20 17:21:37 +08:00
|
|
|
|
2021-12-10 13:15:00 +08:00
|
|
|
TextBox.Text = string.Empty;
|
2018-12-20 17:21:37 +08:00
|
|
|
}
|
|
|
|
|
2018-12-21 16:54:12 +08:00
|
|
|
protected virtual ChatLine CreateMessage(Message message) => new StandAloneMessage(message);
|
2018-12-20 15:50:38 +08:00
|
|
|
|
2019-02-21 17:56:34 +08:00
|
|
|
private void channelChanged(ValueChangedEvent<Channel> e)
|
2018-12-20 15:50:38 +08:00
|
|
|
{
|
2018-12-21 16:54:12 +08:00
|
|
|
drawableChannel?.Expire();
|
2018-12-20 15:50:38 +08:00
|
|
|
|
2019-02-21 17:56:34 +08:00
|
|
|
if (e.NewValue == null) return;
|
2018-12-20 15:50:38 +08:00
|
|
|
|
2020-03-23 11:03:33 +08:00
|
|
|
drawableChannel = CreateDrawableChannel(e.NewValue);
|
|
|
|
drawableChannel.CreateChatLineAction = CreateMessage;
|
2021-12-10 13:15:00 +08:00
|
|
|
drawableChannel.Padding = new MarginPadding { Bottom = postingTextBox ? text_box_height : 0 };
|
2020-03-23 11:03:33 +08:00
|
|
|
|
|
|
|
AddInternal(drawableChannel);
|
2018-12-20 15:50:38 +08:00
|
|
|
}
|
|
|
|
|
2022-11-15 23:12:24 +08:00
|
|
|
public partial class ChatTextBox : HistoryTextBox
|
2021-08-17 13:57:38 +08:00
|
|
|
{
|
2022-05-04 18:09:22 +08:00
|
|
|
protected override bool OnKeyDown(KeyDownEvent e)
|
|
|
|
{
|
|
|
|
// Chat text boxes are generally used in places where they retain focus, but shouldn't block interaction with other
|
|
|
|
// elements on the same screen.
|
2022-11-14 05:34:02 +08:00
|
|
|
if (!HoldFocus)
|
2022-05-04 18:09:22 +08:00
|
|
|
{
|
2022-11-14 05:34:02 +08:00
|
|
|
switch (e.Key)
|
|
|
|
{
|
|
|
|
case Key.Up:
|
|
|
|
case Key.Down:
|
|
|
|
return false;
|
|
|
|
}
|
2022-05-04 18:09:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return base.OnKeyDown(e);
|
|
|
|
}
|
|
|
|
|
2021-08-17 13:57:38 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
BackgroundUnfocused = new Color4(10, 10, 10, 10);
|
|
|
|
BackgroundFocused = new Color4(10, 10, 10, 255);
|
|
|
|
}
|
2021-08-18 17:25:21 +08:00
|
|
|
|
|
|
|
protected override void OnFocusLost(FocusLostEvent e)
|
|
|
|
{
|
|
|
|
base.OnFocusLost(e);
|
|
|
|
FocusLost?.Invoke();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Action FocusLost;
|
2021-08-17 13:57:38 +08:00
|
|
|
}
|
|
|
|
|
2020-03-23 11:03:33 +08:00
|
|
|
public partial class StandAloneDrawableChannel : DrawableChannel
|
2018-12-20 15:50:38 +08:00
|
|
|
{
|
2019-02-28 12:31:40 +08:00
|
|
|
public Func<Message, ChatLine> CreateChatLineAction;
|
2018-12-21 16:54:12 +08:00
|
|
|
|
|
|
|
public StandAloneDrawableChannel(Channel channel)
|
|
|
|
: base(channel)
|
2019-06-20 22:01:39 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-05-07 05:56:35 +08:00
|
|
|
protected override ChatLine CreateChatLine(Message m) => CreateChatLineAction(m);
|
2019-10-22 23:24:08 +08:00
|
|
|
|
2022-06-04 23:11:49 +08:00
|
|
|
protected override DaySeparator CreateDaySeparator(DateTimeOffset time) => new StandAloneDaySeparator(time);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected partial class StandAloneDaySeparator : DaySeparator
|
|
|
|
{
|
|
|
|
protected override float TextSize => 14;
|
|
|
|
protected override float LineHeight => 1;
|
2022-06-07 08:37:46 +08:00
|
|
|
protected override float Spacing => 5;
|
|
|
|
protected override float DateAlign => 125;
|
2022-06-04 23:11:49 +08:00
|
|
|
|
2022-11-22 03:30:42 +08:00
|
|
|
public StandAloneDaySeparator(DateTimeOffset date)
|
|
|
|
: base(date)
|
2022-05-07 05:56:35 +08:00
|
|
|
{
|
2022-06-04 23:11:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
{
|
|
|
|
Height = 25;
|
|
|
|
Colour = colours.Yellow;
|
|
|
|
}
|
2018-12-20 15:50:38 +08:00
|
|
|
}
|
|
|
|
|
2018-12-21 16:54:12 +08:00
|
|
|
protected partial class StandAloneMessage : ChatLine
|
2018-12-20 15:50:38 +08:00
|
|
|
{
|
2022-11-29 09:50:12 +08:00
|
|
|
protected override float FontSize => 15;
|
2022-06-07 08:37:46 +08:00
|
|
|
protected override float Spacing => 5;
|
|
|
|
protected override float UsernameWidth => 75;
|
2018-12-20 15:50:38 +08:00
|
|
|
|
2019-02-28 12:31:40 +08:00
|
|
|
public StandAloneMessage(Message message)
|
|
|
|
: base(message)
|
2018-12-20 15:50:38 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|