1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-27 07:53:06 +08:00
osu-lazer/osu.Game.Tests/Visual/Gameplay/TestSceneSkinnableDrawable.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

354 lines
12 KiB
C#
Raw Normal View History

2019-07-18 16:53:01 +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.
2022-06-17 15:37:17 +08:00
#nullable disable
2019-07-18 16:53:01 +08:00
using System;
using System.Collections.Generic;
2019-07-18 16:53:01 +08:00
using System.Globalization;
using System.Linq;
using NUnit.Framework;
2019-09-03 17:21:57 +08:00
using osu.Framework.Allocation;
2019-07-18 16:53:01 +08:00
using osu.Framework.Audio.Sample;
using osu.Framework.Bindables;
2019-07-18 16:53:01 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2020-07-17 15:54:30 +08:00
using osu.Framework.Graphics.OpenGL.Textures;
2019-07-18 16:53:01 +08:00
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Textures;
2019-08-23 19:32:43 +08:00
using osu.Game.Audio;
2019-07-18 16:53:01 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Skinning;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Tests.Visual.Gameplay
{
public class TestSceneSkinnableDrawable : OsuTestScene
{
[Test]
public void TestConfineScaleDown()
{
FillFlowContainer<ExposedSkinnableDrawable> fill = null;
AddStep("setup layout larger source", () =>
{
2019-08-26 11:31:51 +08:00
Child = new SkinProvidingContainer(new SizedSource(50))
2019-07-18 16:53:01 +08:00
{
RelativeSizeAxes = Axes.Both,
Child = fill = new FillFlowContainer<ExposedSkinnableDrawable>
{
Size = new Vector2(30),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Spacing = new Vector2(10),
Children = new[]
{
new ExposedSkinnableDrawable("default", _ => new DefaultBox()),
new ExposedSkinnableDrawable("available", _ => new DefaultBox()),
new ExposedSkinnableDrawable("available", _ => new DefaultBox(), ConfineMode.NoScaling)
2019-07-18 16:53:01 +08:00
}
},
};
});
2020-03-31 13:15:25 +08:00
AddAssert("check sizes", () => fill.Children.Select(c => c.Drawable.DrawWidth).SequenceEqual(new float[] { 30, 30, 50 }));
2019-07-18 16:53:01 +08:00
AddStep("adjust scale", () => fill.Scale = new Vector2(2));
2020-03-31 13:15:25 +08:00
AddAssert("check sizes unchanged by scale", () => fill.Children.Select(c => c.Drawable.DrawWidth).SequenceEqual(new float[] { 30, 30, 50 }));
2019-07-18 16:53:01 +08:00
}
[Test]
public void TestConfineScaleUp()
{
FillFlowContainer<ExposedSkinnableDrawable> fill = null;
AddStep("setup layout larger source", () =>
{
2019-08-26 11:31:51 +08:00
Child = new SkinProvidingContainer(new SizedSource(30))
2019-07-18 16:53:01 +08:00
{
RelativeSizeAxes = Axes.Both,
Child = fill = new FillFlowContainer<ExposedSkinnableDrawable>
{
Size = new Vector2(50),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Spacing = new Vector2(10),
Children = new[]
{
new ExposedSkinnableDrawable("default", _ => new DefaultBox()),
2021-07-05 23:52:39 +08:00
new ExposedSkinnableDrawable("available", _ => new DefaultBox()),
new ExposedSkinnableDrawable("available", _ => new DefaultBox(), ConfineMode.NoScaling)
2019-07-18 16:53:01 +08:00
}
},
};
});
2020-03-31 13:15:25 +08:00
AddAssert("check sizes", () => fill.Children.Select(c => c.Drawable.DrawWidth).SequenceEqual(new float[] { 50, 50, 30 }));
2019-07-18 16:53:01 +08:00
AddStep("adjust scale", () => fill.Scale = new Vector2(2));
2020-03-31 13:15:25 +08:00
AddAssert("check sizes unchanged by scale", () => fill.Children.Select(c => c.Drawable.DrawWidth).SequenceEqual(new float[] { 50, 50, 30 }));
2019-07-18 16:53:01 +08:00
}
[Test]
public void TestInitialLoad()
{
var secondarySource = new SecondarySource();
SkinConsumer consumer = null;
AddStep("setup layout", () =>
{
Child = new SkinSourceContainer
{
RelativeSizeAxes = Axes.Both,
2019-08-26 11:31:51 +08:00
Child = new SkinProvidingContainer(secondarySource)
2019-07-18 16:53:01 +08:00
{
RelativeSizeAxes = Axes.Both,
2022-06-24 20:25:23 +08:00
Child = consumer = new SkinConsumer("test", _ => new NamedBox("Default Implementation"))
2019-07-18 16:53:01 +08:00
}
};
});
AddAssert("consumer using override source", () => consumer.Drawable is SecondarySourceBox);
AddAssert("skinchanged only called once", () => consumer.SkinChangedCount == 1);
}
[Test]
public void TestOverride()
{
var secondarySource = new SecondarySource();
SkinConsumer consumer = null;
Container target = null;
AddStep("setup layout", () =>
{
Child = new SkinSourceContainer
{
RelativeSizeAxes = Axes.Both,
2019-08-26 11:31:51 +08:00
Child = target = new SkinProvidingContainer(secondarySource)
2019-07-18 16:53:01 +08:00
{
RelativeSizeAxes = Axes.Both,
}
};
});
2022-06-24 20:25:23 +08:00
AddStep("add permissive", () => target.Add(consumer = new SkinConsumer("test", _ => new NamedBox("Default Implementation"))));
2019-07-18 16:53:01 +08:00
AddAssert("consumer using override source", () => consumer.Drawable is SecondarySourceBox);
AddAssert("skinchanged only called once", () => consumer.SkinChangedCount == 1);
}
2019-09-03 17:21:57 +08:00
[Test]
public void TestSwitchOff()
{
SkinConsumer consumer = null;
SwitchableSkinProvidingContainer target = null;
AddStep("setup layout", () =>
{
Child = new SkinSourceContainer
{
RelativeSizeAxes = Axes.Both,
Child = target = new SwitchableSkinProvidingContainer(new SecondarySource())
{
RelativeSizeAxes = Axes.Both,
}
};
});
2022-06-24 20:25:23 +08:00
AddStep("add permissive", () => target.Add(consumer = new SkinConsumer("test", _ => new NamedBox("Default Implementation"))));
2019-09-03 17:21:57 +08:00
AddAssert("consumer using override source", () => consumer.Drawable is SecondarySourceBox);
AddStep("disable", () => target.Disable());
AddAssert("consumer using base source", () => consumer.Drawable is BaseSourceBox);
}
private class SwitchableSkinProvidingContainer : SkinProvidingContainer
{
private bool allow = true;
protected override bool AllowDrawableLookup(ISkinComponent component) => allow;
public void Disable()
{
allow = false;
2021-07-06 16:37:34 +08:00
TriggerSourceChanged();
2019-09-03 17:21:57 +08:00
}
public SwitchableSkinProvidingContainer(ISkin skin)
: base(skin)
{
}
}
2019-07-18 16:53:01 +08:00
private class ExposedSkinnableDrawable : SkinnableDrawable
{
public new Drawable Drawable => base.Drawable;
public ExposedSkinnableDrawable(string name, Func<ISkinComponent, Drawable> defaultImplementation, ConfineMode confineMode = ConfineMode.ScaleToFit)
: base(new TestSkinComponent(name), defaultImplementation, confineMode)
2019-07-18 16:53:01 +08:00
{
}
}
private class DefaultBox : DrawWidthBox
{
public DefaultBox()
{
RelativeSizeAxes = Axes.Both;
}
}
private class DrawWidthBox : Container
{
private readonly OsuSpriteText text;
public DrawWidthBox()
{
Children = new Drawable[]
{
new Box
{
Colour = Color4.Gray,
RelativeSizeAxes = Axes.Both,
},
text = new OsuSpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
}
};
}
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
text.Text = DrawWidth.ToString(CultureInfo.InvariantCulture);
}
}
private class NamedBox : Container
{
public NamedBox(string name)
{
Children = new Drawable[]
{
new Box
{
Colour = Color4.Black,
RelativeSizeAxes = Axes.Both,
},
new OsuSpriteText
{
Font = OsuFont.Default.With(size: 40),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Text = name
}
};
}
}
private class SkinConsumer : SkinnableDrawable
{
public new Drawable Drawable => base.Drawable;
public int SkinChangedCount { get; private set; }
public SkinConsumer(string name, Func<ISkinComponent, Drawable> defaultImplementation)
: base(new TestSkinComponent(name), defaultImplementation)
2019-07-18 16:53:01 +08:00
{
}
protected override void SkinChanged(ISkinSource skin)
2019-07-18 16:53:01 +08:00
{
base.SkinChanged(skin);
2019-07-18 16:53:01 +08:00
SkinChangedCount++;
}
}
private class BaseSourceBox : NamedBox
{
public BaseSourceBox()
: base("Base Source")
{
}
}
private class SecondarySourceBox : NamedBox
{
public SecondarySourceBox()
: base("Secondary Source")
{
}
}
private class SizedSource : ISkin
{
private readonly float size;
public SizedSource(float size)
{
this.size = size;
}
public Drawable GetDrawableComponent(ISkinComponent componentName) =>
componentName.LookupName == "available"
2019-07-18 16:53:01 +08:00
? new DrawWidthBox
{
Colour = Color4.Yellow,
Size = new Vector2(size)
}
: null;
2020-07-17 15:54:30 +08:00
public Texture GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT) => throw new NotImplementedException();
2019-07-18 16:53:01 +08:00
public ISample GetSample(ISampleInfo sampleInfo) => throw new NotImplementedException();
2019-07-18 16:53:01 +08:00
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup) => throw new NotImplementedException();
2019-07-18 16:53:01 +08:00
}
private class SecondarySource : ISkin
{
public Drawable GetDrawableComponent(ISkinComponent componentName) => new SecondarySourceBox();
2019-07-18 16:53:01 +08:00
2020-07-17 15:54:30 +08:00
public Texture GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT) => throw new NotImplementedException();
2019-07-18 16:53:01 +08:00
public ISample GetSample(ISampleInfo sampleInfo) => throw new NotImplementedException();
2019-07-18 16:53:01 +08:00
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup) => throw new NotImplementedException();
2019-07-18 16:53:01 +08:00
}
2019-09-03 17:21:57 +08:00
[Cached(typeof(ISkinSource))]
private class SkinSourceContainer : Container, ISkinSource
2019-07-18 16:53:01 +08:00
{
public Drawable GetDrawableComponent(ISkinComponent componentName) => new BaseSourceBox();
2019-07-18 16:53:01 +08:00
2020-07-17 15:54:30 +08:00
public Texture GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT) => throw new NotImplementedException();
2019-07-18 16:53:01 +08:00
public ISample GetSample(ISampleInfo sampleInfo) => throw new NotImplementedException();
2019-07-18 16:53:01 +08:00
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup) => throw new NotImplementedException();
2019-09-03 17:21:57 +08:00
2021-05-31 16:25:21 +08:00
public ISkin FindProvider(Func<ISkin, bool> lookupFunction) => throw new NotImplementedException();
public IEnumerable<ISkin> AllSources => throw new NotImplementedException();
2019-10-30 23:43:13 +08:00
public event Action SourceChanged
{
add { }
remove { }
}
2019-07-18 16:53:01 +08:00
}
private class TestSkinComponent : ISkinComponent
{
public TestSkinComponent(string name)
{
2019-11-12 17:45:42 +08:00
LookupName = name;
}
public string ComponentGroup => string.Empty;
2019-11-12 17:45:42 +08:00
public string LookupName { get; }
}
2019-07-18 16:53:01 +08:00
}
}