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

Cleanup and basic TestCase

This commit is contained in:
Dean Herbert 2017-12-13 19:56:16 +09:00
parent b9298325a3
commit 1b85952441
10 changed files with 193 additions and 42 deletions

View File

@ -0,0 +1,132 @@
// Copyright (c) 2007-2017 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.IO;
using System.Linq;
using System.Text;
using osu.Framework.Allocation;
using osu.Framework.Extensions;
using osu.Framework.Graphics;
using osu.Framework.MathUtils;
using osu.Game.Beatmaps;
using osu.Game.Screens.Select;
using osu.Game.Screens.Select.Carousel;
namespace osu.Game.Tests.Visual
{
internal class TestCaseBeatmapCarousel : OsuTestCase
{
private BeatmapCarousel carousel;
public override IReadOnlyList<Type> RequiredTypes => new[]
{
typeof(CarouselItem),
typeof(CarouselGroup),
typeof(CarouselGroupEagerSelect),
typeof(CarouselBeatmap),
typeof(CarouselBeatmapSet),
typeof(DrawableCarouselItem),
typeof(CarouselItemState),
typeof(DrawableCarouselBeatmap),
typeof(DrawableCarouselBeatmapSet),
};
[BackgroundDependencyLoader]
private void load()
{
Add(carousel = new BeatmapCarousel
{
RelativeSizeAxes = Axes.Both,
});
AddStep("Load Beatmaps", () =>
{
carousel.Beatmaps = new[]
{
createTestBeatmapSet(0),
createTestBeatmapSet(1),
createTestBeatmapSet(2),
createTestBeatmapSet(3),
};
});
AddUntilStep(() => carousel.Beatmaps.Any(), "Wait for load");
AddStep("SelectNext set", () => carousel.SelectNext());
AddAssert("set1 diff1", () => carousel.SelectedBeatmap == carousel.Beatmaps.First().Beatmaps.First());
AddStep("SelectNext diff", () => carousel.SelectNext(1, false));
AddAssert("set1 diff2", () => carousel.SelectedBeatmap == carousel.Beatmaps.First().Beatmaps.Skip(1).First());
AddStep("SelectNext backwards", () => carousel.SelectNext(-1));
AddAssert("set4 diff1", () => carousel.SelectedBeatmap == carousel.Beatmaps.Last().Beatmaps.First());
AddStep("SelectNext diff backwards", () => carousel.SelectNext(-1, false));
AddAssert("set3 diff3", () => carousel.SelectedBeatmap == carousel.Beatmaps.Reverse().Skip(1).First().Beatmaps.Last());
AddStep("SelectNext", () => carousel.SelectNext());
AddStep("SelectNext", () => carousel.SelectNext());
AddAssert("set1 diff1", () => carousel.SelectedBeatmap == carousel.Beatmaps.First().Beatmaps.First());
AddStep("SelectNext diff backwards", () => carousel.SelectNext(-1, false));
AddAssert("set4 diff3", () => carousel.SelectedBeatmap == carousel.Beatmaps.Last().Beatmaps.Last());
// AddStep("Clear beatmaps", () => carousel.Beatmaps = new BeatmapSetInfo[] { });
// AddStep("SelectNext (noop)", () => carousel.SelectNext());
}
private BeatmapSetInfo createTestBeatmapSet(int i)
{
return new BeatmapSetInfo
{
OnlineBeatmapSetID = 1234 + i,
Hash = new MemoryStream(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString())).ComputeMD5Hash(),
Metadata = new BeatmapMetadata
{
OnlineBeatmapSetID = 1234 + i,
// Create random metadata, then we can check if sorting works based on these
Artist = "MONACA " + RNG.Next(0, 9),
Title = "Black Song " + RNG.Next(0, 9),
AuthorString = "Some Guy " + RNG.Next(0, 9),
},
Beatmaps = new List<BeatmapInfo>(new[]
{
new BeatmapInfo
{
OnlineBeatmapID = 1234 + i,
Path = "normal.osu",
Version = "Normal",
BaseDifficulty = new BeatmapDifficulty
{
OverallDifficulty = 3.5f,
}
},
new BeatmapInfo
{
OnlineBeatmapID = 1235 + i,
Path = "hard.osu",
Version = "Hard",
BaseDifficulty = new BeatmapDifficulty
{
OverallDifficulty = 5,
}
},
new BeatmapInfo
{
OnlineBeatmapID = 1236 + i,
Path = "insane.osu",
Version = "Insane",
BaseDifficulty = new BeatmapDifficulty
{
OverallDifficulty = 7,
}
},
}),
};
}
}
}

