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
|
|
|
|
|
2018-07-17 14:48:51 +08:00
|
|
|
|
using System;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using System.Collections.Generic;
|
2018-07-17 14:48:51 +08:00
|
|
|
|
using System.Linq;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Game.Rulesets.Objects.Drawables;
|
|
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 18:04:31 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-07-17 14:48:51 +08:00
|
|
|
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
2018-09-21 13:02:32 +08:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2020-11-10 22:32:30 +08:00
|
|
|
|
using osu.Game.Rulesets.Judgements;
|
2018-08-03 20:03:11 +08:00
|
|
|
|
using osu.Game.Rulesets.Mods;
|
2020-11-10 22:32:30 +08:00
|
|
|
|
using osu.Game.Rulesets.Objects;
|
2018-11-20 15:51:59 +08:00
|
|
|
|
using osuTK;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.UI
|
|
|
|
|
{
|
2018-09-21 13:35:50 +08:00
|
|
|
|
public abstract class Playfield : CompositeDrawable
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2020-11-10 22:32:30 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Invoked when a <see cref="DrawableHitObject"/> is judged.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event Action<DrawableHitObject, JudgementResult> NewResult;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Invoked when a <see cref="DrawableHitObject"/> judgement is reverted.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event Action<DrawableHitObject, JudgementResult> RevertResult;
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
/// <summary>
|
2018-07-17 14:51:10 +08:00
|
|
|
|
/// The <see cref="DrawableHitObject"/> contained in this Playfield.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
/// </summary>
|
2018-09-21 13:35:50 +08:00
|
|
|
|
public HitObjectContainer HitObjectContainer => hitObjectContainerLazy.Value;
|
|
|
|
|
|
|
|
|
|
private readonly Lazy<HitObjectContainer> hitObjectContainerLazy;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-09-21 13:02:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// A function that converts gamefield coordinates to screen space.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Func<Vector2, Vector2> GamefieldToScreenSpace => HitObjectContainer.ToScreenSpace;
|
|
|
|
|
|
2020-03-23 18:18:56 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// A function that converts screen space coordinates to gamefield.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Func<Vector2, Vector2> ScreenSpaceToGamefield => HitObjectContainer.ToLocalSpace;
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
/// <summary>
|
2018-07-17 14:51:10 +08:00
|
|
|
|
/// All the <see cref="DrawableHitObject"/>s contained in this <see cref="Playfield"/> and all <see cref="NestedPlayfields"/>.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
/// </summary>
|
2020-09-22 17:17:04 +08:00
|
|
|
|
public IEnumerable<DrawableHitObject> AllHitObjects
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (HitObjectContainer == null)
|
|
|
|
|
return Enumerable.Empty<DrawableHitObject>();
|
|
|
|
|
|
|
|
|
|
var enumerable = HitObjectContainer.Objects;
|
|
|
|
|
|
|
|
|
|
if (nestedPlayfields.IsValueCreated)
|
|
|
|
|
enumerable = enumerable.Concat(NestedPlayfields.SelectMany(p => p.AllHitObjects));
|
|
|
|
|
|
|
|
|
|
return enumerable;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-17 14:51:10 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// All <see cref="Playfield"/>s nested inside this <see cref="Playfield"/>.
|
|
|
|
|
/// </summary>
|
2018-07-17 14:48:51 +08:00
|
|
|
|
public IEnumerable<Playfield> NestedPlayfields => nestedPlayfields.IsValueCreated ? nestedPlayfields.Value : Enumerable.Empty<Playfield>();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-07-17 14:51:10 +08:00
|
|
|
|
private readonly Lazy<List<Playfield>> nestedPlayfields = new Lazy<List<Playfield>>();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-07-20 16:04:33 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether judgements should be displayed by this and and all nested <see cref="Playfield"/>s.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public readonly BindableBool DisplayJudgements = new BindableBool(true);
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
/// <summary>
|
2018-09-21 13:02:32 +08:00
|
|
|
|
/// Creates a new <see cref="Playfield"/>.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
/// </summary>
|
2018-09-21 13:02:32 +08:00
|
|
|
|
protected Playfield()
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
2018-09-21 13:35:50 +08:00
|
|
|
|
|
2020-11-10 22:32:30 +08:00
|
|
|
|
hitObjectContainerLazy = new Lazy<HitObjectContainer>(() => CreateHitObjectContainer().With(h =>
|
|
|
|
|
{
|
|
|
|
|
h.NewResult += (d, r) => NewResult?.Invoke(d, r);
|
|
|
|
|
h.RevertResult += (d, r) => RevertResult?.Invoke(d, r);
|
|
|
|
|
}));
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 12:31:17 +08:00
|
|
|
|
[Resolved(CanBeNull = true)]
|
2019-04-10 16:11:17 +08:00
|
|
|
|
private IReadOnlyList<Mod> mods { get; set; }
|
2018-08-03 20:03:11 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
2019-04-08 17:32:05 +08:00
|
|
|
|
private void load()
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-03-08 14:01:45 +08:00
|
|
|
|
Cursor = CreateCursor();
|
2019-05-07 12:23:09 +08:00
|
|
|
|
|
2019-03-08 14:01:45 +08:00
|
|
|
|
if (Cursor != null)
|
2019-04-22 16:06:01 +08:00
|
|
|
|
{
|
|
|
|
|
// initial showing of the cursor will be handed by MenuCursorContainer (via DrawableRuleset's IProvideCursor implementation).
|
|
|
|
|
Cursor.Hide();
|
|
|
|
|
|
2019-03-25 18:21:47 +08:00
|
|
|
|
AddInternal(Cursor);
|
2019-04-22 16:06:01 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs post-processing tasks (if any) after all DrawableHitObjects are loaded into this Playfield.
|
|
|
|
|
/// </summary>
|
2018-07-17 14:48:51 +08:00
|
|
|
|
public virtual void PostProcess() => NestedPlayfields.ForEach(p => p.PostProcess());
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds a DrawableHitObject to this Playfield.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="h">The DrawableHitObject to add.</param>
|
2020-11-10 22:32:30 +08:00
|
|
|
|
public virtual void Add(DrawableHitObject h)
|
|
|
|
|
{
|
|
|
|
|
HitObjectContainer.Add(h);
|
|
|
|
|
OnHitObjectAdded(h.HitObject);
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Remove a DrawableHitObject from this Playfield.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="h">The DrawableHitObject to remove.</param>
|
2020-11-10 22:32:30 +08:00
|
|
|
|
public virtual bool Remove(DrawableHitObject h)
|
|
|
|
|
{
|
|
|
|
|
if (!HitObjectContainer.Remove(h))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
OnHitObjectRemoved(h.HitObject);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly Dictionary<HitObject, HitObjectLifetimeEntry> lifetimeEntryMap = new Dictionary<HitObject, HitObjectLifetimeEntry>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-11-12 13:04:16 +08:00
|
|
|
|
/// Adds a <see cref="HitObjectLifetimeEntry"/> for a pooled <see cref="HitObject"/> to this <see cref="Playfield"/>.
|
2020-11-10 22:32:30 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="entry">The <see cref="HitObjectLifetimeEntry"/> controlling the lifetime of the <see cref="HitObject"/>.</param>
|
|
|
|
|
public void Add(HitObjectLifetimeEntry entry)
|
|
|
|
|
{
|
|
|
|
|
HitObjectContainer.Add(entry);
|
|
|
|
|
lifetimeEntryMap[entry.HitObject] = entry;
|
|
|
|
|
OnHitObjectAdded(entry.HitObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-11-12 13:04:16 +08:00
|
|
|
|
/// Removes a <see cref="HitObjectLifetimeEntry"/> for a pooled <see cref="HitObject"/> from this <see cref="Playfield"/>.
|
2020-11-10 22:32:30 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="entry">The <see cref="HitObjectLifetimeEntry"/> controlling the lifetime of the <see cref="HitObject"/>.</param>
|
2020-11-12 13:04:16 +08:00
|
|
|
|
/// <returns>Whether the <see cref="HitObject"/> was successfully removed.</returns>
|
|
|
|
|
public bool Remove(HitObjectLifetimeEntry entry)
|
2020-11-10 22:32:30 +08:00
|
|
|
|
{
|
2020-11-12 13:04:16 +08:00
|
|
|
|
if (lifetimeEntryMap.Remove(entry.HitObject))
|
|
|
|
|
{
|
|
|
|
|
HitObjectContainer.Remove(entry);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool removedFromNested = false;
|
|
|
|
|
|
|
|
|
|
if (nestedPlayfields.IsValueCreated)
|
|
|
|
|
removedFromNested = nestedPlayfields.Value.Any(p => p.Remove(entry));
|
|
|
|
|
|
|
|
|
|
return removedFromNested;
|
2020-11-10 22:32:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Invoked when a <see cref="HitObject"/> is added to this <see cref="Playfield"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="hitObject">The added <see cref="HitObject"/>.</param>
|
|
|
|
|
protected virtual void OnHitObjectAdded(HitObject hitObject)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Invoked when a <see cref="HitObject"/> is removed from this <see cref="Playfield"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="hitObject">The removed <see cref="HitObject"/>.</param>
|
|
|
|
|
protected virtual void OnHitObjectRemoved(HitObject hitObject)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-06 11:34:58 +08:00
|
|
|
|
/// <summary>
|
2019-03-06 16:23:13 +08:00
|
|
|
|
/// The cursor currently being used by this <see cref="Playfield"/>. May be null if no cursor is provided.
|
2019-03-06 11:34:58 +08:00
|
|
|
|
/// </summary>
|
2019-03-25 19:25:16 +08:00
|
|
|
|
public GameplayCursorContainer Cursor { get; private set; }
|
2019-03-08 13:59:45 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-11-29 16:35:11 +08:00
|
|
|
|
/// Provide a cursor which is to be used for gameplay.
|
2019-03-08 13:59:45 +08:00
|
|
|
|
/// </summary>
|
2019-11-29 16:35:11 +08:00
|
|
|
|
/// <remarks>
|
|
|
|
|
/// The default provided cursor is invisible when inside the bounds of the <see cref="Playfield"/>.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <returns>The cursor, or null to show the menu cursor.</returns>
|
|
|
|
|
protected virtual GameplayCursorContainer CreateCursor() => new InvisibleCursorContainer();
|
2019-03-08 13:59:45 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers a <see cref="Playfield"/> as a nested <see cref="Playfield"/>.
|
|
|
|
|
/// This does not add the <see cref="Playfield"/> to the draw hierarchy.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="otherPlayfield">The <see cref="Playfield"/> to add.</param>
|
|
|
|
|
protected void AddNested(Playfield otherPlayfield)
|
|
|
|
|
{
|
2018-07-20 16:04:33 +08:00
|
|
|
|
otherPlayfield.DisplayJudgements.BindTo(DisplayJudgements);
|
2020-11-10 22:32:30 +08:00
|
|
|
|
|
|
|
|
|
otherPlayfield.NewResult += (d, r) => NewResult?.Invoke(d, r);
|
|
|
|
|
otherPlayfield.RevertResult += (d, r) => RevertResult?.Invoke(d, r);
|
|
|
|
|
|
2018-07-23 12:36:10 +08:00
|
|
|
|
nestedPlayfields.Value.Add(otherPlayfield);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-12 18:15:11 +08:00
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
|
|
// in the case a consumer forgets to add the HitObjectContainer, we will add it here.
|
|
|
|
|
if (HitObjectContainer.Parent == null)
|
|
|
|
|
AddInternal(HitObjectContainer);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-03 20:03:11 +08:00
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
|
|
|
|
|
2020-04-01 12:31:17 +08:00
|
|
|
|
if (mods != null)
|
2019-11-11 19:53:22 +08:00
|
|
|
|
{
|
2019-04-10 16:11:17 +08:00
|
|
|
|
foreach (var mod in mods)
|
2019-11-11 19:53:22 +08:00
|
|
|
|
{
|
2018-08-04 06:18:09 +08:00
|
|
|
|
if (mod is IUpdatableByPlayfield updatable)
|
|
|
|
|
updatable.Update(this);
|
2019-11-11 19:53:22 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-03 20:03:11 +08:00
|
|
|
|
}
|
2018-09-21 13:35:50 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates the container that will be used to contain the <see cref="DrawableHitObject"/>s.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual HitObjectContainer CreateHitObjectContainer() => new HitObjectContainer();
|
2019-11-29 16:35:11 +08:00
|
|
|
|
|
|
|
|
|
public class InvisibleCursorContainer : GameplayCursorContainer
|
|
|
|
|
{
|
|
|
|
|
protected override Drawable CreateCursor() => new InvisibleCursor();
|
|
|
|
|
|
|
|
|
|
private class InvisibleCursor : Drawable
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|