Skip to content

Commit

Permalink
Merge pull request ppy#5969 from peppy/skin-configuration-refactor
Browse files Browse the repository at this point in the history
Refactor skin configuration lookups to be more flexible

Co-authored-by: Dan Balasescu <smoogipoo@smgi.me>
  • Loading branch information
peppy and smoogipoo committed Sep 5, 2019
2 parents 986ac08 + 90985b6 commit 9d0151f
Show file tree
Hide file tree
Showing 33 changed files with 457 additions and 121 deletions.
4 changes: 2 additions & 2 deletions osu.Game.Rulesets.Catch.Tests/TestSceneCatcher.cs
Expand Up @@ -14,6 +14,7 @@
using osu.Framework.Graphics.Sprites;
using osuTK.Graphics;
using osu.Framework.Audio.Sample;
using osu.Framework.Bindables;
using osu.Framework.Graphics.Textures;
using osu.Game.Audio;

Expand Down Expand Up @@ -99,8 +100,7 @@ public Drawable GetDrawableComponent(ISkinComponent component)
public Texture GetTexture(string componentName) =>
throw new NotImplementedException();

public TValue GetValue<TConfiguration, TValue>(Func<TConfiguration, TValue> query) where TConfiguration : SkinConfiguration =>
throw new NotImplementedException();
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup) => throw new NotImplementedException();
}
}
}
2 changes: 2 additions & 0 deletions osu.Game.Rulesets.Osu.Tests/TestSceneSkinFallbacks.cs
Expand Up @@ -7,6 +7,7 @@
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Textures;
Expand Down Expand Up @@ -135,6 +136,7 @@ public Drawable GetDrawableComponent(ISkinComponent component)
public SampleChannel GetSample(ISampleInfo sampleInfo) => null;

public TValue GetValue<TConfiguration, TValue>(Func<TConfiguration, TValue> query) where TConfiguration : SkinConfiguration => default;
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup) => null;

public event Action SourceChanged;

Expand Down
9 changes: 5 additions & 4 deletions osu.Game.Rulesets.Osu/Objects/Drawables/DrawableSlider.cs
Expand Up @@ -12,6 +12,7 @@
using osu.Framework.Graphics.Containers;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Osu.Configuration;
using osu.Game.Rulesets.Osu.Skinning;
using osu.Game.Rulesets.Scoring;
using osuTK.Graphics;
using osu.Game.Skinning;
Expand Down Expand Up @@ -166,12 +167,12 @@ protected override void SkinChanged(ISkinSource skin, bool allowFallback)
{
base.SkinChanged(skin, allowFallback);

Body.BorderSize = skin.GetValue<SkinConfiguration, float?>(s => s.SliderBorderSize) ?? SliderBody.DEFAULT_BORDER_SIZE;
sliderPathRadius = skin.GetValue<SkinConfiguration, float?>(s => s.SliderPathRadius) ?? OsuHitObject.OBJECT_RADIUS;
Body.BorderSize = skin.GetConfig<OsuSkinConfiguration, float>(OsuSkinConfiguration.SliderBorderSize)?.Value ?? SliderBody.DEFAULT_BORDER_SIZE;
sliderPathRadius = skin.GetConfig<OsuSkinConfiguration, float>(OsuSkinConfiguration.SliderPathRadius)?.Value ?? OsuHitObject.OBJECT_RADIUS;
updatePathRadius();

Body.AccentColour = skin.GetValue<SkinConfiguration, Color4?>(s => s.CustomColours.ContainsKey("SliderTrackOverride") ? s.CustomColours["SliderTrackOverride"] : (Color4?)null) ?? AccentColour.Value;
Body.BorderColour = skin.GetValue<SkinConfiguration, Color4?>(s => s.CustomColours.ContainsKey("SliderBorder") ? s.CustomColours["SliderBorder"] : (Color4?)null) ?? Color4.White;
Body.AccentColour = skin.GetConfig<OsuSkinColour, Color4>(OsuSkinColour.SliderTrackOverride)?.Value ?? AccentColour.Value;
Body.BorderColour = skin.GetConfig<OsuSkinColour, Color4>(OsuSkinColour.SliderBorder)?.Value ?? Color4.White;
}

