// Copyright (c) 2007-2018 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System; using System.Collections.Generic; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Rulesets.Objects.Drawables; using OpenTK; using osu.Framework.Allocation; namespace osu.Game.Rulesets.UI { public abstract class Playfield : Container { /// /// The HitObjects contained in this Playfield. /// public HitObjectContainer HitObjects { get; private set; } public Container ScaledContent; protected override Container Content => content; private readonly Container content; private List nestedPlayfields; /// /// All the s nested inside this playfield. /// public IReadOnlyList NestedPlayfields => nestedPlayfields; /// /// A container for keeping track of DrawableHitObjects. /// /// Whether we want our internal coordinate system to be scaled to a specified width. protected Playfield(float? customWidth = null) { // Default height since we force relative size axes Size = Vector2.One; AddInternal(ScaledContent = new ScaledContainer { CustomWidth = customWidth, RelativeSizeAxes = Axes.Both, Children = new[] { content = new Container { RelativeSizeAxes = Axes.Both, } } }); } [BackgroundDependencyLoader] private void load() { HitObjects = CreateHitObjectContainer(); HitObjects.RelativeSizeAxes = Axes.Both; 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}"); } } /// /// Performs post-processing tasks (if any) after all DrawableHitObjects are loaded into this Playfield. /// public virtual void PostProcess() => nestedPlayfields?.ForEach(p => p.PostProcess()); /// /// Adds a DrawableHitObject to this Playfield. /// /// The DrawableHitObject to add. public virtual void Add(DrawableHitObject h) => HitObjects.Add(h); /// /// Remove a DrawableHitObject from this Playfield. /// /// The DrawableHitObject to remove. public virtual void Remove(DrawableHitObject h) => HitObjects.Remove(h); /// /// Registers a as a nested . /// This does not add the to the draw hierarchy. /// /// The to add. protected void AddNested(Playfield otherPlayfield) { if (nestedPlayfields == null) nestedPlayfields = new List(); nestedPlayfields.Add(otherPlayfield); } /// /// Creates the container that will be used to contain the s. /// protected virtual HitObjectContainer CreateHitObjectContainer() => new HitObjectContainer(); private class ScaledContainer : Container { /// /// A value (in game pixels that we should scale our content to match). /// public float? CustomWidth; //dividing by the customwidth will effectively scale our content to the required container size. protected override Vector2 DrawScale => CustomWidth.HasValue ? new Vector2(DrawSize.X / CustomWidth.Value) : base.DrawScale; protected override void Update() { base.Update(); RelativeChildSize = new Vector2(DrawScale.X, RelativeChildSize.Y); } } } }