osu/osu.Game/Rulesets/UI/Playfield.cs

136 lines
5.1 KiB
C#
Raw Normal View History

// 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
2018-07-17 06:48:51 +00:00
using System;
2018-04-13 09:19:50 +00:00
using System.Collections.Generic;
2018-07-17 06:48:51 +00:00
using System.Linq;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics;
using osu.Game.Rulesets.Objects.Drawables;
using osu.Framework.Allocation;
2019-02-21 10:04:31 +00:00
using osu.Framework.Bindables;
2018-07-17 06:48:51 +00:00
using osu.Framework.Extensions.IEnumerableExtensions;
2018-09-21 05:02:32 +00:00
using osu.Framework.Graphics.Containers;
2019-03-06 03:34:58 +00:00
using osu.Framework.Graphics.Cursor;
2018-08-03 12:03:11 +00:00
using osu.Game.Beatmaps;
using osu.Game.Rulesets.Mods;
2018-11-20 07:51:59 +00:00
using osuTK;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Rulesets.UI
{
2018-09-21 05:35:50 +00:00
public abstract class Playfield : CompositeDrawable
2018-04-13 09:19:50 +00:00
{
/// <summary>
/// The <see cref="DrawableHitObject"/> contained in this Playfield.
2018-04-13 09:19:50 +00:00
/// </summary>
2018-09-21 05:35:50 +00:00
public HitObjectContainer HitObjectContainer => hitObjectContainerLazy.Value;
private readonly Lazy<HitObjectContainer> hitObjectContainerLazy;
2018-04-13 09:19:50 +00:00
2018-09-21 05:02:32 +00:00
/// <summary>
/// A function that converts gamefield coordinates to screen space.
/// </summary>
public Func<Vector2, Vector2> GamefieldToScreenSpace => HitObjectContainer.ToScreenSpace;
2018-04-13 09:19:50 +00:00
/// <summary>
/// All the <see cref="DrawableHitObject"/>s contained in this <see cref="Playfield"/> and all <see cref="NestedPlayfields"/>.
2018-04-13 09:19:50 +00:00
/// </summary>
public IEnumerable<DrawableHitObject> AllHitObjects => HitObjectContainer?.Objects.Concat(NestedPlayfields.SelectMany(p => p.AllHitObjects)) ?? Enumerable.Empty<DrawableHitObject>();
/// <summary>
/// All <see cref="Playfield"/>s nested inside this <see cref="Playfield"/>.
/// </summary>
2018-07-17 06:48:51 +00:00
public IEnumerable<Playfield> NestedPlayfields => nestedPlayfields.IsValueCreated ? nestedPlayfields.Value : Enumerable.Empty<Playfield>();
2018-04-13 09:19:50 +00:00
private readonly Lazy<List<Playfield>> nestedPlayfields = new Lazy<List<Playfield>>();
2018-04-13 09:19:50 +00: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);
2019-03-06 03:34:58 +00:00
public readonly BindableBool HasReplayLoaded = new BindableBool();
2018-04-13 09:19:50 +00:00
/// <summary>
2018-09-21 05:02:32 +00:00
/// Creates a new <see cref="Playfield"/>.
2018-04-13 09:19:50 +00:00
/// </summary>
2018-09-21 05:02:32 +00:00
protected Playfield()
2018-04-13 09:19:50 +00:00
{
RelativeSizeAxes = Axes.Both;
2018-09-21 05:35:50 +00:00
hitObjectContainerLazy = new Lazy<HitObjectContainer>(CreateHitObjectContainer);
2019-03-06 03:34:58 +00:00
Cursor = CreateCursor();
2018-04-13 09:19:50 +00:00
}
2018-08-03 12:03:11 +00:00
private WorkingBeatmap beatmap;
2018-04-13 09:19:50 +00:00
[BackgroundDependencyLoader]
2019-02-01 06:42:15 +00:00
private void load(IBindable<WorkingBeatmap> beatmap)
2018-04-13 09:19:50 +00:00
{
2018-08-21 03:10:00 +00:00
this.beatmap = beatmap.Value;
2018-04-13 09:19:50 +00:00
}
/// <summary>
/// Performs post-processing tasks (if any) after all DrawableHitObjects are loaded into this Playfield.
/// </summary>
2018-07-17 06:48:51 +00:00
public virtual void PostProcess() => NestedPlayfields.ForEach(p => p.PostProcess());
2018-04-13 09:19:50 +00:00
/// <summary>
/// Adds a DrawableHitObject to this Playfield.
/// </summary>
/// <param name="h">The DrawableHitObject to add.</param>
public virtual void Add(DrawableHitObject h) => HitObjectContainer.Add(h);
2018-04-13 09:19:50 +00:00
/// <summary>
/// Remove a DrawableHitObject from this Playfield.
/// </summary>
/// <param name="h">The DrawableHitObject to remove.</param>
2018-11-19 07:20:21 +00:00
public virtual bool Remove(DrawableHitObject h) => HitObjectContainer.Remove(h);
2018-04-13 09:19:50 +00:00
2019-03-06 03:34:58 +00:00
/// <summary>
/// Creates the cursor. May be null if no cursor is required.
/// </summary>
protected virtual CursorContainer CreateCursor() => null;
/// <summary>
/// The cursor provided by this <see cref="RulesetContainer"/>. May be null if no cursor is provided.
/// </summary>
public CursorContainer Cursor { get; }
2018-04-13 09:19:50 +00: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)
{
otherPlayfield.DisplayJudgements.BindTo(DisplayJudgements);
nestedPlayfields.Value.Add(otherPlayfield);
2018-04-13 09:19:50 +00: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 12:03:11 +00:00
protected override void Update()
{
base.Update();
if (beatmap != null)
2018-08-03 22:18:09 +00:00
foreach (var mod in beatmap.Mods.Value)
if (mod is IUpdatableByPlayfield updatable)
updatable.Update(this);
2018-08-03 12:03:11 +00:00
}
2018-09-21 05:35:50 +00:00
/// <summary>
/// Creates the container that will be used to contain the <see cref="DrawableHitObject"/>s.
/// </summary>
protected virtual HitObjectContainer CreateHitObjectContainer() => new HitObjectContainer();
2018-04-13 09:19:50 +00:00
}
}