private void updatePathRadius() => Body.PathRadius = slider.Scale * sliderPathRadius;
Expand Down
3 changes: 2 additions & 1 deletion osu.Game.Rulesets.Osu/Objects/Drawables/Pieces/SliderBall.cs
Expand Up @@ -11,6 +11,7 @@
using osu.Framework.Input.Events;
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.Objects.Types;
using osu.Game.Rulesets.Osu.Skinning;
using osuTK.Graphics;
using osu.Game.Skinning;
using osuTK;
Expand Down Expand Up @@ -218,7 +219,7 @@ private void load(DrawableHitObject drawableObject, ISkinSource skin)
{
RelativeSizeAxes = Axes.Both;

float radius = skin.GetValue<SkinConfiguration, float?>(s => s.SliderPathRadius) ?? OsuHitObject.OBJECT_RADIUS;
float radius = skin.GetConfig<OsuSkinConfiguration, float>(OsuSkinConfiguration.SliderPathRadius)?.Value ?? OsuHitObject.OBJECT_RADIUS;

InternalChild = new CircularContainer
{
Expand Down
2 changes: 1 addition & 1 deletion osu.Game.Rulesets.Osu/OsuRuleset.cs
Expand Up @@ -167,7 +167,7 @@ public override IEnumerable<Mod> GetModsFor(ModType type)

public override RulesetSettingsSubsection CreateSettings() => new OsuSettingsSubsection(this);

public override ISkin CreateLegacySkinProvider(ISkinSource source) => new OsuLegacySkin(source);
public override ISkin CreateLegacySkinProvider(ISkinSource source) => new OsuLegacySkinTransformer(source);

public override int? LegacyID => 0;

Expand Down
2 changes: 1 addition & 1 deletion osu.Game.Rulesets.Osu/Skinning/LegacySliderBall.cs
Expand Up @@ -23,7 +23,7 @@ public LegacySliderBall(Drawable animationContent)
[BackgroundDependencyLoader]
private void load(ISkinSource skin, DrawableHitObject drawableObject)
{
animationContent.Colour = skin.GetValue<SkinConfiguration, Color4?>(s => s.CustomColours.ContainsKey("SliderBall") ? s.CustomColours["SliderBall"] : (Color4?)null) ?? Color4.White;
animationContent.Colour = skin.GetConfig<OsuSkinColour, Color4>(OsuSkinColour.SliderBall)?.Value ?? Color4.White;

InternalChildren = new[]
{
Expand Down
Expand Up @@ -3,21 +3,19 @@

using System;
using osu.Framework.Audio.Sample;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Textures;
using osu.Game.Audio;
using osu.Game.Skinning;
using osuTK;
using osuTK.Graphics;

namespace osu.Game.Rulesets.Osu.Skinning
{
public class OsuLegacySkin : ISkin
public class OsuLegacySkinTransformer : ISkin
{
private readonly ISkin source;

private Lazy<SkinConfiguration> configuration;

private Lazy<bool> hasHitCircle;

/// <summary>
Expand All @@ -27,7 +25,7 @@ public class OsuLegacySkin : ISkin
/// </summary>
private const float legacy_circle_radius = 64 - 5;

public OsuLegacySkin(ISkinSource source)
public OsuLegacySkinTransformer(ISkinSource source)
{
this.source = source;

Expand All @@ -37,21 +35,6 @@ public OsuLegacySkin(ISkinSource source)

private void sourceChanged()
{
// these need to be lazy in order to ensure they aren't called before the dependencies have been loaded into our source.
configuration = new Lazy<SkinConfiguration>(() =>
{
var config = new SkinConfiguration();
if (hasHitCircle.Value)
config.SliderPathRadius = legacy_circle_radius;
// defaults should only be applied for non-beatmap skins (which are parsed via this constructor).
config.CustomColours["SliderBall"] =
source.GetValue<SkinConfiguration, Color4?>(s => s.CustomColours.TryGetValue("SliderBall", out var val) ? val : (Color4?)null)
?? new Color4(2, 170, 255, 255);
return config;
});

hasHitCircle = new Lazy<bool>(() => source.GetTexture("hitcircle") != null);
}

Expand Down Expand Up @@ -96,8 +79,8 @@ public Drawable GetDrawableComponent(ISkinComponent component)
return null;

case OsuSkinComponents.HitCircleText:
string font = GetValue<SkinConfiguration, string>(config => config.HitCircleFont);
var overlap = GetValue<SkinConfiguration, float>(config => config.HitCircleOverlap);
var font = GetConfig<OsuSkinConfiguration, string>(OsuSkinConfiguration.HitCircleFont)?.Value ?? "default";
var overlap = GetConfig<OsuSkinConfiguration, float>(OsuSkinConfiguration.HitCircleOverlap)?.Value ?? 0;

return !hasFont(font)
? null
Expand All @@ -116,13 +99,27 @@ public Drawable GetDrawableComponent(ISkinComponent component)

public SampleChannel GetSample(ISampleInfo sample) => source.GetSample(sample);

public TValue GetValue<TConfiguration, TValue>(Func<TConfiguration, TValue> query) where TConfiguration : SkinConfiguration
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup)
{
TValue val;
if (configuration.Value is TConfiguration conf && (val = query.Invoke(conf)) != null)
return val;
switch (lookup)
{
case OsuSkinColour colour:
return source.GetConfig<SkinCustomColourLookup, TValue>(new SkinCustomColourLookup(colour));

case OsuSkinConfiguration osuLookup:
switch (osuLookup)
{
case OsuSkinConfiguration.SliderPathRadius:
if (hasHitCircle.Value)
return SkinUtils.As<TValue>(new BindableFloat(legacy_circle_radius));

break;
}

break;
}

return source.GetValue(query);
return source.GetConfig<TLookup, TValue>(lookup);
}

private bool hasFont(string fontName) => source.GetTexture($"{fontName}-0") != null;
Expand Down
12 changes: 12 additions & 0 deletions osu.Game.Rulesets.Osu/Skinning/OsuSkinColour.cs
@@ -0,0 +1,12 @@
// 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.

namespace osu.Game.Rulesets.Osu.Skinning
{
public enum OsuSkinColour
{
SliderTrackOverride,
SliderBorder,
SliderBall
}
}
14 changes: 14 additions & 0 deletions osu.Game.Rulesets.Osu/Skinning/OsuSkinConfiguration.cs
@@ -0,0 +1,14 @@
// 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.

namespace osu.Game.Rulesets.Osu.Skinning
{
public enum OsuSkinConfiguration
{
HitCircleFont,
HitCircleOverlap,
SliderBorderSize,
SliderPathRadius,
CursorExpand,
}
}
3 changes: 2 additions & 1 deletion osu.Game.Rulesets.Osu/UI/Cursor/OsuCursor.cs
Expand Up @@ -10,6 +10,7 @@
using osu.Framework.Graphics.Shapes;
using osu.Game.Beatmaps;
using osu.Game.Configuration;
using osu.Game.Rulesets.Osu.Skinning;
using osu.Game.Skinning;
using osuTK;
using osuTK.Graphics;
Expand Down Expand Up @@ -38,7 +39,7 @@ public OsuCursor()

protected override void SkinChanged(ISkinSource skin, bool allowFallback)
{
cursorExpand = skin.GetValue<SkinConfiguration, bool>(s => s.CursorExpand ?? true);
cursorExpand = skin.GetConfig<OsuSkinConfiguration, bool>(OsuSkinConfiguration.CursorExpand)?.Value ?? true;
}

[BackgroundDependencyLoader]
Expand Down
1 change: 1 addition & 0 deletions osu.Game.Tests/Resources/skin.ini
@@ -1,5 +1,6 @@
[General]
Name: test skin
TestLookup: TestValue

[Colours]
Combo1 : 142,199,255
Expand Down
15 changes: 15 additions & 0 deletions osu.Game.Tests/Skins/LegacySkinDecoderTest.cs
Expand Up @@ -41,5 +41,20 @@ public void TestDecodeSkinColours(bool hasColours)
Assert.AreEqual(expectedColors[i], comboColors[i]);
}
}

[Test]
public void TestDecodeGeneral()
{
var decoder = new LegacySkinDecoder();

using (var resStream = TestResources.OpenResource("skin.ini"))
using (var stream = new StreamReader(resStream))
{
var config = decoder.Decode(stream);

Assert.AreEqual("test skin", config.SkinInfo.Name);
Assert.AreEqual("TestValue", config.ConfigDictionary["TestLookup"]);
}
}
}
}

0 comments on commit 9d0151f

Please sign in to comment.