Skip to content

Commit

Permalink
da code
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyAmphibian committed Dec 27, 2024
1 parent a7b7cab commit 555e4e2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
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;
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

0 comments on commit 555e4e2

Please sign in to comment.