1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 01:47:24 +08:00
osu-lazer/osu.Game/Overlays/ChatOverlay.cs

239 lines
7.4 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.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;
using osu.Game.Online.Chat.Drawables;
2017-02-19 17:02:25 +08:00
using osu.Game.Graphics.UserInterface;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.UserInterface;
using OpenTK.Graphics;
using osu.Framework.Input;
namespace osu.Game.Overlays
{
public class ChatOverlay : FocusedOverlayContainer, IOnlineComponent
{
2017-03-07 09:59:19 +08:00
private const float textbox_height = 40;
2017-02-22 12:38:22 +08:00
private ScheduledDelegate messageRequest;
private readonly Container content;
protected override Container<Drawable> Content => content;
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;
private GetMessagesRequest fetchReq;
public ChatOverlay()
{
RelativeSizeAxes = Axes.X;
Size = new Vector2(1, 300);
Anchor = Anchor.BottomLeft;
Origin = Anchor.BottomLeft;
AddInternal(new Drawable[]
{
2016-10-09 21:21:44 +08:00
new Box
{
2016-11-30 03:50:12 +08:00
Depth = float.MaxValue,
2016-10-09 21:21:44 +08:00
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black,
Alpha = 0.9f,
2016-10-09 21:21:44 +08:00
},
content = new Container
{
RelativeSizeAxes = Axes.Both,
2017-02-22 12:38:22 +08:00
Padding = new MarginPadding { Top = 5, Bottom = textbox_height + 5 },
2017-02-19 17:02:25 +08:00
},
new Container
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
RelativeSizeAxes = Axes.X,
2017-02-22 12:38:22 +08:00
Height = textbox_height,
2017-02-19 17:02:25 +08:00
Padding = new MarginPadding(5),
Children = new Drawable[]
{
inputTextBox = new FocusedTextBox
{
RelativeSizeAxes = Axes.Both,
Height = 1,
2017-02-19 17:02:25 +08:00
PlaceholderText = "type your message",
Exit = () => State = Visibility.Hidden,
OnCommit = postMessage,
HoldFocus = true,
}
}
2016-10-09 21:21:44 +08:00
}
});
}
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);
}
2017-04-19 17:46:26 +08:00
2017-04-19 18:15:21 +08:00
protected override void PopOut()
{
MoveToY(DrawSize.Y, transition_length, EasingTypes.InSine);
FadeOut(transition_length, EasingTypes.InSine);
2017-02-19 17:02:25 +08:00
}
[BackgroundDependencyLoader]
2016-11-30 13:52:24 +08:00
private void load(APIAccess api)
{
2016-11-30 13:52:24 +08:00
this.api = api;
2016-11-30 14:49:15 +08:00
api.Register(this);
}
private long? lastMessageId;
2016-10-13 22:57:05 +08:00
private List<Channel> careChannels;
2017-04-19 18:15:21 +08:00
private void initializeChannels()
{
Clear();
careChannels = new List<Channel>();
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)
{
Debug.Assert(careChannels.Count == 0);
Scheduler.Add(delegate
{
loading.FadeOut(100);
addChannel(channels.Find(c => c.Name == @"#lazer"));
});
messageRequest = Scheduler.AddDelayed(fetchNewMessages, 1000, true);
};
api.Queue(req);
}
private void addChannel(Channel channel)
{
Add(new DrawableChannel(channel));
careChannels.Add(channel);
}
2017-04-19 18:15:21 +08:00
private void fetchNewMessages()
{
if (fetchReq != null) return;
fetchReq = new GetMessagesRequest(careChannels, lastMessageId);
fetchReq.Success += delegate (List<Message> messages)
{
2017-04-19 16:39:57 +08:00
var ids = messages.Where(m => m.TargetType == TargetType.Channel).Select(m => m.TargetId).Distinct();
2017-04-19 17:46:26 +08:00
2017-02-20 20:09:34 +08:00
//batch messages per channel.
foreach (var id in ids)
2017-04-19 16:39:57 +08:00
careChannels.Find(c => c.Id == id)?.AddNewMessages(messages.Where(m => m.TargetId == id));
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-04-19 18:15:21 +08:00
if (!string.IsNullOrEmpty(postText) && api.LocalUser.Value != null)
2016-11-30 16:07:09 +08:00
{
2017-04-19 18:15:21 +08:00
var currentChannel = careChannels.FirstOrDefault();
2016-11-30 16:07:09 +08:00
2017-04-19 18:15:21 +08:00
if (currentChannel == null) return;
2016-11-30 16:07:09 +08:00
2017-04-19 18:15:21 +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
2017-04-19 18:15:21 +08:00
textbox.ReadOnly = true;
var req = new PostMessageRequest(message);
2016-11-30 16:07:09 +08:00
2017-04-19 18:15:21 +08:00
req.Failure += e =>
{
textbox.FlashColour(Color4.Red, 1000);
textbox.ReadOnly = false;
};
2017-02-22 12:38:10 +08:00
2017-04-19 18:15:21 +08:00
req.Success += m =>
2016-11-30 16:07:09 +08:00
{
2017-04-19 18:15:21 +08:00
currentChannel.AddNewMessages(new[] { m });
2016-11-30 16:07:09 +08:00
2017-04-19 18:15:21 +08:00
textbox.ReadOnly = false;
textbox.Text = string.Empty;
};
2016-11-30 16:07:09 +08:00
2017-04-19 18:15:21 +08:00
api.Queue(req);
}
}
}
}