// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. #nullable disable using System; using System.Linq; using osu.Framework; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Primitives; using osu.Framework.Graphics.UserInterface; using osu.Game.Graphics.UserInterface; using osuTK; namespace osu.Game.Rulesets.Edit { /// /// A blueprint placed above a displaying item adding editing functionality. /// public abstract partial class SelectionBlueprint : CompositeDrawable, IStateful { public readonly T Item; /// /// Invoked when this has been selected. /// public event Action> Selected; /// /// Invoked when this has been deselected. /// public event Action> Deselected; public override bool HandlePositionalInput => ShouldBeAlive; public override bool RemoveWhenNotAlive => false; protected SelectionBlueprint(T item) { Item = item; RelativeSizeAxes = Axes.Both; AlwaysPresent = true; } protected override void LoadComplete() { base.LoadComplete(); updateState(); } private SelectionState state; public event Action StateChanged; public SelectionState State { get => state; set { if (state == value) return; state = value; if (IsLoaded) updateState(); StateChanged?.Invoke(state); } } private void updateState() { switch (state) { case SelectionState.Selected: OnSelected(); Selected?.Invoke(this); break; case SelectionState.NotSelected: OnDeselected(); Deselected?.Invoke(this); break; } } protected virtual void OnDeselected() { // selection blueprints are AlwaysPresent while the related item is visible // set the body piece's alpha directly to avoid arbitrarily rendering frame buffers etc. of children. foreach (var d in InternalChildren) d.Hide(); } protected virtual void OnSelected() { foreach (var d in InternalChildren) d.Show(); } // When not selected, input is only required for the blueprint itself to receive IsHovering protected override bool ShouldBeConsideredForInput(Drawable child) => State == SelectionState.Selected; /// /// Selects this , causing it to become visible. /// public void Select() => State = SelectionState.Selected; /// /// Deselects this , causing it to become invisible. /// public void Deselect() => State = SelectionState.NotSelected; /// /// Toggles the selection state of this . /// public void ToggleSelection() => State = IsSelected ? SelectionState.NotSelected : SelectionState.Selected; public bool IsSelected => State == SelectionState.Selected; /// /// The s to be displayed in the context menu for this . /// public virtual MenuItem[] ContextMenuItems => Array.Empty(); /// /// The screen-space main point that causes this to be selected via a drag. /// public virtual Vector2 ScreenSpaceSelectionPoint => ScreenSpaceDrawQuad.Centre; /// /// Any points that should be used for snapping purposes in addition to . Exposed via . /// protected virtual Vector2[] ScreenSpaceAdditionalNodes => Array.Empty(); /// /// The screen-space collection of base points on this that other objects can be snapped to. /// The first element of this collection is /// public Vector2[] ScreenSpaceSnapPoints => ScreenSpaceAdditionalNodes.Prepend(ScreenSpaceSelectionPoint).ToArray(); /// /// The screen-space quad that outlines this for selections. /// public virtual Quad SelectionQuad => ScreenSpaceDrawQuad; /// /// Handle to perform a partial deletion when the user requests a quick delete (Shift+Right Click). /// /// True if the deletion operation was handled by this blueprint. Returning false will delete the full blueprint. public virtual bool HandleQuickDeletion() => false; } }