| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599 |
- using System.Linq;
- using System.Numerics;
- using Content.Client.Stylesheets;
- using Content.Client.UserInterface.Systems.MenuBar.Widgets;
- using Content.Shared.Construction.Prototypes;
- using Content.Shared.Whitelist;
- using Robust.Client.GameObjects;
- using Robust.Client.Graphics;
- using Robust.Client.Placement;
- using Robust.Client.Player;
- using Robust.Client.UserInterface;
- using Robust.Client.UserInterface.Controls;
- using Robust.Client.Utility;
- using Robust.Shared.Enums;
- using Robust.Shared.Prototypes;
- using static Robust.Client.UserInterface.Controls.BaseButton;
- using Robust.Shared.Map;
- using Content.Shared.Civ14.CivResearch;
- namespace Content.Client.Construction.UI
- {
- /// <summary>
- /// This class presents the Construction/Crafting UI to the client, linking the <see cref="ConstructionSystem" /> with the
- /// model. This is where the bulk of UI work is done, either calling functions in the model to change state, or collecting
- /// data out of the model to *present* to the screen though the UI framework.
- /// </summary>
- internal sealed class ConstructionMenuPresenter : IDisposable
- {
- [Dependency] private readonly EntityManager _entManager = default!;
- [Dependency] private readonly IEntitySystemManager _systemManager = default!;
- [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
- [Dependency] private readonly IPlacementManager _placementManager = default!;
- [Dependency] private readonly IUserInterfaceManager _uiManager = default!;
- [Dependency] private readonly IPlayerManager _playerManager = default!;
- [Dependency] private readonly IMapManager _mapManager = default!;
- [Dependency] private readonly ILogManager _logManager = default!;
- private ISawmill _sawmill = default!;
- private readonly IConstructionMenuView _constructionView;
- private readonly EntityWhitelistSystem _whitelistSystem;
- private readonly SpriteSystem _spriteSystem;
- private ConstructionSystem? _constructionSystem;
- private ConstructionPrototype? _selected;
- private List<ConstructionPrototype> _favoritedRecipes = [];
- private Dictionary<string, TextureButton> _recipeButtons = new();
- private string _selectedCategory = string.Empty;
- private string _favoriteCatName = "construction-category-favorites";
- private string _forAllCategoryName = "construction-category-all";
- private bool CraftingAvailable
- {
- get => _uiManager.GetActiveUIWidget<GameTopMenuBar>().CraftingButton.Visible;
- set
- {
- _uiManager.GetActiveUIWidget<GameTopMenuBar>().CraftingButton.Visible = value;
- if (!value)
- _constructionView.Close();
- }
- }
- /// <summary>
- /// Does the window have focus? If the window is closed, this will always return false.
- /// </summary>
- private bool IsAtFront => _constructionView.IsOpen && _constructionView.IsAtFront();
- private bool WindowOpen
- {
- get => _constructionView.IsOpen;
- set
- {
- if (value && CraftingAvailable)
- {
- if (_constructionView.IsOpen)
- _constructionView.MoveToFront();
- else
- _constructionView.OpenCentered();
- if (_selected != null)
- PopulateInfo(_selected);
- }
- else
- _constructionView.Close();
- }
- }
- /// <summary>
- /// Constructs a new instance of <see cref="ConstructionMenuPresenter" />.
- /// <summary>
- /// Initializes the ConstructionMenuPresenter, injecting dependencies, setting up the construction UI, binding event handlers, and populating initial categories and recipes.
- /// </summary>
- public ConstructionMenuPresenter()
- {
- // This is a lot easier than a factory
- IoCManager.InjectDependencies(this);
- _constructionView = new ConstructionMenu();
- _whitelistSystem = _entManager.System<EntityWhitelistSystem>();
- _spriteSystem = _entManager.System<SpriteSystem>();
- // This is required so that if we load after the system is initialized, we can bind to it immediately
- if (_systemManager.TryGetEntitySystem<ConstructionSystem>(out var constructionSystem))
- SystemBindingChanged(constructionSystem);
- _systemManager.SystemLoaded += OnSystemLoaded;
- _systemManager.SystemUnloaded += OnSystemUnloaded;
- _placementManager.PlacementChanged += OnPlacementChanged;
- _constructionView.OnClose += () => _uiManager.GetActiveUIWidget<GameTopMenuBar>().CraftingButton.Pressed = false;
- _constructionView.ClearAllGhosts += (_, _) => _constructionSystem?.ClearAllGhosts();
- _constructionView.PopulateRecipes += OnViewPopulateRecipes;
- _constructionView.RecipeSelected += OnViewRecipeSelected;
- _constructionView.BuildButtonToggled += (_, b) => BuildButtonToggled(b);
- _constructionView.EraseButtonToggled += (_, b) =>
- {
- if (_constructionSystem is null) return;
- if (b) _placementManager.Clear();
- _placementManager.ToggleEraserHijacked(new ConstructionPlacementHijack(_constructionSystem, null));
- _constructionView.EraseButtonPressed = b;
- };
- _constructionView.RecipeFavorited += (_, _) => OnViewFavoriteRecipe();
- PopulateCategories();
- OnViewPopulateRecipes(_constructionView, (string.Empty, string.Empty));
- }
- public void OnHudCraftingButtonToggled(ButtonToggledEventArgs args)
- {
- WindowOpen = args.Pressed;
- }
- /// <inheritdoc />
- public void Dispose()
- {
- _constructionView.Dispose();
- SystemBindingChanged(null);
- _systemManager.SystemLoaded -= OnSystemLoaded;
- _systemManager.SystemUnloaded -= OnSystemUnloaded;
- _placementManager.PlacementChanged -= OnPlacementChanged;
- }
- private void OnPlacementChanged(object? sender, EventArgs e)
- {
- _constructionView.ResetPlacement();
- }
- /// <summary>
- /// Handles selection of a construction recipe from the UI, updating the selected recipe and displaying its details.
- /// </summary>
- private void OnViewRecipeSelected(object? sender, ItemList.Item? item)
- {
- if (item is null)
- {
- _selected = null;
- _constructionView.ClearRecipeInfo();
- return;
- }
- _selected = (ConstructionPrototype)item.Metadata!;
- if (_placementManager.IsActive && !_placementManager.Eraser) UpdateGhostPlacement();
- PopulateInfo(_selected);
- }
- private void OnGridViewRecipeSelected(object? sender, ConstructionPrototype? recipe)
- {
- if (recipe is null)
- {
- _selected = null;
- _constructionView.ClearRecipeInfo();
- return;
- }
- _selected = recipe;
- if (_placementManager.IsActive && !_placementManager.Eraser) UpdateGhostPlacement();
- PopulateInfo(_selected);
- }
- /// <summary>
- /// Populates the construction recipe list or grid in the UI based on the current search term and selected category, filtering recipes by visibility, player eligibility, whitelist, and research age.
- /// </summary>
- /// <param name="sender">The event sender (unused).</param>
- /// <param name="args">A tuple containing the search string and selected category.</param>
- private void OnViewPopulateRecipes(object? sender, (string search, string category) args)
- {
- var (search, category) = args;
- var recipes = new List<ConstructionPrototype>();
- var isEmptyCategory = string.IsNullOrEmpty(category) || category == _forAllCategoryName;
- if (isEmptyCategory)
- _selectedCategory = string.Empty;
- else
- _selectedCategory = category;
- foreach (var recipe in _prototypeManager.EnumeratePrototypes<ConstructionPrototype>())
- {
- if (recipe.Hide)
- continue;
- var currentAge = 0;
- // Get the entity UID associated with the first map
- var mapId = _mapManager.GetAllMapIds().FirstOrDefault();
- var mapUid = _mapManager.GetMapEntityId(mapId);
- if (_entManager.TryGetComponent<CivResearchComponent>(mapUid, out var comp))
- {
- currentAge = (int)MathF.Floor(comp.ResearchLevel / 100);
- }
- if (currentAge < recipe.AgeMin || currentAge > recipe.AgeMax)
- continue;
- if (_playerManager.LocalSession == null
- || _playerManager.LocalEntity == null
- || _whitelistSystem.IsWhitelistFail(recipe.EntityWhitelist, _playerManager.LocalEntity.Value))
- continue;
- if (!string.IsNullOrEmpty(search))
- {
- if (!recipe.Name.ToLowerInvariant().Contains(search.Trim().ToLowerInvariant()))
- continue;
- }
- if (!isEmptyCategory)
- {
- if (category == _favoriteCatName)
- {
- if (!_favoritedRecipes.Contains(recipe))
- {
- continue;
- }
- }
- else if (recipe.Category != category)
- {
- continue;
- }
- }
- recipes.Add(recipe);
- }
- recipes.Sort((a, b) => string.Compare(a.Name, b.Name, StringComparison.InvariantCulture));
- var recipesList = _constructionView.Recipes;
- recipesList.Clear();
- var recipesGrid = _constructionView.RecipesGrid;
- recipesGrid.RemoveAllChildren();
- _constructionView.RecipesGridScrollContainer.Visible = _constructionView.GridViewButtonPressed;
- _constructionView.Recipes.Visible = !_constructionView.GridViewButtonPressed;
- if (_constructionView.GridViewButtonPressed)
- {
- foreach (var recipe in recipes)
- {
- var itemButton = new TextureButton
- {
- TextureNormal = _spriteSystem.Frame0(recipe.Icon),
- VerticalAlignment = Control.VAlignment.Center,
- Name = recipe.Name,
- ToolTip = recipe.Name,
- Scale = new Vector2(1.35f),
- ToggleMode = true,
- };
- var itemButtonPanelContainer = new PanelContainer
- {
- PanelOverride = new StyleBoxFlat { BackgroundColor = StyleNano.ButtonColorDefault },
- Children = { itemButton },
- };
- itemButton.OnToggled += buttonToggledEventArgs =>
- {
- SelectGridButton(itemButton, buttonToggledEventArgs.Pressed);
- if (buttonToggledEventArgs.Pressed &&
- _selected != null &&
- _recipeButtons.TryGetValue(_selected.Name, out var oldButton))
- {
- oldButton.Pressed = false;
- SelectGridButton(oldButton, false);
- }
- OnGridViewRecipeSelected(this, buttonToggledEventArgs.Pressed ? recipe : null);
- };
- recipesGrid.AddChild(itemButtonPanelContainer);
- _recipeButtons[recipe.Name] = itemButton;
- var isCurrentButtonSelected = _selected == recipe;
- itemButton.Pressed = isCurrentButtonSelected;
- SelectGridButton(itemButton, isCurrentButtonSelected);
- }
- }
- else
- {
- foreach (var recipe in recipes)
- {
- recipesList.Add(GetItem(recipe, recipesList));
- }
- }
- }
- private void SelectGridButton(TextureButton button, bool select)
- {
- if (button.Parent is not PanelContainer buttonPanel)
- return;
- button.Modulate = select ? Color.Green : Color.White;
- var buttonColor = select ? StyleNano.ButtonColorDefault : Color.Transparent;
- buttonPanel.PanelOverride = new StyleBoxFlat { BackgroundColor = buttonColor };
- }
- private void PopulateCategories(string? selectCategory = null)
- {
- var uniqueCategories = new HashSet<string>();
- foreach (var prototype in _prototypeManager.EnumeratePrototypes<ConstructionPrototype>())
- {
- var category = prototype.Category;
- if (!string.IsNullOrEmpty(category))
- uniqueCategories.Add(category);
- }
- var isFavorites = _favoritedRecipes.Count > 0;
- var categoriesArray = new string[isFavorites ? uniqueCategories.Count + 2 : uniqueCategories.Count + 1];
- // hard-coded to show all recipes
- var idx = 0;
- categoriesArray[idx++] = _forAllCategoryName;
- // hard-coded to show favorites if it need
- if (isFavorites)
- {
- categoriesArray[idx++] = _favoriteCatName;
- }
- var sortedProtoCategories = uniqueCategories.OrderBy(Loc.GetString);
- foreach (var cat in sortedProtoCategories)
- {
- categoriesArray[idx++] = cat;
- }
- _constructionView.OptionCategories.Clear();
- for (var i = 0; i < categoriesArray.Length; i++)
- {
- _constructionView.OptionCategories.AddItem(Loc.GetString(categoriesArray[i]), i);
- if (!string.IsNullOrEmpty(selectCategory) && selectCategory == categoriesArray[i])
- _constructionView.OptionCategories.SelectId(i);
- }
- _constructionView.Categories = categoriesArray;
- }
- private void PopulateInfo(ConstructionPrototype prototype)
- {
- _constructionView.ClearRecipeInfo();
- _constructionView.SetRecipeInfo(
- prototype.Name, prototype.Description, _spriteSystem.Frame0(prototype.Icon),
- prototype.Type != ConstructionType.Item,
- !_favoritedRecipes.Contains(prototype));
- var stepList = _constructionView.RecipeStepList;
- GenerateStepList(prototype, stepList);
- }
- private void GenerateStepList(ConstructionPrototype prototype, ItemList stepList)
- {
- if (_constructionSystem?.GetGuide(prototype) is not { } guide)
- return;
- foreach (var entry in guide.Entries)
- {
- var text = entry.Arguments != null
- ? Loc.GetString(entry.Localization, entry.Arguments) : Loc.GetString(entry.Localization);
- if (entry.EntryNumber is { } number)
- {
- text = Loc.GetString("construction-presenter-step-wrapper",
- ("step-number", number), ("text", text));
- }
- // The padding needs to be applied regardless of text length... (See PadLeft documentation)
- text = text.PadLeft(text.Length + entry.Padding);
- var icon = entry.Icon != null ? _spriteSystem.Frame0(entry.Icon) : Texture.Transparent;
- stepList.AddItem(text, icon, false);
- }
- }
- private ItemList.Item GetItem(ConstructionPrototype recipe, ItemList itemList)
- {
- return new(itemList)
- {
- Metadata = recipe,
- Text = recipe.Name,
- Icon = _spriteSystem.Frame0(recipe.Icon),
- TooltipEnabled = true,
- TooltipText = recipe.Description,
- };
- }
- private void BuildButtonToggled(bool pressed)
- {
- if (pressed)
- {
- if (_selected == null) return;
- // not bound to a construction system
- if (_constructionSystem is null)
- {
- _constructionView.BuildButtonPressed = false;
- return;
- }
- if (_selected.Type == ConstructionType.Item)
- {
- _constructionSystem.TryStartItemConstruction(_selected.ID);
- _constructionView.BuildButtonPressed = false;
- return;
- }
- _placementManager.BeginPlacing(new PlacementInformation
- {
- IsTile = false,
- PlacementOption = _selected.PlacementMode
- }, new ConstructionPlacementHijack(_constructionSystem, _selected));
- UpdateGhostPlacement();
- }
- else
- _placementManager.Clear();
- _constructionView.BuildButtonPressed = pressed;
- }
- private void UpdateGhostPlacement()
- {
- if (_selected == null)
- return;
- if (_selected.Type != ConstructionType.Structure)
- {
- _placementManager.Clear();
- return;
- }
- var constructSystem = _systemManager.GetEntitySystem<ConstructionSystem>();
- _placementManager.BeginPlacing(new PlacementInformation()
- {
- IsTile = false,
- PlacementOption = _selected.PlacementMode,
- }, new ConstructionPlacementHijack(constructSystem, _selected));
- _constructionView.BuildButtonPressed = true;
- }
- private void OnSystemLoaded(object? sender, SystemChangedArgs args)
- {
- if (args.System is ConstructionSystem system) SystemBindingChanged(system);
- }
- private void OnSystemUnloaded(object? sender, SystemChangedArgs args)
- {
- if (args.System is ConstructionSystem) SystemBindingChanged(null);
- }
- private void OnViewFavoriteRecipe()
- {
- if (_selected is not ConstructionPrototype recipe)
- return;
- if (!_favoritedRecipes.Remove(_selected))
- _favoritedRecipes.Add(_selected);
- if (_selectedCategory == _favoriteCatName)
- {
- if (_favoritedRecipes.Count > 0)
- OnViewPopulateRecipes(_constructionView, (string.Empty, _favoriteCatName));
- else
- OnViewPopulateRecipes(_constructionView, (string.Empty, string.Empty));
- }
- PopulateInfo(_selected);
- PopulateCategories(_selectedCategory);
- }
- private void SystemBindingChanged(ConstructionSystem? newSystem)
- {
- if (newSystem is null)
- {
- if (_constructionSystem is null)
- return;
- UnbindFromSystem();
- }
- else
- {
- if (_constructionSystem is null)
- {
- BindToSystem(newSystem);
- return;
- }
- UnbindFromSystem();
- BindToSystem(newSystem);
- }
- }
- private void BindToSystem(ConstructionSystem system)
- {
- _constructionSystem = system;
- system.ToggleCraftingWindow += SystemOnToggleMenu;
- system.FlipConstructionPrototype += SystemFlipConstructionPrototype;
- system.CraftingAvailabilityChanged += SystemCraftingAvailabilityChanged;
- system.ConstructionGuideAvailable += SystemGuideAvailable;
- if (_uiManager.GetActiveUIWidgetOrNull<GameTopMenuBar>() != null)
- {
- CraftingAvailable = system.CraftingEnabled;
- }
- }
- private void UnbindFromSystem()
- {
- var system = _constructionSystem;
- if (system is null)
- throw new InvalidOperationException();
- system.ToggleCraftingWindow -= SystemOnToggleMenu;
- system.FlipConstructionPrototype -= SystemFlipConstructionPrototype;
- system.CraftingAvailabilityChanged -= SystemCraftingAvailabilityChanged;
- system.ConstructionGuideAvailable -= SystemGuideAvailable;
- _constructionSystem = null;
- }
- private void SystemCraftingAvailabilityChanged(object? sender, CraftingAvailabilityChangedArgs e)
- {
- if (_uiManager.ActiveScreen == null)
- return;
- CraftingAvailable = e.Available;
- }
- private void SystemOnToggleMenu(object? sender, EventArgs eventArgs)
- {
- if (!CraftingAvailable)
- return;
- if (WindowOpen)
- {
- if (IsAtFront)
- {
- WindowOpen = false;
- _uiManager.GetActiveUIWidget<GameTopMenuBar>().CraftingButton.SetClickPressed(false); // This does not call CraftingButtonToggled
- }
- else
- _constructionView.MoveToFront();
- }
- else
- {
- WindowOpen = true;
- _uiManager.GetActiveUIWidget<GameTopMenuBar>().CraftingButton.SetClickPressed(true); // This does not call CraftingButtonToggled
- }
- }
- private void SystemFlipConstructionPrototype(object? sender, EventArgs eventArgs)
- {
- if (!_placementManager.IsActive || _placementManager.Eraser)
- {
- return;
- }
- if (_selected == null || _selected.Mirror == null)
- {
- return;
- }
- _selected = _prototypeManager.Index<ConstructionPrototype>(_selected.Mirror);
- UpdateGhostPlacement();
- }
- private void SystemGuideAvailable(object? sender, string e)
- {
- if (!CraftingAvailable)
- return;
- if (!WindowOpen)
- return;
- if (_selected == null)
- return;
- PopulateInfo(_selected);
- }
- }
- }
|