Skip to content

Commit

Permalink
Merge pull request ppy#30330 from jhk2601/mod_acronym_search_fix
Browse files Browse the repository at this point in the history
Prioritize selecting exact searched acronym with select keybind
  • Loading branch information
bdach authored Oct 22, 2024
2 parents a35551e + 13fba9f commit 84e08d9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
16 changes: 16 additions & 0 deletions osu.Game/Overlays/Mods/ModPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Effects;
using osu.Framework.Localisation;
using osu.Game.Graphics;
using osu.Game.Rulesets.Mods;
Expand Down Expand Up @@ -58,6 +59,21 @@ protected override void LoadComplete()

modState.ValidForSelection.BindValueChanged(_ => updateFilterState());
modState.MatchingTextFilter.BindValueChanged(_ => updateFilterState(), true);
modState.Preselected.BindValueChanged(b =>
{
if (b.NewValue)
{
Content.EdgeEffect = new EdgeEffectParameters
{
Type = EdgeEffectType.Glow,
Colour = AccentColour,
Hollow = true,
Radius = 2,
};
}
else
Content.EdgeEffect = default;
}, true);
}

protected override void Select()
Expand Down
37 changes: 33 additions & 4 deletions osu.Game/Overlays/Mods/ModSelectOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ protected override void LoadComplete()
{
foreach (var column in columnFlow.Columns)
column.SearchTerm = query.NewValue;

if (SearchTextBox.HasFocus)
preselectMod();
}, true);

// Start scrolling from the end, to give the user a sense that
Expand All @@ -254,6 +257,26 @@ protected override void LoadComplete()
});
}

private void preselectMod()
{
var visibleMods = columnFlow.Columns.OfType<ModColumn>().Where(c => c.IsPresent).SelectMany(c => c.AvailableMods.Where(m => m.Visible));

// Search for an exact acronym or name match, or otherwise default to the first visible mod.
ModState? matchingMod =
visibleMods.FirstOrDefault(m => m.Mod.Acronym.Equals(SearchTerm, StringComparison.OrdinalIgnoreCase) || m.Mod.Name.Equals(SearchTerm, StringComparison.OrdinalIgnoreCase))
?? visibleMods.FirstOrDefault();
var preselectedMod = matchingMod;

foreach (var mod in AllAvailableMods)
mod.Preselected.Value = mod == preselectedMod && SearchTextBox.Current.Value.Length > 0;
}

private void clearPreselection()
{
foreach (var mod in AllAvailableMods)
mod.Preselected.Value = false;
}

public new ModSelectFooterContent? DisplayedFooterContent => base.DisplayedFooterContent as ModSelectFooterContent;

public override VisibilityContainer CreateFooterContent() => new ModSelectFooterContent(this)
Expand Down Expand Up @@ -383,7 +406,7 @@ private void updateCustomisationVisualState()
{
columnScroll.FadeColour(OsuColour.Gray(0.5f), 400, Easing.OutQuint);
SearchTextBox.FadeColour(OsuColour.Gray(0.5f), 400, Easing.OutQuint);
SearchTextBox.KillFocus();
setTextBoxFocus(false);
}
else
{
Expand Down Expand Up @@ -590,11 +613,11 @@ public override bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
return true;
}

ModState? firstMod = columnFlow.Columns.OfType<ModColumn>().FirstOrDefault(m => m.IsPresent)?.AvailableMods.FirstOrDefault(x => x.Visible);
var matchingMod = AllAvailableMods.SingleOrDefault(m => m.Preselected.Value);

if (firstMod is not null)
if (matchingMod is not null)
{
firstMod.Active.Value = !firstMod.Active.Value;
matchingMod.Active.Value = !matchingMod.Active.Value;
SearchTextBox.SelectAll();
}

Expand Down Expand Up @@ -648,9 +671,15 @@ protected override bool OnKeyDown(KeyDownEvent e)
private void setTextBoxFocus(bool focus)
{
if (focus)
{
SearchTextBox.TakeFocus();
preselectMod();
}
else
{
SearchTextBox.KillFocus();
clearPreselection();
}
}

#endregion
Expand Down
2 changes: 2 additions & 0 deletions osu.Game/Overlays/Mods/ModState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class ModState
/// </summary>
public BindableBool Active { get; } = new BindableBool();

public BindableBool Preselected { get; } = new BindableBool();

/// <summary>
/// Whether the mod requires further customisation.
/// This flag is read by the <see cref="ModSelectOverlay"/> to determine if the customisation panel should be opened after a mod change
Expand Down

0 comments on commit 84e08d9

Please sign in to comment.