// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using osu.Framework.Allocation; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Beatmaps; using osu.Game.Rulesets.Judgements; using osu.Game.Rulesets.Mods; using osu.Game.Rulesets.Objects; using osu.Game.Rulesets.Objects.Drawables; using System; using System.Collections.Generic; using System.Linq; using osu.Framework.Bindables; using osu.Framework.Graphics.Cursor; using osu.Framework.Input; using osu.Framework.Input.Events; using osu.Game.Configuration; using osu.Game.Graphics.Cursor; using osu.Game.Input.Handlers; using osu.Game.Overlays; using osu.Game.Replays; using osu.Game.Rulesets.Configuration; using osu.Game.Rulesets.Scoring; using osu.Game.Scoring; using osu.Game.Screens.Play; namespace osu.Game.Rulesets.UI { /// /// Displays an interactive ruleset gameplay instance. /// /// The type of HitObject contained by this DrawableRuleset. public abstract class DrawableRuleset : DrawableRuleset, IProvideCursor, ICanAttachKeyCounter where TObject : HitObject { /// /// The selected variant. /// public virtual int Variant => 0; /// /// The key conversion input manager for this DrawableRuleset. /// public PassThroughInputManager KeyBindingInputManager; public override double GameplayStartTime => Objects.First().StartTime - 2000; private readonly Lazy playfield; /// /// The playfield. /// public Playfield Playfield => playfield.Value; /// /// Place to put drawables above hit objects but below UI. /// public Container Overlays { get; private set; } /// /// Invoked when a has been applied by a . /// public event Action OnNewResult; /// /// Invoked when a is being reverted by a . /// public event Action OnRevertResult; /// /// The beatmap. /// public Beatmap Beatmap; public override IEnumerable Objects => Beatmap.HitObjects; protected IRulesetConfigManager Config { get; private set; } /// /// The mods which are to be applied. /// [Cached(typeof(IReadOnlyList))] private readonly IReadOnlyList mods; private FrameStabilityContainer frameStabilityContainer; private OnScreenDisplay onScreenDisplay; /// /// Creates a ruleset visualisation for the provided ruleset and beatmap. /// /// The ruleset being represented. /// The beatmap to create the hit renderer for. /// The s to apply. protected DrawableRuleset(Ruleset ruleset, WorkingBeatmap workingBeatmap, IReadOnlyList mods) : base(ruleset) { if (workingBeatmap == null) throw new ArgumentException("Beatmap cannot be null.", nameof(workingBeatmap)); this.mods = mods.ToArray(); RelativeSizeAxes = Axes.Both; Beatmap = (Beatmap)workingBeatmap.GetPlayableBeatmap(ruleset.RulesetInfo, mods); applyBeatmapMods(mods); KeyBindingInputManager = CreateInputManager(); playfield = new Lazy(CreatePlayfield); IsPaused.ValueChanged += paused => { if (HasReplayLoaded.Value) return; KeyBindingInputManager.UseParentInput = !paused.NewValue; }; } protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent) { var dependencies = new DependencyContainer(base.CreateChildDependencies(parent)); onScreenDisplay = dependencies.Get(); Config = dependencies.Get().GetConfigFor(Ruleset); if (Config != null) { dependencies.Cache(Config); onScreenDisplay?.BeginTracking(this, Config); } return dependencies; } public virtual PlayfieldAdjustmentContainer CreatePlayfieldAdjustmentContainer() => new PlayfieldAdjustmentContainer(); [BackgroundDependencyLoader] private void load(OsuConfigManager config) { InternalChildren = new Drawable[] { frameStabilityContainer = new FrameStabilityContainer { Child = KeyBindingInputManager .WithChild(CreatePlayfieldAdjustmentContainer() .WithChild(Playfield) ) }, Overlays = new Container { RelativeSizeAxes = Axes.Both } }; if ((ResumeOverlay = CreateResumeOverlay()) != null) { AddInternal(CreateInputManager() .WithChild(CreatePlayfieldAdjustmentContainer() .WithChild(ResumeOverlay))); } applyRulesetMods(mods, config); loadObjects(); } /// /// Creates and adds drawable representations of hit objects to the play field. /// private void loadObjects() { foreach (TObject h in Beatmap.HitObjects) addHitObject(h); Playfield.PostProcess(); foreach (var mod in mods.OfType()) mod.ApplyToDrawableHitObjects(Playfield.HitObjectContainer.Objects); } public override void RequestResume(Action continueResume) { if (ResumeOverlay != null && (Cursor == null || (Cursor.LastFrameState == Visibility.Visible && Contains(Cursor.ActiveCursor.ScreenSpaceDrawQuad.Centre)))) { ResumeOverlay.GameplayCursor = Cursor; ResumeOverlay.ResumeAction = continueResume; ResumeOverlay.Show(); } else continueResume(); } public ResumeOverlay ResumeOverlay { get; private set; } protected virtual ResumeOverlay CreateResumeOverlay() => null; /// /// Creates and adds the visual representation of a to this . /// /// The to add the visual representation for. private void addHitObject(TObject hitObject) { var drawableObject = CreateDrawableRepresentation(hitObject); if (drawableObject == null) return; drawableObject.OnNewResult += (_, r) => OnNewResult?.Invoke(r); drawableObject.OnRevertResult += (_, r) => OnRevertResult?.Invoke(r); Playfield.Add(drawableObject); } public override void SetReplayScore(Score replayScore) { if (!(KeyBindingInputManager is IHasReplayHandler replayInputManager)) throw new InvalidOperationException($"A {nameof(KeyBindingInputManager)} which supports replay loading is not available"); var handler = (ReplayScore = replayScore) != null ? CreateReplayInputHandler(replayScore.Replay) : null; replayInputManager.ReplayInputHandler = handler; frameStabilityContainer.ReplayInputHandler = handler; HasReplayLoaded.Value = replayInputManager.ReplayInputHandler != null; if (replayInputManager.ReplayInputHandler != null) replayInputManager.ReplayInputHandler.GamefieldToScreenSpace = Playfield.GamefieldToScreenSpace; if (!ProvidingUserCursor) { // The cursor is hidden by default (see Playfield.load()), but should be shown when there's a replay Playfield.Cursor?.Show(); } } /// /// Creates a DrawableHitObject from a HitObject. /// /// The HitObject to make drawable. /// The DrawableHitObject. public abstract DrawableHitObject CreateDrawableRepresentation(TObject h); public void Attach(KeyCounterDisplay keyCounter) => (KeyBindingInputManager as ICanAttachKeyCounter)?.Attach(keyCounter); /// /// Creates a key conversion input manager. An exception will be thrown if a valid is not returned. /// /// The input manager. protected abstract PassThroughInputManager CreateInputManager(); protected virtual ReplayInputHandler CreateReplayInputHandler(Replay replay) => null; /// /// Creates a Playfield. /// /// The Playfield. protected abstract Playfield CreatePlayfield(); public override ScoreProcessor CreateScoreProcessor() => new ScoreProcessor(this); /// /// Applies the active mods to the Beatmap. /// /// private void applyBeatmapMods(IReadOnlyList mods) { if (mods == null) return; foreach (var mod in mods.OfType>()) mod.ApplyToBeatmap(Beatmap); } /// /// Applies the active mods to this DrawableRuleset. /// /// The s to apply. /// The to apply. private void applyRulesetMods(IReadOnlyList mods, OsuConfigManager config) { if (mods == null) return; foreach (var mod in mods.OfType>()) mod.ApplyToDrawableRuleset(this); foreach (var mod in mods.OfType()) mod.ReadFromConfig(config); } #region IProvideCursor protected override bool OnHover(HoverEvent e) => true; // required for IProvideCursor CursorContainer IProvideCursor.Cursor => Playfield.Cursor; public override GameplayCursorContainer Cursor => Playfield.Cursor; public bool ProvidingUserCursor => Playfield.Cursor != null && !HasReplayLoaded.Value; #endregion protected override void Dispose(bool isDisposing) { base.Dispose(isDisposing); if (Config != null) { onScreenDisplay?.StopTracking(this, Config); Config = null; } } } /// /// Displays an interactive ruleset gameplay instance. /// /// This type is required only for adding non-generic type to the draw hierarchy. /// Once IDrawable is a thing, this can also become an interface. /// /// public abstract class DrawableRuleset : CompositeDrawable { /// /// Whether a replay is currently loaded. /// public readonly BindableBool HasReplayLoaded = new BindableBool(); /// /// Whether the game is paused. Used to block user input. /// public readonly BindableBool IsPaused = new BindableBool(); /// ~ /// The associated ruleset. /// public readonly Ruleset Ruleset; /// /// Creates a ruleset visualisation for the provided ruleset. /// /// The ruleset. internal DrawableRuleset(Ruleset ruleset) { Ruleset = ruleset; } /// /// All the converted hit objects contained by this hit renderer. /// public abstract IEnumerable Objects { get; } /// /// The point in time at which gameplay starts, including any required lead-in for display purposes. /// Defaults to two seconds before the first . Override as necessary. /// public abstract double GameplayStartTime { get; } /// /// The currently loaded replay. Usually null in the case of a local player. /// public Score ReplayScore { get; protected set; } /// /// The cursor being displayed by the . May be null if no cursor is provided. /// public abstract GameplayCursorContainer Cursor { get; } /// /// Sets a replay to be used, overriding local input. /// /// The replay, null for local input. public abstract void SetReplayScore(Score replayScore); /// /// Invoked when the interactive user requests resuming from a paused state. /// Allows potentially delaying the resume process until an interaction is performed. /// /// The action to run when resuming is to be completed. public abstract void RequestResume(Action continueResume); /// /// Create a for the associated ruleset and link with this /// . /// /// A score processor. public abstract ScoreProcessor CreateScoreProcessor(); } public class BeatmapInvalidForRulesetException : ArgumentException { public BeatmapInvalidForRulesetException(string text) : base(text) { } } }