| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- using System.Linq;
- using System.Text;
- using Content.Client.Materials;
- using Content.Shared.Lathe;
- using Content.Shared.Lathe.Prototypes;
- using Content.Shared.Research.Prototypes;
- using Robust.Client.AutoGenerated;
- using Robust.Client.GameObjects;
- using Robust.Client.UserInterface;
- using Robust.Client.UserInterface.Controls;
- using Robust.Client.UserInterface.CustomControls;
- using Robust.Client.UserInterface.XAML;
- using Robust.Shared.Prototypes;
- namespace Content.Client.Lathe.UI;
- [GenerateTypedNameReferences]
- public sealed partial class LatheMenu : DefaultWindow
- {
- [Dependency] private readonly IEntityManager _entityManager = default!;
- [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
- private readonly SpriteSystem _spriteSystem;
- private readonly LatheSystem _lathe;
- private readonly MaterialStorageSystem _materialStorage;
- public event Action<BaseButton.ButtonEventArgs>? OnServerListButtonPressed;
- public event Action<string, int>? RecipeQueueAction;
- public List<ProtoId<LatheRecipePrototype>> Recipes = new();
- public List<ProtoId<LatheCategoryPrototype>>? Categories;
- public ProtoId<LatheCategoryPrototype>? CurrentCategory;
- public EntityUid Entity;
- public LatheMenu()
- {
- RobustXamlLoader.Load(this);
- IoCManager.InjectDependencies(this);
- _spriteSystem = _entityManager.System<SpriteSystem>();
- _lathe = _entityManager.System<LatheSystem>();
- _materialStorage = _entityManager.System<MaterialStorageSystem>();
- SearchBar.OnTextChanged += _ =>
- {
- PopulateRecipes();
- };
- AmountLineEdit.OnTextChanged += _ =>
- {
- PopulateRecipes();
- };
- FilterOption.OnItemSelected += OnItemSelected;
- ServerListButton.OnPressed += a => OnServerListButtonPressed?.Invoke(a);
- }
- public void SetEntity(EntityUid uid)
- {
- Entity = uid;
- if (_entityManager.TryGetComponent<LatheComponent>(Entity, out var latheComponent))
- {
- if (!latheComponent.DynamicPacks.Any())
- {
- ServerListButton.Visible = false;
- }
- }
- MaterialsList.SetOwner(Entity);
- }
- protected override void Opened()
- {
- base.Opened();
- if (_entityManager.TryGetComponent<LatheComponent>(Entity, out var latheComp))
- {
- AmountLineEdit.SetText(latheComp.DefaultProductionAmount.ToString());
- }
- }
- /// <summary>
- /// Populates the list of all the recipes
- /// </summary>
- public void PopulateRecipes()
- {
- var recipesToShow = new List<LatheRecipePrototype>();
- foreach (var recipe in Recipes)
- {
- if (!_prototypeManager.TryIndex(recipe, out var proto))
- continue;
- // Category filtering
- if (CurrentCategory != null)
- {
- if (proto.Categories.Count <= 0)
- continue;
- var validRecipe = proto.Categories.Any(category => category == CurrentCategory);
- if (!validRecipe)
- continue;
- }
- if (SearchBar.Text.Trim().Length != 0)
- {
- if (_lathe.GetRecipeName(recipe).ToLowerInvariant().Contains(SearchBar.Text.Trim().ToLowerInvariant()))
- recipesToShow.Add(proto);
- }
- else
- {
- recipesToShow.Add(proto);
- }
- }
- if (!int.TryParse(AmountLineEdit.Text, out var quantity) || quantity <= 0)
- quantity = 1;
- RecipeCount.Text = Loc.GetString("lathe-menu-recipe-count", ("count", recipesToShow.Count));
- var sortedRecipesToShow = recipesToShow.OrderBy(_lathe.GetRecipeName);
- RecipeList.Children.Clear();
- _entityManager.TryGetComponent(Entity, out LatheComponent? lathe);
- foreach (var prototype in sortedRecipesToShow)
- {
- var canProduce = _lathe.CanProduce(Entity, prototype, quantity, component: lathe);
- var control = new RecipeControl(_lathe, prototype, () => GenerateTooltipText(prototype), canProduce, GetRecipeDisplayControl(prototype));
- control.OnButtonPressed += s =>
- {
- if (!int.TryParse(AmountLineEdit.Text, out var amount) || amount <= 0)
- amount = 1;
- RecipeQueueAction?.Invoke(s, amount);
- };
- RecipeList.AddChild(control);
- }
- }
- private string GenerateTooltipText(LatheRecipePrototype prototype)
- {
- StringBuilder sb = new();
- var multiplier = _entityManager.GetComponent<LatheComponent>(Entity).MaterialUseMultiplier;
- foreach (var (id, amount) in prototype.Materials)
- {
- if (!_prototypeManager.TryIndex(id, out var proto))
- continue;
- var adjustedAmount = SharedLatheSystem.AdjustMaterial(amount, prototype.ApplyMaterialDiscount, multiplier);
- var sheetVolume = _materialStorage.GetSheetVolume(proto);
- var unit = Loc.GetString(proto.Unit);
- var sheets = adjustedAmount / (float) sheetVolume;
- var availableAmount = _materialStorage.GetMaterialAmount(Entity, id);
- var missingAmount = Math.Max(0, adjustedAmount - availableAmount);
- var missingSheets = missingAmount / (float) sheetVolume;
- var name = Loc.GetString(proto.Name);
- string tooltipText;
- if (missingSheets > 0)
- {
- tooltipText = Loc.GetString("lathe-menu-material-amount-missing", ("amount", sheets), ("missingAmount", missingSheets), ("unit", unit), ("material", name));
- }
- else
- {
- var amountText = Loc.GetString("lathe-menu-material-amount", ("amount", sheets), ("unit", unit));
- tooltipText = Loc.GetString("lathe-menu-tooltip-display", ("material", name), ("amount", amountText));
- }
- sb.AppendLine(tooltipText);
- }
- var desc = _lathe.GetRecipeDescription(prototype);
- if (!string.IsNullOrWhiteSpace(desc))
- sb.AppendLine(Loc.GetString("lathe-menu-description-display", ("description", desc)));
- // Remove last newline
- if (sb.Length > 0)
- sb.Remove(sb.Length - 1, 1);
- return sb.ToString();
- }
- public void UpdateCategories()
- {
- // Get categories from recipes
- var currentCategories = new List<ProtoId<LatheCategoryPrototype>>();
- foreach (var recipeId in Recipes)
- {
- var recipe = _prototypeManager.Index(recipeId);
- if (recipe.Categories.Count <= 0)
- continue;
- foreach (var category in recipe.Categories)
- {
- if (currentCategories.Contains(category))
- continue;
- currentCategories.Add(category);
- }
- }
- 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>
- /// <param name="queue"></param>
- public void PopulateQueueList(List<LatheRecipePrototype> queue)
- {
- QueueList.DisposeAllChildren();
- var idx = 1;
- foreach (var recipe in queue)
- {
- var queuedRecipeBox = new BoxContainer();
- queuedRecipeBox.Orientation = BoxContainer.LayoutOrientation.Horizontal;
- queuedRecipeBox.AddChild(GetRecipeDisplayControl(recipe));
- var queuedRecipeLabel = new Label();
- queuedRecipeLabel.Text = $"{idx}. {_lathe.GetRecipeName(recipe)}";
- queuedRecipeBox.AddChild(queuedRecipeLabel);
- QueueList.AddChild(queuedRecipeBox);
- idx++;
- }
- }
- public void SetQueueInfo(LatheRecipePrototype? recipe)
- {
- FabricatingContainer.Visible = recipe != null;
- if (recipe == null)
- return;
- FabricatingDisplayContainer.Children.Clear();
- FabricatingDisplayContainer.AddChild(GetRecipeDisplayControl(recipe));
- NameLabel.Text = _lathe.GetRecipeName(recipe);
- }
- public Control GetRecipeDisplayControl(LatheRecipePrototype recipe)
- {
- if (recipe.Icon != null)
- {
- var textRect = new TextureRect();
- textRect.Texture = _spriteSystem.Frame0(recipe.Icon);
- return textRect;
- }
- if (recipe.Result is { } result)
- {
- var entProtoView = new EntityPrototypeView();
- entProtoView.SetPrototype(result);
- return entProtoView;
- }
- return new Control();
- }
- private void OnItemSelected(OptionButton.ItemSelectedEventArgs obj)
- {
- FilterOption.SelectId(obj.Id);
- if (obj.Id == -1)
- {
- CurrentCategory = null;
- }
- else
- {
- CurrentCategory = Categories?[obj.Id];
- }
- PopulateRecipes();
- }
- }
|