1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 06:07:28 +08:00
osu-lazer/osu.Game/Screens/Edit/Screens/Compose/Layers/BlueprintContainer.cs

157 lines
5.5 KiB
C#
Raw Normal View History

2018-04-13 17:19:50 +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;
2018-10-18 15:36:06 +08:00
using System.Linq;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
using osu.Framework.Input.States;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Objects.Drawables;
using OpenTK;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens.Edit.Screens.Compose.Layers
{
public class BlueprintContainer : CompositeDrawable
2018-04-13 17:19:50 +08:00
{
private SelectionBlueprintContainer selectionBlueprints;
2018-11-06 16:52:47 +08:00
private SelectionBox selectionBox;
private IEnumerable<SelectionMask> aliveMasks => selectionBlueprints.Children.Where(c => c.IsAlive);
2018-04-13 17:19:50 +08:00
2018-10-17 15:17:12 +08:00
[Resolved]
private HitObjectComposer composer { get; set; }
2018-10-17 15:17:12 +08:00
public BlueprintContainer()
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.Both;
}
[BackgroundDependencyLoader]
private void load()
2018-04-13 17:19:50 +08:00
{
2018-11-06 16:52:47 +08:00
selectionBox = composer.CreateMaskSelection();
selectionBox.DeselectAll = deselectAll;
2018-04-13 17:19:50 +08:00
var dragBox = new DragBox(select);
2018-11-06 16:52:47 +08:00
dragBox.DragEnd += () => selectionBox.UpdateVisibility();
2018-04-13 17:19:50 +08:00
2018-04-20 17:19:17 +08:00
InternalChildren = new[]
2018-04-13 17:19:50 +08:00
{
2018-11-06 16:24:38 +08:00
dragBox,
2018-11-06 16:52:47 +08:00
selectionBox,
selectionBlueprints = new SelectionBlueprintContainer { RelativeSizeAxes = Axes.Both },
2018-11-06 16:24:38 +08:00
dragBox.CreateProxy()
2018-04-13 17:19:50 +08:00
};
foreach (var obj in composer.HitObjects)
AddMaskFor(obj);
2018-04-13 17:19:50 +08:00
}
protected override bool OnClick(ClickEvent e)
2018-04-13 17:19:50 +08:00
{
deselectAll();
2018-04-13 17:19:50 +08:00
return true;
}
/// <summary>
/// Adds a mask for a <see cref="DrawableHitObject"/> which adds movement support.
/// </summary>
/// <param name="hitObject">The <see cref="DrawableHitObject"/> to create a mask for.</param>
public void AddMaskFor(DrawableHitObject hitObject)
2018-04-13 17:19:50 +08:00
{
var mask = composer.CreateMaskFor(hitObject);
if (mask == null)
return;
mask.Selected += onMaskSelected;
mask.Deselected += onMaskDeselected;
mask.SelectionRequested += onSelectionRequested;
mask.DragRequested += onDragRequested;
selectionBlueprints.Add(mask);
2018-04-13 17:19:50 +08:00
}
2018-10-18 15:36:06 +08:00
/// <summary>
/// Removes a mask for a <see cref="DrawableHitObject"/>.
/// </summary>
/// <param name="hitObject">The <see cref="DrawableHitObject"/> for which to remove the mask.</param>
public void RemoveMaskFor(DrawableHitObject hitObject)
{
var maskToRemove = selectionBlueprints.Single(m => m.HitObject == hitObject);
2018-10-18 15:36:06 +08:00
if (maskToRemove == null)
return;
maskToRemove.Deselect();
maskToRemove.Selected -= onMaskSelected;
maskToRemove.Deselected -= onMaskDeselected;
maskToRemove.SelectionRequested -= onSelectionRequested;
maskToRemove.DragRequested -= onDragRequested;
selectionBlueprints.Remove(maskToRemove);
}
/// <summary>
/// Select all masks in a given rectangle selection area.
/// </summary>
/// <param name="rect">The rectangle to perform a selection on in screen-space coordinates.</param>
private void select(RectangleF rect)
{
foreach (var mask in aliveMasks.ToList())
{
if (mask.IsPresent && rect.Contains(mask.SelectionPoint))
mask.Select();
else
mask.Deselect();
}
}
/// <summary>
/// Deselects all selected <see cref="SelectionMask"/>s.
/// </summary>
private void deselectAll() => aliveMasks.ToList().ForEach(m => m.Deselect());
private void onMaskSelected(SelectionMask mask)
{
2018-11-06 16:52:47 +08:00
selectionBox.HandleSelected(mask);
selectionBlueprints.ChangeChildDepth(mask, 1);
}
private void onMaskDeselected(SelectionMask mask)
{
2018-11-06 16:52:47 +08:00
selectionBox.HandleDeselected(mask);
selectionBlueprints.ChangeChildDepth(mask, 0);
}
2018-11-06 16:52:47 +08:00
private void onSelectionRequested(SelectionMask mask, InputState state) => selectionBox.HandleSelectionRequested(mask, state);
2018-11-06 16:52:47 +08:00
private void onDragRequested(SelectionMask mask, Vector2 delta, InputState state) => selectionBox.HandleDrag(mask, delta, state);
private class SelectionBlueprintContainer : Container<SelectionMask>
{
protected override int Compare(Drawable x, Drawable y)
{
if (!(x is SelectionMask xMask) || !(y is SelectionMask yMask))
return base.Compare(x, y);
return Compare(xMask, yMask);
}
public int Compare(SelectionMask x, SelectionMask y)
{
// dpeth is used to denote selected status (we always want selected masks to handle input first).
int d = x.Depth.CompareTo(y.Depth);
if (d != 0)
return d;
// Put earlier hitobjects towards the end of the list, so they handle input first
int i = y.HitObject.HitObject.StartTime.CompareTo(x.HitObject.HitObject.StartTime);
return i == 0 ? CompareReverseChildID(x, y) : i;
}
2018-10-18 15:36:06 +08:00
}
2018-04-13 17:19:50 +08:00
}
}