using Content.Shared.GameObjects.Components.Storage;
using Robust.Client.Interfaces.GameObjects.Components;
using Robust.Client.UserInterface;
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.IoC;
using Robust.Shared.Utility;
using System;
using System.Collections.Generic;
using Robust.Client.Interfaces.Graphics;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Client.GameObjects.Components.Storage
{
///
/// Client version of item storage containers, contains a UI which displays stored entities and their size
///
public class ClientStorageComponent : SharedStorageComponent
{
private Dictionary StoredEntities { get; set; } = new Dictionary();
private int StorageSizeUsed;
private int StorageCapacityMax;
private StorageWindow Window;
[ViewVariables] private string _closedState;
[ViewVariables] private string _openState;
public bool Open
{
get => _open;
set
{
_open = value;
SetDoorSprite(_open);
}
}
public override void OnAdd()
{
base.OnAdd();
Window = new StorageWindow(IoCManager.Resolve())
{ StorageEntity = this };
}
public override void OnRemove()
{
Window.Dispose();
base.OnRemove();
}
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _closedState, "state_door_closed", null);
serializer.DataField(ref _openState, "state_door_open", null);
}
///
public override void HandleComponentState(ComponentState curState, ComponentState nextState)
{
base.HandleComponentState(curState, nextState);
if (!(curState is StorageComponentState storageState))
return;
Open = storageState.Open;
}
public override void HandleMessage(ComponentMessage message, INetChannel netChannel = null, IComponent component = null)
{
switch (message)
{
//Updates what we are storing for the UI
case StorageHeldItemsMessage msg:
HandleStorageMessage(msg);
break;
//Opens the UI
case OpenStorageUIMessage msg:
OpenUI();
break;
case CloseStorageUIMessage msg:
CloseUI();
break;
}
}
///
/// Copies received values from server about contents of storage container
///
///
private void HandleStorageMessage(StorageHeldItemsMessage storagestate)
{
StoredEntities = new Dictionary(storagestate.StoredEntities);
StorageSizeUsed = storagestate.StorageSizeUsed;
StorageCapacityMax = storagestate.StorageSizeMax;
Window.BuildEntityList();
}
///
/// Opens the storage UI
///
private void OpenUI()
{
Window.AddToScreen();
Window.Open();
}
private void CloseUI()
{
Window.Close();
}
///
/// Function for clicking one of the stored entity buttons in the UI, tells server to remove that entity
///
///
private void Interact(EntityUid entityuid)
{
SendNetworkMessage(new RemoveEntityMessage(entityuid));
}
private void SetDoorSprite(bool open)
{
if(!Owner.TryGetComponent(out var spriteComp))
return;
if(!spriteComp.Running)
return;
if (spriteComp.BaseRSI == null)
{
return;
}
var baseName = spriteComp.LayerGetState(0).Name;
var stateId = open ? _openState ?? $"{baseName}_open" : _closedState ?? $"{baseName}_door";
if (spriteComp.BaseRSI.TryGetState(stateId, out _))
spriteComp.LayerSetState(1, stateId);
}
///
/// GUI class for client storage component
///
private class StorageWindow : SS14Window
{
private Control VSplitContainer;
private VBoxContainer EntityList;
private Label Information;
public ClientStorageComponent StorageEntity;
protected override ResourcePath ScenePath => new ResourcePath("/Scenes/Storage/Storage.tscn");
public StorageWindow(IDisplayManager displayMan) : base(displayMan) { }
protected override void Initialize()
{
base.Initialize();
HideOnClose = true;
// Get all the controls.
VSplitContainer = Contents.GetChild("VSplitContainer");
EntityList = VSplitContainer.GetChild("ListScrollContainer").GetChild("EntityList");
Information = VSplitContainer.GetChild