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

Added a Search Bar to recipe database #756

Merged
merged 1 commit into from
Feb 17, 2024
Merged
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
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