// 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.Caching; using osu.Framework.Graphics; using osu.Framework.Graphics.Animations; using osuTK; namespace osu.Game.Skinning { /// /// A drawable which can be skinned via an . /// public partial class SkinnableDrawable : SkinReloadableDrawable { /// /// The displayed component. /// public Drawable Drawable { get; private set; } = null!; /// /// Whether the drawable component should be centered in available space. /// Defaults to true. /// public bool CentreComponent = true; public new Axes AutoSizeAxes { get => base.AutoSizeAxes; set => base.AutoSizeAxes = value; } protected readonly ISkinComponentLookup ComponentLookup; private readonly ConfineMode confineMode; /// /// 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. /// How (if at all) the should be resize to fit within our own bounds. public SkinnableDrawable(ISkinComponentLookup lookup, Func? defaultImplementation = null, ConfineMode confineMode = ConfineMode.NoScaling) : this(lookup, confineMode) { createDefault = defaultImplementation; } protected SkinnableDrawable(ISkinComponentLookup lookup, ConfineMode confineMode = ConfineMode.NoScaling) { ComponentLookup = lookup; this.confineMode = confineMode; RelativeSizeAxes = Axes.Both; } /// /// Seeks to the 0-th frame if the content of this is an . /// public void ResetAnimation() => (Drawable as IFramedAnimation)?.GotoFrame(0); private readonly Func? createDefault; private readonly Cached scaling = new Cached(); private bool isDefault; protected virtual Drawable CreateDefault(ISkinComponentLookup lookup) => createDefault?.Invoke(lookup) ?? Empty(); /// /// Whether to apply size restrictions (specified via ) to the default implementation. /// protected virtual bool ApplySizeRestrictionsToDefault => false; protected override void SkinChanged(ISkinSource skin) { var retrieved = skin.GetDrawableComponent(ComponentLookup); if (retrieved == null) { Drawable = CreateDefault(ComponentLookup); isDefault = true; } else { Drawable = retrieved; isDefault = false; } scaling.Invalidate(); if (CentreComponent) { Drawable.Origin = Anchor.Centre; Drawable.Anchor = Anchor.Centre; } InternalChild = Drawable; } protected override void Update() { base.Update(); if (!scaling.IsValid) { try { if (isDefault && !ApplySizeRestrictionsToDefault) return; switch (confineMode) { case ConfineMode.ScaleToFit: Drawable.RelativeSizeAxes = Axes.Both; Drawable.Size = Vector2.One; Drawable.Scale = Vector2.One; Drawable.FillMode = FillMode.Fit; break; } } finally { scaling.Validate(); } } } } public enum ConfineMode { /// /// Don't apply any scaling. This allows the user element to be of any size, exceeding specified bounds. /// NoScaling, ScaleToFit, } }