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;
|
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-02-07 13:58:29 +08:00
|
|
|
public static Drawable GetAnimation(this ISkin source, string componentName, bool animatable, bool looping, bool applyConfigFrameRate = false, string animationSeparator = "-")
|
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)
|
|
|
|
{
|
|
|
|
var animation = new TextureAnimation
|
|
|
|
{
|
|
|
|
DefaultFrameLength = getFrameLength(source, applyConfigFrameRate, textures),
|
|
|
|
Repeat = looping,
|
|
|
|
};
|
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-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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|