2019-06-24 13:39:20 +08: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 16:36:02 +08:00
|
|
|
using System;
|
2022-03-25 16:31:03 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2019-06-24 13:39:20 +08:00
|
|
|
using osu.Framework.Allocation;
|
2022-03-25 16:31:03 +08:00
|
|
|
using osu.Framework.Bindables;
|
2019-07-19 17:38:24 +08:00
|
|
|
using osu.Framework.Graphics;
|
2019-06-24 13:39:20 +08:00
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
using osu.Framework.Graphics.Textures;
|
2022-03-25 16:31:03 +08:00
|
|
|
using osu.Game.Configuration;
|
|
|
|
using osu.Game.Overlays.Settings;
|
2019-06-24 13:39:20 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Skinning
|
|
|
|
{
|
2019-06-24 14:25:01 +08:00
|
|
|
/// <summary>
|
|
|
|
/// A skinnable element which uses a stable sprite and can therefore share implementation logic.
|
|
|
|
/// </summary>
|
2022-03-23 14:11:35 +08:00
|
|
|
public class SkinnableSprite : SkinnableDrawable, ISkinnableDrawable
|
2019-06-24 13:39:20 +08:00
|
|
|
{
|
2019-06-24 14:25:01 +08:00
|
|
|
protected override bool ApplySizeRestrictionsToDefault => true;
|
2019-06-24 13:39:20 +08:00
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private TextureStore textures { get; set; }
|
|
|
|
|
2022-03-25 16:31:03 +08:00
|
|
|
[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; }
|
|
|
|
|
2022-03-25 16:36:02 +08:00
|
|
|
public IEnumerable<string> AvailableFiles => (source.AllSources.First() as Skin)?.SkinInfo.PerformRead(s => s.Files
|
|
|
|
.Where(f =>
|
|
|
|
f.Filename.EndsWith(".png", StringComparison.Ordinal)
|
|
|
|
|| f.Filename.EndsWith(".jpg", StringComparison.Ordinal)
|
|
|
|
)
|
|
|
|
.Select(f => f.Filename).Distinct());
|
2022-03-25 16:31:03 +08:00
|
|
|
|
2021-05-27 13:50:42 +08:00
|
|
|
public SkinnableSprite(string textureName, ConfineMode confineMode = ConfineMode.NoScaling)
|
|
|
|
: base(new SpriteComponent(textureName), confineMode)
|
2019-06-24 13:39:20 +08:00
|
|
|
{
|
2022-03-25 16:31:03 +08: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 13:39:20 +08:00
|
|
|
}
|
2019-06-24 14:27:46 +08:00
|
|
|
|
2020-11-18 15:18:27 +08:00
|
|
|
protected override Drawable CreateDefault(ISkinComponent component)
|
|
|
|
{
|
|
|
|
var texture = textures.Get(component.LookupName);
|
|
|
|
|
|
|
|
if (texture == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return new Sprite { Texture = texture };
|
|
|
|
}
|
2019-09-03 13:21:54 +08:00
|
|
|
|
2022-03-25 16:31:03 +08:00
|
|
|
public bool UsesFixedAnchor { get; set; }
|
|
|
|
|
2019-09-03 13:21:54 +08:00
|
|
|
private class SpriteComponent : ISkinComponent
|
|
|
|
{
|
2022-03-25 16:31:03 +08:00
|
|
|
public string LookupName { get; set; }
|
|
|
|
|
2019-09-03 13:21:54 +08:00
|
|
|
public SpriteComponent(string textureName)
|
|
|
|
{
|
2019-11-12 17:45:42 +08:00
|
|
|
LookupName = textureName;
|
2019-09-03 13:21:54 +08:00
|
|
|
}
|
|
|
|
}
|
2022-03-23 14:11:35 +08:00
|
|
|
|
2022-03-25 16:31:03 +08:00
|
|
|
public class SpriteSelectorControl : SettingsDropdown<string>
|
|
|
|
{
|
|
|
|
public SkinnableSprite Source { get; set; }
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
Items = Source.AvailableFiles;
|
|
|
|
}
|
|
|
|
}
|
2019-06-24 13:39:20 +08:00
|
|
|
}
|
|
|
|
}
|