// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using osu.Framework.Graphics; using osuTK; namespace osu.Game.Skinning { public class SkinnableDrawable : SkinnableDrawable { public SkinnableDrawable(string name, Func defaultImplementation, Func allowFallback = null, bool restrictSize = true) : base(name, defaultImplementation, allowFallback, restrictSize) { } } /// /// A drawable which can be skinned via an . /// /// The type of drawable. public class SkinnableDrawable : SkinReloadableDrawable where T : Drawable { /// /// The displayed component. May or may not be a type- member. /// protected Drawable Drawable { get; private set; } private readonly string componentName; private readonly bool restrictSize; /// /// Create a new skinnable drawable. /// /// The namespace-complete resource name for this skinnable element. /// A function to create the default skin implementation of this element. /// A conditional to decide whether to allow fallback to the default implementation if a skinned element is not present. /// Whether a user-skin drawable should be limited to the size of our parent. public SkinnableDrawable(string name, Func defaultImplementation, Func allowFallback = null, bool restrictSize = true) : this(name, allowFallback, restrictSize) { createDefault = defaultImplementation; } protected SkinnableDrawable(string name, Func allowFallback = null, bool restrictSize = true) : base(allowFallback) { componentName = name; this.restrictSize = restrictSize; RelativeSizeAxes = Axes.Both; } private readonly Func createDefault; protected virtual T CreateDefault(string name) => createDefault(name); /// /// Whether to apply size restrictions (specified via ) to the default implementation. /// protected virtual bool ApplySizeRestrictionsToDefault => false; protected override void SkinChanged(ISkinSource skin, bool allowFallback) { Drawable = skin.GetDrawableComponent(componentName); bool isDefault = false; if (Drawable == null && allowFallback) { Drawable = CreateDefault(componentName); isDefault = true; } if (Drawable != null) { if (restrictSize && (!isDefault || ApplySizeRestrictionsToDefault)) { Drawable.RelativeSizeAxes = Axes.Both; Drawable.Size = Vector2.One; Drawable.Scale = Vector2.One; Drawable.FillMode = FillMode.Fit; } Drawable.Origin = Anchor.Centre; Drawable.Anchor = Anchor.Centre; InternalChild = Drawable; } else ClearInternal(); } } }