1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-17 22:22:54 +08:00
osu-lazer/osu.Game/Overlays/ChatOverlay.cs

395 lines
13 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
2016-10-13 22:57:05 +08:00
using OpenTK;
2016-11-09 07:13:20 +08:00
using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Threading;
using osu.Game.Graphics.Sprites;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.Chat;
2017-02-19 17:02:25 +08:00
using osu.Game.Graphics.UserInterface;
using osu.Framework.Graphics.UserInterface;
using OpenTK.Graphics;
using osu.Framework.Input;
using osu.Game.Configuration;
using osu.Game.Graphics;
using osu.Game.Overlays.Chat;
namespace osu.Game.Overlays
{
public class ChatOverlay : FocusedOverlayContainer, IOnlineComponent
{
2017-05-12 13:22:11 +08:00
private const float textbox_height = 60;
2017-02-22 12:38:22 +08:00
private ScheduledDelegate messageRequest;
private readonly Container currentChannelContainer;
private readonly FocusedTextBox inputTextBox;
2017-02-19 17:02:25 +08:00
2016-10-12 14:22:57 +08:00
private APIAccess api;
2017-04-19 18:15:21 +08:00
private const int transition_length = 500;
public const float DEFAULT_HEIGHT = 0.4f;
2017-05-12 14:32:52 +08:00
public const float TAB_AREA_HEIGHT = 50;
2017-05-12 13:22:11 +08:00
2017-04-19 18:15:21 +08:00
private GetMessagesRequest fetchReq;
2017-05-12 14:32:52 +08:00
private readonly ChatTabControl channelTabs;
private readonly Box chatBackground;
private readonly Box tabBackground;
private Bindable<double> chatHeight;
public ChatOverlay()
{
RelativeSizeAxes = Axes.Both;
RelativePositionAxes = Axes.Both;
Size = new Vector2(1, DEFAULT_HEIGHT);
Anchor = Anchor.BottomLeft;
Origin = Anchor.BottomLeft;
2017-05-12 13:22:11 +08:00
const float padding = 5;
Children = new Drawable[]
{
2017-02-19 17:02:25 +08:00
new Container
{
2017-05-12 13:22:11 +08:00
Name = @"chat area",
RelativeSizeAxes = Axes.Both,
2017-05-12 14:32:52 +08:00
Padding = new MarginPadding { Top = TAB_AREA_HEIGHT },
2017-02-19 17:02:25 +08:00
Children = new Drawable[]
{
2017-05-12 14:32:52 +08:00
chatBackground = new Box
2017-05-12 13:22:11 +08:00
{
RelativeSizeAxes = Axes.Both,
},
currentChannelContainer = new Container
2017-02-19 17:02:25 +08:00
{
RelativeSizeAxes = Axes.Both,
2017-05-12 13:22:11 +08:00
Padding = new MarginPadding
{
Bottom = textbox_height + padding
},
},
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[]
{
inputTextBox = new FocusedTextBox
{
RelativeSizeAxes = Axes.Both,
Height = 1,
PlaceholderText = "type your message",
Exit = () => State = Visibility.Hidden,
OnCommit = postMessage,
HoldFocus = true,
}
}
2017-02-19 17:02:25 +08:00
}
}
2017-05-12 13:22:11 +08:00
},
2017-05-12 14:32:52 +08:00
new Container
{
Name = @"tabs area",
RelativeSizeAxes = Axes.X,
Height = TAB_AREA_HEIGHT,
Children = new Drawable[]
{
tabBackground = new Box
2017-05-12 14:32:52 +08:00
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black,
},
channelTabs = new ChatTabControl
{
RelativeSizeAxes = Axes.Both,
},
}
},
};
channelTabs.Current.ValueChanged += newChannel => CurrentChannel = newChannel;
}
protected override bool OnDragStart(InputState state)
{
if (channelTabs.Hovering)
return true;
return base.OnDragStart(state);
}
protected override bool OnDrag(InputState state)
{
chatHeight.Value = Height - state.Mouse.Delta.Y / Parent.DrawSize.Y;
return base.OnDrag(state);
}
2017-04-19 18:15:21 +08:00
public void APIStateChanged(APIAccess api, APIState state)
{
switch (state)
{
case APIState.Online:
initializeChannels();
break;
default:
messageRequest?.Cancel();
break;
}
}
protected override bool OnFocus(InputState state)
{
//this is necessary as inputTextBox is masked away and therefore can't get focus :(
inputTextBox.TriggerFocus();
return false;
}
2017-04-19 18:15:21 +08:00
protected override void PopIn()
2017-02-19 17:02:25 +08:00
{
2017-04-19 18:15:21 +08:00
MoveToY(0, transition_length, EasingTypes.OutQuint);
FadeIn(transition_length, EasingTypes.OutQuint);
inputTextBox.HoldFocus = true;
base.PopIn();
2017-04-19 18:15:21 +08:00
}
2017-04-19 17:46:26 +08:00
2017-04-19 18:15:21 +08:00
protected override void PopOut()
{
MoveToY(Height, transition_length, EasingTypes.InSine);
2017-04-19 18:15:21 +08:00
FadeOut(transition_length, EasingTypes.InSine);
inputTextBox.HoldFocus = false;
base.PopOut();
2017-02-19 17:02:25 +08:00
}
[BackgroundDependencyLoader]
2017-05-12 14:32:52 +08:00
private void load(APIAccess api, OsuConfigManager config, OsuColour colours)
{
2016-11-30 13:52:24 +08:00
this.api = api;
2016-11-30 14:49:15 +08:00
api.Register(this);
2017-05-15 09:56:27 +08:00
chatHeight = config.GetBindable<double>(OsuSetting.ChatDisplayHeight);
chatHeight.ValueChanged += h =>
{
Height = (float)h;
tabBackground.FadeTo(Height == 1 ? 1 : 0.8f, 200);
};
chatHeight.TriggerChange();
2017-05-12 14:32:52 +08:00
chatBackground.Colour = colours.ChatBlue;
}
private long? lastMessageId;
2017-05-17 21:13:56 +08:00
private readonly List<Channel> careChannels = new List<Channel>();
2017-05-12 12:25:50 +08:00
private readonly List<DrawableChannel> loadedChannels = new List<DrawableChannel>();
2017-04-19 18:15:21 +08:00
private void initializeChannels()
{
SpriteText loading;
Add(loading = new OsuSpriteText
{
Text = @"initialising chat...",
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
TextSize = 40,
});
messageRequest?.Cancel();
ListChannelsRequest req = new ListChannelsRequest();
req.Success += delegate (List<Channel> channels)
{
Scheduler.Add(delegate
{
loading.FadeOut(100);
2017-05-18 01:30:24 +08:00
loading.Expire();
2017-04-19 18:15:21 +08:00
addChannel(channels.Find(c => c.Name == @"#lazer"));
addChannel(channels.Find(c => c.Name == @"#osu"));
addChannel(channels.Find(c => c.Name == @"#lobby"));
2017-04-19 18:15:21 +08:00
});
messageRequest = Scheduler.AddDelayed(fetchNewMessages, 1000, true);
2017-04-19 18:15:21 +08:00
};
2017-04-19 18:15:21 +08:00
api.Queue(req);
}
private Channel currentChannel;
protected Channel CurrentChannel
{
get
{
return currentChannel;
}
set
{
if (currentChannel == value) return;
if (currentChannel != null)
currentChannelContainer.Clear(false);
currentChannel = value;
var loaded = loadedChannels.Find(d => d.Channel == value);
if (loaded == null)
loadedChannels.Add(loaded = new DrawableChannel(currentChannel));
inputTextBox.Current.Disabled = currentChannel.ReadOnly;
currentChannelContainer.Add(loaded);
channelTabs.Current.Value = value;
}
}
private void addChannel(Channel channel)
{
if (channel == null) return;
var existing = careChannels.Find(c => c.Id == channel.Id);
if (existing != null)
{
// if we already have this channel loaded, we don't want to make a second one.
channel = existing;
}
else
{
careChannels.Add(channel);
channelTabs.AddItem(channel);
}
// let's fetch a small number of messages to bring us up-to-date with the backlog.
fetchInitialMessages(channel);
if (CurrentChannel == null)
CurrentChannel = channel;
}
private void fetchInitialMessages(Channel channel)
{
var req = new GetMessagesRequest(new List<Channel> { channel }, null);
req.Success += delegate (List<Message> messages)
{
channel.AddNewMessages(messages.ToArray());
Debug.Write("success!");
};
req.Failure += delegate
{
Debug.Write("failure!");
};
api.Queue(req);
}
private void fetchNewMessages()
{
if (fetchReq != null) return;
fetchReq = new GetMessagesRequest(careChannels, lastMessageId);
fetchReq.Success += delegate (List<Message> messages)
{
2017-05-18 02:30:17 +08:00
foreach (var group in messages.Where(m => m.TargetType == TargetType.Channel).GroupBy(m => m.TargetId))
careChannels.Find(c => c.Id == group.Key)?.AddNewMessages(group.ToArray());
lastMessageId = messages.LastOrDefault()?.Id ?? lastMessageId;
Debug.Write("success!");
fetchReq = null;
};
fetchReq.Failure += delegate
{
Debug.Write("failure!");
fetchReq = null;
};
api.Queue(fetchReq);
}
2017-04-19 18:15:21 +08:00
private void postMessage(TextBox textbox, bool newText)
2016-10-13 22:57:05 +08:00
{
2017-04-19 18:15:21 +08:00
var postText = textbox.Text;
2017-05-16 18:55:45 +08:00
if (string.IsNullOrEmpty(postText))
return;
if (!api.IsLoggedIn)
{
currentChannel?.AddNewMessages(new ErrorMessage("Please login to participate in chat!"));
textbox.Text = string.Empty;
return;
}
2016-11-30 16:07:09 +08:00
if (currentChannel == null) return;
2016-11-30 16:07:09 +08:00
if (postText[0] == '/')
2016-11-30 16:07:09 +08:00
{
// TODO: handle commands
currentChannel.AddNewMessages(new ErrorMessage("Chat commands are not supported yet!"));
textbox.Text = string.Empty;
return;
}
2016-11-30 16:07:09 +08:00
var message = new Message
{
Sender = api.LocalUser.Value,
Timestamp = DateTimeOffset.Now,
TargetType = TargetType.Channel, //TODO: read this from currentChannel
TargetId = currentChannel.Id,
Content = postText
};
2016-11-30 16:07:09 +08:00
textbox.ReadOnly = true;
var req = new PostMessageRequest(message);
2016-11-30 16:07:09 +08:00
req.Failure += e =>
{
textbox.FlashColour(Color4.Red, 1000);
textbox.ReadOnly = false;
};
2017-02-22 12:38:10 +08:00
req.Success += m =>
{
currentChannel.AddNewMessages(m);
2016-11-30 16:07:09 +08:00
textbox.ReadOnly = false;
textbox.Text = string.Empty;
};
2016-11-30 16:07:09 +08:00
api.Queue(req);
}
}
}