Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crafting menu fuzzy search framework #26

Merged
merged 4 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Content.Client/Construction/UI/ConstructionMenu.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<BoxContainer Orientation="Vertical" HorizontalExpand="True">
<BoxContainer Orientation="Horizontal">
<Button Name="MenuGridViewButton" ToggleMode="True" Text="{Loc construction-menu-grid-view}"/>
<Button Name="FuzzySearchButton" ToggleMode="True" Text="Fuzzy Search"/>
<Button Name="FavoriteButton" Visible="false"/>
</BoxContainer>
<Control>
Expand Down
24 changes: 17 additions & 7 deletions Content.Client/Construction/UI/ConstructionMenu.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public interface IConstructionMenuView : IDisposable
ScrollContainer RecipesGridScrollContainer { get; }
GridContainer RecipesGrid { get; }

event EventHandler<(string search, string catagory)> PopulateRecipes;
event EventHandler<(string search, string catagory, bool fuzzysearch)> PopulateRecipes;
CrazyAmphibian marked this conversation as resolved.
Show resolved Hide resolved
event EventHandler<ItemList.Item?> RecipeSelected;
event EventHandler RecipeFavorited;
event EventHandler<bool> BuildButtonToggled;
Expand Down Expand Up @@ -82,11 +82,17 @@ public bool GridViewButtonPressed
get => MenuGridViewButton.Pressed;
set => MenuGridViewButton.Pressed = value;
}

public bool FuzzySearchEnabled
{
get => FuzzySearchButton.Pressed;
set => FuzzySearchButton.Pressed = value;
}

public ConstructionMenu()
{
SetSize = new Vector2(560, 450);
MinSize = new Vector2(560, 320);
SetSize = new Vector2(620, 450);
MinSize = new Vector2(620, 320);

IoCManager.InjectDependencies(this);
RobustXamlLoader.Load(this);
Expand All @@ -98,12 +104,12 @@ public ConstructionMenu()
Recipes.OnItemDeselected += _ => RecipeSelected?.Invoke(this, null);

SearchBar.OnTextChanged += _ =>
PopulateRecipes?.Invoke(this, (SearchBar.Text, Categories[OptionCategories.SelectedId]));
PopulateRecipes?.Invoke(this, (SearchBar.Text, Categories[OptionCategories.SelectedId], FuzzySearchEnabled));
OptionCategories.OnItemSelected += obj =>
{
OptionCategories.SelectId(obj.Id);
SearchBar.SetText(string.Empty);
PopulateRecipes?.Invoke(this, (SearchBar.Text, Categories[obj.Id]));
PopulateRecipes?.Invoke(this, (SearchBar.Text, Categories[obj.Id], FuzzySearchEnabled));
};

BuildButton.Text = Loc.GetString("construction-menu-place-ghost");
Expand All @@ -116,11 +122,15 @@ public ConstructionMenu()
FavoriteButton.OnPressed += args => RecipeFavorited?.Invoke(this, EventArgs.Empty);

MenuGridViewButton.OnPressed += _ =>
PopulateRecipes?.Invoke(this, (SearchBar.Text, Categories[OptionCategories.SelectedId]));
PopulateRecipes?.Invoke(this, (SearchBar.Text, Categories[OptionCategories.SelectedId], FuzzySearchEnabled));

FuzzySearchButton.OnPressed += _ =>
PopulateRecipes?.Invoke(this, (SearchBar.Text, Categories[OptionCategories.SelectedId], FuzzySearchEnabled));

}

public event EventHandler? ClearAllGhosts;
public event EventHandler<(string search, string catagory)>? PopulateRecipes;
public event EventHandler<(string search, string catagory, bool fuzzysearch) >? PopulateRecipes;
public event EventHandler<ItemList.Item?>? RecipeSelected;
public event EventHandler? RecipeFavorited;
public event EventHandler<bool>? BuildButtonToggled;
Expand Down
27 changes: 20 additions & 7 deletions Content.Client/Construction/UI/ConstructionMenuPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public ConstructionMenuPresenter()
_constructionView.RecipeFavorited += (_, _) => OnViewFavoriteRecipe();

PopulateCategories();
OnViewPopulateRecipes(_constructionView, (string.Empty, string.Empty));
OnViewPopulateRecipes(_constructionView, (string.Empty, string.Empty,false));
}

public void OnHudCraftingButtonToggled(ButtonToggledEventArgs args)
Expand Down Expand Up @@ -166,10 +166,23 @@ private void OnGridViewRecipeSelected(object? sender, ConstructionPrototype? rec
if (_placementManager.IsActive && !_placementManager.Eraser) UpdateGhostPlacement();
PopulateInfo(_selected);
}

private void OnViewPopulateRecipes(object? sender, (string search, string catagory) args)

private int CheckFuzzySearch(string hostfield,string searchtext){// return an int for futureproofing, if you wanted to sort by likeness, or something. doesn't matter much now, ints are compatible with boolean logic, anyways.
int matchedtokens=0;
char[] str_seps={' ',':','.',',','/'}; //flatten punctuation.
string[] searchtokens = searchtext.Split(str_seps); //turn the search into tokens

foreach (string stoken in searchtokens){
if(hostfield.Contains(stoken,StringComparison.OrdinalIgnoreCase)) matchedtokens++; //thanks chatGPT for helping me.
}

return matchedtokens;
}


private void OnViewPopulateRecipes(object? sender, (string search, string catagory, bool fuzzySearch) args)
{
var (search, category) = args;
var (search, category,fuzzySearch) = args;

var recipes = new List<ConstructionPrototype>();

Expand All @@ -192,7 +205,7 @@ private void OnViewPopulateRecipes(object? sender, (string search, string catago

if (!string.IsNullOrEmpty(search))
{
if (!recipe.Name.ToLowerInvariant().Contains(search.Trim().ToLowerInvariant()))
if ( fuzzySearch? CheckFuzzySearch( string.IsNullOrEmpty(recipe.FuzzyName) ? recipe.Name : recipe.FuzzyName,search)==0 :(!recipe.Name.ToLowerInvariant().Contains(search.Trim().ToLowerInvariant())))
continue;
}

Expand Down Expand Up @@ -458,9 +471,9 @@ private void OnViewFavoriteRecipe()
if (_selectedCategory == _favoriteCatName)
{
if (_favoritedRecipes.Count > 0)
OnViewPopulateRecipes(_constructionView, (string.Empty, _favoriteCatName));
OnViewPopulateRecipes(_constructionView, (string.Empty, _favoriteCatName,false));
else
OnViewPopulateRecipes(_constructionView, (string.Empty, string.Empty));
OnViewPopulateRecipes(_constructionView, (string.Empty, string.Empty,false));
}

PopulateInfo(_selected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public sealed partial class ConstructionPrototype : IPrototype
[DataField("name")]
public string Name = string.Empty;

/// <summary>
/// The string used when performing a fuzzy search
/// </summary>
[DataField("fuzzyname")]
public string FuzzyName = string.Empty;

/// <summary>
/// "Useful" description displayed in the construction GUI.
/// </summary>
Expand Down
Loading