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,7 +1,6 @@
using System.Collections.Generic;
using Content.Client.Construction;
using System;
using System.Collections.Generic;
using Content.Client.GameObjects.Components.Construction;
using Content.Client.UserInterface;
using Content.Shared.Construction;
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.Input;
@@ -9,12 +8,15 @@ using Content.Shared.Utility;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Client.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.Input;
using Robust.Shared.Input.Binding;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
#nullable enable
namespace Content.Client.GameObjects.EntitySystems
{
/// <summary>
@@ -23,12 +25,12 @@ namespace Content.Client.GameObjects.EntitySystems
[UsedImplicitly]
public class ConstructionSystem : SharedConstructionSystem
{
[Dependency] private readonly IGameHud _gameHud = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
private readonly Dictionary<int, ConstructionGhostComponent> _ghosts = new();
private int _nextId;
private readonly Dictionary<int, ConstructionGhostComponent> _ghosts = new();
private ConstructionMenu _constructionMenu;
private bool CraftingEnabled { get; set; }
/// <inheritdoc />
public override void Initialize()
@@ -46,6 +48,16 @@ namespace Content.Client.GameObjects.EntitySystems
.Register<ConstructionSystem>();
}
/// <inheritdoc />
public override void Shutdown()
{
CommandBinds.Unregister<ConstructionSystem>();
base.Shutdown();
}
public event EventHandler<CraftingAvailabilityChangedArgs>? CraftingAvailabilityChanged;
public event EventHandler? ToggleCraftingWindow;
private void HandleAckStructure(AckStructureConstructionMessage msg)
{
ClearGhost(msg.GhostId);
@@ -53,66 +65,32 @@ namespace Content.Client.GameObjects.EntitySystems
private void HandlePlayerAttached(PlayerAttachSysMessage msg)
{
if (msg.AttachedEntity == null)
{
_gameHud.CraftingButtonVisible = false;
return;
}
if (_constructionMenu == null)
{
_constructionMenu = new ConstructionMenu();
_constructionMenu.OnClose += () => _gameHud.CraftingButtonDown = false;
}
_gameHud.CraftingButtonVisible = true;
_gameHud.CraftingButtonToggled = b =>
{
if (b)
{
_constructionMenu.Open();
}
else
{
_constructionMenu.Close();
}
};
}
/// <inheritdoc />
public override void Shutdown()
{
_constructionMenu?.Dispose();
CommandBinds.Unregister<ConstructionSystem>();
base.Shutdown();
var available = IsCrafingAvailable(msg.AttachedEntity);
UpdateCraftingAvailability(available);
}
private bool HandleOpenCraftingMenu(in PointerInputCmdHandler.PointerInputCmdArgs args)
{
if (_playerManager.LocalPlayer.ControlledEntity == null)
{
if (args.State == BoundKeyState.Down)
ToggleCraftingWindow?.Invoke(this, EventArgs.Empty);
return true;
}
private void UpdateCraftingAvailability(bool available)
{
if (CraftingEnabled == available)
return;
CraftingAvailabilityChanged?.Invoke(this, new CraftingAvailabilityChangedArgs(available));
CraftingEnabled = available;
}
private static bool IsCrafingAvailable(IEntity? entity)
{
if (entity == null)
return false;
}
var menu = _constructionMenu;
if (menu.IsOpen)
{
if (menu.IsAtFront())
{
SetOpenValue(menu, false);
}
else
{
menu.MoveToFront();
}
}
else
{
SetOpenValue(menu, true);
}
// TODO: Decide if entity can craft, using capabilities or something
return true;
}
@@ -123,26 +101,11 @@ namespace Content.Client.GameObjects.EntitySystems
var entity = EntityManager.GetEntity(args.EntityUid);
if (!entity.TryGetComponent(out ConstructionGhostComponent ghostComp))
if (!entity.TryGetComponent<ConstructionGhostComponent>(out var ghostComp))
return false;
TryStartConstruction(ghostComp.GhostID);
return true;
}
private void SetOpenValue(ConstructionMenu menu, bool value)
{
if (value)
{
_gameHud.CraftingButtonDown = true;
menu.OpenCentered();
}
else
{
_gameHud.CraftingButtonDown = false;
menu.Close();
}
}
/// <summary>
@@ -153,10 +116,7 @@ namespace Content.Client.GameObjects.EntitySystems
var user = _playerManager.LocalPlayer?.ControlledEntity;
// This InRangeUnobstructed should probably be replaced with "is there something blocking us in that tile?"
if (user == null || GhostPresent(loc) || !user.InRangeUnobstructed(loc, 20f, ignoreInsideBlocker:prototype.CanBuildInImpassable))
{
return;
}
if (user == null || GhostPresent(loc) || !user.InRangeUnobstructed(loc, 20f, ignoreInsideBlocker: prototype.CanBuildInImpassable)) return;
foreach (var condition in prototype.Conditions)
{
@@ -185,10 +145,7 @@ namespace Content.Client.GameObjects.EntitySystems
{
foreach (var ghost in _ghosts)
{
if (ghost.Value.Owner.Transform.Coordinates.Equals(loc))
{
return true;
}
if (ghost.Value.Owner.Transform.Coordinates.Equals(loc)) return true;
}
return false;
@@ -211,7 +168,7 @@ namespace Content.Client.GameObjects.EntitySystems
}
/// <summary>
/// Removes a construction ghost entity with the given ID.
/// Removes a construction ghost entity with the given ID.
/// </summary>
public void ClearGhost(int ghostId)
{
@@ -223,7 +180,7 @@ namespace Content.Client.GameObjects.EntitySystems
}
/// <summary>
/// Removes all construction ghosts.
/// Removes all construction ghosts.
/// </summary>
public void ClearAllGhosts()
{
@@ -235,4 +192,14 @@ namespace Content.Client.GameObjects.EntitySystems
_ghosts.Clear();
}
}
public class CraftingAvailabilityChangedArgs : EventArgs
{
public bool Available { get; }
public CraftingAvailabilityChangedArgs(bool available)
{
Available = available;
}
}
}