1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-24 00:13:29 +08:00
osu-lazer/osu.Game/Rulesets/Edit/Layers/Selection/SelectionLayer.cs

118 lines
3.8 KiB
C#
Raw Normal View History

2018-01-05 19:21:19 +08:00
// 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.Collections.Generic;
using System.Linq;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Input;
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.UI;
using OpenTK;
namespace osu.Game.Rulesets.Edit.Layers.Selection
{
public class SelectionLayer : CompositeDrawable
{
private readonly Playfield playfield;
public SelectionLayer(Playfield playfield)
{
this.playfield = playfield;
RelativeSizeAxes = Axes.Both;
}
private SelectionBox selectionBox;
private CaptureBox captureBox;
2018-02-14 16:53:04 +08:00
private readonly List<DrawableHitObject> selectedHitObjects = new List<DrawableHitObject>();
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args)
{
2018-02-14 16:53:04 +08:00
clearSelection();
return true;
}
protected override bool OnDragStart(InputState state)
{
AddInternal(selectionBox = new SelectionBox());
return true;
}
protected override bool OnDrag(InputState state)
{
2018-02-15 18:07:53 +08:00
selectionBox.Show();
var dragPosition = state.Mouse.NativeState.Position;
var dragStartPosition = state.Mouse.NativeState.PositionMouseDown ?? dragPosition;
var screenSpaceDragQuad = new Quad(dragStartPosition.X, dragStartPosition.Y, dragPosition.X - dragStartPosition.X, dragPosition.Y - dragStartPosition.Y);
selectionBox.SetDragRectangle(screenSpaceDragQuad.AABBFloat);
2018-02-14 16:54:43 +08:00
selectQuad(screenSpaceDragQuad);
return true;
}
protected override bool OnDragEnd(InputState state)
{
selectionBox.Hide();
2018-02-15 18:07:53 +08:00
selectionBox.Expire();
2018-02-15 17:06:33 +08:00
finishSelection();
return true;
}
protected override bool OnClick(InputState state)
{
2018-02-14 16:54:43 +08:00
selectPoint(state.Mouse.NativeState.Position);
2018-02-15 17:06:33 +08:00
finishSelection();
return true;
}
2018-02-14 16:53:04 +08:00
/// <summary>
/// Deselects all selected <see cref="DrawableHitObject"/>s.
/// </summary>
private void clearSelection()
{
selectedHitObjects.Clear();
captureBox?.Hide();
}
/// <summary>
2018-02-14 16:54:43 +08:00
/// Selects all hitobjects that are present within the area of a <see cref="Quad"/>.
/// </summary>
2018-02-14 16:54:43 +08:00
/// <param name="screenSpaceQuad">The selection <see cref="Quad"/>.</param>
private void selectQuad(Quad screenSpaceQuad)
{
foreach (var obj in playfield.HitObjects.Objects.Where(h => h.IsAlive && h.IsPresent && screenSpaceQuad.Contains(h.SelectionPoint)))
2018-02-14 16:53:04 +08:00
selectedHitObjects.Add(obj);
}
/// <summary>
2018-02-14 16:54:43 +08:00
/// Selects the top-most hitobject that is present under a specific point.
/// </summary>
2018-02-14 16:54:43 +08:00
/// <param name="screenSpacePoint">The <see cref="Vector2"/> to select at.</param>
private void selectPoint(Vector2 screenSpacePoint)
{
2018-02-14 16:54:43 +08:00
var selected = playfield.HitObjects.Objects.Reverse().Where(h => h.IsAlive && h.IsPresent).FirstOrDefault(h => h.ReceiveMouseInputAt(screenSpacePoint));
if (selected == null)
return;
2018-02-14 16:54:43 +08:00
selectedHitObjects.Add(selected);
}
2018-02-15 17:06:33 +08:00
private void finishSelection()
{
2018-02-14 16:53:04 +08:00
if (selectedHitObjects.Count == 0)
return;
2018-02-15 17:06:33 +08:00
AddInternal(captureBox = new CaptureBox(this, selectedHitObjects.ToList()));
}
}
}