Skip to content

Commit

Permalink
Merge pull request #756 from zymex22/issue292searchbar
Browse files Browse the repository at this point in the history
Added a Search Bar to recipe database
  • Loading branch information
Sn1p3rr3c0n authored Feb 17, 2024
2 parents 1ecdd4a + c63c6de commit 87fe0cb
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions Source/ProjectRimFactory/SAL3/Things/ITab_RecipeHolder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using RimWorld;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using Verse;

namespace ProjectRimFactory.SAL3.Things
Expand Down Expand Up @@ -85,12 +86,14 @@ public ITab_RecipeHolder()
private bool showSaved = true;
private bool showLearnable = true;
private bool showQuered = true;
private string searchText = "";

private bool ShouldDrawRow(RecipeDef recipe, ref float curY, float ViewRecthight, float scrollY)
private bool ShouldDrawRow(RecipeDef recipe, ref float curY, float ViewRecthight, float scrollY, string search)
{
if (!showLearnable && Recipes[recipe] == enum_RecipeStatus.Learnable) return false;
if (!showQuered && Recipes[recipe] == enum_RecipeStatus.Quered) return false;
if (!showSaved && Recipes[recipe] == enum_RecipeStatus.Saved) return false;
if (search != "" && !recipe.label.ToLower().Contains(search.ToLower())) return false;

//The item is above the view (including a safty margin of one item)
if ((curY + ROW_HIGHT - scrollY) < 0)
Expand All @@ -111,7 +114,7 @@ private bool ShouldDrawRow(RecipeDef recipe, ref float curY, float ViewRecthight
return false;
}


protected override void FillTab()
{
RefreshRecipeList();
Expand Down Expand Up @@ -140,6 +143,14 @@ protected override void FillTab()
rect = list.GetRect(10);
currY += 10;
Widgets.DrawLineHorizontal(0, rect.y, rect.width);
rect = list.GetRect(20);
currY += 20;
rect.width -= 30 + 16 + 20 + 100;
rect.x += 20;
searchText = Widgets.TextField(rect, searchText);
rect.x -= 20;
rect.width = 20;
GUI.DrawTexture(rect, TexButton.Search);


var outRect = new Rect(5f, currY + 5, WinSize.x - 30, WinSize.y - currY - 30);
Expand All @@ -151,7 +162,7 @@ protected override void FillTab()

foreach (RecipeDef recipe in Recipes.Keys)
{
if (!ShouldDrawRow(recipe, ref currY, outRect.height, scrollPos.y)) continue;
if (!ShouldDrawRow(recipe, ref currY, outRect.height, scrollPos.y, searchText)) continue;

DrawRecipeRow(recipe, ref currY, viewRect.width);

Expand Down

0 comments on commit 87fe0cb

Please sign in to comment.