1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 01:27:29 +08:00
osu-lazer/osu.Game/Skinning/LegacySkin.cs

471 lines
16 KiB
C#
Raw Normal View History

2019-08-19 18:52:53 +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.
2018-04-13 17:19:50 +08:00
using System;
using System.Collections.Generic;
2018-04-13 17:19:50 +08:00
using System.IO;
using System.Linq;
2018-08-27 16:05:58 +08:00
using System.Threading.Tasks;
2019-07-25 15:17:02 +08:00
using osu.Framework.Allocation;
2018-04-13 17:19:50 +08:00
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2019-07-22 11:34:54 +08:00
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
2019-07-29 03:04:55 +08:00
using osu.Framework.Graphics.Animations;
2019-07-25 15:17:02 +08:00
using osu.Framework.Graphics.Containers;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Textures;
using osu.Framework.IO.Stores;
using osu.Framework.Text;
2018-04-13 17:19:50 +08:00
using osu.Game.Database;
2019-07-22 11:34:54 +08:00
using osu.Game.Graphics;
2018-09-27 15:27:11 +08:00
using osu.Game.Graphics.Sprites;
2019-07-22 11:34:54 +08:00
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.Objects.Types;
2019-08-07 17:45:56 +08:00
using osu.Game.Rulesets.UI;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Skinning
{
public class LegacySkin : Skin
{
protected TextureStore Textures;
protected IResourceStore<SampleChannel> Samples;
2018-04-13 17:19:50 +08:00
/// <summary>
/// On osu-stable, hitcircles have 5 pixels of transparent padding on each side to allow for shadows etc.
/// Their hittable area is 128px, but the actual circle portion is 118px.
/// We must account for some gameplay elements such as slider bodies, where this padding is not present.
/// </summary>
private const float legacy_circle_radius = 64 - 5;
2018-04-13 17:19:50 +08:00
public LegacySkin(SkinInfo skin, IResourceStore<byte[]> storage, AudioManager audioManager)
: this(skin, new LegacySkinResourceStore<SkinFileInfo>(skin, storage), audioManager, "skin.ini")
{
// defaults should only be applied for non-beatmap skins (which are parsed via this constructor).
2019-08-20 14:02:07 +08:00
if (!Configuration.CustomColours.ContainsKey("SliderBall")) Configuration.CustomColours["SliderBall"] = new Color4(2, 170, 255, 255);
2018-04-13 17:19:50 +08:00
}
2019-07-22 11:34:54 +08:00
private readonly bool hasHitCircle;
2019-02-28 12:31:40 +08:00
protected LegacySkin(SkinInfo skin, IResourceStore<byte[]> storage, AudioManager audioManager, string filename)
: base(skin)
2018-04-13 17:19:50 +08:00
{
Stream stream = storage.GetStream(filename);
if (stream != null)
using (StreamReader reader = new StreamReader(stream))
Configuration = new LegacySkinDecoder().Decode(reader);
else
Configuration = new SkinConfiguration();
Samples = audioManager.GetSampleStore(storage);
2018-09-07 17:56:08 +08:00
Textures = new TextureStore(new TextureLoaderStore(storage));
using (var testStream = storage.GetStream("hitcircle@2x") ?? storage.GetStream("hitcircle"))
hasHitCircle |= testStream != null;
if (hasHitCircle)
{
Configuration.SliderPathRadius = legacy_circle_radius;
}
2018-04-13 17:19:50 +08:00
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
Textures?.Dispose();
Samples?.Dispose();
}
2019-08-19 16:46:05 +08:00
private const double default_frame_time = 1000 / 60d;
2018-04-13 17:19:50 +08:00
public override Drawable GetDrawableComponent(string componentName)
{
2019-07-29 03:04:55 +08:00
bool animatable = false;
2019-07-29 17:49:59 +08:00
bool looping = true;
2019-07-29 03:04:55 +08:00
2018-04-13 17:19:50 +08:00
switch (componentName)
{
2019-07-30 18:10:21 +08:00
case "Play/osu/cursor":
2019-07-25 15:17:02 +08:00
if (GetTexture("cursor") != null)
return new LegacyCursor();
return null;
case "Play/osu/sliderball":
2019-08-19 18:52:53 +08:00
var sliderBallContent = getAnimation("sliderb", true, true, "");
if (sliderBallContent != null)
{
var size = sliderBallContent.Size;
sliderBallContent.RelativeSizeAxes = Axes.Both;
sliderBallContent.Size = Vector2.One;
return new LegacySliderBall(sliderBallContent)
{
Size = size
};
}
return null;
2019-07-22 11:34:54 +08:00
case "Play/osu/hitcircle":
if (hasHitCircle)
return new LegacyMainCirclePiece();
2019-07-30 22:24:28 +08:00
return null;
2019-07-25 15:17:02 +08:00
2019-08-19 16:45:54 +08:00
case "Play/osu/sliderfollowcircle":
animatable = true;
break;
2018-04-13 17:19:50 +08:00
case "Play/Miss":
componentName = "hit0";
2019-07-29 03:04:55 +08:00
animatable = true;
2019-07-29 17:49:59 +08:00
looping = false;
2018-04-13 17:19:50 +08:00
break;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case "Play/Meh":
componentName = "hit50";
2019-07-29 03:04:55 +08:00
animatable = true;
2019-07-29 17:49:59 +08:00
looping = false;
2018-04-13 17:19:50 +08:00
break;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case "Play/Good":
componentName = "hit100";
2019-07-29 03:04:55 +08:00
animatable = true;
2019-07-29 17:49:59 +08:00
looping = false;
2018-04-13 17:19:50 +08:00
break;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case "Play/Great":
componentName = "hit300";
2019-07-29 03:04:55 +08:00
animatable = true;
2019-07-29 17:49:59 +08:00
looping = false;
2018-04-13 17:19:50 +08:00
break;
2019-04-01 11:16:05 +08:00
2018-09-27 15:27:11 +08:00
case "Play/osu/number-text":
return !hasFont(Configuration.HitCircleFont)
? null
2019-08-21 13:31:19 +08:00
: new LegacySpriteText(this, Configuration.HitCircleFont)
{
Scale = new Vector2(0.96f),
// Spacing value was reverse-engineered from the ratio of the rendered sprite size in the visual inspector vs the actual texture size
Spacing = new Vector2(-Configuration.HitCircleOverlap * 0.89f, 0)
};
2018-04-13 17:19:50 +08:00
}
2019-08-19 18:23:54 +08:00
return getAnimation(componentName, animatable, looping);
}
private Drawable getAnimation(string componentName, bool animatable, bool looping, string animationSeparator = "-")
{
Texture texture;
2018-04-13 17:19:50 +08:00
2019-08-19 18:23:54 +08:00
Texture getFrameTexture(int frame) => GetTexture($"{componentName}{animationSeparator}{frame}");
TextureAnimation animation = null;
if (animatable)
{
for (int i = 0;; i++)
2019-07-29 03:04:55 +08:00
{
if ((texture = getFrameTexture(i)) == null)
break;
if (animation == null)
animation = new TextureAnimation
{
DefaultFrameLength = default_frame_time,
Repeat = looping
};
2019-07-29 03:04:55 +08:00
animation.AddFrame(texture);
}
}
2019-07-29 03:04:55 +08:00
if (animation != null)
2019-07-29 03:04:55 +08:00
return animation;
2018-04-13 17:19:50 +08:00
if ((texture = GetTexture(componentName)) != null)
return new Sprite { Texture = texture };
return null;
}
public override Texture GetTexture(string componentName)
{
float ratio = 2;
var texture = Textures.Get($"{componentName}@2x");
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
if (texture == null)
{
ratio = 1;
texture = Textures.Get(componentName);
2018-04-13 17:19:50 +08:00
}
if (texture != null)
texture.ScaleAdjust = ratio;
return texture;
2018-04-13 17:19:50 +08:00
}
public override SampleChannel GetSample(string sampleName) => Samples.Get(sampleName);
2018-09-27 15:27:11 +08:00
private bool hasFont(string fontName) => GetTexture($"{fontName}-0") != null;
2018-04-13 17:19:50 +08:00
protected class LegacySkinResourceStore<T> : IResourceStore<byte[]>
where T : INamedFileInfo
{
private readonly IHasFiles<T> source;
private readonly IResourceStore<byte[]> underlyingStore;
private string getPathForFile(string filename)
{
bool hasExtension = filename.Contains('.');
string lastPiece = filename.Split('/').Last();
2019-01-21 13:20:37 +08:00
var legacyName = filename.StartsWith("Gameplay/taiko/") ? "taiko-" + lastPiece : lastPiece;
2018-04-13 17:19:50 +08:00
2019-01-06 00:35:33 +08:00
var file = source.Files.Find(f =>
2019-01-21 13:20:37 +08:00
string.Equals(hasExtension ? f.Filename : Path.ChangeExtension(f.Filename, null), legacyName, StringComparison.InvariantCultureIgnoreCase));
2018-04-13 17:19:50 +08:00
return file?.FileInfo.StoragePath;
}
public LegacySkinResourceStore(IHasFiles<T> source, IResourceStore<byte[]> underlyingStore)
{
this.source = source;
this.underlyingStore = underlyingStore;
}
public Stream GetStream(string name)
{
string path = getPathForFile(name);
return path == null ? null : underlyingStore.GetStream(path);
}
public IEnumerable<string> GetAvailableResources() => source.Files.Select(f => f.Filename);
2018-08-27 16:05:58 +08:00
byte[] IResourceStore<byte[]>.Get(string name) => GetAsync(name).Result;
2018-08-29 19:57:48 +08:00
public Task<byte[]> GetAsync(string name)
2018-04-13 17:19:50 +08:00
{
string path = getPathForFile(name);
2018-08-29 19:57:48 +08:00
return path == null ? Task.FromResult<byte[]>(null) : underlyingStore.GetAsync(path);
2018-04-13 17:19:50 +08:00
}
2018-04-21 17:15:27 +08:00
#region IDisposable Support
private bool isDisposed;
protected virtual void Dispose(bool disposing)
{
if (!isDisposed)
{
isDisposed = true;
}
}
~LegacySkinResourceStore()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
2018-04-13 17:19:50 +08:00
}
2018-09-27 15:27:11 +08:00
private class LegacySpriteText : OsuSpriteText
{
private readonly LegacyGlyphStore glyphStore;
2018-09-27 15:27:11 +08:00
2019-08-21 13:31:19 +08:00
public LegacySpriteText(ISkin skin, string font)
2018-09-27 15:27:11 +08:00
{
Shadow = false;
UseFullGlyphHeight = false;
2019-08-21 13:31:19 +08:00
Font = new FontUsage(font, OsuFont.DEFAULT_FONT_SIZE);
glyphStore = new LegacyGlyphStore(skin);
}
2018-09-27 15:27:11 +08:00
protected override TextBuilder CreateTextBuilder(ITexturedGlyphLookupStore store) => base.CreateTextBuilder(glyphStore);
2018-09-27 15:27:11 +08:00
private class LegacyGlyphStore : ITexturedGlyphLookupStore
{
2019-08-21 13:31:19 +08:00
private readonly ISkin skin;
2019-04-01 11:16:05 +08:00
2019-08-21 13:31:19 +08:00
public LegacyGlyphStore(ISkin skin)
2018-09-27 15:27:11 +08:00
{
2019-08-21 13:31:19 +08:00
this.skin = skin;
2018-09-27 15:27:11 +08:00
}
public ITexturedCharacterGlyph Get(string fontName, char character)
{
2019-08-21 13:31:19 +08:00
var texture = skin.GetTexture($"{fontName}-{character}");
2019-08-21 13:31:19 +08:00
if (texture != null)
// Approximate value that brings character sizing roughly in-line with stable
texture.ScaleAdjust *= 18;
if (texture == null)
2019-08-21 13:31:19 +08:00
return null;
2019-08-21 13:31:19 +08:00
return new TexturedCharacterGlyph(new CharacterGlyph(character, 0, 0, texture?.Width ?? 0, null), texture, 1f / texture.ScaleAdjust);
}
2018-09-27 15:27:11 +08:00
public Task<ITexturedCharacterGlyph> GetAsync(string fontName, char character) => Task.Run(() => Get(fontName, character));
2018-09-27 15:27:11 +08:00
}
}
2019-07-25 15:17:02 +08:00
public class LegacyCursor : CompositeDrawable
{
public LegacyCursor()
{
Size = new Vector2(50);
Anchor = Anchor.Centre;
Origin = Anchor.Centre;
}
[BackgroundDependencyLoader]
private void load(ISkinSource skin)
{
InternalChildren = new Drawable[]
{
new NonPlayfieldSprite
2019-07-25 15:17:02 +08:00
{
Texture = skin.GetTexture("cursormiddle"),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
new NonPlayfieldSprite
2019-07-25 15:17:02 +08:00
{
Texture = skin.GetTexture("cursor"),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
}
};
}
}
2019-08-19 18:52:53 +08:00
public class LegacySliderBall : CompositeDrawable
{
private readonly Drawable animationContent;
public LegacySliderBall(Drawable animationContent)
2019-08-19 18:52:53 +08:00
{
this.animationContent = animationContent;
2019-08-19 18:52:53 +08:00
}
[BackgroundDependencyLoader]
private void load(ISkinSource skin, DrawableHitObject drawableObject)
2019-08-19 18:52:53 +08:00
{
2019-08-20 14:02:07 +08:00
animationContent.Colour = skin.GetValue<SkinConfiguration, Color4?>(s => s.CustomColours.ContainsKey("SliderBall") ? s.CustomColours["SliderBall"] : (Color4?)null) ?? Color4.White;
InternalChildren = new[]
{
new Sprite
{
Texture = skin.GetTexture("sliderb-nd"),
Colour = new Color4(5, 5, 5, 255),
},
animationContent,
new Sprite
{
Texture = skin.GetTexture("sliderb-spec"),
Blending = BlendingMode.Additive,
},
};
2019-08-19 18:52:53 +08:00
}
}
public class LegacyMainCirclePiece : CompositeDrawable
{
public LegacyMainCirclePiece()
{
Size = new Vector2(128);
}
2019-07-22 11:34:54 +08:00
private readonly IBindable<ArmedState> state = new Bindable<ArmedState>();
private readonly Bindable<Color4> accentColour = new Bindable<Color4>();
[BackgroundDependencyLoader]
private void load(DrawableHitObject drawableObject, ISkinSource skin)
{
2019-07-30 21:38:29 +08:00
Sprite hitCircleSprite;
2019-07-22 11:34:54 +08:00
InternalChildren = new Drawable[]
{
2019-07-30 21:38:29 +08:00
hitCircleSprite = new Sprite
2019-07-22 11:34:54 +08:00
{
Texture = skin.GetTexture("hitcircle"),
Colour = drawableObject.AccentColour.Value,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
2019-07-22 11:34:54 +08:00
},
new SkinnableSpriteText("Play/osu/number-text", _ => new OsuSpriteText
{
Font = OsuFont.Numeric.With(size: 40),
UseFullGlyphHeight = false,
}, confineMode: ConfineMode.NoScaling)
{
Text = (((IHasComboInformation)drawableObject.HitObject).IndexInCurrentCombo + 1).ToString()
},
new Sprite
{
Texture = skin.GetTexture("hitcircleoverlay"),
2019-07-25 15:17:02 +08:00
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
}
};
2019-07-22 11:34:54 +08:00
state.BindTo(drawableObject.State);
state.BindValueChanged(updateState, true);
accentColour.BindTo(drawableObject.AccentColour);
2019-07-30 21:38:29 +08:00
accentColour.BindValueChanged(colour => hitCircleSprite.Colour = colour.NewValue, true);
2019-07-22 11:34:54 +08:00
}
private void updateState(ValueChangedEvent<ArmedState> state)
{
const double legacy_fade_duration = 240;
switch (state.NewValue)
{
case ArmedState.Hit:
this.FadeOut(legacy_fade_duration, Easing.Out);
this.ScaleTo(1.4f, legacy_fade_duration, Easing.Out);
break;
}
2019-07-25 15:17:02 +08:00
}
}
2019-08-07 17:45:56 +08:00
/// <summary>
/// A sprite which is displayed within the playfield, but historically was not considered part of the playfield.
/// Performs scale adjustment to undo the scale applied by <see cref="PlayfieldAdjustmentContainer"/> (osu! ruleset specifically).
/// </summary>
private class NonPlayfieldSprite : Sprite
{
public override Texture Texture
{
get => base.Texture;
set
{
2019-07-30 22:44:47 +08:00
if (value != null)
2019-08-07 17:45:56 +08:00
// stable "magic ratio". see OsuPlayfieldAdjustmentContainer for full explanation.
value.ScaleAdjust *= 1.6f;
base.Texture = value;
}
}
}
2018-04-13 17:19:50 +08:00
}
}