Predict StorageComponent (#19682)
This commit is contained in:
60
Content.Client/Storage/UI/StorageUIController.cs
Normal file
60
Content.Client/Storage/UI/StorageUIController.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user