Construction UI MVP Experiment (#3107)

* Refactors the ConstructionSystem into the MVP pattern.

* Refactors the ConstructionMenu into the MVP pattern.

* Moved the ConstructionMenuPresenter to the GameScreen where it belongs.

* Rebase updates.
This commit is contained in:
Acruid
2021-02-20 12:05:59 -08:00
committed by GitHub
parent 1b8c07b76f
commit 008fee4eaf
4 changed files with 689 additions and 443 deletions

View File

@@ -1,314 +1,134 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using Content.Client.GameObjects.EntitySystems;
using Content.Client.Utility;
using Content.Shared.Construction;
using Content.Shared.GameObjects.Components;
using Content.Shared.GameObjects.Components.Interactable;
using Robust.Client.AutoGenerated;
using Robust.Client.Graphics;
using Robust.Client.Placement;
using Robust.Client.ResourceManagement;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Client.Utility;
using Robust.Shared.Enums;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
#nullable enable
namespace Content.Client.Construction
{
[GenerateTypedNameReferences]
public partial class ConstructionMenu : SS14Window
/// <summary>
/// This is the interface for a UI View of the construction window. The point of it is to abstract away the actual
/// UI controls and just provide higher level operations on the entire window. This View is completely passive and
/// just raises events to the outside world. This class is controlled by the <see cref="ConstructionMenuPresenter"/>.
/// </summary>
public interface IConstructionMenuView : IDisposable
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IResourceCache _resourceCache = default!;
[Dependency] private readonly IEntitySystemManager _systemManager = default!;
[Dependency] private readonly IPlacementManager _placementManager = default!;
// It isn't optimal to expose UI controls like this, but the UI control design is
// questionable so it can't be helped.
string[] Categories { get; set; }
OptionButton CategoryButton { get; }
bool EraseButtonPressed { get; set; }
bool BuildButtonPressed { get; set; }
ItemList Recipes { get; }
ItemList RecipeStepList { get; }
event EventHandler<(string search, string catagory)> PopulateRecipes;
event EventHandler<ItemList.Item?> RecipeSelected;
event EventHandler<bool> BuildButtonToggled;
event EventHandler<bool> EraseButtonToggled;
event EventHandler ClearAllGhosts;
void ClearRecipeInfo();
void SetRecipeInfo(string name, string description, Texture iconTexture, bool isItem);
void ResetPlacement();
#region Window Control
event Action? OnClose;
bool IsOpen { get; }
void OpenCentered();
void MoveToFront();
bool IsAtFront();
void Close();
#endregion
}
[GenerateTypedNameReferences]
public partial class ConstructionMenu : SS14Window, IConstructionMenuView
{
protected override Vector2? CustomSize => (720, 320);
private ConstructionPrototype? _selected;
private string[] _categories = Array.Empty<string>();
public bool BuildButtonPressed
{
get => BuildButton.Pressed;
set => BuildButton.Pressed = value;
}
public string[] Categories { get; set; } = Array.Empty<string>();
public OptionButton CategoryButton => Category;
public bool EraseButtonPressed
{
get => EraseButton.Pressed;
set => EraseButton.Pressed = value;
}
/// <inheritdoc />
public ItemList Recipes => RecipesList;
public ItemList RecipeStepList => StepList;
public ConstructionMenu()
{
IoCManager.InjectDependencies(this);
RobustXamlLoader.Load(this);
_placementManager.PlacementChanged += PlacementChanged;
Title = Loc.GetString("Construction");
BuildButton.Text = Loc.GetString("Place construction ghost");
RecipesList.OnItemSelected += obj => RecipeSelected?.Invoke(this, obj.ItemList[obj.ItemIndex]);
RecipesList.OnItemDeselected += _ => RecipeSelected?.Invoke(this, null);
SearchBar.OnTextChanged += _ => PopulateRecipes?.Invoke(this, (SearchBar.Text, Categories[Category.SelectedId]));
Category.OnItemSelected += obj =>
{
Category.SelectId(obj.Id);
PopulateRecipes?.Invoke(this, (SearchBar.Text, Categories[obj.Id]));
};
BuildButton.Text = Loc.GetString("Place construction ghost");
RecipesList.OnItemSelected += RecipeSelected;
RecipesList.OnItemDeselected += RecipeDeselected;
SearchBar.OnTextChanged += SearchTextChanged;
Category.OnItemSelected += CategorySelected;
BuildButton.Text = Loc.GetString("Place construction ghost");
BuildButton.OnToggled += BuildButtonToggled;
BuildButton.OnToggled += args => BuildButtonToggled?.Invoke(this, args.Pressed);
ClearButton.Text = Loc.GetString("Clear All");
ClearButton.OnPressed += ClearAllButtonPressed;
ClearButton.OnPressed += _ => ClearAllGhosts?.Invoke(this, EventArgs.Empty);
EraseButton.Text = Loc.GetString("Eraser Mode");
EraseButton.OnToggled += EraseButtonToggled;
PopulateCategories();
PopulateAll();
EraseButton.OnToggled += args => EraseButtonToggled?.Invoke(this, args.Pressed);
}
private void PlacementChanged(object? sender, EventArgs e)
public event EventHandler? ClearAllGhosts;
public event EventHandler<(string search, string catagory)>? PopulateRecipes;
public event EventHandler<ItemList.Item?>? RecipeSelected;
public event EventHandler<bool>? BuildButtonToggled;
public event EventHandler<bool>? EraseButtonToggled;
public void ResetPlacement()
{
BuildButton.Pressed = false;
EraseButton.Pressed = false;
}
private void PopulateAll()
public void SetRecipeInfo(string name, string description, Texture iconTexture, bool isItem)
{
foreach (var recipe in _prototypeManager.EnumeratePrototypes<ConstructionPrototype>())
{
RecipesList.Add(GetItem(recipe, RecipesList));
}
}
private static ItemList.Item GetItem(ConstructionPrototype recipe, ItemList itemList)
{
return new(itemList)
{
Metadata = recipe,
Text = recipe.Name,
Icon = recipe.Icon.Frame0(),
TooltipEnabled = true,
TooltipText = recipe.Description,
};
}
private void PopulateBy(string search, string category)
{
RecipesList.Clear();
foreach (var recipe in _prototypeManager.EnumeratePrototypes<ConstructionPrototype>())
{
if (!string.IsNullOrEmpty(search))
{
if (!recipe.Name.ToLowerInvariant().Contains(search.Trim().ToLowerInvariant()))
continue;
}
if (!string.IsNullOrEmpty(category) && category != Loc.GetString("All"))
{
if (recipe.Category != category)
continue;
}
RecipesList.Add(GetItem(recipe, RecipesList));
}
}
private void PopulateCategories()
{
var uniqueCategories = new HashSet<string>();
// hard-coded to show all recipes
uniqueCategories.Add(Loc.GetString("All"));
foreach (var prototype in _prototypeManager.EnumeratePrototypes<ConstructionPrototype>())
{
var category = Loc.GetString(prototype.Category);
if (!string.IsNullOrEmpty(category))
uniqueCategories.Add(category);
}
Category.Clear();
var array = uniqueCategories.ToArray();
Array.Sort(array);
for (var i = 0; i < array.Length; i++)
{
var category = array[i];
Category.AddItem(category, i);
}
_categories = array;
}
private void PopulateInfo(ConstructionPrototype prototype)
{
ClearInfo();
var isItem = prototype.Type == ConstructionType.Item;
BuildButton.Disabled = false;
BuildButton.Text = Loc.GetString(!isItem ? "Place construction ghost" : "Craft");
TargetName.SetMessage(prototype.Name);
TargetDesc.SetMessage(prototype.Description);
TargetTexture.Texture = prototype.Icon.Frame0();
if (!_prototypeManager.TryIndex(prototype.Graph, out ConstructionGraphPrototype graph))
return;
var startNode = graph.Nodes[prototype.StartNode];
var targetNode = graph.Nodes[prototype.TargetNode];
var path = graph.Path(startNode.Name, targetNode.Name);
var current = startNode;
var stepNumber = 1;
Texture? GetTextureForStep(ConstructionGraphStep step)
{
switch (step)
{
case MaterialConstructionGraphStep materialStep:
switch (materialStep.Material)
{
case StackType.Metal:
return _resourceCache.GetTexture("/Textures/Objects/Materials/sheets.rsi/metal.png");
case StackType.Glass:
return _resourceCache.GetTexture("/Textures/Objects/Materials/sheets.rsi/glass.png");
case StackType.Plasteel:
return _resourceCache.GetTexture("/Textures/Objects/Materials/sheets.rsi/plasteel.png");
case StackType.Plasma:
return _resourceCache.GetTexture("/Textures/Objects/Materials/sheets.rsi/plasma.png");
case StackType.Cable:
return _resourceCache.GetTexture("/Textures/Objects/Tools/cables.rsi/coil-30.png");
case StackType.MetalRod:
return _resourceCache.GetTexture("/Textures/Objects/Materials/materials.rsi/rods.png");
}
break;
case ToolConstructionGraphStep toolStep:
switch (toolStep.Tool)
{
case ToolQuality.Anchoring:
return _resourceCache.GetTexture("/Textures/Objects/Tools/wrench.rsi/icon.png");
case ToolQuality.Prying:
return _resourceCache.GetTexture("/Textures/Objects/Tools/crowbar.rsi/icon.png");
case ToolQuality.Screwing:
return _resourceCache.GetTexture("/Textures/Objects/Tools/screwdriver.rsi/screwdriver-map.png");
case ToolQuality.Cutting:
return _resourceCache.GetTexture("/Textures/Objects/Tools/wirecutters.rsi/cutters-map.png");
case ToolQuality.Welding:
return _resourceCache.GetTexture("/Textures/Objects/Tools/welder.rsi/welder.png");
case ToolQuality.Multitool:
return _resourceCache.GetTexture("/Textures/Objects/Tools/multitool.rsi/multitool.png");
}
break;
case ComponentConstructionGraphStep componentStep:
return componentStep.Icon?.Frame0();
case PrototypeConstructionGraphStep prototypeStep:
return prototypeStep.Icon?.Frame0();
case NestedConstructionGraphStep _:
return null;
}
return null;
}
foreach (var node in path)
{
var edge = current.GetEdge(node.Name);
var firstNode = current == startNode;
if (firstNode)
{
StepList.AddItem(isItem
? Loc.GetString($"{stepNumber++}. To craft this item, you need:")
: Loc.GetString($"{stepNumber++}. To build this, first you need:"));
}
foreach (var step in edge.Steps)
{
var icon = GetTextureForStep(step);
switch (step)
{
case MaterialConstructionGraphStep materialStep:
StepList.AddItem(
!firstNode
? Loc.GetString(
"{0}. Add {1}x {2}.", stepNumber++, materialStep.Amount, materialStep.Material)
: Loc.GetString(" {0}x {1}", materialStep.Amount, materialStep.Material), icon);
break;
case ToolConstructionGraphStep toolStep:
StepList.AddItem(Loc.GetString("{0}. Use a {1}.", stepNumber++, toolStep.Tool.GetToolName()), icon);
break;
case PrototypeConstructionGraphStep prototypeStep:
StepList.AddItem(Loc.GetString("{0}. Add {1}.", stepNumber++, prototypeStep.Name), icon);
break;
case ComponentConstructionGraphStep componentStep:
StepList.AddItem(Loc.GetString("{0}. Add {1}.", stepNumber++, componentStep.Name), icon);
break;
case NestedConstructionGraphStep nestedStep:
var parallelNumber = 1;
StepList.AddItem(Loc.GetString("{0}. In parallel...", stepNumber++));
foreach (var steps in nestedStep.Steps)
{
var subStepNumber = 1;
foreach (var subStep in steps)
{
icon = GetTextureForStep(subStep);
switch (subStep)
{
case MaterialConstructionGraphStep materialStep:
if (!isItem)
StepList.AddItem(Loc.GetString(" {0}.{1}.{2}. Add {3}x {4}.", stepNumber, parallelNumber, subStepNumber++, materialStep.Amount, materialStep.Material), icon);
break;
case ToolConstructionGraphStep toolStep:
StepList.AddItem(Loc.GetString(" {0}.{1}.{2}. Use a {3}.", stepNumber, parallelNumber, subStepNumber++, toolStep.Tool.GetToolName()), icon);
break;
case PrototypeConstructionGraphStep prototypeStep:
StepList.AddItem(Loc.GetString(" {0}.{1}.{2}. Add {3}.", stepNumber, parallelNumber, subStepNumber++, prototypeStep.Name), icon);
break;
case ComponentConstructionGraphStep componentStep:
StepList.AddItem(Loc.GetString(" {0}.{1}.{2}. Add {3}.", stepNumber, parallelNumber, subStepNumber++, componentStep.Name), icon);
break;
}
}
parallelNumber++;
}
break;
}
}
current = node;
}
BuildButton.Text = Loc.GetString(isItem ? "Place construction ghost" : "Craft");
TargetName.SetMessage(name);
TargetDesc.SetMessage(description);
TargetTexture.Texture = iconTexture;
}
private void ClearInfo()
public void ClearRecipeInfo()
{
BuildButton.Disabled = true;
TargetName.SetMessage(string.Empty);
@@ -317,92 +137,12 @@ namespace Content.Client.Construction
StepList.Clear();
}
private void RecipeSelected(ItemList.ItemListSelectedEventArgs obj)
{
_selected = (ConstructionPrototype) obj.ItemList[obj.ItemIndex].Metadata!;
if(_placementManager.IsActive && !_placementManager.Eraser) UpdateGhostPlacement();
PopulateInfo(_selected);
}
private void RecipeDeselected(ItemList.ItemListDeselectedEventArgs obj)
{
_selected = null;
ClearInfo();
}
private void CategorySelected(OptionButton.ItemSelectedEventArgs obj)
{
Category.SelectId(obj.Id);
PopulateBy(SearchBar.Text, _categories[obj.Id]);
}
private void SearchTextChanged(LineEdit.LineEditEventArgs obj)
{
PopulateBy(SearchBar.Text, _categories[Category.SelectedId]);
}
private void BuildButtonToggled(BaseButton.ButtonToggledEventArgs args)
{
if (args.Pressed)
{
if(_selected == null) return;
var constructSystem = EntitySystem.Get<ConstructionSystem>();
if (_selected.Type == ConstructionType.Item)
{
constructSystem.TryStartItemConstruction(_selected.ID);
BuildButton.Pressed = false;
return;
}
UpdateGhostPlacement();
}
else
{
_placementManager.Clear();
}
BuildButton.Pressed = args.Pressed;
}
private void UpdateGhostPlacement()
{
if (_selected == null || _selected.Type != ConstructionType.Structure) return;
var constructSystem = EntitySystem.Get<ConstructionSystem>();
_placementManager.BeginPlacing(new PlacementInformation()
{
IsTile = false,
PlacementOption = _selected.PlacementMode,
}, new ConstructionPlacementHijack(constructSystem, _selected));
BuildButton.Pressed = true;
}
private void EraseButtonToggled(BaseButton.ButtonToggledEventArgs args)
{
if (args.Pressed) _placementManager.Clear();
_placementManager.ToggleEraserHijacked(new ConstructionPlacementHijack(_systemManager.GetEntitySystem<ConstructionSystem>(), null));
EraseButton.Pressed = args.Pressed;
}
private void ClearAllButtonPressed(BaseButton.ButtonEventArgs obj)
{
var constructionSystem = EntitySystem.Get<ConstructionSystem>();
constructionSystem.ClearAllGhosts();
}
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
_placementManager.PlacementChanged -= PlacementChanged;
}
if (disposing) { }
}
}
}