// 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.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Input.Bindings; using osu.Framework.Input.Events; using osu.Game.Input.Bindings; using osu.Game.Rulesets.Objects; using osuTK.Input; namespace osu.Game.Rulesets.Edit { /// /// A blueprint which governs the placement of something. /// public abstract partial class PlacementBlueprint : VisibilityContainer, IKeyBindingHandler { /// /// Whether the is currently mid-placement, but has not necessarily finished being placed. /// public PlacementState PlacementActive { get; private set; } /// /// Whether this blueprint is currently in a state that can be committed. /// /// /// Override this with any preconditions that should be double-checked on committing. /// If false is returned and a commit is attempted, the blueprint will be destroyed instead. /// protected virtual bool IsValidForPlacement => true; // the blueprint should still be considered for input even if it is hidden, // especially when such input is the reason for making the blueprint become visible. public override bool PropagatePositionalInputSubTree => true; public override bool PropagateNonPositionalInputSubTree => true; protected PlacementBlueprint() { RelativeSizeAxes = Axes.Both; // the blueprint should still be considered for input even if it is hidden, // especially when such input is the reason for making the blueprint become visible. AlwaysPresent = true; } /// /// Signals that the placement has started. /// /// Whether this call is committing a value and continuing with further adjustments. protected virtual void BeginPlacement(bool commitStart = false) { if (commitStart) PlacementActive = PlacementState.Active; } /// /// Signals that the placement of has finished. /// This will destroy this , and commit the changes. /// /// Whether the changes should be committed. Note that a commit may fail if is false. public virtual void EndPlacement(bool commit) { switch (PlacementActive) { case PlacementState.Finished: return; case PlacementState.Waiting: // ensure placement was started before ending to make state handling simpler. BeginPlacement(); break; } PlacementActive = PlacementState.Finished; } /// /// Determines which objects to snap to for the snap result in . /// public virtual SnapType SnapType => SnapType.All; /// /// Updates the time and position of this based on the provided snap information. /// /// The snap result information. public virtual void UpdateTimeAndPosition(SnapResult result) { } public bool OnPressed(KeyBindingPressEvent e) { if (PlacementActive == PlacementState.Waiting) return false; switch (e.Action) { case GlobalAction.Back: EndPlacement(false); return true; default: return false; } } public void OnReleased(KeyBindingReleaseEvent e) { } protected override bool Handle(UIEvent e) { base.Handle(e); switch (e) { case ScrollEvent: return false; case DoubleClickEvent: return false; case MouseButtonEvent mouse: // placement blueprints should generally block mouse from reaching underlying components (ie. performing clicks on interface buttons). return mouse.Button == MouseButton.Left || PlacementActive == PlacementState.Active; default: return false; } } protected override void PopIn() { this.FadeIn(200, Easing.OutQuint); } protected override void PopOut() { this.FadeOut(200, Easing.OutQuint); } public enum PlacementState { Waiting, Active, Finished } } }