osu/osu.Game/Rulesets/Edit/SelectionBlueprint.cs

127 lines
4.3 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 09:19:50 +00:00
using System;
using osu.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives;
2019-11-12 04:38:42 +00:00
using osu.Framework.Graphics.UserInterface;
2018-05-11 01:48:07 +00:00
using osu.Game.Graphics.UserInterface;
using osu.Game.Rulesets.Objects;
2018-04-13 09:19:50 +00:00
using osu.Game.Rulesets.Objects.Drawables;
2018-11-20 07:51:59 +00:00
using osuTK;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Rulesets.Edit
{
/// <summary>
2018-11-06 09:06:34 +00:00
/// A blueprint placed above a <see cref="DrawableHitObject"/> adding editing functionality.
2018-04-13 09:19:50 +00:00
/// </summary>
public abstract class SelectionBlueprint : CompositeDrawable, IStateful<SelectionState>
2018-04-13 09:19:50 +00:00
{
public readonly HitObject HitObject;
2018-04-13 09:19:50 +00:00
/// <summary>
2018-11-06 08:56:04 +00:00
/// Invoked when this <see cref="SelectionBlueprint"/> has been selected.
2018-04-13 09:19:50 +00:00
/// </summary>
2018-11-06 08:56:04 +00:00
public event Action<SelectionBlueprint> Selected;
2018-04-13 09:19:50 +00:00
/// <summary>
2018-11-06 08:56:04 +00:00
/// Invoked when this <see cref="SelectionBlueprint"/> has been deselected.
2018-04-13 09:19:50 +00:00
/// </summary>
2018-11-06 08:56:04 +00:00
public event Action<SelectionBlueprint> Deselected;
2018-04-13 09:19:50 +00:00
public override bool HandlePositionalInput => ShouldBeAlive;
2018-04-13 09:19:50 +00:00
public override bool RemoveWhenNotAlive => false;
[Resolved(CanBeNull = true)]
private HitObjectComposer composer { get; set; }
protected SelectionBlueprint(HitObject hitObject)
2018-04-13 09:19:50 +00:00
{
HitObject = hitObject;
RelativeSizeAxes = Axes.Both;
}
[BackgroundDependencyLoader]
private void load()
{
2020-01-21 05:21:00 +00:00
OnDeselected();
AlwaysPresent = true;
2018-04-13 09:19:50 +00:00
}
private SelectionState state;
public event Action<SelectionState> StateChanged;
public SelectionState State
{
get => state;
set
{
2019-04-25 08:36:17 +00:00
if (state == value)
return;
2018-04-13 09:19:50 +00:00
state = value;
2019-04-01 03:16:05 +00:00
2018-04-13 09:19:50 +00:00
switch (state)
{
case SelectionState.Selected:
2020-01-21 05:21:00 +00:00
OnSelected();
2018-04-13 09:19:50 +00:00
Selected?.Invoke(this);
break;
2019-04-01 03:16:05 +00:00
2018-04-13 09:19:50 +00:00
case SelectionState.NotSelected:
2020-01-21 05:21:00 +00:00
OnDeselected();
2018-04-13 09:19:50 +00:00
Deselected?.Invoke(this);
break;
}
2019-04-25 08:36:17 +00:00
StateChanged?.Invoke(state);
2018-04-13 09:19:50 +00:00
}
}
2020-01-21 05:21:00 +00:00
protected virtual void OnDeselected() => Hide();
protected virtual void OnSelected() => Show();
// When not selected, input is only required for the blueprint itself to receive IsHovering
protected override bool ShouldBeConsideredForInput(Drawable child) => State == SelectionState.Selected;
2018-04-13 09:19:50 +00:00
/// <summary>
/// Selects this <see cref="OverlaySelectionBlueprint"/>, causing it to become visible.
2018-04-13 09:19:50 +00:00
/// </summary>
public void Select() => State = SelectionState.Selected;
/// <summary>
/// Deselects this <see cref="OverlaySelectionBlueprint"/>, causing it to become invisible.
2018-04-13 09:19:50 +00:00
/// </summary>
public void Deselect() => State = SelectionState.NotSelected;
public bool IsSelected => State == SelectionState.Selected;
/// <summary>
/// Updates the <see cref="Objects.HitObject"/>, invoking <see cref="Objects.HitObject.ApplyDefaults"/> and re-processing the beatmap.
/// </summary>
protected void UpdateHitObject() => composer?.UpdateHitObject(HitObject);
2019-11-12 04:38:42 +00:00
/// <summary>
/// The <see cref="MenuItem"/>s to be displayed in the context menu for this <see cref="OverlaySelectionBlueprint"/>.
2019-11-12 04:38:42 +00:00
/// </summary>
public virtual MenuItem[] ContextMenuItems => Array.Empty<MenuItem>();
2018-04-13 09:19:50 +00:00
/// <summary>
/// The screen-space point that causes this <see cref="OverlaySelectionBlueprint"/> to be selected.
2018-04-13 09:19:50 +00:00
/// </summary>
public virtual Vector2 SelectionPoint => ScreenSpaceDrawQuad.Centre;
2018-04-13 09:19:50 +00:00
/// <summary>
/// The screen-space quad that outlines this <see cref="OverlaySelectionBlueprint"/> for selections.
2018-04-13 09:19:50 +00:00
/// </summary>
public virtual Quad SelectionQuad => ScreenSpaceDrawQuad;
public Vector2 GetInstantDelta(Vector2 screenSpacePosition) => Parent.ToLocalSpace(screenSpacePosition) - Position;
2018-04-13 09:19:50 +00:00
}
}