2019-01-24 16:43:03 +08:00
|
|
|
|
// 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 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2019-12-18 01:56:29 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2020-10-29 14:20:10 +08:00
|
|
|
|
using osu.Framework.Graphics.Performance;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Game.Rulesets.Objects.Drawables;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.UI
|
|
|
|
|
{
|
2018-12-13 13:55:28 +08:00
|
|
|
|
public class HitObjectContainer : LifetimeManagementContainer
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2018-06-06 13:18:38 +08:00
|
|
|
|
public IEnumerable<DrawableHitObject> Objects => InternalChildren.Cast<DrawableHitObject>().OrderBy(h => h.HitObject.StartTime);
|
|
|
|
|
public IEnumerable<DrawableHitObject> AliveObjects => AliveInternalChildren.Cast<DrawableHitObject>().OrderBy(h => h.HitObject.StartTime);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-12-18 01:56:29 +08:00
|
|
|
|
private readonly Dictionary<DrawableHitObject, (IBindable<double> bindable, double timeAtAdd)> startTimeMap = new Dictionary<DrawableHitObject, (IBindable<double>, double)>();
|
|
|
|
|
|
2018-09-21 13:35:50 +08:00
|
|
|
|
public HitObjectContainer()
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-18 01:56:29 +08:00
|
|
|
|
public virtual void Add(DrawableHitObject hitObject)
|
|
|
|
|
{
|
|
|
|
|
// Added first for the comparer to remain ordered during AddInternal
|
|
|
|
|
startTimeMap[hitObject] = (hitObject.HitObject.StartTimeBindable.GetBoundCopy(), hitObject.HitObject.StartTime);
|
|
|
|
|
startTimeMap[hitObject].bindable.BindValueChanged(_ => onStartTimeChanged(hitObject));
|
|
|
|
|
|
|
|
|
|
AddInternal(hitObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual bool Remove(DrawableHitObject hitObject)
|
|
|
|
|
{
|
2019-12-18 11:03:15 +08:00
|
|
|
|
if (!RemoveInternal(hitObject))
|
|
|
|
|
return false;
|
2019-12-18 01:56:29 +08:00
|
|
|
|
|
|
|
|
|
// Removed last for the comparer to remain ordered during RemoveInternal
|
|
|
|
|
startTimeMap[hitObject].bindable.UnbindAll();
|
|
|
|
|
startTimeMap.Remove(hitObject);
|
|
|
|
|
|
2019-12-18 11:03:15 +08:00
|
|
|
|
return true;
|
2019-12-18 01:56:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-21 17:27:15 +08:00
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
unbindStartTimeMap();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-23 10:16:59 +08:00
|
|
|
|
public virtual void Clear(bool disposeChildren = true)
|
|
|
|
|
{
|
|
|
|
|
ClearInternal(disposeChildren);
|
2020-09-21 17:27:15 +08:00
|
|
|
|
unbindStartTimeMap();
|
|
|
|
|
}
|
2020-04-23 10:16:59 +08:00
|
|
|
|
|
2020-09-21 17:27:15 +08:00
|
|
|
|
private void unbindStartTimeMap()
|
|
|
|
|
{
|
2020-04-23 10:16:59 +08:00
|
|
|
|
foreach (var kvp in startTimeMap)
|
|
|
|
|
kvp.Value.bindable.UnbindAll();
|
|
|
|
|
startTimeMap.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-18 17:51:12 +08:00
|
|
|
|
public int IndexOf(DrawableHitObject hitObject) => IndexOfInternal(hitObject);
|
|
|
|
|
|
2019-12-18 01:56:29 +08:00
|
|
|
|
private void onStartTimeChanged(DrawableHitObject hitObject)
|
|
|
|
|
{
|
|
|
|
|
if (!RemoveInternal(hitObject))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Update the stored time, preserving the existing bindable
|
|
|
|
|
startTimeMap[hitObject] = (startTimeMap[hitObject].bindable, hitObject.HitObject.StartTime);
|
|
|
|
|
AddInternal(hitObject);
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
protected override int Compare(Drawable x, Drawable y)
|
|
|
|
|
{
|
|
|
|
|
if (!(x is DrawableHitObject xObj) || !(y is DrawableHitObject yObj))
|
|
|
|
|
return base.Compare(x, y);
|
|
|
|
|
|
|
|
|
|
// Put earlier hitobjects towards the end of the list, so they handle input first
|
2019-12-18 01:56:29 +08:00
|
|
|
|
int i = startTimeMap[yObj].timeAtAdd.CompareTo(startTimeMap[xObj].timeAtAdd);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
return i == 0 ? CompareReverseChildID(x, y) : i;
|
|
|
|
|
}
|
2019-01-29 14:25:27 +08:00
|
|
|
|
|
|
|
|
|
protected override void OnChildLifetimeBoundaryCrossed(LifetimeBoundaryCrossedEvent e)
|
|
|
|
|
{
|
2019-07-16 17:19:13 +08:00
|
|
|
|
if (!(e.Child is DrawableHitObject hitObject))
|
|
|
|
|
return;
|
|
|
|
|
|
2019-07-17 21:38:17 +08:00
|
|
|
|
if ((e.Kind == LifetimeBoundaryKind.End && e.Direction == LifetimeBoundaryCrossingDirection.Forward)
|
|
|
|
|
|| (e.Kind == LifetimeBoundaryKind.Start && e.Direction == LifetimeBoundaryCrossingDirection.Backward))
|
2019-07-16 17:19:13 +08:00
|
|
|
|
{
|
|
|
|
|
hitObject.OnKilled();
|
|
|
|
|
}
|
2019-01-29 14:25:27 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|