1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-13 15:03:13 +08:00

feat: integrate attachment flow in SkinComponentsContainer

This commit is contained in:
tsrk 2023-06-06 00:13:29 +02:00
parent 4c397085c5
commit 6fc6729677
No known key found for this signature in database
GPG Key ID: EBD46BB3049B56D6
3 changed files with 31 additions and 6 deletions

View File

@ -30,8 +30,6 @@ using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.Scoring;
using osu.Game.Scoring;
using osu.Game.Screens.Play;
using osu.Game.Screens.Play.HUD;
using osu.Game.Screens.Play.HUD.ClicksPerSecond;
using osuTK;
namespace osu.Game.Rulesets.UI
@ -329,11 +327,11 @@ namespace osu.Game.Rulesets.UI
/// <returns>The representing <see cref="DrawableHitObject{TObject}"/>.</returns>
public abstract DrawableHitObject<TObject> CreateDrawableRepresentation(TObject h);
public void Attach(KeyCounterDisplay keyCounter) =>
(KeyBindingInputManager as ICanAttachHUDPieces)?.Attach(keyCounter);
public void Attach(IAttachableSkinComponent skinComponent) =>
(KeyBindingInputManager as ICanAttachHUDPieces)?.Attach(skinComponent);
public void Attach(ClicksPerSecondCalculator calculator) =>
(KeyBindingInputManager as ICanAttachHUDPieces)?.Attach(calculator);
public void Detach(IAttachableSkinComponent skinComponent) =>
(KeyBindingInputManager as ICanAttachHUDPieces)?.Detach(skinComponent);
/// <summary>
/// Creates a key conversion input manager. An exception will be thrown if a valid <see cref="RulesetInputManager{T}"/> is not returned.

View File

@ -321,6 +321,7 @@ namespace osu.Game.Screens.Play
{
attachTarget.Attach(KeyCounter);
attachTarget.Attach(clicksPerSecondCalculator);
mainComponents.SetAttachTarget(attachTarget);
}
replayLoaded.BindTo(drawableRuleset.HasReplayLoaded);

View File

@ -6,8 +6,10 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading;
using osu.Framework.Bindables;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Rulesets.UI;
namespace osu.Game.Skinning
{
@ -39,6 +41,8 @@ namespace osu.Game.Skinning
private CancellationTokenSource? cancellationSource;
private ICanAttachHUDPieces? attachTarget;
public SkinComponentsContainer(SkinComponentsContainerLookup lookup)
{
Lookup = lookup;
@ -62,6 +66,10 @@ namespace osu.Game.Skinning
public void Reload(Container? componentsContainer)
{
components
.OfType<IAttachableSkinComponent>()
.ForEach(c => attachTarget?.Detach(c));
ClearInternal();
components.Clear();
ComponentsLoaded = false;
@ -77,6 +85,7 @@ namespace osu.Game.Skinning
LoadComponentAsync(content, wrapper =>
{
AddInternal(wrapper);
wrapper.Children.OfType<IAttachableSkinComponent>().ForEach(c => attachTarget?.Attach(c));
components.AddRange(wrapper.Children.OfType<ISerialisableDrawable>());
ComponentsLoaded = true;
}, (cancellationSource = new CancellationTokenSource()).Token);
@ -93,6 +102,9 @@ namespace osu.Game.Skinning
if (!(component is Drawable drawable))
throw new ArgumentException($"Provided argument must be of type {nameof(Drawable)}.", nameof(component));
if (component is IAttachableSkinComponent attachableSkinComponent)
attachTarget?.Attach(attachableSkinComponent);
content.Add(drawable);
components.Add(component);
}
@ -108,10 +120,24 @@ namespace osu.Game.Skinning
if (!(component is Drawable drawable))
throw new ArgumentException($"Provided argument must be of type {nameof(Drawable)}.", nameof(component));
if (component is IAttachableSkinComponent attachableSkinComponent)
attachTarget?.Detach(attachableSkinComponent);
content.Remove(drawable, disposeImmediately);
components.Remove(component);
}
public void SetAttachTarget(ICanAttachHUDPieces target)
{
attachTarget = target;
foreach (var child in InternalChildren)
{
if (child is IAttachableSkinComponent attachable)
attachTarget.Attach(attachable);
}
}
protected override void SkinChanged(ISkinSource skin)
{
base.SkinChanged(skin);