2016-10-07 23:43:06 +08:00
|
|
|
|
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
2016-11-30 16:07:09 +08:00
|
|
|
|
using System;
|
2016-10-07 23:43:06 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
2016-10-13 22:57:05 +08:00
|
|
|
|
using OpenTK;
|
|
|
|
|
using OpenTK.Graphics;
|
2016-11-09 07:13:20 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2016-10-07 23:43:06 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
|
using osu.Framework.Graphics.Transformations;
|
|
|
|
|
using osu.Framework.Threading;
|
2017-01-11 02:44:40 +08:00
|
|
|
|
using osu.Game.Graphics;
|
2016-10-07 23:43:06 +08:00
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
|
using osu.Game.Online.API.Requests;
|
|
|
|
|
using osu.Game.Online.Chat;
|
2016-11-30 14:15:43 +08:00
|
|
|
|
using osu.Game.Online.Chat.Drawables;
|
2016-10-07 23:43:06 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays
|
|
|
|
|
{
|
2016-11-30 14:15:43 +08:00
|
|
|
|
public class ChatOverlay : OverlayContainer, IOnlineComponent
|
2016-10-07 23:43:06 +08:00
|
|
|
|
{
|
|
|
|
|
private ChannelDisplay channelDisplay;
|
|
|
|
|
|
|
|
|
|
private ScheduledDelegate messageRequest;
|
|
|
|
|
|
|
|
|
|
private Container content;
|
|
|
|
|
|
2016-11-07 20:13:56 +08:00
|
|
|
|
protected override Container<Drawable> Content => content;
|
2016-10-07 23:43:06 +08:00
|
|
|
|
|
2016-10-12 14:22:57 +08:00
|
|
|
|
private APIAccess api;
|
|
|
|
|
|
2016-11-30 14:15:43 +08:00
|
|
|
|
public ChatOverlay()
|
2016-10-07 23:43:06 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
|
Size = new Vector2(1, 300);
|
|
|
|
|
Anchor = Anchor.BottomLeft;
|
|
|
|
|
Origin = Anchor.BottomLeft;
|
|
|
|
|
|
2016-10-12 12:35:21 +08:00
|
|
|
|
AddInternal(new Drawable[]
|
2016-10-07 23:43:06 +08:00
|
|
|
|
{
|
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,
|
2017-01-13 05:38:27 +08:00
|
|
|
|
Colour = OsuColour.Gray(0.1f).Opacity(0.4f),
|
2016-10-09 21:21:44 +08:00
|
|
|
|
},
|
|
|
|
|
content = new Container
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
}
|
2016-10-12 12:35:21 +08:00
|
|
|
|
});
|
2016-10-07 23:43:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-12 18:44:16 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
2016-11-30 13:52:24 +08:00
|
|
|
|
private void load(APIAccess api)
|
2016-10-07 23:43:06 +08:00
|
|
|
|
{
|
2016-11-30 13:52:24 +08:00
|
|
|
|
this.api = api;
|
2016-11-30 14:49:15 +08:00
|
|
|
|
api.Register(this);
|
2016-10-07 23:43:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private long? lastMessageId;
|
|
|
|
|
|
2016-10-13 22:57:05 +08:00
|
|
|
|
private List<Channel> careChannels;
|
2016-10-07 23:43:06 +08:00
|
|
|
|
|
|
|
|
|
private void addChannel(Channel channel)
|
|
|
|
|
{
|
|
|
|
|
Add(channelDisplay = new ChannelDisplay(channel));
|
|
|
|
|
careChannels.Add(channel);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-13 22:57:05 +08:00
|
|
|
|
private GetMessagesRequest fetchReq;
|
2016-10-07 23:43:06 +08:00
|
|
|
|
|
|
|
|
|
public void FetchNewMessages(APIAccess api)
|
|
|
|
|
{
|
|
|
|
|
if (fetchReq != null) return;
|
|
|
|
|
|
|
|
|
|
fetchReq = new GetMessagesRequest(careChannels, lastMessageId);
|
|
|
|
|
fetchReq.Success += delegate (List<Message> messages)
|
|
|
|
|
{
|
|
|
|
|
foreach (Message m in messages)
|
|
|
|
|
{
|
|
|
|
|
careChannels.Find(c => c.Id == m.ChannelId).AddNewMessages(m);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lastMessageId = messages.LastOrDefault()?.Id ?? lastMessageId;
|
|
|
|
|
|
|
|
|
|
Debug.Write("success!");
|
|
|
|
|
fetchReq = null;
|
|
|
|
|
};
|
|
|
|
|
fetchReq.Failure += delegate
|
|
|
|
|
{
|
|
|
|
|
Debug.Write("failure!");
|
|
|
|
|
fetchReq = null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
api.Queue(fetchReq);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-13 22:57:05 +08:00
|
|
|
|
private const int transition_length = 500;
|
2016-10-07 23:43:06 +08:00
|
|
|
|
|
2016-10-13 22:57:05 +08:00
|
|
|
|
protected override void PopIn()
|
2016-10-07 23:43:06 +08:00
|
|
|
|
{
|
2016-10-13 22:57:05 +08:00
|
|
|
|
MoveToY(0, transition_length, EasingTypes.OutQuint);
|
|
|
|
|
FadeIn(transition_length, EasingTypes.OutQuint);
|
|
|
|
|
}
|
2016-10-08 11:53:46 +08:00
|
|
|
|
|
2016-10-13 22:57:05 +08:00
|
|
|
|
protected override void PopOut()
|
|
|
|
|
{
|
2016-11-30 13:52:24 +08:00
|
|
|
|
MoveToY(DrawSize.Y, transition_length, EasingTypes.InSine);
|
|
|
|
|
FadeOut(transition_length, EasingTypes.InSine);
|
2016-10-07 23:43:06 +08:00
|
|
|
|
}
|
2016-11-30 14:15:43 +08:00
|
|
|
|
|
|
|
|
|
public void APIStateChanged(APIAccess api, APIState state)
|
|
|
|
|
{
|
2016-11-30 16:07:09 +08:00
|
|
|
|
switch (state)
|
|
|
|
|
{
|
|
|
|
|
case APIState.Online:
|
|
|
|
|
initializeChannels();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
messageRequest?.Cancel();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void initializeChannels()
|
|
|
|
|
{
|
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
|
|
careChannels = new List<Channel>();
|
|
|
|
|
|
|
|
|
|
//if (api.State != APIAccess.APIState.Online)
|
|
|
|
|
// return;
|
|
|
|
|
|
|
|
|
|
SpriteText loading;
|
|
|
|
|
Add(loading = new SpriteText
|
|
|
|
|
{
|
|
|
|
|
Text = @"Loading available channels...",
|
|
|
|
|
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);
|
|
|
|
|
addChannel(channels.Find(c => c.Name == @"#osu"));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//addChannel(channels.Find(c => c.Name == @"#lobby"));
|
|
|
|
|
//addChannel(channels.Find(c => c.Name == @"#english"));
|
|
|
|
|
|
|
|
|
|
messageRequest = Scheduler.AddDelayed(() => FetchNewMessages(api), 1000, true);
|
|
|
|
|
};
|
|
|
|
|
api.Queue(req);
|
2016-11-30 14:15:43 +08:00
|
|
|
|
}
|
2016-10-07 23:43:06 +08:00
|
|
|
|
}
|
|
|
|
|
}
|