From 1fbb5915aa99e0a2a7d0c1e2be023b14783bf538 Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Tue, 23 Jul 2019 23:24:47 +0200 Subject: [PATCH] Better inventory window, inventory buttons on game HUD. Part of #272 --- .../HUD/Inventory/ClientInventoryComponent.cs | 209 ++---------------- .../HumanInventoryInterfaceController.cs | 194 ++++++++++++++++ .../HUD/Inventory/InventoryButton.cs | 40 ++++ .../Inventory/InventoryInterfaceController.cs | 79 +++++++ .../Components/Items/ClientHandsComponent.cs | 29 +-- .../EntitySystems/ClientInventorySystem.cs | 2 +- Content.Client/UserInterface/GameHud.cs | 34 ++- Content.Client/UserInterface/HandsGui.cs | 1 - .../Components/GUI/InventoryComponent.cs | 42 ++-- .../Inventory/InventoryTemplates.cs | 9 +- .../Inventory/SharedInventoryComponent.cs | 42 +++- RobustToolbox | 2 +- 12 files changed, 431 insertions(+), 252 deletions(-) create mode 100644 Content.Client/GameObjects/Components/HUD/Inventory/HumanInventoryInterfaceController.cs create mode 100644 Content.Client/GameObjects/Components/HUD/Inventory/InventoryButton.cs create mode 100644 Content.Client/GameObjects/Components/HUD/Inventory/InventoryInterfaceController.cs diff --git a/Content.Client/GameObjects/Components/HUD/Inventory/ClientInventoryComponent.cs b/Content.Client/GameObjects/Components/HUD/Inventory/ClientInventoryComponent.cs index 97a3c943da..0d672b28d2 100644 --- a/Content.Client/GameObjects/Components/HUD/Inventory/ClientInventoryComponent.cs +++ b/Content.Client/GameObjects/Components/HUD/Inventory/ClientInventoryComponent.cs @@ -1,20 +1,15 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using Content.Client.GameObjects.Components.Clothing; -using Content.Client.UserInterface; using Content.Shared.GameObjects; using Robust.Client.GameObjects; using Robust.Client.Interfaces.GameObjects.Components; -using Robust.Client.UserInterface.Controls; -using Robust.Client.UserInterface.CustomControls; using Robust.Shared.GameObjects; using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.Network; -using Robust.Shared.Interfaces.Reflection; using Robust.Shared.IoC; -using Robust.Shared.Serialization; using Robust.Shared.Utility; +using Robust.Shared.ViewVariables; using static Content.Shared.GameObjects.Components.Inventory.EquipmentSlotDefines; using static Content.Shared.GameObjects.SharedInventoryComponent.ClientInventoryMessage; @@ -27,24 +22,8 @@ namespace Content.Client.GameObjects { private readonly Dictionary _slots = new Dictionary(); - [Dependency] -#pragma warning disable 649 - private readonly IGameHud _gameHud; -#pragma warning restore 649 - - /// - /// Holds the godot control for the inventory window - /// - private InventoryWindow _window; - - public SS14Window Window => _window; - - private string _templateName = "HumanInventory"; //stored for serialization purposes - - /// - /// Inventory template after being loaded from instance creator and string name - /// - private Inventory _inventory; + [ViewVariables] + public InventoryInterfaceController InterfaceController { get; private set; } private ISpriteComponent _sprite; @@ -52,27 +31,21 @@ namespace Content.Client.GameObjects { base.OnRemove(); - _window.Dispose(); + InterfaceController?.Dispose(); } public override void Initialize() { base.Initialize(); - //Loads inventory template - var reflectionManager = IoCManager.Resolve(); - var type = reflectionManager.LooseGetType(_templateName); - DebugTools.Assert(type != null); - _inventory = (Inventory) Activator.CreateInstance(type); - - //Creates godot control class for inventory - _window = new InventoryWindow(this); - _window.CreateInventory(_inventory); - _window.OnClose += () => _gameHud.InventoryButtonDown = false; + var controllerType = ReflectionManager.LooseGetType(InventoryInstance.InterfaceControllerTypeName); + var args = new object[] {this}; + InterfaceController = DynamicTypeFactory.CreateInstance(controllerType, args); + InterfaceController.Initialize(); if (Owner.TryGetComponent(out _sprite)) { - foreach (var mask in _inventory.SlotMasks.OrderBy(s => _inventory.SlotDrawingOrder(s))) + foreach (var mask in InventoryInstance.SlotMasks.OrderBy(s => InventoryInstance.SlotDrawingOrder(s))) { if (mask == Slots.NONE) { @@ -90,13 +63,6 @@ namespace Content.Client.GameObjects } } - public override void ExposeData(ObjectSerializer serializer) - { - base.ExposeData(serializer); - - serializer.DataField(ref _templateName, "Template", "HumanInventory"); - } - public override void HandleComponentState(ComponentState curState, ComponentState nextState) { base.HandleComponentState(curState, nextState); @@ -151,12 +117,12 @@ namespace Content.Client.GameObjects } } - _window?.AddToSlot(slot, entity); + InterfaceController?.AddToSlot(slot, entity); } private void _clearSlot(Slots slot) { - _window?.RemoveFromSlot(slot); + InterfaceController?.RemoveFromSlot(slot); _sprite?.LayerSetVisible(slot, false); } @@ -180,160 +146,13 @@ namespace Content.Client.GameObjects switch (message) { case PlayerAttachedMsg _: - _gameHud.InventoryButtonVisible = true; - _gameHud.InventoryButtonToggled = b => - { - if (b) - { - Window.Open(); - } - else - { - Window.Close(); - } - }; - + InterfaceController.PlayerAttached(); break; case PlayerDetachedMsg _: - _gameHud.InventoryButtonVisible = false; - _window.Close(); - + InterfaceController.PlayerDetached(); break; } } - - /// - /// Temporary window to hold the basis for inventory hud - /// - private sealed class InventoryWindow : SS14Window - { - private List IndexedSlots; - - private Dictionary - InventorySlots = new Dictionary(); //ordered dictionary? - - private readonly ClientInventoryComponent InventoryComponent; - private readonly GridContainer _gridContainer; - - public InventoryWindow(ClientInventoryComponent inventory) - { - Title = "Inventory"; - - InventoryComponent = inventory; - _gridContainer = new GridContainer(); - Contents.AddChild(_gridContainer); - - Size = CombinedMinimumSize; - } - - /// - /// Creates a grid container filled with slot buttons loaded from an inventory template - /// - public void CreateInventory(Inventory inventory) - { - _gridContainer.Columns = inventory.Columns; - - IndexedSlots = new List(inventory.SlotMasks); - - foreach (var slot in IndexedSlots) - { - var newButton = new InventoryButton(slot); - - if (slot == Slots.NONE) - { - //TODO: Re-enable when godot grid container maintains grid with invisible elements - //newbutton.Visible = false; - } - else - { - // Store slot button and give it the default onpress behavior for empty elements - newButton.GetChild