Skip to content

Commit

Permalink
Merge pull request #31652 from peppy/beatmap-carousel-v2-split-panels
Browse files Browse the repository at this point in the history
Split out beatmap and set panels in  beatmap carousel v2
  • Loading branch information
bdach authored Jan 24, 2025
2 parents 7845c5c + 092c7b4 commit 3c76397
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ protected void CreateCarousel()

protected void SortBy(FilterCriteria criteria) => AddStep($"sort by {criteria.Sort}", () => Carousel.Filter(criteria));

protected void WaitForDrawablePanels() => AddUntilStep("drawable panels loaded", () => Carousel.ChildrenOfType<BeatmapCarouselPanel>().Count(), () => Is.GreaterThan(0));
protected void WaitForDrawablePanels() => AddUntilStep("drawable panels loaded", () => Carousel.ChildrenOfType<ICarouselPanel>().Count(), () => Is.GreaterThan(0));
protected void WaitForSorting() => AddUntilStep("sorting finished", () => Carousel.IsFiltering, () => Is.False);
protected void WaitForScrolling() => AddUntilStep("scroll finished", () => Scroll.Current, () => Is.EqualTo(Scroll.Target));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ public void TestScrollPositionMaintainedOnAddSecondSelected()
WaitForDrawablePanels();

AddStep("select middle beatmap", () => Carousel.CurrentSelection = BeatmapSets.ElementAt(BeatmapSets.Count - 2));
AddStep("scroll to selected item", () => Scroll.ScrollTo(Scroll.ChildrenOfType<BeatmapCarouselPanel>().Single(p => p.Selected.Value)));
AddStep("scroll to selected item", () => Scroll.ScrollTo(Scroll.ChildrenOfType<BeatmapPanel>().Single(p => p.Selected.Value)));

WaitForScrolling();

AddStep("save selected screen position", () => positionBefore = Carousel.ChildrenOfType<BeatmapCarouselPanel>().FirstOrDefault(p => p.Selected.Value)!.ScreenSpaceDrawQuad);
AddStep("save selected screen position", () => positionBefore = Carousel.ChildrenOfType<BeatmapPanel>().FirstOrDefault(p => p.Selected.Value)!.ScreenSpaceDrawQuad);

RemoveFirstBeatmap();
WaitForSorting();

AddAssert("select screen position unchanged", () => Carousel.ChildrenOfType<BeatmapCarouselPanel>().Single(p => p.Selected.Value).ScreenSpaceDrawQuad,
AddAssert("select screen position unchanged", () => Carousel.ChildrenOfType<BeatmapPanel>().Single(p => p.Selected.Value).ScreenSpaceDrawQuad,
() => Is.EqualTo(positionBefore));
}

Expand All @@ -83,11 +83,11 @@ public void TestScrollPositionMaintainedOnAddLastSelected()

WaitForScrolling();

AddStep("save selected screen position", () => positionBefore = Carousel.ChildrenOfType<BeatmapCarouselPanel>().FirstOrDefault(p => p.Selected.Value)!.ScreenSpaceDrawQuad);
AddStep("save selected screen position", () => positionBefore = Carousel.ChildrenOfType<BeatmapPanel>().FirstOrDefault(p => p.Selected.Value)!.ScreenSpaceDrawQuad);

RemoveFirstBeatmap();
WaitForSorting();
AddAssert("select screen position unchanged", () => Carousel.ChildrenOfType<BeatmapCarouselPanel>().Single(p => p.Selected.Value).ScreenSpaceDrawQuad,
AddAssert("select screen position unchanged", () => Carousel.ChildrenOfType<BeatmapPanel>().Single(p => p.Selected.Value).ScreenSpaceDrawQuad,
() => Is.EqualTo(positionBefore));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void TestCarouselRemembersSelection()
AddUntilStep("drawable selection restored", () => getSelectedPanel()?.Item?.Model, () => Is.EqualTo(selection));
AddAssert("drawable selection matches carousel selection", () => selection, () => Is.EqualTo(Carousel.CurrentSelection));

BeatmapCarouselPanel? getSelectedPanel() => Carousel.ChildrenOfType<BeatmapCarouselPanel>().SingleOrDefault(p => p.Selected.Value);
BeatmapPanel? getSelectedPanel() => Carousel.ChildrenOfType<BeatmapPanel>().SingleOrDefault(p => p.Selected.Value);
}

