mirror of
https://github.com/ppy/osu.git
synced 2025-01-28 14:12:55 +08:00
Add test and fix several issues
This commit is contained in:
parent
648a9d5258
commit
024adb699c
@ -0,0 +1,115 @@
|
||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
using System.Linq;
|
||||
using NUnit.Framework;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Shapes;
|
||||
using osu.Framework.Utils;
|
||||
using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate;
|
||||
using osuTK.Graphics;
|
||||
using osuTK.Input;
|
||||
|
||||
namespace osu.Game.Tests.Visual.Multiplayer
|
||||
{
|
||||
public class TestSceneMultiplayerSpectatorPlayerGrid : OsuManualInputManagerTestScene
|
||||
{
|
||||
private PlayerGrid grid;
|
||||
|
||||
[SetUp]
|
||||
public void Setup() => Schedule(() =>
|
||||
{
|
||||
Child = grid = new PlayerGrid { RelativeSizeAxes = Axes.Both };
|
||||
});
|
||||
|
||||
[Test]
|
||||
public void TestMaximiseAndMinimise()
|
||||
{
|
||||
addCells(2);
|
||||
|
||||
assertMaximisation(0, false, true);
|
||||
assertMaximisation(1, false, true);
|
||||
|
||||
clickCell(0);
|
||||
assertMaximisation(0, true);
|
||||
assertMaximisation(1, false, true);
|
||||
clickCell(0);
|
||||
assertMaximisation(0, false);
|
||||
assertMaximisation(1, false, true);
|
||||
|
||||
clickCell(1);
|
||||
assertMaximisation(1, true);
|
||||
assertMaximisation(0, false, true);
|
||||
clickCell(1);
|
||||
assertMaximisation(1, false);
|
||||
assertMaximisation(0, false, true);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestClickBothCellsSimultaneously()
|
||||
{
|
||||
addCells(2);
|
||||
|
||||
AddStep("click cell 0 then 1", () =>
|
||||
{
|
||||
InputManager.MoveMouseTo(grid.Content.ElementAt(0));
|
||||
InputManager.Click(MouseButton.Left);
|
||||
|
||||
InputManager.MoveMouseTo(grid.Content.ElementAt(1));
|
||||
InputManager.Click(MouseButton.Left);
|
||||
});
|
||||
|
||||
assertMaximisation(1, true);
|
||||
assertMaximisation(0, false);
|
||||
}
|
||||
|
||||
[TestCase(1)]
|
||||
[TestCase(2)]
|
||||
[TestCase(3)]
|
||||
[TestCase(4)]
|
||||
[TestCase(5)]
|
||||
[TestCase(9)]
|
||||
[TestCase(11)]
|
||||
[TestCase(12)]
|
||||
[TestCase(15)]
|
||||
[TestCase(16)]
|
||||
public void TestCellCount(int count)
|
||||
{
|
||||
addCells(count);
|
||||
AddWaitStep("wait for display", 2);
|
||||
}
|
||||
|
||||
private void addCells(int count) => AddStep($"add {count} grid cells", () =>
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
grid.Add(new GridContent());
|
||||
});
|
||||
|
||||
private void clickCell(int index) => AddStep($"click cell index {index}", () =>
|
||||
{
|
||||
InputManager.MoveMouseTo(grid.Content.ElementAt(index));
|
||||
InputManager.Click(MouseButton.Left);
|
||||
});
|
||||
|
||||
private void assertMaximisation(int index, bool shouldBeMaximised, bool instant = false)
|
||||
{
|
||||
string assertionText = $"cell index {index} {(shouldBeMaximised ? "is" : "is not")} maximised";
|
||||
|
||||
if (instant)
|
||||
AddAssert(assertionText, checkAction);
|
||||
else
|
||||
AddUntilStep(assertionText, checkAction);
|
||||
|
||||
bool checkAction() => Precision.AlmostEquals(grid.MaximisedFacade.DrawSize, grid.Content.ElementAt(index).DrawSize, 10) == shouldBeMaximised;
|
||||
}
|
||||
|
||||
private class GridContent : Box
|
||||
{
|
||||
public GridContent()
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both;
|
||||
Colour = new Color4(RNG.NextSingle(), RNG.NextSingle(), RNG.NextSingle(), 1f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -13,9 +13,9 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate
|
||||
|
||||
public Drawable MaximisedFacade => maximisedFacade;
|
||||
|
||||
private readonly PlayerGridFacade maximisedFacade;
|
||||
private readonly Facade maximisedFacade;
|
||||
private readonly Container paddingContainer;
|
||||
private readonly FillFlowContainer<PlayerGridFacade> facadeContainer;
|
||||
private readonly FillFlowContainer<Facade> facadeContainer;
|
||||
private readonly Container<Cell> cellContainer;
|
||||
|
||||
public PlayerGrid()
|
||||
@ -31,38 +31,50 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate
|
||||
new Container
|
||||
{
|
||||
RelativeSizeAxes = Axes.Both,
|
||||
Child = facadeContainer = new FillFlowContainer<PlayerGridFacade>
|
||||
Child = facadeContainer = new FillFlowContainer<Facade>
|
||||
{
|
||||
RelativeSizeAxes = Axes.X,
|
||||
AutoSizeAxes = Axes.Y,
|
||||
Spacing = new Vector2(player_spacing),
|
||||
}
|
||||
},
|
||||
maximisedFacade = new PlayerGridFacade { RelativeSizeAxes = Axes.Both }
|
||||
maximisedFacade = new Facade { RelativeSizeAxes = Axes.Both }
|
||||
}
|
||||
},
|
||||
cellContainer = new Container<Cell> { RelativeSizeAxes = Axes.Both }
|
||||
};
|
||||
}
|
||||
|
||||
public void AddContent(Drawable content)
|
||||
/// <summary>
|
||||
/// Adds a new cell with content to this grid.
|
||||
/// </summary>
|
||||
/// <param name="content">The content the cell should contain.</param>
|
||||
/// <exception cref="InvalidOperationException">If more than 16 cells are added.</exception>
|
||||
public void Add(Drawable content)
|
||||
{
|
||||
var facade = new PlayerGridFacade();
|
||||
int index = cellContainer.Count;
|
||||
|
||||
var facade = new Facade();
|
||||
facadeContainer.Add(facade);
|
||||
|
||||
var cell = new Cell(content) { ToggleMaximisationState = toggleMaximisationState };
|
||||
var cell = new Cell(index, content) { ToggleMaximisationState = toggleMaximisationState };
|
||||
cell.SetFacade(facade);
|
||||
|
||||
cellContainer.Add(cell);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The content added to this grid.
|
||||
/// </summary>
|
||||
public IEnumerable<Drawable> Content => cellContainer.OrderBy(c => c.FacadeIndex).Select(c => c.Content);
|
||||
|
||||
// A depth value that gets decremented every time a new instance is maximised in order to reduce underlaps.
|
||||
private float maximisedInstanceDepth;
|
||||
|
||||
private void toggleMaximisationState(Cell target)
|
||||
{
|
||||
// Iterate through all cells to ensure only one is maximised at any time.
|
||||
foreach (var i in cellContainer)
|
||||
foreach (var i in cellContainer.ToList())
|
||||
{
|
||||
if (i == target)
|
||||
i.IsMaximised = !i.IsMaximised;
|
||||
@ -78,7 +90,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate
|
||||
else
|
||||
{
|
||||
// Transfer cell back to its original facade.
|
||||
i.SetFacade(facadeContainer[cellContainer.IndexOf(target)]);
|
||||
i.SetFacade(facadeContainer[i.FacadeIndex]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,17 +14,28 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate
|
||||
{
|
||||
private class Cell : CompositeDrawable
|
||||
{
|
||||
/// <summary>
|
||||
/// The index of the original facade of this cell.
|
||||
/// </summary>
|
||||
public readonly int FacadeIndex;
|
||||
|
||||
/// <summary>
|
||||
/// The contained content.
|
||||
/// </summary>
|
||||
public readonly Drawable Content;
|
||||
|
||||
public Action<Cell> ToggleMaximisationState;
|
||||
public bool IsMaximised;
|
||||
|
||||
private PlayerGridFacade facade;
|
||||
private Facade facade;
|
||||
private bool isTracking = true;
|
||||
|
||||
public Cell(Drawable content)
|
||||
public Cell(int facadeIndex, Drawable content)
|
||||
{
|
||||
Origin = Anchor.Centre;
|
||||
FacadeIndex = facadeIndex;
|
||||
|
||||
InternalChild = content;
|
||||
Origin = Anchor.Centre;
|
||||
InternalChild = Content = content;
|
||||
}
|
||||
|
||||
protected override void Update()
|
||||
@ -38,9 +49,12 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate
|
||||
}
|
||||
}
|
||||
|
||||
public void SetFacade([NotNull] PlayerGridFacade newFacade)
|
||||
/// <summary>
|
||||
/// Makes this cell track a new facade.
|
||||
/// </summary>
|
||||
public void SetFacade([NotNull] Facade newFacade)
|
||||
{
|
||||
PlayerGridFacade lastFacade = facade;
|
||||
Facade lastFacade = facade;
|
||||
facade = newFacade;
|
||||
|
||||
if (lastFacade == null || lastFacade == newFacade)
|
||||
|
@ -7,9 +7,12 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate
|
||||
{
|
||||
public partial class PlayerGrid
|
||||
{
|
||||
private class PlayerGridFacade : Drawable
|
||||
/// <summary>
|
||||
/// A facade of the grid which is used as a dummy object to store the required position/size of cells.
|
||||
/// </summary>
|
||||
private class Facade : Drawable
|
||||
{
|
||||
public PlayerGridFacade()
|
||||
public Facade()
|
||||
{
|
||||
Anchor = Anchor.Centre;
|
||||
Origin = Anchor.Centre;
|
||||
|
Loading…
Reference in New Issue
Block a user