Files
crystall-punk-14/Content.Client/_CP14/Workbench/CP14WorkbenchWindow.xaml.cs
Ed b3eed32588 Workbench Refactor (#826)
* Workbench refactor

* fixes

* update all recipes protos

* scrap resprite

* scrap melting

* scrap weapon drop

* demiplane loot update

* weapon parts melting

* fix

* boilerplate

* material localization
2025-01-31 14:47:51 +03:00

131 lines
3.8 KiB
C#

/*
* This file is sublicensed under MIT License
* https://github.com/space-wizards/space-station-14/blob/master/LICENSE.TXT
*/
using Content.Shared._CP14.Workbench;
using Content.Shared._CP14.Workbench.Prototypes;
using Robust.Client.AutoGenerated;
using Robust.Client.GameObjects;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Prototypes;
namespace Content.Client._CP14.Workbench;
[GenerateTypedNameReferences]
public sealed partial class CP14WorkbenchWindow : DefaultWindow
{
[Dependency] private readonly IEntityManager _entity = default!;
[Dependency] private readonly IPrototypeManager _prototype = default!;
private CP14WorkbenchUiRecipesState? _cachedState;
public event Action<CP14WorkbenchUiRecipesEntry>? OnCraft;
public event Action<string>? OnTextUpdated;
private CP14WorkbenchUiRecipesEntry? _selectedEntry;
public CP14WorkbenchWindow()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
SearchBar.OnTextChanged += _ =>
{
OnTextUpdated?.Invoke(SearchBar.Text);
};
CraftButton.OnPressed += _ =>
{
if (_selectedEntry is null)
return;
OnCraft?.Invoke(_selectedEntry.Value);
};
}
public void UpdateFilter(string? search)
{
if (_cachedState is null)
return;
UpdateRecipes(_cachedState, search);
}
public void UpdateRecipes(CP14WorkbenchUiRecipesState recipesState, string? search = null)
{
_cachedState = recipesState;
CraftsContainer.RemoveAllChildren();
List<CP14WorkbenchUiRecipesEntry> uncraftableList = new();
foreach (var entry in recipesState.Recipes)
{
if (search is not null && search != "")
{
if (!_prototype.TryIndex(entry.ProtoId, out var indexedEntry))
continue;
if (!_prototype.TryIndex(indexedEntry.Result, out var indexedResult))
continue;
if (!indexedResult.Name.Contains(search))
continue;
}
if (entry.Craftable)
{
var control = new CP14WorkbenchRecipeControl(entry);
control.OnSelect += RecipeSelect;
CraftsContainer.AddChild(control);
}
else
uncraftableList.Add(entry);
}
foreach (var entry in uncraftableList)
{
var control = new CP14WorkbenchRecipeControl(entry);
control.OnSelect += RecipeSelect;
CraftsContainer.AddChild(control);
}
if (_selectedEntry is not null && recipesState.Recipes.Contains(_selectedEntry.Value))
{
RecipeSelect(_selectedEntry.Value, _prototype.Index(_selectedEntry.Value.ProtoId));
return;
}
RecipeSelect(recipesState);
}
private void RecipeSelect(CP14WorkbenchUiRecipesState recipesState)
{
foreach (var entry in recipesState.Recipes)
{
RecipeSelect(entry, _prototype.Index(entry.ProtoId));
break;
}
}
private void RecipeSelect(CP14WorkbenchUiRecipesEntry entry, CP14WorkbenchRecipePrototype recipe)
{
_selectedEntry = entry;
var result = _prototype.Index(recipe.Result);
ItemView.SetPrototype(recipe.Result);
var counter = recipe.ResultCount > 1 ? $" x{recipe.ResultCount}" : "";
ItemName.Text = result.Name + counter;
ItemDescription.Text = result.Description;
ItemRequirements.RemoveAllChildren();
foreach (var requirement in recipe.Requirements)
{
ItemRequirements.AddChild(new CP14WorkbenchRequirementControl(requirement));
}
CraftButton.Disabled = !entry.Craftable;
}
}