[Test]
Expand Down
20 changes: 17 additions & 3 deletions osu.Game/Screens/SelectV2/BeatmapCarousel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,28 @@ public void Filter(FilterCriteria criteria)

#region Drawable pooling

private readonly DrawablePool<BeatmapCarouselPanel> carouselPanelPool = new DrawablePool<BeatmapCarouselPanel>(100);
private readonly DrawablePool<BeatmapPanel> beatmapPanelPool = new DrawablePool<BeatmapPanel>(100);
private readonly DrawablePool<BeatmapSetPanel> setPanelPool = new DrawablePool<BeatmapSetPanel>(100);

private void setupPools()
{
AddInternal(carouselPanelPool);
AddInternal(beatmapPanelPool);
AddInternal(setPanelPool);
}

protected override Drawable GetDrawableForDisplay(CarouselItem item) => carouselPanelPool.Get();
protected override Drawable GetDrawableForDisplay(CarouselItem item)
{
switch (item.Model)
{
case BeatmapInfo:
return beatmapPanelPool.Get();

case BeatmapSetInfo:
return setPanelPool.Get();
}

throw new InvalidOperationException();
}

#endregion
}
Expand Down
2 changes: 1 addition & 1 deletion osu.Game/Screens/SelectV2/BeatmapCarouselFilterGrouping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public async Task<IEnumerable<CarouselItem>> Run(IEnumerable<CarouselItem> items
{
newItems.Add(new CarouselItem(b.BeatmapSet!)
{
DrawHeight = 80,
DrawHeight = BeatmapSetPanel.HEIGHT,
IsGroupSelectionTarget = true
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,25 @@

namespace osu.Game.Screens.SelectV2
{
public partial class BeatmapCarouselPanel : PoolableDrawable, ICarouselPanel
public partial class BeatmapPanel : PoolableDrawable, ICarouselPanel
{
[Resolved]
private BeatmapCarousel carousel { get; set; } = null!;

private Box activationFlash = null!;
private Box background = null!;
private OsuSpriteText text = null!;

[BackgroundDependencyLoader]
private void load()
{
Size = new Vector2(500, CarouselItem.DEFAULT_HEIGHT);
Masking = true;

InternalChildren = new Drawable[]
{
background = new Box
new Box
{
Colour = Color4.Aqua.Darken(5),
Alpha = 0.8f,
RelativeSizeAxes = Axes.Both,
},
Expand Down Expand Up @@ -69,63 +72,40 @@ private void load()
});
}

protected override void FreeAfterUse()
{
base.FreeAfterUse();
Item = null;
Selected.Value = false;
KeyboardSelected.Value = false;
}

protected override void PrepareForUse()
{
base.PrepareForUse();

Debug.Assert(Item != null);
var beatmap = (BeatmapInfo)Item.Model;

DrawYPosition = Item.CarouselYPosition;

Size = new Vector2(500, Item.DrawHeight);
Masking = true;

background.Colour = (Item.Model is BeatmapInfo ? Color4.Aqua : Color4.Yellow).Darken(5);
text.Text = getTextFor(Item.Model);
text.Text = $"Difficulty: {beatmap.DifficultyName} ({beatmap.StarRating:N1}*)";

this.FadeInFromZero(500, Easing.OutQuint);
}

private string getTextFor(object item)
protected override bool OnClick(ClickEvent e)
{
switch (item)
if (carousel.CurrentSelection != Item!.Model)
{
case BeatmapInfo bi:
return $"Difficulty: {bi.DifficultyName} ({bi.StarRating:N1}*)";

case BeatmapSetInfo si:
return $"{si.Metadata}";
carousel.CurrentSelection = Item!.Model;
return true;
}

return "unknown";
}

protected override bool OnClick(ClickEvent e)
{
if (carousel.CurrentSelection == Item!.Model)
carousel.TryActivateSelection();
else
carousel.CurrentSelection = Item!.Model;
carousel.TryActivateSelection();
return true;
}

#region ICarouselPanel

public CarouselItem? Item { get; set; }
public BindableBool Selected { get; } = new BindableBool();
public BindableBool KeyboardSelected { get; } = new BindableBool();

public double DrawYPosition { get; set; }

public void Activated()
{
activationFlash.FadeOutFromOne(500, Easing.OutQuint);
}
public void Activated() => activationFlash.FadeOutFromOne(500, Easing.OutQuint);

#endregion
}
}
101 changes: 101 additions & 0 deletions osu.Game/Screens/SelectV2/BeatmapSetPanel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Diagnostics;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Pooling;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events;
using osu.Game.Beatmaps;
using osu.Game.Graphics.Sprites;
using osuTK;
using osuTK.Graphics;

namespace osu.Game.Screens.SelectV2
{
public partial class BeatmapSetPanel : PoolableDrawable, ICarouselPanel
{
public const float HEIGHT = CarouselItem.DEFAULT_HEIGHT * 2;

[Resolved]
private BeatmapCarousel carousel { get; set; } = null!;

private OsuSpriteText text = null!;

[BackgroundDependencyLoader]
private void load()
{
Size = new Vector2(500, HEIGHT);
Masking = true;

InternalChildren = new Drawable[]
{
new Box
{
Colour = Color4.Yellow.Darken(5),
Alpha = 0.8f,
RelativeSizeAxes = Axes.Both,
},
text = new OsuSpriteText
{
Padding = new MarginPadding(5),
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
}
};

KeyboardSelected.BindValueChanged(value =>
{
if (value.NewValue)
{
BorderThickness = 5;
BorderColour = Color4.Pink;
}
else
{
BorderThickness = 0;
}
});
}

protected override void PrepareForUse()
{
base.PrepareForUse();

Debug.Assert(Item != null);
Debug.Assert(Item.IsGroupSelectionTarget);

var beatmapSetInfo = (BeatmapSetInfo)Item.Model;

text.Text = $"{beatmapSetInfo.Metadata}";

this.FadeInFromZero(500, Easing.OutQuint);
}

protected override bool OnClick(ClickEvent e)
{
carousel.CurrentSelection = Item!.Model;
return true;
}

#region ICarouselPanel

public CarouselItem? Item { get; set; }
public BindableBool Selected { get; } = new BindableBool();
public BindableBool KeyboardSelected { get; } = new BindableBool();

public double DrawYPosition { get; set; }

public void Activated()
{
// sets should never be activated.
throw new InvalidOperationException();
}

#endregion
}
}
14 changes: 12 additions & 2 deletions osu.Game/Screens/SelectV2/Carousel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -540,11 +540,13 @@ protected override void Update()
{
var c = (ICarouselPanel)panel;

// panel in the process of expiring, ignore it.
if (c.Item == null)
continue;

if (panel.Depth != c.DrawYPosition)
scroll.Panels.ChangeChildDepth(panel, (float)c.DrawYPosition);

Debug.Assert(c.Item != null);

if (c.DrawYPosition != c.Item.CarouselYPosition)
c.DrawYPosition = Interpolation.DampContinuously(c.DrawYPosition, c.Item.CarouselYPosition, 50, Time.Elapsed);

Expand Down Expand Up @@ -631,7 +633,9 @@ private void updateDisplayedRange(DisplayRange range)
if (drawable is not ICarouselPanel carouselPanel)
throw new InvalidOperationException($"Carousel panel drawables must implement {typeof(ICarouselPanel)}");

carouselPanel.DrawYPosition = item.CarouselYPosition;
carouselPanel.Item = item;

scroll.Add(drawable);
}

Expand All @@ -650,6 +654,12 @@ private static void expirePanelImmediately(Drawable panel)
{
panel.FinishTransforms();
panel.Expire();

var carouselPanel = (ICarouselPanel)panel;

carouselPanel.Item = null;
carouselPanel.Selected.Value = false;
carouselPanel.KeyboardSelected.Value = false;
}

#endregion
Expand Down

0 comments on commit 3c76397

Please sign in to comment.