// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using osu.Framework; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Input.Events; using osu.Framework.Timing; using osu.Game.Beatmaps; using osu.Game.Beatmaps.ControlPoints; using osu.Game.Rulesets.Objects; using osu.Game.Screens.Edit.Compose; using osuTK; namespace osu.Game.Rulesets.Edit { /// /// A blueprint which governs the creation of a new to actualisation. /// public abstract class PlacementBlueprint : CompositeDrawable, IStateful { /// /// Invoked when has changed. /// public event Action StateChanged; /// /// Whether the is currently being placed, but has not necessarily finished being placed. /// public bool PlacementBegun { get; private set; } /// /// The that is being placed. /// protected readonly HitObject HitObject; protected IClock EditorClock { get; private set; } private readonly IBindable beatmap = new Bindable(); [Resolved] private IPlacementHandler placementHandler { get; set; } protected PlacementBlueprint(HitObject hitObject) { HitObject = hitObject; RelativeSizeAxes = Axes.Both; // This is required to allow the blueprint's position to be updated via OnMouseMove/Handle // on the same frame it is made visible via a PlacementState change. AlwaysPresent = true; Alpha = 0; } [BackgroundDependencyLoader] private void load(IBindable beatmap, IAdjustableClock clock) { this.beatmap.BindTo(beatmap); EditorClock = clock; ApplyDefaultsToHitObject(); } private PlacementState state; public PlacementState State { get => state; set { if (state == value) return; state = value; if (state == PlacementState.Shown) Show(); else Hide(); StateChanged?.Invoke(value); } } /// /// Signals that the placement of has started. /// /// The start time of at the placement point. If null, the current clock time is used. protected void BeginPlacement(double? startTime = null) { HitObject.StartTime = startTime ?? EditorClock.CurrentTime; placementHandler.BeginPlacement(HitObject); PlacementBegun = true; } /// /// Signals that the placement of has finished. /// This will destroy this , and add the to the . /// /// Whether the object should be committed. public void EndPlacement(bool commit) { if (!PlacementBegun) BeginPlacement(); placementHandler.EndPlacement(HitObject, commit); } /// /// Updates the position of this to a new screen-space position. /// /// The screen-space position. public abstract void UpdatePosition(Vector2 screenSpacePosition); /// /// Invokes , /// refreshing and parameters for the . /// protected void ApplyDefaultsToHitObject() => HitObject.ApplyDefaults(beatmap.Value.Beatmap.ControlPointInfo, beatmap.Value.Beatmap.BeatmapInfo.BaseDifficulty); public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => Parent?.ReceivePositionalInputAt(screenSpacePos) ?? false; protected override bool Handle(UIEvent e) { base.Handle(e); switch (e) { case ScrollEvent _: return false; case MouseButtonEvent _: return true; default: return false; } } } public enum PlacementState { Hidden, Shown, } }