osu/osu.Game/Skinning/SkinnableDrawable.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

139 lines
4.5 KiB
C#
Raw Normal View History

// 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.
2018-04-13 09:19:50 +00:00
2018-02-22 08:33:47 +00:00
using System;
using osu.Framework.Caching;
2018-02-22 08:33:47 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Animations;
2018-11-20 07:51:59 +00:00
using osuTK;
2018-04-13 09:19:50 +00:00
2018-02-22 08:33:47 +00:00
namespace osu.Game.Skinning
{
2019-06-24 06:25:01 +00:00
/// <summary>
/// A drawable which can be skinned via an <see cref="ISkinSource"/>.
/// </summary>
public partial class SkinnableDrawable : SkinReloadableDrawable
2018-02-22 08:33:47 +00:00
{
2018-09-27 08:33:27 +00:00
/// <summary>
/// The displayed component.
2018-09-27 08:33:27 +00:00
/// </summary>
public Drawable Drawable { get; private set; } = null!;
2018-09-27 07:27:11 +00:00
/// <summary>
/// Whether the drawable component should be centered in available space.
/// Defaults to true.
/// </summary>
2021-05-13 12:59:38 +00:00
public bool CentreComponent = true;
public new Axes AutoSizeAxes
{
get => base.AutoSizeAxes;
set => base.AutoSizeAxes = value;
}
protected readonly ISkinComponentLookup ComponentLookup;
2018-04-13 09:19:50 +00:00
private readonly ConfineMode confineMode;
2018-04-13 09:19:50 +00:00
/// <summary>
2019-06-24 06:25:01 +00:00
/// Create a new skinnable drawable.
/// </summary>
/// <param name="lookup">The namespace-complete resource name for this skinnable element.</param>
2018-03-12 02:02:02 +00:00
/// <param name="defaultImplementation">A function to create the default skin implementation of this element.</param>
/// <param name="confineMode">How (if at all) the <see cref="Drawable"/> should be resize to fit within our own bounds.</param>
public SkinnableDrawable(ISkinComponentLookup lookup, Func<ISkinComponentLookup, Drawable>? defaultImplementation = null, ConfineMode confineMode = ConfineMode.NoScaling)
: this(lookup, confineMode)
2019-06-24 05:39:20 +00:00
{
createDefault = defaultImplementation;
}
protected SkinnableDrawable(ISkinComponentLookup lookup, ConfineMode confineMode = ConfineMode.NoScaling)
2018-02-22 08:33:47 +00:00
{
ComponentLookup = lookup;
this.confineMode = confineMode;
2018-04-13 09:19:50 +00:00
RelativeSizeAxes = Axes.Both;
2018-02-22 08:33:47 +00:00
}
2018-04-13 09:19:50 +00:00
/// <summary>
/// Seeks to the 0-th frame if the content of this <see cref="SkinnableDrawable"/> is an <see cref="IFramedAnimation"/>.
/// </summary>
public void ResetAnimation() => (Drawable as IFramedAnimation)?.GotoFrame(0);
private readonly Func<ISkinComponentLookup, Drawable>? createDefault;
2019-06-24 06:27:46 +00:00
private readonly Cached scaling = new Cached();
private bool isDefault;
protected virtual Drawable CreateDefault(ISkinComponentLookup lookup) => createDefault?.Invoke(lookup) ?? Empty();
2019-06-24 06:27:46 +00:00
2019-06-24 06:25:01 +00:00
/// <summary>
/// Whether to apply size restrictions (specified via <see cref="confineMode"/>) to the default implementation.
2019-06-24 06:25:01 +00:00
/// </summary>
protected virtual bool ApplySizeRestrictionsToDefault => false;
2019-06-24 05:39:20 +00:00
protected override void SkinChanged(ISkinSource skin)
2018-02-22 08:33:47 +00:00
{
var retrieved = skin.GetDrawableComponent(ComponentLookup);
2019-06-24 05:39:20 +00:00
if (retrieved == null)
2019-06-24 05:39:20 +00:00
{
Drawable = CreateDefault(ComponentLookup);
2019-06-24 05:39:20 +00:00
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)
{
2020-03-31 14:35:23 +00:00
case ConfineMode.ScaleToFit:
Drawable.RelativeSizeAxes = Axes.Both;
Drawable.Size = Vector2.One;
Drawable.Scale = Vector2.One;
Drawable.FillMode = FillMode.Fit;
break;
}
}
finally
{
scaling.Validate();
}
}
2018-02-22 08:33:47 +00:00
}
}
public enum ConfineMode
{
/// <summary>
/// Don't apply any scaling. This allows the user element to be of any size, exceeding specified bounds.
/// </summary>
NoScaling,
ScaleToFit,
}
2018-02-22 08:33:47 +00:00
}