2020-10-23 19:40:13 +08:00
|
|
|
// 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.
|
|
|
|
|
2020-10-30 17:27:43 +08:00
|
|
|
using System;
|
2020-10-23 19:40:13 +08:00
|
|
|
using System.Linq;
|
|
|
|
using osu.Framework.Allocation;
|
2020-10-30 00:31:42 +08:00
|
|
|
using osu.Framework.Bindables;
|
2020-10-23 19:40:13 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Textures;
|
|
|
|
using osu.Framework.Utils;
|
2020-10-30 00:31:42 +08:00
|
|
|
using osu.Game.Configuration;
|
2020-10-23 19:40:13 +08:00
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Online.API.Requests;
|
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.Backgrounds
|
|
|
|
{
|
|
|
|
[LongRunningLoad]
|
|
|
|
public class SeasonalBackgroundLoader : Component
|
|
|
|
{
|
2020-10-30 23:43:18 +08:00
|
|
|
private Bindable<APISeasonalBackgrounds> cachedResponse;
|
2020-10-23 19:40:13 +08:00
|
|
|
private int current;
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2020-10-30 00:31:42 +08:00
|
|
|
private void load(SessionStatics sessionStatics, IAPIProvider api)
|
2020-10-23 19:40:13 +08:00
|
|
|
{
|
2020-10-30 23:43:18 +08:00
|
|
|
cachedResponse = sessionStatics.GetBindable<APISeasonalBackgrounds>(Static.SeasonalBackgroundsResponse);
|
2020-10-30 17:27:43 +08:00
|
|
|
|
2020-10-30 23:43:18 +08:00
|
|
|
if (cachedResponse.Value != null) return;
|
2020-10-30 00:31:42 +08:00
|
|
|
|
2020-10-23 19:40:13 +08:00
|
|
|
var request = new GetSeasonalBackgroundsRequest();
|
|
|
|
request.Success += response =>
|
|
|
|
{
|
2020-10-30 23:43:18 +08:00
|
|
|
cachedResponse.Value = response;
|
|
|
|
current = RNG.Next(0, cachedResponse.Value.Backgrounds.Count);
|
2020-10-23 19:40:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
api.PerformAsync(request);
|
|
|
|
}
|
|
|
|
|
2020-10-30 01:03:36 +08:00
|
|
|
public SeasonalBackground LoadBackground()
|
2020-10-23 19:40:13 +08:00
|
|
|
{
|
2020-10-30 23:43:18 +08:00
|
|
|
var backgrounds = cachedResponse.Value.Backgrounds;
|
|
|
|
if (!backgrounds.Any()) return null;
|
2020-10-23 19:40:13 +08:00
|
|
|
|
2020-10-30 23:43:18 +08:00
|
|
|
current = (current + 1) % backgrounds.Count;
|
|
|
|
string url = backgrounds[current].Url;
|
2020-10-23 19:40:13 +08:00
|
|
|
|
2020-10-30 01:03:36 +08:00
|
|
|
return new SeasonalBackground(url);
|
2020-10-23 19:40:13 +08:00
|
|
|
}
|
2020-10-30 17:27:43 +08:00
|
|
|
|
2020-10-30 23:43:18 +08:00
|
|
|
public bool IsInSeason => DateTimeOffset.Now < cachedResponse.Value.EndDate;
|
2020-10-23 19:40:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[LongRunningLoad]
|
|
|
|
public class SeasonalBackground : Background
|
|
|
|
{
|
|
|
|
private readonly string url;
|
2020-10-30 01:03:36 +08:00
|
|
|
private const string fallback_texture_name = @"Backgrounds/bg1";
|
2020-10-23 19:40:13 +08:00
|
|
|
|
2020-10-30 01:03:36 +08:00
|
|
|
public SeasonalBackground(string url)
|
2020-10-23 19:40:13 +08:00
|
|
|
{
|
|
|
|
this.url = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(LargeTextureStore textures)
|
|
|
|
{
|
2020-10-30 01:03:36 +08:00
|
|
|
Sprite.Texture = textures.Get(url) ?? textures.Get(fallback_texture_name);
|
2020-10-23 19:40:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|