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;
|
2021-06-22 07:19:55 +00:00
|
|
|
using System.Collections.Generic;
|
2019-07-18 08:53:01 +00:00
|
|
|
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;
|
2019-09-03 08:57:34 +00:00
|
|
|
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[]
|
|
|
|
{
|
2021-05-27 05:50:42 +00:00
|
|
|
new ExposedSkinnableDrawable("default", _ => new DefaultBox()),
|
|
|
|
new ExposedSkinnableDrawable("available", _ => new DefaultBox()),
|
|
|
|
new ExposedSkinnableDrawable("available", _ => new DefaultBox(), ConfineMode.NoScaling)
|
2019-07-18 08:53:01 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
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[]
|
|
|
|
{
|
2021-05-27 05:50:42 +00:00
|
|
|
new ExposedSkinnableDrawable("default", _ => new DefaultBox()),
|
2021-07-05 15:52:39 +00:00
|
|
|
new ExposedSkinnableDrawable("available", _ => new DefaultBox()),
|
2021-05-27 05:50:42 +00:00
|
|
|
new ExposedSkinnableDrawable("available", _ => new DefaultBox(), ConfineMode.NoScaling)
|
2019-07-18 08:53:01 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
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,
|
2021-05-27 05:50:42 +00:00
|
|
|
Child = consumer = new SkinConsumer("test", name => new NamedBox("Default Implementation"))
|
2019-07-18 08:53:01 +00: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 03:31:51 +00:00
|
|
|
Child = target = new SkinProvidingContainer(secondarySource)
|
2019-07-18 08:53:01 +00:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2021-05-27 05:50:42 +00:00
|
|
|
AddStep("add permissive", () => target.Add(consumer = new SkinConsumer("test", name => new NamedBox("Default Implementation"))));
|
2019-07-18 08:53:01 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2021-05-27 05:50:42 +00:00
|
|
|
AddStep("add permissive", () => target.Add(consumer = new SkinConsumer("test", name => new NamedBox("Default Implementation"))));
|
2019-09-03 09:21:57 +00: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 08:37:34 +00:00
|
|
|
TriggerSourceChanged();
|
2019-09-03 09:21:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public SwitchableSkinProvidingContainer(ISkin skin)
|
|
|
|
: base(skin)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 08:53:01 +00:00
|
|
|
private class ExposedSkinnableDrawable : SkinnableDrawable
|
|
|
|
{
|
|
|
|
public new Drawable Drawable => base.Drawable;
|
|
|
|
|
2021-05-27 05:50:42 +00:00
|
|
|
public ExposedSkinnableDrawable(string name, Func<ISkinComponent, Drawable> defaultImplementation, ConfineMode confineMode = ConfineMode.ScaleToFit)
|
|
|
|
: base(new TestSkinComponent(name), defaultImplementation, 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; }
|
|
|
|
|
2021-05-27 05:50:42 +00:00
|
|
|
public SkinConsumer(string name, Func<ISkinComponent, Drawable> defaultImplementation)
|
|
|
|
: base(new TestSkinComponent(name), defaultImplementation)
|
2019-07-18 08:53:01 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-05-27 05:50:42 +00:00
|
|
|
protected override void SkinChanged(ISkinSource skin)
|
2019-07-18 08:53:01 +00:00
|
|
|
{
|
2021-05-27 05:50:42 +00:00
|
|
|
base.SkinChanged(skin);
|
2019-07-18 08:53:01 +00: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;
|
|
|
|
}
|
|
|
|
|
2019-08-30 05:39:02 +00:00
|
|
|
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
|
|
|
|
2021-02-18 09:32:28 +00:00
|
|
|
public ISample GetSample(ISampleInfo sampleInfo) => throw new NotImplementedException();
|
2019-07-18 08:53:01 +00:00
|
|
|
|
2019-09-03 08:57:34 +00:00
|
|
|
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup) => throw new NotImplementedException();
|
2021-05-31 08:25:21 +00:00
|
|
|
|
|
|
|
public ISkin FindProvider(Func<ISkin, bool> lookupFunction) => throw new NotImplementedException();
|
2019-07-18 08:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private class SecondarySource : ISkin
|
|
|
|
{
|
2019-08-30 05:39:02 +00:00
|
|
|
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
|
|
|
|
2021-02-18 09:32:28 +00:00
|
|
|
public ISample GetSample(ISampleInfo sampleInfo) => throw new NotImplementedException();
|
2019-07-18 08:53:01 +00:00
|
|
|
|
2019-09-03 08:57:34 +00:00
|
|
|
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup) => throw new NotImplementedException();
|
2021-05-31 08:25:21 +00:00
|
|
|
|
|
|
|
public ISkin FindProvider(Func<ISkin, bool> lookupFunction) => 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
|
|
|
{
|
2019-08-30 05:39:02 +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
|
|
|
|
2021-02-18 09:32:28 +00:00
|
|
|
public ISample GetSample(ISampleInfo sampleInfo) => throw new NotImplementedException();
|
2019-07-18 08:53:01 +00:00
|
|
|
|
2019-09-03 08:57:34 +00:00
|
|
|
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup) => throw new NotImplementedException();
|
2019-09-03 09:21:57 +00:00
|
|
|
|
2021-05-31 08:25:21 +00:00
|
|
|
public ISkin FindProvider(Func<ISkin, bool> lookupFunction) => throw new NotImplementedException();
|
|
|
|
|
2021-06-22 09:02:21 +00:00
|
|
|
public IEnumerable<ISkin> AllSources => throw new NotImplementedException();
|
2021-06-22 07:19:55 +00:00
|
|
|
|
2019-10-30 15:43:13 +00:00
|
|
|
public event Action SourceChanged
|
|
|
|
{
|
|
|
|
add { }
|
|
|
|
remove { }
|
|
|
|
}
|
2019-07-18 08:53:01 +00:00
|
|
|
}
|
2019-08-30 05:39:02 +00:00
|
|
|
|
|
|
|
private class TestSkinComponent : ISkinComponent
|
|
|
|
{
|
|
|
|
public TestSkinComponent(string name)
|
|
|
|
{
|
2019-11-12 09:45:42 +00:00
|
|
|
LookupName = name;
|
2019-08-30 05:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public string ComponentGroup => string.Empty;
|
|
|
|
|
2019-11-12 09:45:42 +00:00
|
|
|
public string LookupName { get; }
|
2019-08-30 05:39:02 +00:00
|
|
|
}
|
2019-07-18 08:53:01 +00:00
|
|
|
}
|
|
|
|
}
|