Predict StorageComponent (#19682)

This commit is contained in:
metalgearsloth
2023-09-11 21:20:46 +10:00
committed by GitHub
parent 99b77bc2d3
commit d5bd1c6f86
68 changed files with 1124 additions and 1121 deletions

View File

@@ -0,0 +1,60 @@
using Content.Client.Storage.Systems;
using Content.Shared.Storage;
using Robust.Client.UserInterface.Controllers;
namespace Content.Client.Storage.UI;
public sealed class StorageUIController : UIController, IOnSystemChanged<StorageSystem>
{
// This is mainly to keep legacy functionality for now.
private readonly Dictionary<EntityUid, StorageWindow> _storageWindows = new();
public override void Initialize()
{
base.Initialize();
EntityManager.EventBus.SubscribeLocalEvent<StorageComponent, ComponentShutdown>(OnStorageShutdown);
}
public StorageWindow EnsureStorageWindow(EntityUid uid)
{
if (_storageWindows.TryGetValue(uid, out var window))
{
UIManager.WindowRoot.AddChild(window);
return window;
}
window = new StorageWindow(EntityManager);
_storageWindows[uid] = window;
window.OpenCenteredLeft();
return window;
}
private void OnStorageShutdown(EntityUid uid, StorageComponent component, ComponentShutdown args)
{
if (!_storageWindows.TryGetValue(uid, out var window))
return;
_storageWindows.Remove(uid);
window.Dispose();
}
private void OnStorageUpdate(EntityUid uid, StorageComponent component)
{
if (EntityManager.TryGetComponent<UserInterfaceComponent>(uid, out var uiComp) &&
uiComp.OpenInterfaces.TryGetValue(StorageComponent.StorageUiKey.Key, out var bui))
{
var storageBui = (StorageBoundUserInterface) bui;
storageBui.BuildEntityList(uid, component);
}
}
public void OnSystemLoaded(StorageSystem system)
{
system.StorageUpdated += OnStorageUpdate;
}
public void OnSystemUnloaded(StorageSystem system)
{
system.StorageUpdated -= OnStorageUpdate;
}
}

View File

@@ -8,7 +8,9 @@ using Content.Client.UserInterface.Controls;
using Content.Shared.IdentityManagement;
using Content.Shared.Item;
using Content.Shared.Stacks;
using Content.Shared.Storage;
using Robust.Client.UserInterface;
using Robust.Shared.Containers;
using static Robust.Client.UserInterface.Controls.BoxContainer;
using static Content.Shared.Storage.SharedStorageComponent;
using Direction = Robust.Shared.Maths.Direction;
@@ -18,9 +20,9 @@ namespace Content.Client.Storage.UI
/// <summary>
/// GUI class for client storage component
/// </summary>
public sealed class StorageWindow : DefaultWindow
public sealed class StorageWindow : FancyWindow
{
private IEntityManager _entityManager;
private readonly IEntityManager _entityManager;
private readonly Label _information;
public readonly ContainerButton StorageContainerButton;
@@ -41,7 +43,7 @@ namespace Content.Client.Storage.UI
MouseFilter = MouseFilterMode.Pass,
};
Contents.AddChild(StorageContainerButton);
ContentsContainer.AddChild(StorageContainerButton);
var innerContainerButton = new PanelContainer
{
@@ -54,6 +56,7 @@ namespace Content.Client.Storage.UI
{
Orientation = LayoutOrientation.Vertical,
MouseFilter = MouseFilterMode.Ignore,
Margin = new Thickness(5),
};
StorageContainerButton.AddChild(vBox);
@@ -87,20 +90,27 @@ namespace Content.Client.Storage.UI
/// <summary>
/// Loops through stored entities creating buttons for each, updates information labels
/// </summary>
public void BuildEntityList(StorageBoundUserInterfaceState state)
public void BuildEntityList(EntityUid entity, StorageComponent component)
{
var list = state.StoredEntities.ConvertAll(nent => new EntityListData(_entityManager.GetEntity(nent)));
var storedCount = component.Container.ContainedEntities.Count;
var list = new List<EntityListData>(storedCount);
foreach (var uid in component.Container.ContainedEntities)
{
list.Add(new EntityListData(uid));
}
EntityList.PopulateList(list);
//Sets information about entire storage container current capacity
if (state.StorageCapacityMax != 0)
// Sets information about entire storage container current capacity
if (component.StorageCapacityMax != 0)
{
_information.Text = Loc.GetString("comp-storage-window-volume", ("itemCount", state.StoredEntities.Count),
("usedVolume", state.StorageSizeUsed), ("maxVolume", state.StorageCapacityMax));
_information.Text = Loc.GetString("comp-storage-window-volume", ("itemCount", storedCount),
("usedVolume", component.StorageUsed), ("maxVolume", component.StorageCapacityMax));
}
else
{
_information.Text = Loc.GetString("comp-storage-window-volume-unlimited", ("itemCount", state.StoredEntities.Count));
_information.Text = Loc.GetString("comp-storage-window-volume-unlimited", ("itemCount", storedCount));
}
}