1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 21:27:24 +08:00
osu-lazer/osu.Game/Rulesets/UI/Playfield.cs

116 lines
4.4 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2017-04-18 15:05:58 +08:00
using osu.Game.Rulesets.Objects.Drawables;
2017-03-07 14:43:44 +08:00
using OpenTK;
2017-04-18 15:05:58 +08:00
using osu.Game.Rulesets.Judgements;
using osu.Framework.Allocation;
using System.Collections.Generic;
using System.Linq;
2017-04-18 15:05:58 +08:00
namespace osu.Game.Rulesets.UI
{
public abstract class Playfield : Container
{
/// <summary>
/// The HitObjects contained in this Playfield.
/// </summary>
public HitObjectContainer HitObjects { get; protected set; }
2017-03-06 12:59:11 +08:00
internal Container<Drawable> ScaledContent;
/// <summary>
/// Whether we are currently providing the local user a gameplay cursor.
/// </summary>
public virtual bool ProvidingUserCursor => false;
2017-03-09 14:53:16 +08:00
protected override Container<Drawable> Content => content;
private readonly Container<Drawable> content;
2017-03-07 14:43:44 +08:00
/// <summary>
/// A container for keeping track of DrawableHitObjects.
/// </summary>
/// <param name="customWidth">Whether we want our internal coordinate system to be scaled to a specified width.</param>
protected Playfield(float? customWidth = null)
{
2017-04-12 14:04:11 +08:00
// Default height since we force relative size axes
Size = Vector2.One;
AddInternal(ScaledContent = new ScaledContainer
{
2017-03-07 14:43:44 +08:00
CustomWidth = customWidth,
RelativeSizeAxes = Axes.Both,
2017-02-28 19:14:48 +08:00
Children = new[]
{
2017-03-09 14:53:16 +08:00
content = new Container
2017-02-28 19:14:48 +08:00
{
RelativeSizeAxes = Axes.Both,
}
}
});
HitObjects = new HitObjectContainer
{
RelativeSizeAxes = Axes.Both,
};
}
[BackgroundDependencyLoader]
private void load()
{
Add(HitObjects);
}
public override Axes RelativeSizeAxes
{
get { return Axes.Both; }
set { throw new InvalidOperationException($@"{nameof(Playfield)}'s {nameof(RelativeSizeAxes)} should never be changed from {Axes.Both}"); }
}
/// <summary>
/// Performs post-processing tasks (if any) after all DrawableHitObjects are loaded into this Playfield.
/// </summary>
public virtual void PostProcess() { }
/// <summary>
/// Adds a DrawableHitObject to this Playfield.
/// </summary>
/// <param name="h">The DrawableHitObject to add.</param>
public virtual void Add(DrawableHitObject h) => HitObjects.Add(h);
/// <summary>
/// Remove a DrawableHitObject from this Playfield.
/// </summary>
/// <param name="h">The DrawableHitObject to remove.</param>
public virtual void Remove(DrawableHitObject h) => HitObjects.Remove(h);
/// <summary>
2017-09-06 17:05:51 +08:00
/// Triggered when a new <see cref="Judgement"/> occurs on a <see cref="DrawableHitObject"/>.
/// </summary>
2017-09-06 17:05:51 +08:00
/// <param name="judgedObject">The object that <paramref name="judgement"/> occured for.</param>
/// <param name="judgement">The <see cref="Judgement"/> that occurred.</param>
public virtual void OnJudgement(DrawableHitObject judgedObject, Judgement judgement) { }
2017-02-10 13:16:23 +08:00
public class HitObjectContainer : CompositeDrawable
{
public virtual IEnumerable<DrawableHitObject> Objects => InternalChildren.OfType<DrawableHitObject>();
public virtual void Add(DrawableHitObject hitObject) => AddInternal(hitObject);
public virtual bool Remove(DrawableHitObject hitObject) => RemoveInternal(hitObject);
}
2017-03-07 14:43:44 +08:00
private class ScaledContainer : Container
{
2017-03-08 10:54:52 +08:00
/// <summary>
/// A value (in game pixels that we should scale our content to match).
/// </summary>
2017-03-07 14:43:44 +08:00
public float? CustomWidth;
2017-03-08 10:54:52 +08:00
//dividing by the customwidth will effectively scale our content to the required container size.
2017-03-07 14:43:44 +08:00
protected override Vector2 DrawScale => CustomWidth.HasValue ? new Vector2(DrawSize.X / CustomWidth.Value) : base.DrawScale;
}
}
}