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.
|
|
|
|
|
2021-03-07 07:15:23 +08:00
|
|
|
using System;
|
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;
|
2020-11-22 03:06:30 +08:00
|
|
|
using osu.Framework.Bindables;
|
2019-08-30 11:59:58 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Animations;
|
2020-07-17 15:57:33 +08:00
|
|
|
using osu.Framework.Graphics.OpenGL.Textures;
|
2019-08-30 11:59:58 +08:00
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
using osu.Framework.Graphics.Textures;
|
2020-07-29 15:34:09 +08:00
|
|
|
using static osu.Game.Skinning.LegacySkinConfiguration;
|
2019-08-30 11:59:58 +08:00
|
|
|
|
|
|
|
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)
|
2020-07-17 15:57:33 +08:00
|
|
|
=> source.GetAnimation(componentName, default, default, animatable, looping, applyConfigFrameRate, animationSeparator, startAtCurrentTime, frameLength);
|
|
|
|
|
|
|
|
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 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.
|
2020-07-17 15:57:33 +08:00
|
|
|
if ((texture = source.GetTexture(componentName, wrapModeS, wrapModeT)) != null)
|
2020-02-07 13:58:29 +08:00
|
|
|
return new Sprite { Texture = texture };
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
IEnumerable<Texture> getTextures()
|
2019-08-30 11:59:58 +08:00
|
|
|
{
|
2021-06-07 23:02:57 +08:00
|
|
|
ISkin lookupSource = null;
|
|
|
|
|
2019-11-11 19:53:22 +08:00
|
|
|
for (int i = 0; true; i++)
|
2019-08-30 11:59:58 +08:00
|
|
|
{
|
2021-06-07 23:02:57 +08:00
|
|
|
string frameName = $"{componentName}{animationSeparator}{i}";
|
|
|
|
|
|
|
|
// ensure all textures are retrieved from the same skin source.
|
|
|
|
lookupSource ??= (source as ISkinSource)?.FindProvider(s => s.GetTexture(frameName, wrapModeS, wrapModeT) != null) ?? source;
|
|
|
|
|
|
|
|
if ((texture = lookupSource?.GetTexture(frameName, wrapModeS, wrapModeT)) == 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
|
|
|
|
2021-03-07 07:15:23 +08: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 15:35:46 +08:00
|
|
|
return source.GetConfig<LegacySetting, float>(LegacySetting.ScoreOverlap)?.Value ?? 0f;
|
2021-03-07 07:15:23 +08:00
|
|
|
|
|
|
|
case LegacyFont.Combo:
|
2021-03-26 15:35:46 +08:00
|
|
|
return source.GetConfig<LegacySetting, float>(LegacySetting.ComboOverlap)?.Value ?? 0f;
|
2021-03-07 07:15:23 +08:00
|
|
|
|
|
|
|
case LegacyFont.HitCircle:
|
|
|
|
return source.GetConfig<LegacySetting, float>(LegacySetting.HitCircleOverlap)?.Value ?? -2f;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(font));
|
|
|
|
}
|
|
|
|
}
|
2020-08-19 12:58:23 +08:00
|
|
|
|
2020-03-27 17:03:02 +08:00
|
|
|
public class SkinnableTextureAnimation : TextureAnimation
|
|
|
|
{
|
|
|
|
[Resolved(canBeNull: true)]
|
|
|
|
private IAnimationTimeReference timeReference { get; set; }
|
|
|
|
|
2020-11-22 03:06:30 +08:00
|
|
|
private readonly Bindable<double> animationStartTime = new BindableDouble();
|
|
|
|
|
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;
|
2020-11-22 20:11:22 +08:00
|
|
|
animationStartTime.BindTo(timeReference.AnimationStartTime);
|
2020-04-03 14:59:56 +08:00
|
|
|
}
|
2020-11-22 03:06:30 +08:00
|
|
|
|
|
|
|
animationStartTime.BindValueChanged(_ => updatePlaybackPosition(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updatePlaybackPosition()
|
|
|
|
{
|
|
|
|
if (timeReference == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PlaybackPosition = timeReference.Clock.CurrentTime - timeReference.AnimationStartTime.Value;
|
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-07-29 15:34:09 +08:00
|
|
|
var iniRate = source.GetConfig<LegacySetting, int>(LegacySetting.AnimationFramerate);
|
2020-02-07 13:58:29 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|