2019-01-24 08:43:03 +00: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 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2019-12-17 17:56:29 +00:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2020-10-29 06:20:10 +00:00
|
|
|
|
using osu.Framework.Graphics.Performance;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Game.Rulesets.Objects.Drawables;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.UI
|
|
|
|
|
{
|
2018-12-13 05:55:28 +00:00
|
|
|
|
public class HitObjectContainer : LifetimeManagementContainer
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2018-06-06 05:18:38 +00: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 09:19:50 +00:00
|
|
|
|
|
2020-11-06 13:15:00 +00:00
|
|
|
|
private readonly Dictionary<DrawableHitObject, IBindable> startTimeMap = new Dictionary<DrawableHitObject, IBindable>();
|
2019-12-17 17:56:29 +00:00
|
|
|
|
|
2018-09-21 05:35:50 +00:00
|
|
|
|
public HitObjectContainer()
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-17 17:56:29 +00:00
|
|
|
|
public virtual void Add(DrawableHitObject hitObject)
|
|
|
|
|
{
|
2020-11-06 13:15:00 +00:00
|
|
|
|
bindStartTime(hitObject);
|
2019-12-17 17:56:29 +00:00
|
|
|
|
AddInternal(hitObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual bool Remove(DrawableHitObject hitObject)
|
|
|
|
|
{
|
2019-12-18 03:03:15 +00:00
|
|
|
|
if (!RemoveInternal(hitObject))
|
|
|
|
|
return false;
|
2019-12-17 17:56:29 +00:00
|
|
|
|
|
2020-11-06 13:15:00 +00:00
|
|
|
|
unbindStartTime(hitObject);
|
2019-12-17 17:56:29 +00:00
|
|
|
|
|
2019-12-18 03:03:15 +00:00
|
|
|
|
return true;
|
2019-12-17 17:56:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-23 02:16:59 +00:00
|
|
|
|
public virtual void Clear(bool disposeChildren = true)
|
|
|
|
|
{
|
|
|
|
|
ClearInternal(disposeChildren);
|
2020-11-06 13:15:00 +00:00
|
|
|
|
unbindAllStartTimes();
|
2020-09-21 09:27:15 +00:00
|
|
|
|
}
|
2020-04-23 02:16:59 +00:00
|
|
|
|
|
2020-11-06 13:15:00 +00:00
|
|
|
|
public int IndexOf(DrawableHitObject hitObject) => IndexOfInternal(hitObject);
|
|
|
|
|
|
|
|
|
|
protected override void OnChildLifetimeBoundaryCrossed(LifetimeBoundaryCrossedEvent e)
|
2020-09-21 09:27:15 +00:00
|
|
|
|
{
|
2020-11-06 13:15:00 +00:00
|
|
|
|
if (!(e.Child is DrawableHitObject hitObject))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if ((e.Kind == LifetimeBoundaryKind.End && e.Direction == LifetimeBoundaryCrossingDirection.Forward)
|
|
|
|
|
|| (e.Kind == LifetimeBoundaryKind.Start && e.Direction == LifetimeBoundaryCrossingDirection.Backward))
|
|
|
|
|
{
|
|
|
|
|
hitObject.OnKilled();
|
|
|
|
|
}
|
2020-04-23 02:16:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-06 13:15:00 +00:00
|
|
|
|
#region Comparator + StartTime tracking
|
2019-12-18 09:51:12 +00:00
|
|
|
|
|
2020-11-06 13:15:00 +00:00
|
|
|
|
private void bindStartTime(DrawableHitObject hitObject)
|
2019-12-17 17:56:29 +00:00
|
|
|
|
{
|
2020-11-06 13:15:00 +00:00
|
|
|
|
var bindable = hitObject.StartTimeBindable.GetBoundCopy();
|
|
|
|
|
bindable.BindValueChanged(_ => onStartTimeChanged(hitObject));
|
2019-12-17 17:56:29 +00:00
|
|
|
|
|
2020-11-06 13:15:00 +00:00
|
|
|
|
startTimeMap[hitObject] = bindable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void unbindStartTime(DrawableHitObject hitObject)
|
|
|
|
|
{
|
|
|
|
|
startTimeMap[hitObject].UnbindAll();
|
|
|
|
|
startTimeMap.Remove(hitObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void unbindAllStartTimes()
|
|
|
|
|
{
|
|
|
|
|
foreach (var kvp in startTimeMap)
|
|
|
|
|
kvp.Value.UnbindAll();
|
|
|
|
|
startTimeMap.Clear();
|
2019-12-17 17:56:29 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2020-11-06 13:15:00 +00:00
|
|
|
|
private void onStartTimeChanged(DrawableHitObject hitObject) => SortInternal();
|
|
|
|
|
|
2018-04-13 09:19:50 +00: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
|
2020-11-06 13:15:00 +00:00
|
|
|
|
int i = yObj.HitObject.StartTime.CompareTo(xObj.HitObject.StartTime);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
return i == 0 ? CompareReverseChildID(x, y) : i;
|
|
|
|
|
}
|
2019-01-29 06:25:27 +00:00
|
|
|
|
|
2020-11-06 13:15:00 +00:00
|
|
|
|
#endregion
|
2019-07-16 09:19:13 +00:00
|
|
|
|
|
2020-11-06 13:15:00 +00:00
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
unbindAllStartTimes();
|
2019-01-29 06:25:27 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|