1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 21:43:04 +08:00

Encapsulate logic for selecting/deselecting further

This commit is contained in:
smoogipoo 2018-02-20 14:54:45 +09:00
parent 995ecb7d94
commit 285df52f43

View File

@ -1,6 +1,7 @@
// 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.Extensions.IEnumerableExtensions;
@ -16,6 +17,9 @@ namespace osu.Game.Rulesets.Edit.Layers.Selection
{
public class SelectionLayer : CompositeDrawable
{
public event Action<DrawableHitObject> ObjectSelected;
public event Action<DrawableHitObject> ObjectDeselected;
private readonly Playfield playfield;
public SelectionLayer(Playfield playfield)
@ -75,15 +79,69 @@ namespace osu.Game.Rulesets.Edit.Layers.Selection
return true;
}
/// <summary>
/// Selects a <see cref="DrawableHitObject"/>.
/// </summary>
/// <param name="hitObject">The <see cref="DrawableHitObject"/> to select.</param>
public void Select(DrawableHitObject hitObject)
{
if (!select(hitObject))
return;
clearCapture();
finishSelection();
}
/// <summary>
/// Selects a <see cref="DrawableHitObject"/> without performing capture updates.
/// </summary>
/// <param name="hitObject">The <see cref="DrawableHitObject"/> to select.</param>
/// <returns>Whether <paramref name="hitObject"/> was selected.</returns>
private bool select(DrawableHitObject hitObject)
{
if (!selectedHitObjects.Add(hitObject))
return false;
ObjectSelected?.Invoke(hitObject);
return true;
}
/// <summary>
/// Deselects a <see cref="DrawableHitObject"/>.
/// </summary>
/// <param name="hitObject">The <see cref="DrawableHitObject"/> to deselect.</param>
public void Deselect(DrawableHitObject hitObject)
{
if (!deselect(hitObject))
return;
clearCapture();
finishSelection();
}
/// <summary>
/// Deselects a <see cref="DrawableHitObject"/> without performing capture updates.
/// </summary>
/// <param name="hitObject">The <see cref="DrawableHitObject"/> to deselect.</param>
/// <returns>Whether the <see cref="DrawableHitObject"/> was deselected.</returns>
private bool deselect(DrawableHitObject hitObject)
{
if (!selectedHitObjects.Remove(hitObject))
return false;
ObjectDeselected?.Invoke(hitObject);
return true;
}
/// <summary>
/// Deselects all selected <see cref="DrawableHitObject"/>s.
/// </summary>
public void DeselectAll()
{
selectedHitObjects.ForEach(h => ObjectDeselected?.Invoke(h));
selectedHitObjects.Clear();
captureBox?.Hide();
captureBox?.Expire();
clearCapture();
}
/// <summary>
@ -92,8 +150,7 @@ namespace osu.Game.Rulesets.Edit.Layers.Selection
/// <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)))
selectedHitObjects.Add(obj);
playfield.HitObjects.Objects.Where(h => h.IsAlive && h.IsPresent && screenSpaceQuad.Contains(h.SelectionPoint)).ForEach(h => select(h));
}
/// <summary>
@ -102,11 +159,17 @@ namespace osu.Game.Rulesets.Edit.Layers.Selection
/// <param name="screenSpacePoint">The <see cref="Vector2"/> to select at.</param>
private void selectPoint(Vector2 screenSpacePoint)
{
var selected = playfield.HitObjects.Objects.Reverse().Where(h => h.IsAlive && h.IsPresent).FirstOrDefault(h => h.ReceiveMouseInputAt(screenSpacePoint));
if (selected == null)
var target = playfield.HitObjects.Objects.Reverse().Where(h => h.IsAlive && h.IsPresent).FirstOrDefault(h => h.ReceiveMouseInputAt(screenSpacePoint));
if (target == null)
return;
selectedHitObjects.Add(selected);
select(target);
}
private void clearCapture()
{
captureBox?.Hide();
captureBox?.Expire();
}
private void finishSelection()