osu/osu.Game.Tests/Visual/Gameplay/TestSceneSkinnableDrawable.cs

348 lines
12 KiB
C#
Raw Normal View History

2019-07-18 08:53:01 +00: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.Globalization;
using System.Linq;
using NUnit.Framework;
2019-09-03 09:21:57 +00:00
using osu.Framework.Allocation;
2019-07-18 08:53:01 +00:00
using osu.Framework.Audio.Sample;
using osu.Framework.Bindables;
2019-07-18 08:53:01 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2020-07-17 07:54:30 +00:00
using osu.Framework.Graphics.OpenGL.Textures;
2019-07-18 08:53:01 +00:00
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Textures;
2019-08-23 11:32:43 +00:00
using osu.Game.Audio;
2019-07-18 08:53:01 +00: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 03:31:51 +00:00
Child = new SkinProvidingContainer(new SizedSource(50))
2019-07-18 08:53:01 +00: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(), _ => true),
new ExposedSkinnableDrawable("available", _ => new DefaultBox(), _ => true),
new ExposedSkinnableDrawable("available", _ => new DefaultBox(), _ => true, ConfineMode.NoScaling)
}
},
};
});
2020-03-31 05:15:25 +00:00
AddAssert("check sizes", () => fill.Children.Select(c => c.Drawable.DrawWidth).SequenceEqual(new float[] { 30, 30, 50 }));
2019-07-18 08:53:01 +00:00
AddStep("adjust scale", () => fill.Scale = new Vector2(2));
2020-03-31 05:15:25 +00:00
AddAssert("check sizes unchanged by scale", () => fill.Children.Select(c => c.Drawable.DrawWidth).SequenceEqual(new float[] { 30, 30, 50 }));
2019-07-18 08:53:01 +00:00
}
[Test]
public void TestConfineScaleUp()
{
FillFlowContainer<ExposedSkinnableDrawable> fill = null;
AddStep("setup layout larger source", () =>
{
2019-08-26 03:31:51 +00:00
Child = new SkinProvidingContainer(new SizedSource(30))
2019-07-18 08:53:01 +00: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(), _ => true),
new ExposedSkinnableDrawable("available", _ => new DefaultBox(), _ => true, ConfineMode.ScaleToFit),
new ExposedSkinnableDrawable("available", _ => new DefaultBox(), _ => true, ConfineMode.NoScaling)
}
},
};
});
2020-03-31 05:15:25 +00:00
AddAssert("check sizes", () => fill.Children.Select(c => c.Drawable.DrawWidth).SequenceEqual(new float[] { 50, 50, 30 }));
2019-07-18 08:53:01 +00:00
AddStep("adjust scale", () => fill.Scale = new Vector2(2));
2020-03-31 05:15:25 +00:00
AddAssert("check sizes unchanged by scale", () => fill.Children.Select(c => c.Drawable.DrawWidth).SequenceEqual(new float[] { 50, 50, 30 }));
2019-07-18 08:53:01 +00:00
}
[Test]
public void TestInitialLoad()
{
var secondarySource = new SecondarySource();
SkinConsumer consumer = null;
AddStep("setup layout", () =>
{
Child = new SkinSourceContainer
{
RelativeSizeAxes = Axes.Both,
2019-08-26 03:31:51 +00:00
Child = new SkinProvidingContainer(secondarySource)
2019-07-18 08:53:01 +00:00
{
RelativeSizeAxes = Axes.Both,
Child = consumer = new SkinConsumer("test", name => new NamedBox("Default Implementation"), source => true)
}
};
});
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 03:31:51 +00:00
Child = target = new SkinProvidingContainer(secondarySource)
2019-07-18 08:53:01 +00:00
{
RelativeSizeAxes = Axes.Both,
}
};
});
AddStep("add permissive", () => target.Add(consumer = new SkinConsumer("test", name => new NamedBox("Default Implementation"), source => true)));
AddAssert("consumer using override source", () => consumer.Drawable is SecondarySourceBox);
AddAssert("skinchanged only called once", () => consumer.SkinChangedCount == 1);
}
2019-09-03 09:21:57 +00: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,
}
};
});
AddStep("add permissive", () => target.Add(consumer = new SkinConsumer("test", name => new NamedBox("Default Implementation"), source => true)));
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;
TriggerSourceChanged();
}
public SwitchableSkinProvidingContainer(ISkin skin)
: base(skin)
{
}
}
2019-07-18 08:53:01 +00:00
private class ExposedSkinnableDrawable : SkinnableDrawable
{
public new Drawable Drawable => base.Drawable;
public ExposedSkinnableDrawable(string name, Func<ISkinComponent, Drawable> defaultImplementation, Func<ISkinSource, bool> allowFallback = null,
ConfineMode confineMode = ConfineMode.ScaleToFit)
: base(new TestSkinComponent(name), defaultImplementation, allowFallback, confineMode)
2019-07-18 08:53:01 +00: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, Func<ISkinSource, bool> allowFallback = null)
: base(new TestSkinComponent(name), defaultImplementation, allowFallback)
2019-07-18 08:53:01 +00:00
{
}
protected override void SkinChanged(ISkinSource skin, bool allowFallback)
{
base.SkinChanged(skin, allowFallback);
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 08:53:01 +00:00
? new DrawWidthBox
{
Colour = Color4.Yellow,
Size = new Vector2(size)
}
: null;
2020-07-17 07:54:30 +00:00
public Texture GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT) => throw new NotImplementedException();
2019-07-18 08:53:01 +00:00
public ISample GetSample(ISampleInfo sampleInfo) => throw new NotImplementedException();
2019-07-18 08:53:01 +00:00
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup) => throw new NotImplementedException();
2019-07-18 08:53:01 +00:00
}
private class SecondarySource : ISkin
{
public Drawable GetDrawableComponent(ISkinComponent componentName) => new SecondarySourceBox();
2019-07-18 08:53:01 +00:00
2020-07-17 07:54:30 +00:00
public Texture GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT) => throw new NotImplementedException();
2019-07-18 08:53:01 +00:00
public ISample GetSample(ISampleInfo sampleInfo) => throw new NotImplementedException();
2019-07-18 08:53:01 +00:00
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup) => throw new NotImplementedException();
2019-07-18 08:53:01 +00:00
}
2019-09-03 09:21:57 +00:00
[Cached(typeof(ISkinSource))]
private class SkinSourceContainer : Container, ISkinSource
2019-07-18 08:53:01 +00:00
{
public Drawable GetDrawableComponent(ISkinComponent componentName) => new BaseSourceBox();
2019-07-18 08:53:01 +00:00
2020-07-17 07:54:30 +00:00
public Texture GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT) => throw new NotImplementedException();
2019-07-18 08:53:01 +00:00
public ISample GetSample(ISampleInfo sampleInfo) => throw new NotImplementedException();
2019-07-18 08:53:01 +00:00
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup) => throw new NotImplementedException();
2019-09-03 09:21:57 +00:00
2019-10-30 15:43:13 +00:00
public event Action SourceChanged
{
add { }
remove { }
}
2019-07-18 08:53:01 +00:00
}
private class TestSkinComponent : ISkinComponent
{
public TestSkinComponent(string name)
{
2019-11-12 09:45:42 +00:00
LookupName = name;
}
public string ComponentGroup => string.Empty;
2019-11-12 09:45:42 +00:00
public string LookupName { get; }
}
2019-07-18 08:53:01 +00:00
}
}