2020-10-23 11:40:13 +00: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.
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2020-10-30 09:27:43 +00:00
|
|
|
using System;
|
2020-10-23 11:40:13 +00:00
|
|
|
using System.Linq;
|
|
|
|
using osu.Framework.Allocation;
|
2020-10-29 16:31:42 +00:00
|
|
|
using osu.Framework.Bindables;
|
2020-10-23 11:40:13 +00:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Textures;
|
|
|
|
using osu.Framework.Utils;
|
2020-10-29 16:31:42 +00:00
|
|
|
using osu.Game.Configuration;
|
2020-10-23 11:40:13 +00:00
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Online.API.Requests;
|
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.Backgrounds
|
|
|
|
{
|
2022-11-24 05:32:20 +00:00
|
|
|
public partial class SeasonalBackgroundLoader : Component
|
2020-10-23 11:40:13 +00:00
|
|
|
{
|
2020-10-30 21:03:26 +00:00
|
|
|
/// <summary>
|
2020-10-30 21:39:34 +00:00
|
|
|
/// Fired when background should be changed due to receiving backgrounds from API
|
2020-10-30 21:03:26 +00:00
|
|
|
/// or when the user setting is changed (as it might require unloading the seasonal background).
|
|
|
|
/// </summary>
|
|
|
|
public event Action SeasonalBackgroundChanged;
|
|
|
|
|
2020-10-30 20:49:14 +00:00
|
|
|
[Resolved]
|
|
|
|
private IAPIProvider api { get; set; }
|
|
|
|
|
|
|
|
private readonly IBindable<APIState> apiState = new Bindable<APIState>();
|
2020-10-30 19:32:14 +00:00
|
|
|
private Bindable<SeasonalBackgroundMode> seasonalBackgroundMode;
|
2020-10-30 18:55:17 +00:00
|
|
|
private Bindable<APISeasonalBackgrounds> seasonalBackgrounds;
|
2020-10-30 20:49:14 +00:00
|
|
|
|
2020-10-23 11:40:13 +00:00
|
|
|
private int current;
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2020-10-30 20:49:14 +00:00
|
|
|
private void load(OsuConfigManager config, SessionStatics sessionStatics)
|
2020-10-23 11:40:13 +00:00
|
|
|
{
|
2020-10-30 19:32:14 +00:00
|
|
|
seasonalBackgroundMode = config.GetBindable<SeasonalBackgroundMode>(OsuSetting.SeasonalBackgroundMode);
|
2022-09-01 12:42:38 +00:00
|
|
|
seasonalBackgroundMode.BindValueChanged(_ => SeasonalBackgroundChanged?.Invoke());
|
2020-10-30 21:03:26 +00:00
|
|
|
|
2020-10-30 18:55:17 +00:00
|
|
|
seasonalBackgrounds = sessionStatics.GetBindable<APISeasonalBackgrounds>(Static.SeasonalBackgrounds);
|
2022-09-01 12:42:38 +00:00
|
|
|
seasonalBackgrounds.BindValueChanged(_ =>
|
|
|
|
{
|
|
|
|
if (shouldShowSeasonal)
|
|
|
|
SeasonalBackgroundChanged?.Invoke();
|
|
|
|
});
|
2020-10-30 09:27:43 +00:00
|
|
|
|
2020-10-30 20:49:14 +00:00
|
|
|
apiState.BindTo(api.State);
|
|
|
|
apiState.BindValueChanged(fetchSeasonalBackgrounds, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void fetchSeasonalBackgrounds(ValueChangedEvent<APIState> stateChanged)
|
|
|
|
{
|
|
|
|
if (seasonalBackgrounds.Value != null || stateChanged.NewValue != APIState.Online)
|
|
|
|
return;
|
2020-10-29 16:31:42 +00:00
|
|
|
|
2020-10-23 11:40:13 +00:00
|
|
|
var request = new GetSeasonalBackgroundsRequest();
|
|
|
|
request.Success += response =>
|
|
|
|
{
|
2020-10-30 18:55:17 +00:00
|
|
|
seasonalBackgrounds.Value = response;
|
2020-10-30 16:57:29 +00:00
|
|
|
current = RNG.Next(0, response.Backgrounds?.Count ?? 0);
|
2020-10-23 11:40:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
api.PerformAsync(request);
|
|
|
|
}
|
|
|
|
|
2020-10-30 19:32:14 +00:00
|
|
|
public SeasonalBackground LoadNextBackground()
|
2020-10-23 11:40:13 +00:00
|
|
|
{
|
2022-07-08 06:07:30 +00:00
|
|
|
if (!shouldShowSeasonal)
|
2020-10-30 19:32:14 +00:00
|
|
|
return null;
|
|
|
|
|
2022-07-08 06:07:30 +00:00
|
|
|
var backgrounds = seasonalBackgrounds.Value.Backgrounds;
|
2020-10-23 11:40:13 +00:00
|
|
|
|
2020-10-30 15:43:18 +00:00
|
|
|
current = (current + 1) % backgrounds.Count;
|
|
|
|
string url = backgrounds[current].Url;
|
2020-10-23 11:40:13 +00:00
|
|
|
|
2020-10-29 17:03:36 +00:00
|
|
|
return new SeasonalBackground(url);
|
2020-10-23 11:40:13 +00:00
|
|
|
}
|
2020-10-30 09:27:43 +00:00
|
|
|
|
2022-07-08 06:07:30 +00:00
|
|
|
private bool shouldShowSeasonal
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (seasonalBackgroundMode.Value == SeasonalBackgroundMode.Never)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (seasonalBackgroundMode.Value == SeasonalBackgroundMode.Sometimes && !isInSeason)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return seasonalBackgrounds.Value?.Backgrounds?.Any() == true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-30 19:32:14 +00:00
|
|
|
private bool isInSeason => seasonalBackgrounds.Value != null && DateTimeOffset.Now < seasonalBackgrounds.Value.EndDate;
|
2020-10-23 11:40:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[LongRunningLoad]
|
2022-11-24 05:32:20 +00:00
|
|
|
public partial class SeasonalBackground : Background
|
2020-10-23 11:40:13 +00:00
|
|
|
{
|
|
|
|
private readonly string url;
|
2020-10-29 17:03:36 +00:00
|
|
|
private const string fallback_texture_name = @"Backgrounds/bg1";
|
2020-10-23 11:40:13 +00:00
|
|
|
|
2020-10-29 17:03:36 +00:00
|
|
|
public SeasonalBackground(string url)
|
2020-10-23 11:40:13 +00:00
|
|
|
{
|
|
|
|
this.url = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(LargeTextureStore textures)
|
|
|
|
{
|
2020-10-29 17:03:36 +00:00
|
|
|
Sprite.Texture = textures.Get(url) ?? textures.Get(fallback_texture_name);
|
2020-10-23 11:40:13 +00:00
|
|
|
}
|
2021-06-15 21:21:48 +00:00
|
|
|
|
|
|
|
public override bool Equals(Background other)
|
|
|
|
{
|
|
|
|
if (ReferenceEquals(null, other)) return false;
|
|
|
|
if (ReferenceEquals(this, other)) return true;
|
|
|
|
|
|
|
|
return other.GetType() == GetType()
|
|
|
|
&& ((SeasonalBackground)other).url == url;
|
|
|
|
}
|
2020-10-23 11:40:13 +00:00
|
|
|
}
|
|
|
|
}
|