2019-08-30 11:59:58 +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-02-07 13:58:29 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2020-03-27 17:03:02 +08:00
|
|
|
using osu.Framework.Allocation;
|
2019-08-30 11:59:58 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Animations;
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
using osu.Framework.Graphics.Textures;
|
|
|
|
|
|
|
|
namespace osu.Game.Skinning
|
|
|
|
{
|
|
|
|
public static class LegacySkinExtensions
|
|
|
|
{
|
2020-04-02 13:30:22 +08:00
|
|
|
public static Drawable GetAnimation(this ISkin source, string componentName, bool animatable, bool looping, bool applyConfigFrameRate = false, string animationSeparator = "-",
|
2020-04-04 16:35:15 +08:00
|
|
|
bool startAtCurrentTime = true, double? frameLength = null)
|
2019-08-30 11:59:58 +08:00
|
|
|
{
|
|
|
|
Texture texture;
|
|
|
|
|
2020-02-07 13:58:29 +08:00
|
|
|
if (animatable)
|
|
|
|
{
|
|
|
|
var textures = getTextures().ToArray();
|
2019-08-30 11:59:58 +08:00
|
|
|
|
2020-02-07 13:58:29 +08:00
|
|
|
if (textures.Length > 0)
|
|
|
|
{
|
2020-04-02 13:30:22 +08:00
|
|
|
var animation = new SkinnableTextureAnimation(startAtCurrentTime)
|
2020-02-07 13:58:29 +08:00
|
|
|
{
|
2020-04-02 21:55:42 +08:00
|
|
|
DefaultFrameLength = frameLength ?? getFrameLength(source, applyConfigFrameRate, textures),
|
2020-04-03 14:59:56 +08:00
|
|
|
Loop = looping,
|
2020-02-07 13:58:29 +08:00
|
|
|
};
|
2019-08-30 11:59:58 +08:00
|
|
|
|
2020-02-07 13:58:29 +08:00
|
|
|
foreach (var t in textures)
|
|
|
|
animation.AddFrame(t);
|
|
|
|
|
|
|
|
return animation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if an animation was not allowed or not found, fall back to a sprite retrieval.
|
|
|
|
if ((texture = source.GetTexture(componentName)) != null)
|
|
|
|
return new Sprite { Texture = texture };
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
IEnumerable<Texture> getTextures()
|
2019-08-30 11:59:58 +08:00
|
|
|
{
|
2019-11-11 19:53:22 +08:00
|
|
|
for (int i = 0; true; i++)
|
2019-08-30 11:59:58 +08:00
|
|
|
{
|
2020-02-07 13:58:29 +08:00
|
|
|
if ((texture = source.GetTexture($"{componentName}{animationSeparator}{i}")) == null)
|
2019-08-30 11:59:58 +08:00
|
|
|
break;
|
|
|
|
|
2020-02-07 13:58:29 +08:00
|
|
|
yield return texture;
|
2019-08-30 11:59:58 +08:00
|
|
|
}
|
|
|
|
}
|
2020-02-07 13:58:29 +08:00
|
|
|
}
|
2019-08-30 11:59:58 +08:00
|
|
|
|
2020-03-27 17:03:02 +08:00
|
|
|
public class SkinnableTextureAnimation : TextureAnimation
|
|
|
|
{
|
|
|
|
[Resolved(canBeNull: true)]
|
|
|
|
private IAnimationTimeReference timeReference { get; set; }
|
|
|
|
|
2020-04-02 13:30:22 +08:00
|
|
|
public SkinnableTextureAnimation(bool startAtCurrentTime = true)
|
|
|
|
: base(startAtCurrentTime)
|
2020-03-27 17:03:02 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
if (timeReference != null)
|
2020-04-03 14:59:56 +08:00
|
|
|
{
|
|
|
|
Clock = timeReference.Clock;
|
|
|
|
PlaybackPosition = timeReference.AnimationStartTime - timeReference.Clock.CurrentTime;
|
|
|
|
}
|
2020-03-27 17:03:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 13:58:29 +08:00
|
|
|
private const double default_frame_time = 1000 / 60d;
|
2019-08-30 11:59:58 +08:00
|
|
|
|
2020-02-07 13:58:29 +08:00
|
|
|
private static double getFrameLength(ISkin source, bool applyConfigFrameRate, Texture[] textures)
|
|
|
|
{
|
|
|
|
if (applyConfigFrameRate)
|
2019-11-11 19:53:22 +08:00
|
|
|
{
|
2020-02-07 13:58:29 +08:00
|
|
|
var iniRate = source.GetConfig<GlobalSkinConfiguration, int>(GlobalSkinConfiguration.AnimationFramerate);
|
|
|
|
|
2020-03-12 19:18:57 +08:00
|
|
|
if (iniRate?.Value > 0)
|
2020-02-07 13:58:29 +08:00
|
|
|
return 1000f / iniRate.Value;
|
|
|
|
|
|
|
|
return 1000f / textures.Length;
|
2019-11-11 19:53:22 +08:00
|
|
|
}
|
2019-08-30 11:59:58 +08:00
|
|
|
|
2020-02-07 13:58:29 +08:00
|
|
|
return default_frame_time;
|
2019-08-30 11:59:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|