View File

@ -13,6 +13,7 @@ using osu.Game.Beatmaps;
using osu.Game.Database; using osu.Game.Database;
using osu.Game.Rulesets; using osu.Game.Rulesets;
using osu.Game.Screens.Select; using osu.Game.Screens.Select;
using osu.Game.Screens.Select.Carousel;
using osu.Game.Screens.Select.Filter; using osu.Game.Screens.Select.Filter;
using osu.Game.Tests.Platform; using osu.Game.Tests.Platform;
@ -28,8 +29,20 @@ namespace osu.Game.Tests.Visual
public override IReadOnlyList<Type> RequiredTypes => new[] public override IReadOnlyList<Type> RequiredTypes => new[]
{ {
typeof(BeatmapCarousel),
typeof(SongSelect), typeof(SongSelect),
typeof(BeatmapCarousel),
typeof(CarouselItem),
typeof(CarouselGroup),
typeof(CarouselGroupEagerSelect),
typeof(CarouselBeatmap),
typeof(CarouselBeatmapSet),
typeof(DrawableCarouselItem),
typeof(CarouselItemState),
typeof(DrawableCarouselBeatmap),
typeof(DrawableCarouselBeatmapSet),
}; };
protected override IReadOnlyDependencyContainer CreateLocalDependencies(IReadOnlyDependencyContainer parent) => dependencies = new DependencyContainer(parent); protected override IReadOnlyDependencyContainer CreateLocalDependencies(IReadOnlyDependencyContainer parent) => dependencies = new DependencyContainer(parent);

View File

@ -88,6 +88,7 @@
<Compile Include="Beatmaps\IO\ImportBeatmapTest.cs" /> <Compile Include="Beatmaps\IO\ImportBeatmapTest.cs" />
<Compile Include="Resources\Resource.cs" /> <Compile Include="Resources\Resource.cs" />
<Compile Include="Beatmaps\Formats\LegacyBeatmapDecoderTest.cs" /> <Compile Include="Beatmaps\Formats\LegacyBeatmapDecoderTest.cs" />
<Compile Include="Visual\TestCaseBeatmapCarousel.cs" />
<Compile Include="Visual\TestCaseBeatmapDetailArea.cs" /> <Compile Include="Visual\TestCaseBeatmapDetailArea.cs" />
<Compile Include="Visual\TestCaseBeatmapDetails.cs" /> <Compile Include="Visual\TestCaseBeatmapDetails.cs" />
<Compile Include="Visual\TestCaseBeatmapInfoWedge.cs" /> <Compile Include="Visual\TestCaseBeatmapInfoWedge.cs" />

View File

