1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 18:52:56 +08:00
osu-lazer/osu.Game/Tests/Visual/SkinnableTestScene.cs

177 lines
6.8 KiB
C#
Raw Normal View History

2019-07-26 18:29:06 +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.
using System;
using System.Collections.Generic;
2019-08-20 16:39:16 +08:00
using System.Text.RegularExpressions;
using JetBrains.Annotations;
2019-07-26 18:29:06 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2020-07-17 15:54:30 +08:00
using osu.Framework.Graphics.OpenGL.Textures;
using osu.Framework.Graphics.Shapes;
2019-08-20 16:39:16 +08:00
using osu.Framework.Graphics.Textures;
2019-07-26 18:29:06 +08:00
using osu.Framework.IO.Stores;
using osu.Game.Beatmaps;
using osu.Game.Graphics.Sprites;
using osu.Game.Rulesets;
2019-07-26 18:29:06 +08:00
using osu.Game.Skinning;
using osuTK;
using osuTK.Graphics;
2019-07-26 18:29:06 +08:00
namespace osu.Game.Tests.Visual
2019-07-26 18:29:06 +08:00
{
public abstract class SkinnableTestScene : OsuGridTestScene
{
private Skin metricsSkin;
private Skin defaultSkin;
private Skin specialSkin;
2019-12-12 21:27:11 +08:00
private Skin oldSkin;
2019-07-26 18:29:06 +08:00
2019-07-30 20:53:28 +08:00
protected SkinnableTestScene()
2019-12-12 21:27:11 +08:00
: base(2, 3)
2019-07-30 20:53:28 +08:00
{
}
2019-07-26 18:29:06 +08:00
[BackgroundDependencyLoader]
private void load(AudioManager audio, SkinManager skinManager)
2019-07-26 18:29:06 +08:00
{
var dllStore = new DllResourceStore(DynamicCompilationOriginal.GetType().Assembly);
2019-07-26 18:29:06 +08:00
metricsSkin = new TestLegacySkin(new SkinInfo { Name = "metrics-skin" }, new NamespacedResourceStore<byte[]>(dllStore, "Resources/metrics_skin"), audio, true);
defaultSkin = skinManager.GetSkin(DefaultLegacySkin.Info);
specialSkin = new TestLegacySkin(new SkinInfo { Name = "special-skin" }, new NamespacedResourceStore<byte[]>(dllStore, "Resources/special_skin"), audio, true);
oldSkin = new TestLegacySkin(new SkinInfo { Name = "old-skin" }, new NamespacedResourceStore<byte[]>(dllStore, "Resources/old_skin"), audio, true);
2019-07-26 18:29:06 +08:00
}
private readonly List<Drawable> createdDrawables = new List<Drawable>();
2019-07-26 18:29:06 +08:00
public void SetContents(Func<Drawable> creationFunction)
{
createdDrawables.Clear();
var beatmap = CreateBeatmapForSkinProvider();
Cell(0).Child = createProvider(null, creationFunction, beatmap);
Cell(1).Child = createProvider(metricsSkin, creationFunction, beatmap);
Cell(2).Child = createProvider(defaultSkin, creationFunction, beatmap);
Cell(3).Child = createProvider(specialSkin, creationFunction, beatmap);
Cell(4).Child = createProvider(oldSkin, creationFunction, beatmap);
2019-08-26 11:31:51 +08:00
}
protected IEnumerable<Drawable> CreatedDrawables => createdDrawables;
private Drawable createProvider(Skin skin, Func<Drawable> creationFunction, IBeatmap beatmap)
2019-08-26 11:31:51 +08:00
{
var created = creationFunction();
createdDrawables.Add(created);
var autoSize = created.RelativeSizeAxes == Axes.None;
2019-08-26 11:31:51 +08:00
var mainProvider = new SkinProvidingContainer(skin)
{
RelativeSizeAxes = !autoSize ? Axes.Both : Axes.None,
AutoSizeAxes = autoSize ? Axes.Both : Axes.None,
};
return new Container
{
RelativeSizeAxes = Axes.Both,
BorderColour = Color4.White,
BorderThickness = 5,
Masking = true,
Children = new Drawable[]
{
new Box
{
AlwaysPresent = true,
Alpha = 0,
RelativeSizeAxes = Axes.Both,
},
new OsuSpriteText
{
Text = skin?.SkinInfo?.Name ?? "none",
Scale = new Vector2(1.5f),
Padding = new MarginPadding(5),
},
new Container
{
RelativeSizeAxes = !autoSize ? Axes.Both : Axes.None,
AutoSizeAxes = autoSize ? Axes.Both : Axes.None,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Children = new Drawable[]
{
new OutlineBox { Alpha = autoSize ? 1 : 0 },
mainProvider.WithChild(
new SkinProvidingContainer(Ruleset.Value.CreateInstance().CreateLegacySkinProvider(mainProvider, beatmap))
{
Child = created,
RelativeSizeAxes = !autoSize ? Axes.Both : Axes.None,
AutoSizeAxes = autoSize ? Axes.Both : Axes.None,
}
)
}
},
}
};
}
/// <summary>
/// Creates the ruleset for adding the corresponding skin transforming component.
/// </summary>
[NotNull]
protected abstract Ruleset CreateRulesetForSkinProvider();
protected sealed override Ruleset CreateRuleset() => CreateRulesetForSkinProvider();
protected virtual IBeatmap CreateBeatmapForSkinProvider() => CreateWorkingBeatmap(Ruleset.Value).GetPlayableBeatmap(Ruleset.Value);
private class OutlineBox : CompositeDrawable
{
public OutlineBox()
{
BorderColour = Color4.IndianRed;
BorderThickness = 5;
Masking = true;
RelativeSizeAxes = Axes.Both;
InternalChild = new Box
2019-08-26 11:31:51 +08:00
{
RelativeSizeAxes = Axes.Both,
Alpha = 0,
Colour = Color4.Brown,
AlwaysPresent = true
};
}
2019-07-26 18:29:06 +08:00
}
2019-08-20 16:39:16 +08:00
private class TestLegacySkin : LegacySkin
2019-07-26 18:29:06 +08:00
{
2019-08-20 16:39:16 +08:00
private readonly bool extrapolateAnimations;
2019-07-30 20:53:28 +08:00
2019-08-20 16:39:16 +08:00
public TestLegacySkin(SkinInfo skin, IResourceStore<byte[]> storage, AudioManager audioManager, bool extrapolateAnimations)
: base(skin, storage, audioManager, "skin.ini")
{
this.extrapolateAnimations = extrapolateAnimations;
}
2019-07-30 20:53:28 +08:00
2020-07-17 15:54:30 +08:00
public override Texture GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT)
2019-08-20 16:39:16 +08:00
{
// extrapolate frames to test longer animations
if (extrapolateAnimations)
{
var match = Regex.Match(componentName, "-([0-9]*)");
2019-07-30 20:53:28 +08:00
if (match.Length > 0 && int.TryParse(match.Groups[1].Value, out var number) && number < 60)
2020-07-17 15:54:30 +08:00
return base.GetTexture(componentName.Replace($"-{number}", $"-{number % 2}"), wrapModeS, wrapModeT);
2019-08-20 16:39:16 +08:00
}
2019-07-30 20:53:28 +08:00
2020-07-17 15:54:30 +08:00
return base.GetTexture(componentName, wrapModeS, wrapModeT);
2019-07-30 20:53:28 +08:00
}
2019-07-26 18:29:06 +08:00
}
}
}