mirror of
https://github.com/osukey/osukey.git
synced 2025-07-02 16:59:53 +09:00
Remove intermediate Screens namespace
This commit is contained in:
186
osu.Game/Screens/Edit/Compose/Components/SelectionBox.cs
Normal file
186
osu.Game/Screens/Edit/Compose/Components/SelectionBox.cs
Normal file
@ -0,0 +1,186 @@
|
||||
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
||||
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.Graphics.Shapes;
|
||||
using osu.Framework.Input.Events;
|
||||
using osu.Framework.Input.States;
|
||||
using osu.Game.Graphics;
|
||||
using osu.Game.Rulesets.Edit;
|
||||
using osu.Game.Rulesets.Edit.Types;
|
||||
using OpenTK;
|
||||
using OpenTK.Input;
|
||||
|
||||
namespace osu.Game.Screens.Edit.Compose.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// A box which surrounds <see cref="SelectionBlueprint"/>s and provides interactive handles, context menus etc.
|
||||
/// </summary>
|
||||
public class SelectionBox : CompositeDrawable
|
||||
{
|
||||
public const float BORDER_RADIUS = 2;
|
||||
|
||||
private readonly List<SelectionBlueprint> selectedBlueprints;
|
||||
|
||||
private Drawable outline;
|
||||
|
||||
[Resolved]
|
||||
private IPlacementHandler placementHandler { get; set; }
|
||||
|
||||
public SelectionBox()
|
||||
{
|
||||
selectedBlueprints = new List<SelectionBlueprint>();
|
||||
|
||||
RelativeSizeAxes = Axes.Both;
|
||||
AlwaysPresent = true;
|
||||
Alpha = 0;
|
||||
}
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(OsuColour colours)
|
||||
{
|
||||
InternalChild = outline = new Container
|
||||
{
|
||||
Masking = true,
|
||||
BorderThickness = BORDER_RADIUS,
|
||||
BorderColour = colours.Yellow,
|
||||
Child = new Box
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both,
|
||||
AlwaysPresent = true,
|
||||
Alpha = 0
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#region User Input Handling
|
||||
|
||||
public void HandleDrag(SelectionBlueprint m, Vector2 delta, InputState state)
|
||||
{
|
||||
// Todo: Various forms of snapping
|
||||
|
||||
foreach (var blueprint in selectedBlueprints)
|
||||
{
|
||||
switch (blueprint.HitObject.HitObject)
|
||||
{
|
||||
case IHasEditablePosition editablePosition:
|
||||
editablePosition.OffsetPosition(delta);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override bool OnKeyDown(KeyDownEvent e)
|
||||
{
|
||||
if (e.Repeat)
|
||||
return base.OnKeyDown(e);
|
||||
|
||||
switch (e.Key)
|
||||
{
|
||||
case Key.Delete:
|
||||
foreach (var h in selectedBlueprints.ToList())
|
||||
placementHandler.Delete(h.HitObject.HitObject);
|
||||
return true;
|
||||
}
|
||||
|
||||
return base.OnKeyDown(e);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Selection Handling
|
||||
|
||||
/// <summary>
|
||||
/// Bind an action to deselect all selected blueprints.
|
||||
/// </summary>
|
||||
public Action DeselectAll { private get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Handle a blueprint becoming selected.
|
||||
/// </summary>
|
||||
/// <param name="blueprint">The blueprint.</param>
|
||||
public void HandleSelected(SelectionBlueprint blueprint) => selectedBlueprints.Add(blueprint);
|
||||
|
||||
/// <summary>
|
||||
/// Handle a blueprint becoming deselected.
|
||||
/// </summary>
|
||||
/// <param name="blueprint">The blueprint.</param>
|
||||
public void HandleDeselected(SelectionBlueprint blueprint)
|
||||
{
|
||||
selectedBlueprints.Remove(blueprint);
|
||||
|
||||
// We don't want to update visibility if > 0, since we may be deselecting blueprints during drag-selection
|
||||
if (selectedBlueprints.Count == 0)
|
||||
UpdateVisibility();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle a blueprint requesting selection.
|
||||
/// </summary>
|
||||
/// <param name="blueprint">The blueprint.</param>
|
||||
public void HandleSelectionRequested(SelectionBlueprint blueprint, InputState state)
|
||||
{
|
||||
if (state.Keyboard.ControlPressed)
|
||||
{
|
||||
if (blueprint.IsSelected)
|
||||
blueprint.Deselect();
|
||||
else
|
||||
blueprint.Select();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (blueprint.IsSelected)
|
||||
return;
|
||||
|
||||
DeselectAll?.Invoke();
|
||||
blueprint.Select();
|
||||
}
|
||||
|
||||
UpdateVisibility();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Updates whether this <see cref="SelectionBox"/> is visible.
|
||||
/// </summary>
|
||||
internal void UpdateVisibility()
|
||||
{
|
||||
if (selectedBlueprints.Count > 0)
|
||||
Show();
|
||||
else
|
||||
Hide();
|
||||
}
|
||||
|
||||
protected override void Update()
|
||||
{
|
||||
base.Update();
|
||||
|
||||
if (selectedBlueprints.Count == 0)
|
||||
return;
|
||||
|
||||
// Move the rectangle to cover the hitobjects
|
||||
var topLeft = new Vector2(float.MaxValue, float.MaxValue);
|
||||
var bottomRight = new Vector2(float.MinValue, float.MinValue);
|
||||
|
||||
bool hasSelection = false;
|
||||
|
||||
foreach (var blueprint in selectedBlueprints)
|
||||
{
|
||||
topLeft = Vector2.ComponentMin(topLeft, ToLocalSpace(blueprint.SelectionQuad.TopLeft));
|
||||
bottomRight = Vector2.ComponentMax(bottomRight, ToLocalSpace(blueprint.SelectionQuad.BottomRight));
|
||||
}
|
||||
|
||||
topLeft -= new Vector2(5);
|
||||
bottomRight += new Vector2(5);
|
||||
|
||||
outline.Size = bottomRight - topLeft;
|
||||
outline.Position = topLeft;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user