// Copyright (c) 2007-2018 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System; using osu.Framework.Graphics; using OpenTK; 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) { } } 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 Func createDefault; private readonly string componentName; private readonly bool restrictSize; /// /// /// /// 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) : base(allowFallback) { componentName = name; createDefault = defaultImplementation; this.restrictSize = restrictSize; RelativeSizeAxes = Axes.Both; } protected override void SkinChanged(ISkinSource skin, bool allowFallback) { Drawable = skin.GetDrawableComponent(componentName); if (Drawable != null) { if (restrictSize) { Drawable.RelativeSizeAxes = Axes.Both; Drawable.Size = Vector2.One; Drawable.Scale = Vector2.One; Drawable.FillMode = FillMode.Fit; } } else if (allowFallback) Drawable = createDefault(componentName); if (Drawable != null) { Drawable.Origin = Anchor.Centre; Drawable.Anchor = Anchor.Centre; InternalChild = Drawable; } else ClearInternal(); } } }