Skip to content

Commit

Permalink
Remove IndexOf overhead
Browse files Browse the repository at this point in the history
  • Loading branch information
peppy committed Jan 22, 2025
1 parent ee7972b commit 0a15bbd
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions osu.Game/Screens/SelectV2/Carousel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,33 +332,43 @@ private bool selectNext(int direction, bool isGroupSelection)
if (displayedCarouselItems == null || displayedCarouselItems.Count == 0)
return false;

CarouselItem? originalItem = currentKeyboardSelection.CarouselItem;
CarouselItem? originalSelection = currentKeyboardSelection.CarouselItem;
int currentSelectionIndex = currentKeyboardSelection.Index ?? -1;

// To keep things simple, let's first handle the cases where there's no selection yet.
// Once we've confirmed that the first or last isn't valid, we can handle everything
// using the same process.
if (originalItem == null)
if (originalSelection == null || currentSelectionIndex < 0)
{
originalItem = direction > 0
? displayedCarouselItems.First()
: displayedCarouselItems.Last();
if (direction > 0)
{
originalSelection = displayedCarouselItems.First();
currentSelectionIndex = 0;
}
else
{
originalSelection = displayedCarouselItems.Last();
currentSelectionIndex = displayedCarouselItems.Count - 1;
}

if (isValidItem(originalItem))
if (isValidItem(originalSelection))
{
performSelection(originalItem);
performSelection(originalSelection);
return true;
}
}

Debug.Assert(originalSelection != null);
Debug.Assert(currentSelectionIndex >= 0);

// As a special case, if the user has a different keyboard selection and requests
// group selection, first transfer the keyboard selection.
if (isGroupSelection && currentSelection.CarouselItem != originalItem)
if (isGroupSelection && currentSelection.CarouselItem != originalSelection)
{
setSelection(originalItem.Model);
setSelection(originalSelection.Model);
return true;
}

int currentSelectionIndex = displayedCarouselItems.IndexOf(originalItem);
int newSelectionIndex = currentSelectionIndex;

// As a second special case, if we're group selecting backwards and the current selection isn't
Expand All @@ -369,17 +379,15 @@ private bool selectNext(int direction, bool isGroupSelection)
newSelectionIndex--;
}

Debug.Assert(currentSelectionIndex >= 0);

CarouselItem newItem;

while ((newItem = selectNextPanel()) != originalItem)
while ((newItem = selectNextPanel()) != originalSelection)
{
if (isValidItem(newItem))
break;
}

if (newItem == originalItem)
if (newItem == originalSelection)
return false;

performSelection(newItem);
Expand Down Expand Up @@ -480,7 +488,7 @@ private void updateSelection()
{
double? previousYPosition = currentKeyboardSelection?.YPosition;

currentKeyboardSelection = new Selection(item.Model, item, item.CarouselYPosition);
currentKeyboardSelection = new Selection(item.Model, item, item.CarouselYPosition, i);

if (previousYPosition != null && previousYPosition != item.CarouselYPosition)
{
Expand All @@ -490,7 +498,7 @@ private void updateSelection()
}

if (isSelected)
currentSelection = new Selection(item.Model, item, item.CarouselYPosition);
currentSelection = new Selection(item.Model, item, item.CarouselYPosition, i);

item.CarouselYPosition = yPos;

Expand Down Expand Up @@ -666,7 +674,7 @@ private static void expirePanelImmediately(Drawable panel)

#region Internal helper classes

private record Selection(object? Model = null, CarouselItem? CarouselItem = null, double? YPosition = null);
private record Selection(object? Model = null, CarouselItem? CarouselItem = null, double? YPosition = null, int? Index = null);

private record DisplayRange(int First, int Last);

Expand Down

0 comments on commit 0a15bbd

Please sign in to comment.