Add checks guarding against setting tracking on multiple trackingcongtainers and setting facade size

This commit is contained in:
David Zhao 2019-04-05 13:56:08 +09:00
parent 7047f305a1
commit b1d74e57e5
2 changed files with 27 additions and 23 deletions

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.Reflection.Metadata.Ecma335;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.MathUtils; using osu.Framework.MathUtils;
@ -20,7 +21,24 @@ namespace osu.Game.Graphics.Containers
/// <summary> /// <summary>
/// Whether or not the logo assigned to this FacadeContainer should be tracking the position of its facade. /// Whether or not the logo assigned to this FacadeContainer should be tracking the position of its facade.
/// </summary> /// </summary>
public bool Tracking { get; set; } public bool Tracking
{
get => tracking;
set
{
if (Logo != null)
{
if (value && !tracking && Logo.IsTracking)
throw new InvalidOperationException($"Cannot track an instance of {typeof(OsuLogo)} to multiple {typeof(LogoTrackingContainer)}s");
Logo.IsTracking = value;
}
tracking = value;
}
}
private bool tracking;
protected OsuLogo Logo; protected OsuLogo Logo;
@ -44,23 +62,15 @@ namespace osu.Game.Graphics.Containers
/// <param name="easing">The easing type of the initial transform.</param> /// <param name="easing">The easing type of the initial transform.</param>
public void SetLogo(OsuLogo logo, float facadeScale = 1.0f, double duration = 0, Easing easing = Easing.None) public void SetLogo(OsuLogo logo, float facadeScale = 1.0f, double duration = 0, Easing easing = Easing.None)
{ {
if (Logo != logo) if (Logo != logo && Logo != null)
{ {
if (logo?.HasTrackingContainer ?? throw new ArgumentNullException(nameof(logo))) // If we're replacing the logo to be tracked, the old one no longer has a tracking container
{ Logo.IsTracking = false;
// Prevent the same logo from being added to multiple LogoTrackingContainers.
throw new InvalidOperationException($"Cannot add an instance of {typeof(OsuLogo)} to multiple {typeof(LogoTrackingContainer)}s");
}
if (Logo != null)
{
// If we're replacing the logo to be tracked, the old one no longer has a tracking container
Logo.HasTrackingContainer = false;
}
} }
Logo = logo; Logo = logo ?? throw new ArgumentNullException(nameof(logo));
Logo.HasTrackingContainer = true; Logo.IsTracking = Tracking;
this.facadeScale = facadeScale; this.facadeScale = facadeScale;
this.duration = duration; this.duration = duration;
this.easing = easing; this.easing = easing;
@ -115,7 +125,7 @@ namespace osu.Game.Graphics.Containers
protected override void Dispose(bool isDisposing) protected override void Dispose(bool isDisposing)
{ {
if (Logo != null) if (Logo != null)
Logo.HasTrackingContainer = false; Tracking = false;
base.Dispose(isDisposing); base.Dispose(isDisposing);
} }
@ -124,12 +134,6 @@ namespace osu.Game.Graphics.Containers
private class ExposedFacade : Facade private class ExposedFacade : Facade
{ {
public override Vector2 Size
{
get => base.Size;
set => throw new InvalidOperationException($"Cannot set the Size of a {typeof(Facade)} outside of a {typeof(LogoTrackingContainer)}");
}
public void SetSize(Vector2 size) public void SetSize(Vector2 size)
{ {
base.SetSize(size); base.SetSize(size);

View File

@ -60,7 +60,7 @@ namespace osu.Game.Screens.Menu
/// <remarks>Does not account for the scale of this <see cref="OsuLogo"/></remarks> /// <remarks>Does not account for the scale of this <see cref="OsuLogo"/></remarks>
public float SizeForFlow => logo == null ? 0 : logo.DrawSize.X * logo.Scale.X * logoBounceContainer.Scale.X * logoHoverContainer.Scale.X; public float SizeForFlow => logo == null ? 0 : logo.DrawSize.X * logo.Scale.X * logoBounceContainer.Scale.X * logoHoverContainer.Scale.X;
public bool HasTrackingContainer { get; set; } public bool IsTracking { get; set; }
private readonly Sprite ripple; private readonly Sprite ripple;