/* * 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? OnCraft; public event Action? 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 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; } }