2019-08-30 03:59:58 +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.
|
|
|
|
|
2021-03-06 23:15:23 +00:00
|
|
|
using System;
|
2020-02-07 05:58:29 +00:00
|
|
|
using System.Collections.Generic;
|
2022-04-07 08:26:23 +00:00
|
|
|
using System.Diagnostics;
|
2020-02-07 05:58:29 +00:00
|
|
|
using System.Linq;
|
2020-03-27 09:03:02 +00:00
|
|
|
using osu.Framework.Allocation;
|
2020-11-21 19:06:30 +00:00
|
|
|
using osu.Framework.Bindables;
|
2019-08-30 03:59:58 +00:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Animations;
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
using osu.Framework.Graphics.Textures;
|
2021-10-22 05:41:59 +00:00
|
|
|
using static osu.Game.Skinning.SkinConfiguration;
|
2019-08-30 03:59:58 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Skinning
|
|
|
|
{
|
|
|
|
public static partial class LegacySkinExtensions
|
|
|
|
{
|
2022-04-07 08:26:23 +00:00
|
|
|
public static Drawable? GetAnimation(this ISkin? source, string componentName, bool animatable, bool looping, bool applyConfigFrameRate = false, string animationSeparator = "-",
|
|
|
|
bool startAtCurrentTime = true, double? frameLength = null)
|
2020-07-17 07:57:33 +00:00
|
|
|
=> source.GetAnimation(componentName, default, default, animatable, looping, applyConfigFrameRate, animationSeparator, startAtCurrentTime, frameLength);
|
|
|
|
|
2022-04-07 08:26:23 +00:00
|
|
|
public static Drawable? GetAnimation(this ISkin? source, string componentName, WrapMode wrapModeS, WrapMode wrapModeT, bool animatable, bool looping, bool applyConfigFrameRate = false,
|
|
|
|
string animationSeparator = "-", bool startAtCurrentTime = true, double? frameLength = null)
|
2019-08-30 03:59:58 +00:00
|
|
|
{
|
2022-04-07 08:26:23 +00:00
|
|
|
if (source == null)
|
|
|
|
return null;
|
2019-08-30 03:59:58 +00:00
|
|
|
|
2022-04-07 08:26:23 +00:00
|
|
|
var textures = GetTextures(source, componentName, wrapModeS, wrapModeT, animatable, animationSeparator, out var retrievalSource);
|
2021-06-07 16:17:20 +00:00
|
|
|
|
2022-04-07 08:26:23 +00:00
|
|
|
switch (textures.Length)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return null;
|
2021-06-07 16:17:20 +00:00
|
|
|
|
2022-04-07 08:26:23 +00:00
|
|
|
case 1:
|
|
|
|
return new Sprite { Texture = textures[0] };
|
2021-06-07 16:17:20 +00:00
|
|
|
|
2022-04-07 08:26:23 +00:00
|
|
|
default:
|
|
|
|
Debug.Assert(retrievalSource != null);
|
2019-08-30 03:59:58 +00:00
|
|
|
|
2020-04-02 05:30:22 +00:00
|
|
|
var animation = new SkinnableTextureAnimation(startAtCurrentTime)
|
2020-02-07 05:58:29 +00:00
|
|
|
{
|
2022-04-07 08:26:23 +00:00
|
|
|
DefaultFrameLength = frameLength ?? getFrameLength(retrievalSource, applyConfigFrameRate, textures),
|
2020-04-03 06:59:56 +00:00
|
|
|
Loop = looping,
|
2020-02-07 05:58:29 +00:00
|
|
|
};
|
2019-08-30 03:59:58 +00:00
|
|
|
|
2020-02-07 05:58:29 +00:00
|
|
|
foreach (var t in textures)
|
|
|
|
animation.AddFrame(t);
|
|
|
|
|
|
|
|
return animation;
|
2022-04-07 08:26:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Texture[] GetTextures(this ISkin? source, string componentName, WrapMode wrapModeS, WrapMode wrapModeT, bool animatable, string animationSeparator, out ISkin? retrievalSource)
|
|
|
|
{
|
|
|
|
retrievalSource = null;
|
|
|
|
|
|
|
|
if (source == null)
|
|
|
|
return Array.Empty<Texture>();
|
|
|
|
|
|
|
|
// find the first source which provides either the animated or non-animated version.
|
|
|
|
retrievalSource = (source as ISkinSource)?.FindProvider(s =>
|
|
|
|
{
|
|
|
|
if (animatable && s.GetTexture(getFrameName(0)) != null)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return s.GetTexture(componentName, wrapModeS, wrapModeT) != null;
|
|
|
|
}) ?? source;
|
|
|
|
|
|
|
|
if (animatable)
|
|
|
|
{
|
|
|
|
var textures = getTextures(retrievalSource).ToArray();
|
|
|
|
|
|
|
|
if (textures.Length > 0)
|
|
|
|
return textures;
|
2020-02-07 05:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if an animation was not allowed or not found, fall back to a sprite retrieval.
|
2022-04-07 08:26:23 +00:00
|
|
|
var singleTexture = retrievalSource.GetTexture(componentName, wrapModeS, wrapModeT);
|
2020-02-07 05:58:29 +00:00
|
|
|
|
2022-04-07 08:26:23 +00:00
|
|
|
return singleTexture != null
|
|
|
|
? new[] { singleTexture }
|
|
|
|
: Array.Empty<Texture>();
|
2020-02-07 05:58:29 +00:00
|
|
|
|
2022-04-07 08:26:23 +00:00
|
|
|
IEnumerable<Texture> getTextures(ISkin skin)
|
2019-08-30 03:59:58 +00:00
|
|
|
{
|
2019-11-11 11:53:22 +00:00
|
|
|
for (int i = 0; true; i++)
|
2019-08-30 03:59:58 +00:00
|
|
|
{
|
2022-04-07 08:26:23 +00:00
|
|
|
Texture? texture;
|
|
|
|
|
2021-06-07 16:17:20 +00:00
|
|
|
if ((texture = skin.GetTexture(getFrameName(i), wrapModeS, wrapModeT)) == null)
|
2019-08-30 03:59:58 +00:00
|
|
|
break;
|
|
|
|
|
2020-02-07 05:58:29 +00:00
|
|
|
yield return texture;
|
2019-08-30 03:59:58 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-07 16:17:20 +00:00
|
|
|
|
|
|
|
string getFrameName(int frameIndex) => $"{componentName}{animationSeparator}{frameIndex}";
|
2020-02-07 05:58:29 +00:00
|
|
|
}
|
2019-08-30 03:59:58 +00:00
|
|
|
|
2021-03-06 23:15:23 +00:00
|
|
|
public static bool HasFont(this ISkin source, LegacyFont font)
|
|
|
|
{
|
|
|
|
return source.GetTexture($"{source.GetFontPrefix(font)}-0") != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string GetFontPrefix(this ISkin source, LegacyFont font)
|
|
|
|
{
|
|
|
|
switch (font)
|
|
|
|
{
|
|
|
|
case LegacyFont.Score:
|
|
|
|
return source.GetConfig<LegacySetting, string>(LegacySetting.ScorePrefix)?.Value ?? "score";
|
|
|
|
|
|
|
|
case LegacyFont.Combo:
|
|
|
|
return source.GetConfig<LegacySetting, string>(LegacySetting.ComboPrefix)?.Value ?? "score";
|
|
|
|
|
|
|
|
case LegacyFont.HitCircle:
|
|
|
|
return source.GetConfig<LegacySetting, string>(LegacySetting.HitCirclePrefix)?.Value ?? "default";
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(font));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the numeric overlap of number sprites to use.
|
|
|
|
/// A positive number will bring the number sprites closer together, while a negative number
|
|
|
|
/// will split them apart more.
|
|
|
|
/// </summary>
|
|
|
|
public static float GetFontOverlap(this ISkin source, LegacyFont font)
|
|
|
|
{
|
|
|
|
switch (font)
|
|
|
|
{
|
|
|
|
case LegacyFont.Score:
|
2021-03-26 07:35:46 +00:00
|
|
|
return source.GetConfig<LegacySetting, float>(LegacySetting.ScoreOverlap)?.Value ?? 0f;
|
2021-03-06 23:15:23 +00:00
|
|
|
|
|
|
|
case LegacyFont.Combo:
|
2021-03-26 07:35:46 +00:00
|
|
|
return source.GetConfig<LegacySetting, float>(LegacySetting.ComboOverlap)?.Value ?? 0f;
|
2021-03-06 23:15:23 +00:00
|
|
|
|
|
|
|
case LegacyFont.HitCircle:
|
|
|
|
return source.GetConfig<LegacySetting, float>(LegacySetting.HitCircleOverlap)?.Value ?? -2f;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(font));
|
|
|
|
}
|
|
|
|
}
|
2020-08-19 04:58:23 +00:00
|
|
|
|
2020-03-27 09:03:02 +00:00
|
|
|
public partial class SkinnableTextureAnimation : TextureAnimation
|
|
|
|
{
|
|
|
|
[Resolved(canBeNull: true)]
|
2022-04-07 08:26:23 +00:00
|
|
|
private IAnimationTimeReference? timeReference { get; set; }
|
2020-03-27 09:03:02 +00:00
|
|
|
|
2020-11-21 19:06:30 +00:00
|
|
|
private readonly Bindable<double> animationStartTime = new BindableDouble();
|
|
|
|
|
2020-04-02 05:30:22 +00:00
|
|
|
public SkinnableTextureAnimation(bool startAtCurrentTime = true)
|
|
|
|
: base(startAtCurrentTime)
|
2020-03-27 09:03:02 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
if (timeReference != null)
|
2020-04-03 06:59:56 +00:00
|
|
|
{
|
|
|
|
Clock = timeReference.Clock;
|
2020-11-22 12:11:22 +00:00
|
|
|
animationStartTime.BindTo(timeReference.AnimationStartTime);
|
2020-04-03 06:59:56 +00:00
|
|
|
}
|
2020-11-21 19:06:30 +00:00
|
|
|
|
|
|
|
animationStartTime.BindValueChanged(_ => updatePlaybackPosition(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updatePlaybackPosition()
|
|
|
|
{
|
|
|
|
if (timeReference == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PlaybackPosition = timeReference.Clock.CurrentTime - timeReference.AnimationStartTime.Value;
|
2020-03-27 09:03:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 05:58:29 +00:00
|
|
|
private const double default_frame_time = 1000 / 60d;
|
2019-08-30 03:59:58 +00:00
|
|
|
|
2020-02-07 05:58:29 +00:00
|
|
|
private static double getFrameLength(ISkin source, bool applyConfigFrameRate, Texture[] textures)
|
|
|
|
{
|
|
|
|
if (applyConfigFrameRate)
|
2019-11-11 11:53:22 +00:00
|
|
|
{
|
2020-07-29 07:34:09 +00:00
|
|
|
var iniRate = source.GetConfig<LegacySetting, int>(LegacySetting.AnimationFramerate);
|
2020-02-07 05:58:29 +00:00
|
|
|
|
2020-03-12 11:18:57 +00:00
|
|
|
if (iniRate?.Value > 0)
|
2020-02-07 05:58:29 +00:00
|
|
|
return 1000f / iniRate.Value;
|
|
|
|
|
|
|
|
return 1000f / textures.Length;
|
2019-11-11 11:53:22 +00:00
|
|
|
}
|
2019-08-30 03:59:58 +00:00
|
|
|
|
2020-02-07 05:58:29 +00:00
|
|
|
return default_frame_time;
|
2019-08-30 03:59:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|