// Copyright (c) 2007-2018 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System; using osu.Framework; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Primitives; using osu.Framework.Input.EventArgs; using osu.Framework.Input.States; using osu.Game.Graphics.UserInterface; using osu.Game.Rulesets.Objects.Drawables; using OpenTK; namespace osu.Game.Rulesets.Edit { /// /// A mask placed above a adding editing functionality. /// public class HitObjectMask : CompositeDrawable, IStateful { /// /// Invoked when this has been selected. /// public event Action Selected; /// /// Invoked when this has been deselected. /// public event Action Deselected; /// /// Invoked when this has requested selection. /// Will fire even if already selected. Does not actually perform selection. /// public event Action SelectionRequested; /// /// Invoked when this has requested drag. /// public event Action DragRequested; /// /// The which this applies to. /// public readonly DrawableHitObject HitObject; protected override bool ShouldBeAlive => HitObject.IsAlive && HitObject.IsPresent || State == SelectionState.Selected; public override bool HandlePositionalInput => ShouldBeAlive; public override bool RemoveWhenNotAlive => false; public HitObjectMask(DrawableHitObject hitObject) { HitObject = hitObject; AlwaysPresent = true; Alpha = 0; } private SelectionState state; public event Action StateChanged; public SelectionState State { get => state; set { if (state == value) return; state = value; switch (state) { case SelectionState.Selected: Show(); Selected?.Invoke(this); break; case SelectionState.NotSelected: Hide(); Deselected?.Invoke(this); break; } } } /// /// 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; public bool IsSelected => State == SelectionState.Selected; private bool selectionRequested; protected override bool OnMouseDown(InputState state, MouseDownEventArgs args) { selectionRequested = false; if (State == SelectionState.NotSelected) { SelectionRequested?.Invoke(this, state); selectionRequested = true; } return IsSelected; } protected override bool OnClick(InputState state) { if (State == SelectionState.Selected && !selectionRequested) { selectionRequested = true; SelectionRequested?.Invoke(this, state); return true; } return base.OnClick(state); } protected override bool OnDragStart(InputState state) => true; protected override bool OnDrag(InputState state) { DragRequested?.Invoke(this, state); return true; } /// /// The screen-space point that causes this to be selected. /// public virtual Vector2 SelectionPoint => ScreenSpaceDrawQuad.Centre; /// /// The screen-space quad that outlines this for selections. /// public virtual Quad SelectionQuad => ScreenSpaceDrawQuad; } }