1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-15 07:22:55 +08:00

Merge MaskContainer into BlueprintContainer

This commit is contained in:
smoogipoo 2018-11-06 17:51:26 +09:00
parent 26c9390c16
commit 65bb91dcf7
2 changed files with 85 additions and 145 deletions

View File

@ -1,19 +1,26 @@
// 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.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Input.Events;
using osu.Framework.Input.States;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Objects.Drawables;
using OpenTK;
namespace osu.Game.Screens.Edit.Screens.Compose.Layers
{
public class BlueprintContainer : CompositeDrawable
{
private MaskContainer maskContainer;
private SelectionBlueprintContainer selectionBlueprints;
private MaskSelection maskSelection;
private IEnumerable<SelectionMask> aliveMasks => selectionBlueprints.Children.Where(c => c.IsAlive);
[Resolved]
private HitObjectComposer composer { get; set; }
@ -26,25 +33,17 @@ namespace osu.Game.Screens.Edit.Screens.Compose.Layers
[BackgroundDependencyLoader]
private void load()
{
maskContainer = new MaskContainer();
maskSelection = composer.CreateMaskSelection();
maskSelection.DeselectAll = deselectAll;
var maskSelection = composer.CreateMaskSelection();
maskContainer.MaskSelected += maskSelection.HandleSelected;
maskContainer.MaskDeselected += maskSelection.HandleDeselected;
maskContainer.MaskSelectionRequested += maskSelection.HandleSelectionRequested;
maskContainer.MaskDragRequested += maskSelection.HandleDrag;
maskSelection.DeselectAll = maskContainer.DeselectAll;
var dragBox = new DragBox(maskContainer.Select);
var dragBox = new DragBox(select);
dragBox.DragEnd += () => maskSelection.UpdateVisibility();
InternalChildren = new[]
{
dragBox,
maskSelection,
maskContainer,
selectionBlueprints = new SelectionBlueprintContainer { RelativeSizeAxes = Axes.Both },
dragBox.CreateProxy()
};
@ -54,7 +53,7 @@ namespace osu.Game.Screens.Edit.Screens.Compose.Layers
protected override bool OnClick(ClickEvent e)
{
maskContainer.DeselectAll();
deselectAll();
return true;
}
@ -68,7 +67,12 @@ namespace osu.Game.Screens.Edit.Screens.Compose.Layers
if (mask == null)
return;
maskContainer.Add(mask);
mask.Selected += onMaskSelected;
mask.Deselected += onMaskDeselected;
mask.SelectionRequested += onSelectionRequested;
mask.DragRequested += onDragRequested;
selectionBlueprints.Add(mask);
}
/// <summary>
@ -77,12 +81,76 @@ namespace osu.Game.Screens.Edit.Screens.Compose.Layers
/// <param name="hitObject">The <see cref="DrawableHitObject"/> for which to remove the mask.</param>
public void RemoveMaskFor(DrawableHitObject hitObject)
{
var maskToRemove = maskContainer.Single(m => m.HitObject == hitObject);
var maskToRemove = selectionBlueprints.Single(m => m.HitObject == hitObject);
if (maskToRemove == null)
return;
maskToRemove.Deselect();
maskContainer.Remove(maskToRemove);
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)
{
maskSelection.HandleSelected(mask);
selectionBlueprints.ChangeChildDepth(mask, 1);
}
private void onMaskDeselected(SelectionMask mask)
{
maskSelection.HandleDeselected(mask);
selectionBlueprints.ChangeChildDepth(mask, 0);
}
private void onSelectionRequested(SelectionMask mask, InputState state) => maskSelection.HandleSelectionRequested(mask, state);
private void onDragRequested(SelectionMask mask, Vector2 delta, InputState state) => maskSelection.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;
}
}
}
}

View File

@ -1,128 +0,0 @@
// 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.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.States;
using osu.Game.Rulesets.Edit;
using OpenTK;
using RectangleF = osu.Framework.Graphics.Primitives.RectangleF;
namespace osu.Game.Screens.Edit.Screens.Compose.Layers
{
public class MaskContainer : Container<SelectionMask>
{
/// <summary>
/// Invoked when any <see cref="SelectionMask"/> is selected.
/// </summary>
public event Action<SelectionMask> MaskSelected;
/// <summary>
/// Invoked when any <see cref="SelectionMask"/> is deselected.
/// </summary>
public event Action<SelectionMask> MaskDeselected;
/// <summary>
/// Invoked when any <see cref="SelectionMask"/> requests selection.
/// </summary>
public event Action<SelectionMask, InputState> MaskSelectionRequested;
/// <summary>
/// Invoked when any <see cref="SelectionMask"/> requests drag.
/// </summary>
public event Action<SelectionMask, Vector2, InputState> MaskDragRequested;
private IEnumerable<SelectionMask> aliveMasks => AliveInternalChildren.Cast<SelectionMask>();
public MaskContainer()
{
RelativeSizeAxes = Axes.Both;
}
public override void Add(SelectionMask drawable)
{
if (drawable == null) throw new ArgumentNullException(nameof(drawable));
base.Add(drawable);
drawable.Selected += onMaskSelected;
drawable.Deselected += onMaskDeselected;
drawable.SelectionRequested += onSelectionRequested;
drawable.DragRequested += onDragRequested;
}
public override bool Remove(SelectionMask drawable)
{
if (drawable == null) throw new ArgumentNullException(nameof(drawable));
var result = base.Remove(drawable);
if (result)
{
drawable.Selected -= onMaskSelected;
drawable.Deselected -= onMaskDeselected;
drawable.SelectionRequested -= onSelectionRequested;
drawable.DragRequested -= onDragRequested;
}
return result;
}
/// <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>
public 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>
public void DeselectAll() => aliveMasks.ToList().ForEach(m => m.Deselect());
private void onMaskSelected(SelectionMask mask)
{
MaskSelected?.Invoke(mask);
ChangeChildDepth(mask, 1);
}
private void onMaskDeselected(SelectionMask mask)
{
MaskDeselected?.Invoke(mask);
ChangeChildDepth(mask, 0);
}
private void onSelectionRequested(SelectionMask mask, InputState state) => MaskSelectionRequested?.Invoke(mask, state);
private void onDragRequested(SelectionMask mask, Vector2 delta, InputState state) => MaskDragRequested?.Invoke(mask, delta, state);
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;
}
}
}