Lathe Categories (#24247)

* Lathe Categories

* serilog my beloathed
This commit is contained in:
Nemanja
2024-01-19 19:45:03 -05:00
committed by GitHub
parent 7583662672
commit 73e94dfa92
19 changed files with 472 additions and 32 deletions

View File

@@ -43,7 +43,8 @@ namespace Content.Client.Lathe.UI
case LatheUpdateState msg:
if (_menu != null)
_menu.Recipes = msg.Recipes;
_menu?.PopulateRecipes(Owner);
_menu?.PopulateRecipes();
_menu?.UpdateCategories();
_menu?.PopulateQueueList(msg.Queue);
_menu?.SetQueueInfo(msg.CurrentlyProducing);
break;

View File

@@ -26,14 +26,10 @@
PlaceHolder="{Loc 'lathe-menu-search-designs'}"
HorizontalExpand="True">
</LineEdit>
<Button
Name="FilterButton"
Text="{Loc 'lathe-menu-search-filter'}"
TextAlign="Center"
Margin="5 0 0 0"
Disabled="True"
StyleClasses="ButtonSquare">
</Button>
<OptionButton
Name="FilterOption"
MinWidth="100"
StyleClasses="ButtonSquare"/>
</BoxContainer>
<BoxContainer Orientation="Vertical"
MinHeight="225"

View File

@@ -2,6 +2,7 @@ using System.Linq;
using System.Text;
using Content.Client.Materials;
using Content.Shared.Lathe;
using Content.Shared.Lathe.Prototypes;
using Content.Shared.Materials;
using Content.Shared.Research.Prototypes;
using Robust.Client.AutoGenerated;
@@ -18,6 +19,7 @@ public sealed partial class LatheMenu : DefaultWindow
{
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
private EntityUid _owner;
private readonly SpriteSystem _spriteSystem;
private readonly LatheSystem _lathe;
private readonly MaterialStorageSystem _materialStorage;
@@ -27,8 +29,13 @@ public sealed partial class LatheMenu : DefaultWindow
public List<ProtoId<LatheRecipePrototype>> Recipes = new();
public List<ProtoId<LatheCategoryPrototype>>? Categories;
public ProtoId<LatheCategoryPrototype>? CurrentCategory;
public LatheMenu(LatheBoundUserInterface owner)
{
_owner = owner.Owner;
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
@@ -40,13 +47,15 @@ public sealed partial class LatheMenu : DefaultWindow
SearchBar.OnTextChanged += _ =>
{
PopulateRecipes(owner.Owner);
PopulateRecipes();
};
AmountLineEdit.OnTextChanged += _ =>
{
PopulateRecipes(owner.Owner);
PopulateRecipes();
};
FilterOption.OnItemSelected += OnItemSelected;
ServerListButton.OnPressed += a => OnServerListButtonPressed?.Invoke(a);
if (_entityManager.TryGetComponent<LatheComponent>(owner.Owner, out var latheComponent))
@@ -63,10 +72,9 @@ public sealed partial class LatheMenu : DefaultWindow
/// <summary>
/// Populates the list of all the recipes
/// </summary>
/// <param name="lathe"></param>
public void PopulateRecipes(EntityUid lathe)
public void PopulateRecipes()
{
if (!_entityManager.TryGetComponent<LatheComponent>(lathe, out var component))
if (!_entityManager.TryGetComponent<LatheComponent>(_owner, out var component))
return;
var recipesToShow = new List<LatheRecipePrototype>();
@@ -75,6 +83,9 @@ public sealed partial class LatheMenu : DefaultWindow
if (!_prototypeManager.TryIndex(recipe, out var proto))
continue;
if (CurrentCategory != null && proto.Category != CurrentCategory)
continue;
if (SearchBar.Text.Trim().Length != 0)
{
if (proto.Name.ToLowerInvariant().Contains(SearchBar.Text.Trim().ToLowerInvariant()))
@@ -89,8 +100,9 @@ public sealed partial class LatheMenu : DefaultWindow
if (!int.TryParse(AmountLineEdit.Text, out var quantity) || quantity <= 0)
quantity = 1;
var sortedRecipesToShow = recipesToShow.OrderBy(p => p.Name);
RecipeList.Children.Clear();
foreach (var prototype in recipesToShow)
foreach (var prototype in sortedRecipesToShow)
{
StringBuilder sb = new();
var first = true;
@@ -115,13 +127,16 @@ public sealed partial class LatheMenu : DefaultWindow
sb.Append(Loc.GetString("lathe-menu-tooltip-display", ("material", name), ("amount", amountText)));
}
sb.Append('\n');
sb.Append(Loc.GetString("lathe-menu-description-display", ("description", prototype.Description)));
if (!string.IsNullOrWhiteSpace(prototype.Description))
{
sb.Append('\n');
sb.Append(Loc.GetString("lathe-menu-description-display", ("description", prototype.Description)));
}
var icon = prototype.Icon == null
? _spriteSystem.GetPrototypeIcon(prototype.Result).Default
: _spriteSystem.Frame0(prototype.Icon);
var canProduce = _lathe.CanProduce(lathe, prototype, quantity);
var canProduce = _lathe.CanProduce(_owner, prototype, quantity);
var control = new RecipeControl(prototype, sb.ToString(), canProduce, icon);
control.OnButtonPressed += s =>
@@ -134,6 +149,41 @@ public sealed partial class LatheMenu : DefaultWindow
}
}
public void UpdateCategories()
{
var currentCategories = new List<ProtoId<LatheCategoryPrototype>>();
foreach (var recipeId in Recipes)
{
var recipe = _prototypeManager.Index(recipeId);
if (recipe.Category == null)
continue;
if (currentCategories.Contains(recipe.Category.Value))
continue;
currentCategories.Add(recipe.Category.Value);
}
if (Categories != null && (Categories.Count == currentCategories.Count || !Categories.All(currentCategories.Contains)))
return;
Categories = currentCategories;
var sortedCategories = currentCategories
.Select(p => _prototypeManager.Index(p))
.OrderBy(p => Loc.GetString(p.Name))
.ToList();
FilterOption.Clear();
FilterOption.AddItem(Loc.GetString("lathe-menu-category-all"), -1);
foreach (var category in sortedCategories)
{
FilterOption.AddItem(Loc.GetString(category.Name), Categories.IndexOf(category.ID));
}
FilterOption.SelectId(-1);
}
/// <summary>
/// Populates the build queue list with all queued items
/// </summary>
@@ -162,4 +212,18 @@ public sealed partial class LatheMenu : DefaultWindow
: _spriteSystem.Frame0(recipe.Icon);
NameLabel.Text = $"{recipe.Name}";
}
private void OnItemSelected(OptionButton.ItemSelectedEventArgs obj)
{
FilterOption.SelectId(obj.Id);
if (obj.Id == -1)
{
CurrentCategory = null;
}
else
{
CurrentCategory = Categories?[obj.Id];
}
PopulateRecipes();
}
}

View File

@@ -1,20 +1,17 @@
using Content.Shared.Research.Prototypes;
using Content.Client.Message;
using Robust.Client.AutoGenerated;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Graphics;
namespace Content.Client.Lathe.UI;
[GenerateTypedNameReferences]
public sealed partial class RecipeTooltip : Control
{
public RecipeTooltip(string tooltip)
{
RobustXamlLoader.Load(this);
RecipeTooltipLabel.SetMessage(tooltip);
RecipeTooltipLabel.SetMarkup(tooltip);
}
}