@ -26,20 +26,35 @@ namespace osu.Game.Screens.Select
{ {
public class BeatmapCarousel : OsuScrollContainer public class BeatmapCarousel : OsuScrollContainer
{ {
/// <summary>
/// Triggered when the <see cref="Beatmaps"/> loaded change and are completely loaded.
/// </summary>
public Action BeatmapsChanged;
/// <summary>
/// The currently selected beatmap.
/// </summary>
public BeatmapInfo SelectedBeatmap => selectedBeatmap?.Beatmap; public BeatmapInfo SelectedBeatmap => selectedBeatmap?.Beatmap;
public override bool HandleInput => AllowSelection; /// <summary>
/// Raised when the <see cref="SelectedBeatmap"/> is changed.
/// </summary>
public Action<BeatmapInfo> SelectionChanged;
public Action BeatmapsChanged; public override bool HandleInput => AllowSelection;
public IEnumerable<BeatmapSetInfo> Beatmaps public IEnumerable<BeatmapSetInfo> Beatmaps
{ {
get { return carouselSets.Select(g => g.BeatmapSet); } get { return carouselSets.Select(g => g.BeatmapSet); }
set set
{ {
scrollableContent.Clear(false); Schedule(() =>
items.Clear(); {
carouselSets.Clear(); scrollableContent.Clear(false);
items.Clear();
carouselSets.Clear();
yPositionsCache.Invalidate();
});
List<CarouselBeatmapSet> newSets = null; List<CarouselBeatmapSet> newSets = null;
@ -51,8 +66,7 @@ namespace osu.Game.Screens.Select
{ {
Schedule(() => Schedule(() =>
{ {
foreach (var g in newSets) carouselSets.AddRange(newSets);
addBeatmapSet(g);
root = new CarouselGroup(newSets.OfType<CarouselItem>().ToList()); root = new CarouselGroup(newSets.OfType<CarouselItem>().ToList());
items = root.Drawables.Value.ToList(); items = root.Drawables.Value.ToList();
@ -94,7 +108,7 @@ namespace osu.Game.Screens.Select
}); });
} }
public void RemoveBeatmap(BeatmapSetInfo beatmapSet) public void RemoveBeatmapSet(BeatmapSetInfo beatmapSet)
{ {
Schedule(() => removeBeatmapSet(carouselSets.Find(b => b.BeatmapSet.ID == beatmapSet.ID))); Schedule(() => removeBeatmapSet(carouselSets.Find(b => b.BeatmapSet.ID == beatmapSet.ID)));
} }
@ -116,8 +130,8 @@ namespace osu.Game.Screens.Select
{ {
if (index >= 0) if (index >= 0)
carouselSets.Insert(index, newSet); carouselSets.Insert(index, newSet);
else //else
addBeatmapSet(newSet); // addBeatmapSet(newSet);
} }
if (hadSelection && newSet == null) if (hadSelection && newSet == null)
@ -158,8 +172,6 @@ namespace osu.Game.Screens.Select
} }
} }
public Action<BeatmapInfo> SelectionChanged;
private void selectNullBeatmap() private void selectNullBeatmap()
{ {
selectedBeatmap = null; selectedBeatmap = null;
@ -180,7 +192,7 @@ namespace osu.Game.Screens.Select
return; return;
} }
int originalIndex = Math.Max(0, items.IndexOf(selectedBeatmap?.Drawables.Value.First())); int originalIndex = items.IndexOf(selectedBeatmap?.Drawables.Value.First());
int currentIndex = originalIndex; int currentIndex = originalIndex;
// local function to increment the index in the required direction, wrapping over extremities. // local function to increment the index in the required direction, wrapping over extremities.
@ -338,17 +350,6 @@ namespace osu.Game.Screens.Select
[BackgroundDependencyLoader(permitNulls: true)] [BackgroundDependencyLoader(permitNulls: true)]
private void load(OsuConfigManager config) private void load(OsuConfigManager config)
{ {
}
private void addBeatmapSet(CarouselBeatmapSet set)
{
// prevent duplicates by concurrent independent actions trying to add a group
//todo: check this
if (carouselSets.Any(g => g.BeatmapSet.ID == set.BeatmapSet.ID))
return;
//todo: add to root
carouselSets.Add(set);
randomSelectAlgorithm = config.GetBindable<RandomSelectAlgorithm>(OsuSetting.RandomSelectAlgorithm); randomSelectAlgorithm = config.GetBindable<RandomSelectAlgorithm>(OsuSetting.RandomSelectAlgorithm);
} }
@ -422,7 +423,6 @@ namespace osu.Game.Screens.Select
private void select(CarouselItem item) private void select(CarouselItem item)
{ {
if (item == null) return; if (item == null) return;
item.State.Value = CarouselItemState.Selected; item.State.Value = CarouselItemState.Selected;
} }
@ -466,7 +466,7 @@ namespace osu.Game.Screens.Select
computeYPositions(); computeYPositions();
// Remove all items that should no longer be on-screen // Remove all items that should no longer be on-screen
scrollableContent.RemoveAll(delegate (DrawableCarouselItem p) scrollableContent.RemoveAll(delegate(DrawableCarouselItem p)
{ {
float itemPosY = p.Position.Y; float itemPosY = p.Position.Y;
bool remove = itemPosY < Current - p.DrawHeight || itemPosY > Current + drawHeight || !p.IsPresent; bool remove = itemPosY < Current - p.DrawHeight || itemPosY > Current + drawHeight || !p.IsPresent;

View File

@ -52,11 +52,4 @@ namespace osu.Game.Screens.Select.Carousel
}*/ }*/
} }
} }
public enum CarouselItemState
{
Hidden,
NotSelected,
Selected,
}
} }

