osu/osu.Game/Overlays/ChatOverlay.cs

470 lines
18 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-04-13 09:19:50 +00:00
using System.Collections.Generic;
2020-09-01 02:56:23 +00:00
using System.Collections.Specialized;
using System.Linq;
2018-11-20 07:51:59 +00:00
using osuTK;
using osuTK.Graphics;
2018-04-13 09:19:50 +00:00
using osu.Framework.Allocation;
2019-02-21 10:04:31 +00:00
using osu.Framework.Bindables;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.UserInterface;
2018-10-02 03:02:47 +00:00
using osu.Framework.Input.Events;
2018-04-13 09:19:50 +00:00
using osu.Game.Configuration;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.Chat;
using osu.Game.Overlays.Chat;
using osu.Game.Overlays.Chat.Selection;
using osu.Game.Overlays.Chat.Tabs;
using osuTK.Input;
2019-10-06 17:22:55 +00:00
using osu.Framework.Graphics.Sprites;
2020-09-17 21:56:08 +00:00
using osu.Framework.Graphics.Textures;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Overlays
{
public class ChatOverlay : OsuFocusedOverlayContainer, INamedOverlayComponent
2018-04-13 09:19:50 +00:00
{
public string IconTexture => "Icons/Hexacons/messaging";
public string Title => "chat";
2020-09-07 17:18:22 +00:00
public string Description => "join the real-time discussion";
2018-04-13 09:19:50 +00:00
private const float textbox_height = 60;
private const float channel_selection_min_height = 0.3f;
2020-02-14 13:14:00 +00:00
[Resolved]
private ChannelManager channelManager { get; set; }
2018-04-13 09:19:50 +00:00
2019-06-20 14:02:02 +00:00
private Container<DrawableChannel> currentChannelContainer;
2018-07-09 16:23:40 +00:00
private readonly List<DrawableChannel> loadedChannels = new List<DrawableChannel>();
2018-04-13 09:19:50 +00:00
private LoadingSpinner loading;
2018-04-13 09:19:50 +00:00
2019-06-20 14:02:02 +00:00
private FocusedTextBox textbox;
2018-04-13 09:19:50 +00:00
private const int transition_length = 500;
public const float DEFAULT_HEIGHT = 0.4f;
public const float TAB_AREA_HEIGHT = 50;
2019-06-25 10:52:31 +00:00
protected ChannelTabControl ChannelTabControl;
protected virtual ChannelTabControl CreateChannelTabControl() => new ChannelTabControl();
2018-04-13 09:19:50 +00:00
2019-06-20 14:02:02 +00:00
private Container chatContainer;
private TabsArea tabsArea;
private Box chatBackground;
private Box tabBackground;
2018-04-13 09:19:50 +00:00
2019-09-02 22:50:52 +00:00
public Bindable<float> ChatHeight { get; set; }
2018-04-13 09:19:50 +00:00
2019-06-20 14:02:02 +00:00
private Container channelSelectionContainer;
2019-06-25 10:52:31 +00:00
protected ChannelSelectionOverlay ChannelSelectionOverlay;
2018-04-13 09:19:50 +00:00
2019-06-25 10:52:31 +00:00
public override bool Contains(Vector2 screenSpacePos) => chatContainer.ReceivePositionalInputAt(screenSpacePos)
|| (ChannelSelectionOverlay.State.Value == Visibility.Visible && ChannelSelectionOverlay.ReceivePositionalInputAt(screenSpacePos));
2018-04-13 09:19:50 +00:00
public ChatOverlay()
{
RelativeSizeAxes = Axes.Both;
RelativePositionAxes = Axes.Both;
Anchor = Anchor.BottomLeft;
Origin = Anchor.BottomLeft;
2019-06-20 14:02:02 +00:00
}
2018-04-13 09:19:50 +00:00
2019-06-20 14:02:02 +00:00
[BackgroundDependencyLoader]
2020-09-17 21:56:08 +00:00
private void load(OsuConfigManager config, OsuColour colours, TextureStore textures)
2019-06-20 14:02:02 +00:00
{
2018-04-13 09:19:50 +00:00
const float padding = 5;
Children = new Drawable[]
{
channelSelectionContainer = new Container
{
RelativeSizeAxes = Axes.Both,
Height = 1f - DEFAULT_HEIGHT,
Masking = true,
Children = new[]
{
2019-06-25 10:52:31 +00:00
ChannelSelectionOverlay = new ChannelSelectionOverlay
2018-04-13 09:19:50 +00:00
{
RelativeSizeAxes = Axes.Both,
},
},
},
chatContainer = new Container
{
Name = @"chat container",
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
RelativeSizeAxes = Axes.Both,
Height = DEFAULT_HEIGHT,
Children = new[]
{
new Container
{
Name = @"chat area",
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Top = TAB_AREA_HEIGHT },
Children = new Drawable[]
{
chatBackground = new Box
{
RelativeSizeAxes = Axes.Both,
},
currentChannelContainer = new Container<DrawableChannel>
2018-04-13 09:19:50 +00:00
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding
{
Bottom = textbox_height
},
},
new Container
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
RelativeSizeAxes = Axes.X,
Height = textbox_height,
Padding = new MarginPadding
{
Top = padding * 2,
Bottom = padding * 2,
Left = ChatLine.LEFT_PADDING + padding * 2,
Right = padding * 2,
},
Children = new Drawable[]
{
textbox = new FocusedTextBox
{
RelativeSizeAxes = Axes.Both,
Height = 1,
PlaceholderText = "type your message",
ReleaseFocusOnCommit = false,
HoldFocus = true,
}
}
},
loading = new LoadingSpinner(),
2018-04-13 09:19:50 +00:00
}
},
tabsArea = new TabsArea
2018-04-13 09:19:50 +00:00
{
Children = new Drawable[]
{
tabBackground = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black,
},
2020-09-17 21:56:08 +00:00
new Sprite
2019-10-06 17:22:55 +00:00
{
2020-09-17 21:56:08 +00:00
Texture = textures.Get(IconTexture),
2019-10-06 17:22:55 +00:00
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
2020-09-17 21:56:08 +00:00
Size = new Vector2(OverlayTitle.ICON_SIZE),
Margin = new MarginPadding { Left = 10 },
2019-10-06 17:22:55 +00:00
},
2019-06-25 10:52:31 +00:00
ChannelTabControl = CreateChannelTabControl().With(d =>
{
d.Anchor = Anchor.BottomLeft;
d.Origin = Anchor.BottomLeft;
d.RelativeSizeAxes = Axes.Both;
d.OnRequestLeave = channelManager.LeaveChannel;
2019-12-23 03:47:47 +00:00
d.IsSwitchable = true;
}),
2018-04-13 09:19:50 +00:00
}
},
},
},
};
textbox.OnCommit += postMessage;
2019-06-25 10:52:31 +00:00
ChannelTabControl.Current.ValueChanged += current => channelManager.CurrentChannel.Value = current.NewValue;
ChannelTabControl.ChannelSelectorActive.ValueChanged += active => ChannelSelectionOverlay.State.Value = active.NewValue ? Visibility.Visible : Visibility.Hidden;
ChannelSelectionOverlay.State.ValueChanged += state =>
2018-04-13 09:19:50 +00:00
{
2019-06-25 10:52:31 +00:00
// Propagate the visibility state to ChannelSelectorActive
ChannelTabControl.ChannelSelectorActive.Value = state.NewValue == Visibility.Visible;
2018-04-13 09:19:50 +00:00
if (state.NewValue == Visibility.Visible)
2018-04-13 09:19:50 +00:00
{
textbox.HoldFocus = false;
if (1f - ChatHeight.Value < channel_selection_min_height)
2018-05-02 09:26:23 +00:00
this.TransformBindableTo(ChatHeight, 1f - channel_selection_min_height, 800, Easing.OutQuint);
2018-04-13 09:19:50 +00:00
}
else
textbox.HoldFocus = true;
};
2019-06-25 10:52:31 +00:00
ChannelSelectionOverlay.OnRequestJoin = channel => channelManager.JoinChannel(channel);
ChannelSelectionOverlay.OnRequestLeave = channelManager.LeaveChannel;
2019-06-20 14:02:02 +00:00
2019-09-02 22:50:52 +00:00
ChatHeight = config.GetBindable<float>(OsuSetting.ChatDisplayHeight);
ChatHeight.BindValueChanged(height =>
2019-06-20 14:02:02 +00:00
{
2019-09-02 22:50:52 +00:00
chatContainer.Height = height.NewValue;
channelSelectionContainer.Height = 1f - height.NewValue;
tabBackground.FadeTo(height.NewValue == 1f ? 1f : 0.8f, 200);
}, true);
2019-06-20 14:02:02 +00:00
chatBackground.Colour = colours.ChatBlue;
loading.Show();
// This is a relatively expensive (and blocking) operation.
// Scheduling it ensures that it won't be performed unless the user decides to open chat.
// TODO: Refactor OsuFocusedOverlayContainer / OverlayContainer to support delayed content loading.
Schedule(() =>
{
// TODO: consider scheduling bindable callbacks to not perform when overlay is not present.
2020-09-01 02:56:23 +00:00
channelManager.JoinedChannels.CollectionChanged += joinedChannelsChanged;
2019-06-20 14:02:02 +00:00
foreach (Channel channel in channelManager.JoinedChannels)
2019-06-25 10:52:31 +00:00
ChannelTabControl.AddChannel(channel);
2019-06-20 14:02:02 +00:00
2020-09-01 02:56:23 +00:00
channelManager.AvailableChannels.CollectionChanged += availableChannelsChanged;
availableChannelsChanged(null, null);
2019-06-20 14:02:02 +00:00
currentChannel = channelManager.CurrentChannel.GetBoundCopy();
currentChannel.BindValueChanged(currentChannelChanged, true);
});
}
2019-06-20 14:02:02 +00:00
private Bindable<Channel> currentChannel;
2019-02-21 09:56:34 +00:00
private void currentChannelChanged(ValueChangedEvent<Channel> e)
{
2019-02-21 09:56:34 +00:00
if (e.NewValue == null)
{
textbox.Current.Disabled = true;
currentChannelContainer.Clear(false);
2019-06-25 10:52:31 +00:00
ChannelSelectionOverlay.Show();
return;
}
2019-05-11 23:21:12 +00:00
if (e.NewValue is ChannelSelectorTabItem.ChannelSelectorTabChannel)
2019-05-11 23:16:15 +00:00
return;
2019-02-21 09:56:34 +00:00
textbox.Current.Disabled = e.NewValue.ReadOnly;
2019-06-25 10:52:31 +00:00
if (ChannelTabControl.Current.Value != e.NewValue)
Scheduler.Add(() => ChannelTabControl.Current.Value = e.NewValue);
2019-02-21 09:56:34 +00:00
var loaded = loadedChannels.Find(d => d.Channel == e.NewValue);
2019-04-01 03:16:05 +00:00
if (loaded == null)
{
currentChannelContainer.FadeOut(500, Easing.OutQuint);
loading.Show();
2019-02-21 09:56:34 +00:00
loaded = new DrawableChannel(e.NewValue);
loadedChannels.Add(loaded);
LoadComponentAsync(loaded, l =>
{
if (currentChannel.Value != e.NewValue)
return;
loading.Hide();
currentChannelContainer.Clear(false);
currentChannelContainer.Add(loaded);
currentChannelContainer.FadeIn(500, Easing.OutQuint);
});
}
else
{
currentChannelContainer.Clear(false);
currentChannelContainer.Add(loaded);
}
// mark channel as read when channel switched
if (e.NewValue.Messages.Any())
channelManager.MarkChannelAsRead(e.NewValue);
2018-04-13 09:19:50 +00:00
}
2019-09-02 22:50:52 +00:00
private float startDragChatHeight;
2018-04-13 09:19:50 +00:00
private bool isDragging;
2018-10-02 03:02:47 +00:00
protected override bool OnDragStart(DragStartEvent e)
2018-04-13 09:19:50 +00:00
{
isDragging = tabsArea.IsHovered;
if (!isDragging)
2018-10-02 03:02:47 +00:00
return base.OnDragStart(e);
2018-04-13 09:19:50 +00:00
startDragChatHeight = ChatHeight.Value;
return true;
}
protected override void OnDrag(DragEvent e)
2018-04-13 09:19:50 +00:00
{
if (isDragging)
{
2019-09-02 22:50:52 +00:00
float targetChatHeight = startDragChatHeight - (e.MousePosition.Y - e.MouseDownPosition.Y) / Parent.DrawSize.Y;
2018-04-13 09:19:50 +00:00
// If the channel selection screen is shown, mind its minimum height
2019-06-25 10:52:31 +00:00
if (ChannelSelectionOverlay.State.Value == Visibility.Visible && targetChatHeight > 1f - channel_selection_min_height)
2018-04-13 09:19:50 +00:00
targetChatHeight = 1f - channel_selection_min_height;
ChatHeight.Value = targetChatHeight;
}
}
protected override void OnDragEnd(DragEndEvent e)
2018-04-13 09:19:50 +00:00
{
isDragging = false;
base.OnDragEnd(e);
2018-04-13 09:19:50 +00:00
}
private void selectTab(int index)
{
var channel = ChannelTabControl.Items
.Where(tab => !(tab is ChannelSelectorTabItem.ChannelSelectorTabChannel))
2020-01-31 16:19:04 +00:00
.ElementAtOrDefault(index);
if (channel != null)
2019-06-25 10:52:31 +00:00
ChannelTabControl.Current.Value = channel;
}
protected override bool OnKeyDown(KeyDownEvent e)
{
if (e.AltPressed)
{
switch (e.Key)
{
case Key.Number1:
case Key.Number2:
case Key.Number3:
case Key.Number4:
case Key.Number5:
case Key.Number6:
case Key.Number7:
case Key.Number8:
case Key.Number9:
selectTab((int)e.Key - (int)Key.Number1);
return true;
2019-04-01 03:16:05 +00:00
case Key.Number0:
selectTab(9);
return true;
}
}
return base.OnKeyDown(e);
}
2018-04-13 09:19:50 +00:00
public override bool AcceptsFocus => true;
2018-10-02 03:02:47 +00:00
protected override void OnFocus(FocusEvent e)
2018-04-13 09:19:50 +00:00
{
2020-05-05 01:31:11 +00:00
// this is necessary as textbox is masked away and therefore can't get focus :(
textbox.TakeFocus();
2018-10-02 03:02:47 +00:00
base.OnFocus(e);
2018-04-13 09:19:50 +00:00
}
protected override void PopIn()
{
this.MoveToY(0, transition_length, Easing.OutQuint);
this.FadeIn(transition_length, Easing.OutQuint);
textbox.HoldFocus = true;
2019-06-25 10:52:31 +00:00
2018-04-13 09:19:50 +00:00
base.PopIn();
}
protected override void PopOut()
{
this.MoveToY(Height, transition_length, Easing.InSine);
this.FadeOut(transition_length, Easing.InSine);
2019-06-25 10:52:31 +00:00
ChannelSelectionOverlay.Hide();
2018-04-13 09:19:50 +00:00
textbox.HoldFocus = false;
base.PopOut();
}
2020-09-01 02:56:23 +00:00
private void joinedChannelsChanged(object sender, NotifyCollectionChangedEventArgs args)
{
2020-09-01 02:56:23 +00:00
switch (args.Action)
{
2020-09-01 02:56:23 +00:00
case NotifyCollectionChangedAction.Add:
foreach (Channel channel in args.NewItems.Cast<Channel>())
ChannelTabControl.AddChannel(channel);
break;
2020-09-01 02:56:23 +00:00
case NotifyCollectionChangedAction.Remove:
foreach (Channel channel in args.OldItems.Cast<Channel>())
{
ChannelTabControl.RemoveChannel(channel);
2020-09-01 02:56:23 +00:00
var loaded = loadedChannels.Find(c => c.Channel == channel);
2020-09-01 02:56:23 +00:00
if (loaded != null)
{
loadedChannels.Remove(loaded);
// Because the container is only cleared in the async load callback of a new channel, it is forcefully cleared
// to ensure that the previous channel doesn't get updated after it's disposed
currentChannelContainer.Remove(loaded);
loaded.Dispose();
}
}
break;
}
}
2020-09-01 02:56:23 +00:00
private void availableChannelsChanged(object sender, NotifyCollectionChangedEventArgs args)
{
ChannelSelectionOverlay.UpdateAvailableChannels(channelManager.AvailableChannels);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
2018-09-06 08:11:23 +00:00
if (channelManager != null)
{
channelManager.CurrentChannel.ValueChanged -= currentChannelChanged;
2020-09-01 02:56:23 +00:00
channelManager.JoinedChannels.CollectionChanged -= joinedChannelsChanged;
channelManager.AvailableChannels.CollectionChanged -= availableChannelsChanged;
2018-09-06 08:11:23 +00:00
}
}
2018-04-13 09:19:50 +00:00
private void postMessage(TextBox textbox, bool newText)
{
var text = textbox.Text.Trim();
2018-04-13 09:19:50 +00:00
if (string.IsNullOrWhiteSpace(text))
2018-04-13 09:19:50 +00:00
return;
if (text[0] == '/')
channelManager.PostCommand(text.Substring(1));
else
channelManager.PostMessage(text);
2018-04-13 09:19:50 +00:00
textbox.Text = string.Empty;
2018-04-13 09:19:50 +00:00
}
private class TabsArea : Container
{
// IsHovered is used
public override bool HandlePositionalInput => true;
public TabsArea()
{
Name = @"tabs area";
RelativeSizeAxes = Axes.X;
Height = TAB_AREA_HEIGHT;
}
}
2018-04-13 09:19:50 +00:00
}
}