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-03-25 08:36:02 +00:00
|
|
|
using System;
|
2022-04-05 07:47:15 +00:00
|
|
|
using System.Collections.Generic;
|
2022-03-25 08:31:03 +00:00
|
|
|
using System.Linq;
|
2019-06-24 05:39:20 +00:00
|
|
|
using osu.Framework.Allocation;
|
2022-03-25 08:31:03 +00:00
|
|
|
using osu.Framework.Bindables;
|
2019-07-19 09:38:24 +00:00
|
|
|
using osu.Framework.Graphics;
|
2022-04-04 11:35:48 +00:00
|
|
|
using osu.Framework.Graphics.Containers;
|
2019-06-24 05:39:20 +00:00
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
using osu.Framework.Graphics.Textures;
|
2022-03-25 08:31:03 +00:00
|
|
|
using osu.Game.Configuration;
|
2022-04-04 11:35:48 +00:00
|
|
|
using osu.Game.Graphics.Sprites;
|
2023-02-03 06:34:57 +00:00
|
|
|
using osu.Game.Localisation.SkinComponents;
|
2022-03-25 08:31:03 +00:00
|
|
|
using osu.Game.Overlays.Settings;
|
2022-04-01 05:46:14 +00:00
|
|
|
using osuTK;
|
2019-06-24 05:39:20 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Skinning
|
|
|
|
{
|
2019-06-24 06:25:01 +00:00
|
|
|
/// <summary>
|
2022-04-04 11:27:46 +00:00
|
|
|
/// A skinnable element which uses a single texture backing.
|
2019-06-24 06:25:01 +00:00
|
|
|
/// </summary>
|
2023-02-15 07:01:26 +00:00
|
|
|
public partial class SkinnableSprite : SkinnableDrawable, ISerialisableDrawable
|
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]
|
2022-11-09 04:44:59 +00:00
|
|
|
private TextureStore textures { get; set; } = null!;
|
2019-06-24 05:39:20 +00:00
|
|
|
|
2023-02-03 07:02:16 +00:00
|
|
|
[SettingSource(typeof(SkinnableComponentStrings), nameof(SkinnableComponentStrings.SpriteName), nameof(SkinnableComponentStrings.SpriteNameDescription), SettingControlType = typeof(SpriteSelectorControl))]
|
2022-03-25 08:31:03 +00:00
|
|
|
public Bindable<string> SpriteName { get; } = new Bindable<string>(string.Empty);
|
|
|
|
|
|
|
|
[Resolved]
|
2022-11-09 04:44:59 +00:00
|
|
|
private ISkinSource source { get; set; } = null!;
|
2022-03-25 08:31:03 +00:00
|
|
|
|
2021-05-27 05:50:42 +00:00
|
|
|
public SkinnableSprite(string textureName, ConfineMode confineMode = ConfineMode.NoScaling)
|
2022-11-09 07:04:56 +00:00
|
|
|
: base(new SpriteComponentLookup(textureName), confineMode)
|
2019-06-24 05:39:20 +00:00
|
|
|
{
|
2022-03-25 08:31:03 +00:00
|
|
|
SpriteName.Value = textureName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public SkinnableSprite()
|
2022-11-09 07:04:56 +00:00
|
|
|
: base(new SpriteComponentLookup(string.Empty), ConfineMode.NoScaling)
|
2022-03-25 08:31:03 +00:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.None;
|
|
|
|
AutoSizeAxes = Axes.Both;
|
|
|
|
|
|
|
|
SpriteName.BindValueChanged(name =>
|
|
|
|
{
|
2022-11-09 07:04:56 +00:00
|
|
|
((SpriteComponentLookup)ComponentLookup).LookupName = name.NewValue ?? string.Empty;
|
2022-03-25 08:31:03 +00:00
|
|
|
if (IsLoaded)
|
|
|
|
SkinChanged(CurrentSkin);
|
|
|
|
});
|
2019-06-24 05:39:20 +00:00
|
|
|
}
|
2019-06-24 06:27:46 +00:00
|
|
|
|
2022-11-09 07:04:56 +00:00
|
|
|
protected override Drawable CreateDefault(ISkinComponentLookup lookup)
|
2020-11-18 07:18:27 +00:00
|
|
|
{
|
2022-11-09 07:39:33 +00:00
|
|
|
var texture = textures.Get(((SpriteComponentLookup)lookup).LookupName);
|
2020-11-18 07:18:27 +00:00
|
|
|
|
|
|
|
if (texture == null)
|
2022-11-09 07:39:33 +00:00
|
|
|
return new SpriteNotFound(((SpriteComponentLookup)lookup).LookupName);
|
2020-11-18 07:18:27 +00:00
|
|
|
|
|
|
|
return new Sprite { Texture = texture };
|
|
|
|
}
|
2019-09-03 05:21:54 +00:00
|
|
|
|
2022-03-25 08:31:03 +00:00
|
|
|
public bool UsesFixedAnchor { get; set; }
|
|
|
|
|
2022-11-09 07:04:56 +00:00
|
|
|
internal class SpriteComponentLookup : ISkinComponentLookup
|
2019-09-03 05:21:54 +00:00
|
|
|
{
|
2022-03-25 08:31:03 +00:00
|
|
|
public string LookupName { get; set; }
|
|
|
|
|
2022-11-09 07:04:56 +00:00
|
|
|
public SpriteComponentLookup(string textureName)
|
2019-09-03 05:21:54 +00:00
|
|
|
{
|
2019-11-12 09:45:42 +00:00
|
|
|
LookupName = textureName;
|
2019-09-03 05:21:54 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-23 06:11:35 +00:00
|
|
|
|
2022-03-25 08:31:03 +00:00
|
|
|
public partial class SpriteSelectorControl : SettingsDropdown<string>
|
|
|
|
{
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2022-04-01 06:14:53 +00:00
|
|
|
// 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;
|
2022-04-01 06:14:53 +00:00
|
|
|
|
2022-11-09 04:44:59 +00:00
|
|
|
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();
|
2022-04-01 06:14:53 +00:00
|
|
|
|
|
|
|
if (availableFiles?.Length > 0)
|
|
|
|
Items = availableFiles;
|
2022-04-05 07:47:15 +00:00
|
|
|
|
2022-11-09 04:44:59 +00:00
|
|
|
static ISkin? getHighestPriorityUserSkin(IEnumerable<ISkin> skins)
|
2022-04-05 07:47:15 +00:00
|
|
|
{
|
|
|
|
foreach (var skin in skins)
|
|
|
|
{
|
2022-09-22 09:43:41 +00:00
|
|
|
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-12-12 06:52:29 +00:00
|
|
|
|| skin.GetType() == typeof(ArgonProSkin)
|
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);
|
2022-03-25 08:31:03 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-04 11:35:48 +00:00
|
|
|
|
|
|
|
public partial 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
|
|
|
}
|
|
|
|
}
|