1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 08:43:20 +08:00

Merge pull request #1899 from Aergwyn/shrink-drawablehitobject_t

Slim down DrawableHitObject<T>
This commit is contained in:
Dan Balasescu 2018-01-14 00:24:52 +09:00 committed by GitHub
commit a9d7eac8be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,79 +29,49 @@ namespace osu.Game.Rulesets.Objects.Drawables
/// </summary>
public virtual Color4 AccentColour { get; set; } = Color4.Gray;
// Todo: Rulesets should be overriding the resources instead, but we need to figure out where/when to apply overrides first
protected virtual string SampleNamespace => null;
protected List<SampleChannel> Samples = new List<SampleChannel>();
protected virtual IEnumerable<SampleInfo> GetSamples() => HitObject.Samples;
private List<DrawableHitObject> nestedHitObjects;
public IReadOnlyList<DrawableHitObject> NestedHitObjects => nestedHitObjects;
public event Action<DrawableHitObject, Judgement> OnJudgement;
public event Action<DrawableHitObject, Judgement> OnJudgementRemoved;
public IReadOnlyList<Judgement> Judgements => judgements;
private readonly List<Judgement> judgements = new List<Judgement>();
/// <summary>
/// Whether a visible judgement should be displayed when this representation is hit.
/// </summary>
public virtual bool DisplayJudgement => true;
public override bool RemoveCompletedTransforms => false;
public override bool RemoveWhenNotAlive => false;
protected override bool RequiresChildrenUpdate => true;
public virtual bool AllJudged => false;
protected DrawableHitObject(HitObject hitObject)
{
HitObject = hitObject;
}
/// <summary>
/// Processes this <see cref="DrawableHitObject"/>, checking if any judgements have occurred.
/// Whether this <see cref="DrawableHitObject"/> and all of its nested <see cref="DrawableHitObject"/>s have been judged.
/// </summary>
/// <param name="userTriggered">Whether the user triggered this process.</param>
/// <returns>Whether a judgement has occurred from this <see cref="DrawableHitObject"/> or any nested <see cref="DrawableHitObject"/>s.</returns>
protected internal virtual bool UpdateJudgement(bool userTriggered) => false;
private List<DrawableHitObject> nestedHitObjects;
public IReadOnlyList<DrawableHitObject> NestedHitObjects => nestedHitObjects;
protected virtual void AddNested(DrawableHitObject h)
{
if (nestedHitObjects == null)
nestedHitObjects = new List<DrawableHitObject>();
nestedHitObjects.Add(h);
}
/// <summary>
/// The screen-space point that causes this <see cref="DrawableHitObject"/> to be selected in the Editor.
/// </summary>
public virtual Vector2 SelectionPoint => ScreenSpaceDrawQuad.Centre;
/// <summary>
/// The screen-space quad that outlines this <see cref="DrawableHitObject"/> for selections in the Editor.
/// </summary>
public virtual Quad SelectionQuad => ScreenSpaceDrawQuad;
}
public abstract class DrawableHitObject<TObject> : DrawableHitObject
where TObject : HitObject
{
public event Action<DrawableHitObject, Judgement> OnJudgement;
public event Action<DrawableHitObject, Judgement> OnJudgementRemoved;
public new readonly TObject HitObject;
public override bool HandleInput => Interactive;
public bool Interactive = true;
public bool AllJudged => (!ProvidesJudgement || judgementFinalized) && (NestedHitObjects?.All(h => h.AllJudged) ?? true);
/// <summary>
/// Whether this <see cref="DrawableHitObject"/> can be judged.
/// </summary>
protected virtual bool ProvidesJudgement => true;
private readonly List<Judgement> judgements = new List<Judgement>();
public IReadOnlyList<Judgement> Judgements => judgements;
private bool judgementOccurred;
private bool judgementFinalized => judgements.LastOrDefault()?.Final == true;
protected List<SampleChannel> Samples = new List<SampleChannel>();
protected virtual IEnumerable<SampleInfo> GetSamples() => HitObject.Samples;
public bool Interactive = true;
public override bool HandleInput => Interactive;
// Todo: Rulesets should be overriding the resources instead, but we need to figure out where/when to apply overrides first
protected virtual string SampleNamespace => null;
public override bool RemoveWhenNotAlive => false;
public override bool RemoveCompletedTransforms => false;
protected override bool RequiresChildrenUpdate => true;
public readonly Bindable<ArmedState> State = new Bindable<ArmedState>();
protected DrawableHitObject(TObject hitObject)
: base(hitObject)
protected DrawableHitObject(HitObject hitObject)
{
HitObject = hitObject;
}
@ -153,18 +123,47 @@ namespace osu.Game.Rulesets.Objects.Drawables
State.TriggerChange();
}
protected void PlaySamples()
{
Samples.ForEach(s => s?.Play());
}
private bool judgementOccurred;
private bool judgementFinalized => judgements.LastOrDefault()?.Final == true;
protected abstract void UpdateState(ArmedState state);
/// <summary>
/// Whether this <see cref="DrawableHitObject"/> and all of its nested <see cref="DrawableHitObject"/>s have been judged.
/// Bind to apply a custom state which can override the default implementation.
/// </summary>
public sealed override bool AllJudged => (!ProvidesJudgement || judgementFinalized) && (NestedHitObjects?.All(h => h.AllJudged) ?? true);
public event Action<DrawableHitObject, ArmedState> ApplyCustomUpdateState;
protected void PlaySamples() => Samples.ForEach(s => s?.Play());
protected override void Update()
{
base.Update();
var endTime = (HitObject as IHasEndTime)?.EndTime ?? HitObject.StartTime;
while (judgements.Count > 0)
{
var lastJudgement = judgements[judgements.Count - 1];
if (lastJudgement.TimeOffset + endTime <= Time.Current)
break;
judgements.RemoveAt(judgements.Count - 1);
State.Value = ArmedState.Idle;
OnJudgementRemoved?.Invoke(this, lastJudgement);
}
}
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
UpdateJudgement(false);
}
protected virtual void AddNested(DrawableHitObject h)
{
if (nestedHitObjects == null)
nestedHitObjects = new List<DrawableHitObject>();
nestedHitObjects.Add(h);
}
/// <summary>
/// Notifies that a new judgement has occurred for this <see cref="DrawableHitObject"/>.
@ -200,7 +199,7 @@ namespace osu.Game.Rulesets.Objects.Drawables
/// </summary>
/// <param name="userTriggered">Whether the user triggered this process.</param>
/// <returns>Whether a judgement has occurred from this <see cref="DrawableHitObject"/> or any nested <see cref="DrawableHitObject"/>s.</returns>
protected internal sealed override bool UpdateJudgement(bool userTriggered)
protected bool UpdateJudgement(bool userTriggered)
{
judgementOccurred = false;
@ -229,51 +228,30 @@ namespace osu.Game.Rulesets.Objects.Drawables
/// <param name="userTriggered">Whether the user triggered this check.</param>
/// <param name="timeOffset">The offset from the <see cref="HitObject"/> end time at which this check occurred. A <paramref name="timeOffset"/> &gt; 0
/// implies that this check occurred after the end time of <see cref="HitObject"/>. </param>
protected virtual void CheckForJudgements(bool userTriggered, double timeOffset) { }
protected override void Update()
protected virtual void CheckForJudgements(bool userTriggered, double timeOffset)
{
base.Update();
var endTime = (HitObject as IHasEndTime)?.EndTime ?? HitObject.StartTime;
while (judgements.Count > 0)
{
var lastJudgement = judgements[judgements.Count - 1];
if (lastJudgement.TimeOffset + endTime <= Time.Current)
break;
judgements.RemoveAt(judgements.Count - 1);
State.Value = ArmedState.Idle;
OnJudgementRemoved?.Invoke(this, lastJudgement);
}
}
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
UpdateJudgement(false);
}
protected override void AddNested(DrawableHitObject h)
{
base.AddNested(h);
if (!(h is DrawableHitObject<TObject> hWithJudgement))
return;
hWithJudgement.OnJudgement += (d, j) => OnJudgement?.Invoke(d, j);
hWithJudgement.OnJudgementRemoved += (d, j) => OnJudgementRemoved?.Invoke(d, j);
hWithJudgement.ApplyCustomUpdateState += (d, s) => ApplyCustomUpdateState?.Invoke(d, s);
}
/// <summary>
/// Bind to apply a custom state which can override the default implementation.
/// The screen-space point that causes this <see cref="DrawableHitObject"/> to be selected in the Editor.
/// </summary>
public event Action<DrawableHitObject, ArmedState> ApplyCustomUpdateState;
public virtual Vector2 SelectionPoint => ScreenSpaceDrawQuad.Centre;
protected abstract void UpdateState(ArmedState state);
/// <summary>
/// The screen-space quad that outlines this <see cref="DrawableHitObject"/> for selections in the Editor.
/// </summary>
public virtual Quad SelectionQuad => ScreenSpaceDrawQuad;
}
public abstract class DrawableHitObject<TObject> : DrawableHitObject
where TObject : HitObject
{
public new readonly TObject HitObject;
protected DrawableHitObject(TObject hitObject)
: base(hitObject)
{
HitObject = hitObject;
}
}
}