View File

@ -54,4 +54,11 @@ namespace osu.Game.Screens.Select.Carousel
public virtual void Filter(FilterCriteria criteria) => Children?.ForEach(c => c.Filter(criteria)); public virtual void Filter(FilterCriteria criteria) => Children?.ForEach(c => c.Filter(criteria));
} }
public enum CarouselItemState
{
Hidden,
NotSelected,
Selected,
}
} }

View File

@ -35,12 +35,17 @@ namespace osu.Game.Screens.Select.Carousel
private readonly Triangles triangles; private readonly Triangles triangles;
private readonly StarCounter starCounter; private readonly StarCounter starCounter;
[BackgroundDependencyLoader] [BackgroundDependencyLoader(true)]
private void load(SongSelect songSelect, BeatmapManager manager) private void load(SongSelect songSelect, BeatmapManager manager)
{ {
StartRequested = songSelect.Start; if (songSelect != null)
EditRequested = songSelect.Edit; {
HideRequested = manager.Hide; StartRequested = songSelect.Start;
EditRequested = songSelect.Edit;
}
if (manager != null)
HideRequested = manager.Hide;
} }
public DrawableCarouselBeatmap(CarouselBeatmap panel) public DrawableCarouselBeatmap(CarouselBeatmap panel)

View File

@ -46,12 +46,10 @@ namespace osu.Game.Screens.Select.Carousel
RestoreHiddenRequested = s => s.Beatmaps.ForEach(manager.Restore); RestoreHiddenRequested = s => s.Beatmaps.ForEach(manager.Restore);
DeleteRequested = manager.Delete; DeleteRequested = manager.Delete;
var working = manager.GetWorkingBeatmap(beatmapSet.Beatmaps.FirstOrDefault());
Children = new Drawable[] Children = new Drawable[]
{ {
new DelayedLoadWrapper( new DelayedLoadWrapper(
new PanelBackground(working) new PanelBackground(manager.GetWorkingBeatmap(beatmapSet.Beatmaps.FirstOrDefault()))
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
OnLoadComplete = d => d.FadeInFromZero(400, Easing.Out), OnLoadComplete = d => d.FadeInFromZero(400, Easing.Out),

View File

@ -42,6 +42,8 @@ namespace osu.Game.Screens.Select.Carousel
Height = MAX_HEIGHT; Height = MAX_HEIGHT;
RelativeSizeAxes = Axes.X; RelativeSizeAxes = Axes.X;
Alpha = 0;
AddInternal(borderContainer = new Container AddInternal(borderContainer = new Container
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,

View File

@ -454,7 +454,7 @@ namespace osu.Game.Screens.Select
private void removeBeatmapSet(BeatmapSetInfo beatmapSet) private void removeBeatmapSet(BeatmapSetInfo beatmapSet)
{ {
carousel.RemoveBeatmap(beatmapSet); carousel.RemoveBeatmapSet(beatmapSet);
if (carousel.SelectedBeatmap == null) if (carousel.SelectedBeatmap == null)
Beatmap.SetDefault(); Beatmap.SetDefault();
} }