osu/osu.Game/Skinning/SkinnableSprite.cs

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

146 lines
5.2 KiB
C#
Raw Normal View History

2019-06-24 05:39:20 +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.
2022-06-17 07:37:17 +00:00
#nullable disable
2022-03-25 08:36:02 +00:00
using System;
2022-04-05 07:47:15 +00:00
using System.Collections.Generic;
using System.Linq;
2019-06-24 05:39:20 +00:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2019-06-24 05:39:20 +00:00
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Textures;
using osu.Game.Configuration;
using osu.Game.Graphics.Sprites;
using osu.Game.Overlays.Settings;
using osuTK;
2019-06-24 05:39:20 +00:00
namespace osu.Game.Skinning
{
2019-06-24 06:25:01 +00:00
/// <summary>
/// A skinnable element which uses a single texture backing.
2019-06-24 06:25:01 +00:00
/// </summary>
public class SkinnableSprite : SkinnableDrawable, ISkinnableDrawable
2019-06-24 05:39:20 +00:00
{
2019-06-24 06:25:01 +00:00
protected override bool ApplySizeRestrictionsToDefault => true;
2019-06-24 05:39:20 +00:00
[Resolved]
private TextureStore textures { get; set; }
[SettingSource("Sprite name", "The filename of the sprite", SettingControlType = typeof(SpriteSelectorControl))]
public Bindable<string> SpriteName { get; } = new Bindable<string>(string.Empty);
[Resolved]
private ISkinSource source { get; set; }
public SkinnableSprite(string textureName, ConfineMode confineMode = ConfineMode.NoScaling)
: base(new SpriteComponent(textureName), confineMode)
2019-06-24 05:39:20 +00:00
{
SpriteName.Value = textureName;
}
public SkinnableSprite()
: base(new SpriteComponent(string.Empty), ConfineMode.NoScaling)
{
RelativeSizeAxes = Axes.None;
AutoSizeAxes = Axes.Both;
SpriteName.BindValueChanged(name =>
{
((SpriteComponent)Component).LookupName = name.NewValue ?? string.Empty;
if (IsLoaded)
SkinChanged(CurrentSkin);
});
2019-06-24 05:39:20 +00:00
}
2019-06-24 06:27:46 +00:00
protected override Drawable CreateDefault(ISkinComponent component)
{
var texture = textures.Get(component.LookupName);
if (texture == null)
return new SpriteNotFound(component.LookupName);
return new Sprite { Texture = texture };
}
public bool UsesFixedAnchor { get; set; }
internal class SpriteComponent : ISkinComponent
{
public string LookupName { get; set; }
public SpriteComponent(string textureName)
{
2019-11-12 09:45:42 +00:00
LookupName = textureName;
}
}
public class SpriteSelectorControl : SettingsDropdown<string>
{
protected override void LoadComplete()
{
base.LoadComplete();
// Round-about way of getting the user's skin to find available resources.
// In the future we'll probably want to allow access to resources from the fallbacks, or potentially other skins
// but that requires further thought.
2022-04-05 07:47:15 +00:00
var highestPrioritySkin = getHighestPriorityUserSkin(((SkinnableSprite)SettingSourceObject).source.AllSources) as Skin;
string[] availableFiles = highestPrioritySkin?.SkinInfo.PerformRead(s => s.Files
.Where(f => f.Filename.EndsWith(".png", StringComparison.Ordinal)
|| f.Filename.EndsWith(".jpg", StringComparison.Ordinal))
.Select(f => f.Filename).Distinct()).ToArray();
if (availableFiles?.Length > 0)
Items = availableFiles;
2022-04-05 07:47:15 +00:00
static ISkin getHighestPriorityUserSkin(IEnumerable<ISkin> skins)
{
foreach (var skin in skins)
{
if (skin is ISkinTransformer transformer && isUserSkin(transformer.Skin))
2022-04-05 07:47:15 +00:00
return transformer.Skin;
if (isUserSkin(skin))
return skin;
}
return null;
}
// Temporarily used to exclude undesirable ISkin implementations
static bool isUserSkin(ISkin skin)
2022-09-17 15:14:49 +00:00
=> skin.GetType() == typeof(TrianglesSkin)
2022-09-15 07:02:57 +00:00
|| skin.GetType() == typeof(ArgonSkin)
2022-04-05 07:47:15 +00:00
|| skin.GetType() == typeof(DefaultLegacySkin)
|| skin.GetType() == typeof(LegacySkin);
}
}
public class SpriteNotFound : CompositeDrawable
{
public SpriteNotFound(string lookup)
{
AutoSizeAxes = Axes.Both;
InternalChildren = new Drawable[]
{
new SpriteIcon
{
Size = new Vector2(50),
Icon = FontAwesome.Solid.QuestionCircle
},
new OsuSpriteText
{
Position = new Vector2(25, 50),
Text = $"missing: {lookup}",
Origin = Anchor.TopCentre,
}
};
}
}
2019-06-24 05:39:20 +00:00
}
}