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
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Graphics.Drawables;
|
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
|
using osu.Framework.Graphics.Transformations;
|
|
|
|
|
using osu.Framework.Threading;
|
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
|
using osu.Game.Online.API.Requests;
|
|
|
|
|
using osu.Game.Online.Chat;
|
|
|
|
|
using osu.Game.Online.Chat.Display;
|
|
|
|
|
using OpenTK;
|
|
|
|
|
using OpenTK.Graphics;
|
|
|
|
|
using osu.Framework.Input;
|
|
|
|
|
using OpenTK.Input;
|
2016-10-08 11:53:46 +08:00
|
|
|
|
using osu.Framework;
|
2016-10-07 23:43:06 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays
|
|
|
|
|
{
|
2016-10-08 11:53:46 +08:00
|
|
|
|
public class ChatConsole : Container, IStateful<ChatConsoleState>
|
2016-10-07 23:43:06 +08:00
|
|
|
|
{
|
|
|
|
|
private APIAccess api => ((OsuGameBase)Game).API;
|
|
|
|
|
|
|
|
|
|
private ChannelDisplay channelDisplay;
|
|
|
|
|
|
|
|
|
|
private ScheduledDelegate messageRequest;
|
|
|
|
|
|
|
|
|
|
private Container content;
|
|
|
|
|
|
2016-10-09 21:21:44 +08:00
|
|
|
|
protected override Container Content => content;
|
2016-10-07 23:43:06 +08:00
|
|
|
|
|
|
|
|
|
public ChatConsole()
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
Depth = float.MinValue,
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2016-10-12 12:35:21 +08:00
|
|
|
|
Colour = new Color4(0.1f, 0.1f, 0.1f, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Load()
|
|
|
|
|
{
|
|
|
|
|
base.Load();
|
|
|
|
|
initializeChannels();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private long? lastMessageId;
|
|
|
|
|
|
|
|
|
|
List<Channel> careChannels;
|
|
|
|
|
|
|
|
|
|
private void initializeChannels()
|
|
|
|
|
{
|
|
|
|
|
careChannels = new List<Channel>();
|
|
|
|
|
|
|
|
|
|
//if (api.State != APIAccess.APIState.Online)
|
2016-10-08 11:53:46 +08:00
|
|
|
|
// return;
|
2016-10-07 23:43:06 +08:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2016-10-09 21:21:44 +08:00
|
|
|
|
Scheduler.Add(delegate
|
2016-10-07 23:43:06 +08:00
|
|
|
|
{
|
|
|
|
|
loading.FadeOut(100);
|
2016-10-12 12:35:21 +08:00
|
|
|
|
addChannel(channels.Find(c => c.Name == @"#osu"));
|
2016-10-07 23:43:06 +08:00
|
|
|
|
});
|
2016-10-12 12:35:21 +08:00
|
|
|
|
|
2016-10-07 23:43:06 +08:00
|
|
|
|
//addChannel(channels.Find(c => c.Name == @"#lobby"));
|
|
|
|
|
//addChannel(channels.Find(c => c.Name == @"#english"));
|
|
|
|
|
|
2016-10-09 21:21:44 +08:00
|
|
|
|
messageRequest = Scheduler.AddDelayed(() => FetchNewMessages(api), 1000, true);
|
2016-10-07 23:43:06 +08:00
|
|
|
|
};
|
|
|
|
|
api.Queue(req);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addChannel(Channel channel)
|
|
|
|
|
{
|
|
|
|
|
Add(channelDisplay = new ChannelDisplay(channel));
|
|
|
|
|
careChannels.Add(channel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GetMessagesRequest fetchReq;
|
|
|
|
|
|
|
|
|
|
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-08 11:53:46 +08:00
|
|
|
|
private ChatConsoleState state;
|
2016-10-07 23:43:06 +08:00
|
|
|
|
|
2016-10-08 11:53:46 +08:00
|
|
|
|
public ChatConsoleState State
|
2016-10-07 23:43:06 +08:00
|
|
|
|
{
|
2016-10-08 11:53:46 +08:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return state;
|
|
|
|
|
}
|
2016-10-07 23:43:06 +08:00
|
|
|
|
|
2016-10-08 11:53:46 +08:00
|
|
|
|
set
|
2016-10-07 23:43:06 +08:00
|
|
|
|
{
|
2016-10-08 11:53:46 +08:00
|
|
|
|
state = value;
|
|
|
|
|
|
|
|
|
|
const int transition_length = 500;
|
|
|
|
|
|
|
|
|
|
switch (state)
|
|
|
|
|
{
|
|
|
|
|
case ChatConsoleState.Hidden:
|
|
|
|
|
MoveToY(-Size.Y, transition_length, EasingTypes.InQuint);
|
|
|
|
|
FadeOut(transition_length, EasingTypes.InQuint);
|
|
|
|
|
break;
|
|
|
|
|
case ChatConsoleState.Visible:
|
|
|
|
|
MoveToY(0, transition_length, EasingTypes.OutQuint);
|
|
|
|
|
FadeIn(transition_length, EasingTypes.OutQuint);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-10-07 23:43:06 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum ChatConsoleState
|
|
|
|
|
{
|
|
|
|
|
Visible,
|
|
|
|
|
Hidden,
|
|
|
|
|
}
|
|
|
|
|
}
|