Skip to content

Commit

Permalink
Emisse told me I can name it the grinch (#34905)
Browse files Browse the repository at this point in the history
  • Loading branch information
VasilisThePikachu authored Feb 5, 2025
2 parents e58d031 + f2c74ef commit 6824ab4
Show file tree
Hide file tree
Showing 13 changed files with 400 additions and 785 deletions.
65 changes: 12 additions & 53 deletions Content.Client/Storage/StorageBoundUserInterface.cs
Original file line number Diff line number Diff line change
@@ -1,80 +1,39 @@
using Content.Client.UserInterface.Systems.Storage;
using Content.Client.UserInterface.Systems.Storage.Controls;
using Content.Client.Storage.Systems;
using Content.Shared.Storage;
using JetBrains.Annotations;
using Robust.Client.UserInterface;

namespace Content.Client.Storage;

[UsedImplicitly]
public sealed class StorageBoundUserInterface : BoundUserInterface
{
private StorageWindow? _window;
[Dependency] private readonly IEntityManager _entManager = default!;

private readonly StorageSystem _storage;

[Obsolete] public override bool DeferredClose => false;

public StorageBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
IoCManager.InjectDependencies(this);
_storage = _entManager.System<StorageSystem>();
}

protected override void Open()
{
base.Open();

_window = IoCManager.Resolve<IUserInterfaceManager>()
.GetUIController<StorageUIController>()
.CreateStorageWindow(Owner);

if (EntMan.TryGetComponent(Owner, out StorageComponent? storage))
{
_window.UpdateContainer((Owner, storage));
}

_window.OnClose += Close;
_window.FlagDirty();
}

public void Refresh()
{
_window?.FlagDirty();
}

public void Reclaim()
{
if (_window == null)
return;

_window.OnClose -= Close;
_window.Orphan();
_window = null;
if (_entManager.TryGetComponent<StorageComponent>(Owner, out var comp))
_storage.OpenStorageWindow((Owner, comp));
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);

Reclaim();
}

public void Hide()
{
if (_window == null)
return;

_window.Visible = false;
}

public void Show()
{
if (_window == null)
if (!disposing)
return;

_window.Visible = true;
}

public void ReOpen()
{
_window?.Orphan();
_window = null;
Open();
_storage.CloseStorageWindow(Owner);
}
}

134 changes: 76 additions & 58 deletions Content.Client/Storage/Systems/StorageSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
using Content.Shared.Hands;
using Content.Shared.Storage;
using Content.Shared.Storage.EntitySystems;
using Robust.Client.Player;
using Robust.Shared.GameStates;
using Robust.Shared.Collections;
using Robust.Shared.Map;
using Robust.Shared.Timing;

Expand All @@ -14,95 +13,114 @@ namespace Content.Client.Storage.Systems;
public sealed class StorageSystem : SharedStorageSystem
{
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IPlayerManager _player = default!;
[Dependency] private readonly EntityPickupAnimationSystem _entityPickupAnimation = default!;

private Dictionary<EntityUid, ItemStorageLocation> _oldStoredItems = new();
private readonly List<Entity<StorageComponent>> _openStorages = new();
public int OpenStorageAmount => _openStorages.Count;

public event Action<Entity<StorageComponent>>? StorageUpdated;
public event Action<Entity<StorageComponent>?>? StorageOrderChanged;

public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<StorageComponent, ComponentHandleState>(OnStorageHandleState);
SubscribeLocalEvent<StorageComponent, ComponentShutdown>(OnShutdown);
SubscribeNetworkEvent<PickupAnimationEvent>(HandlePickupAnimation);
SubscribeAllEvent<AnimateInsertingEntitiesEvent>(HandleAnimatingInsertingEntities);
}

private void OnStorageHandleState(EntityUid uid, StorageComponent component, ref ComponentHandleState args)
public override void UpdateUI(Entity<StorageComponent?> entity)
{
if (args.Current is not StorageComponentState state)
return;

component.Grid.Clear();
component.Grid.AddRange(state.Grid);
component.MaxItemSize = state.MaxItemSize;
component.Whitelist = state.Whitelist;
component.Blacklist = state.Blacklist;

_oldStoredItems.Clear();
if (Resolve(entity.Owner, ref entity.Comp))
StorageUpdated?.Invoke((entity, entity.Comp));
}

foreach (var item in component.StoredItems)
public void OpenStorageWindow(Entity<StorageComponent> entity)
{
if (_openStorages.Contains(entity))
{
_oldStoredItems.Add(item.Key, item.Value);
}
if (_openStorages.LastOrDefault() == entity)
{
CloseStorageWindow((entity, entity.Comp));
}
else
{
var storages = new ValueList<Entity<StorageComponent>>(_openStorages);
var reverseStorages = storages.Reverse();

component.StoredItems.Clear();
foreach (var storageEnt in reverseStorages)
{
if (storageEnt == entity)
break;

foreach (var (nent, location) in state.StoredItems)
{
var ent = EnsureEntity<StorageComponent>(nent, uid);
component.StoredItems[ent] = location;
CloseStorageBoundUserInterface(storageEnt.Owner);
_openStorages.Remove(entity);
}
}
return;
}

component.SavedLocations.Clear();
ClearNonParentStorages(entity);
_openStorages.Add(entity);
Entity<StorageComponent>? last = _openStorages.LastOrDefault();
StorageOrderChanged?.Invoke(last);
}

foreach (var loc in state.SavedLocations)
{
component.SavedLocations[loc.Key] = new(loc.Value);
}
public void CloseStorageWindow(Entity<StorageComponent?> entity)
{
if (!Resolve(entity, ref entity.Comp, false))
return;

var uiDirty = !component.StoredItems.SequenceEqual(_oldStoredItems);
if (!_openStorages.Contains((entity, entity.Comp)))
return;

if (uiDirty && UI.TryGetOpenUi<StorageBoundUserInterface>(uid, StorageComponent.StorageUiKey.Key, out var storageBui))
{
storageBui.Refresh();
// Make sure nesting still updated.
var player = _player.LocalEntity;
var storages = new ValueList<Entity<StorageComponent>>(_openStorages);
var reverseStorages = storages.Reverse();

if (NestedStorage && player != null && ContainerSystem.TryGetContainingContainer((uid, null, null), out var container) &&
UI.TryGetOpenUi<StorageBoundUserInterface>(container.Owner, StorageComponent.StorageUiKey.Key, out var containerBui))
{
containerBui.Hide();
}
else
{
storageBui.Show();
}
foreach (var storage in reverseStorages)
{
CloseStorageBoundUserInterface(storage.Owner);
_openStorages.Remove(storage);
if (storage.Owner == entity.Owner)
break;
}

Entity<StorageComponent>? last = null;
if (_openStorages.Any())
last = _openStorages.LastOrDefault();
StorageOrderChanged?.Invoke(last);
}

public override void UpdateUI(Entity<StorageComponent?> entity)
private void ClearNonParentStorages(EntityUid uid)
{
if (UI.TryGetOpenUi<StorageBoundUserInterface>(entity.Owner, StorageComponent.StorageUiKey.Key, out var sBui))
var storages = new ValueList<Entity<StorageComponent>>(_openStorages);
var reverseStorages = storages.Reverse();

foreach (var storage in reverseStorages)
{
sBui.Refresh();
if (storage.Comp.Container.Contains(uid))
break;

CloseStorageBoundUserInterface(storage.Owner);
_openStorages.Remove(storage);
}
}

protected override void HideStorageWindow(EntityUid uid, EntityUid actor)
private void CloseStorageBoundUserInterface(Entity<UserInterfaceComponent?> entity)
{
if (UI.TryGetOpenUi<StorageBoundUserInterface>(uid, StorageComponent.StorageUiKey.Key, out var storageBui))
{
storageBui.Hide();
}
if (!Resolve(entity, ref entity.Comp, false))
return;

if (entity.Comp.ClientOpenInterfaces.GetValueOrDefault(StorageComponent.StorageUiKey.Key) is not { } bui)
return;

bui.Close();
}

protected override void ShowStorageWindow(EntityUid uid, EntityUid actor)
private void OnShutdown(Entity<StorageComponent> ent, ref ComponentShutdown args)
{
if (UI.TryGetOpenUi<StorageBoundUserInterface>(uid, StorageComponent.StorageUiKey.Key, out var storageBui))
{
storageBui.Show();
}
CloseStorageWindow((ent, ent.Comp));
}

/// <inheritdoc />
Expand All @@ -124,7 +142,7 @@ public void PickupAnimation(EntityUid item, EntityCoordinates initialCoords, Ent
{
if (!_timing.IsFirstTimePredicted)
return;

if (TransformSystem.InRange(finalCoords, initialCoords, 0.1f) ||
!Exists(initialCoords.EntityId) || !Exists(finalCoords.EntityId))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ private void OnScreenLoad()
ReloadHotbar();
}

public void Setup(HandsContainer handsContainer)
public void Setup(HandsContainer handsContainer, StorageContainer storageContainer)
{
_inventory = UIManager.GetUIController<InventoryUIController>();
_hands = UIManager.GetUIController<HandsUIController>();
_storage = UIManager.GetUIController<StorageUIController>();
_hands.RegisterHandContainer(handsContainer);
_storage.RegisterStorageContainer(storageContainer);
}

public void ReloadHotbar()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<widgets:HotbarGui
xmlns="https://spacestation14.io"
xmlns:inventory="clr-namespace:Content.Client.UserInterface.Systems.Inventory.Controls"
xmlns:storage="clr-namespace:Content.Client.UserInterface.Systems.Storage.Controls"
xmlns:hands="clr-namespace:Content.Client.UserInterface.Systems.Hands.Controls"
xmlns:widgets="clr-namespace:Content.Client.UserInterface.Systems.Hotbar.Widgets"
Name="HotbarInterface"
Expand All @@ -12,8 +13,10 @@
<BoxContainer Name="StorageContainer"
Access="Public"
HorizontalAlignment="Center"
HorizontalExpand="True"
Margin="10">
<storage:StorageContainer
Name="StoragePanel"
Visible="False"/>
</BoxContainer>
<BoxContainer Orientation="Horizontal" Name="Hotbar" HorizontalAlignment="Center">
<inventory:ItemSlotButtonContainer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public HotbarGui()
StatusPanelLeft.SetSide(HandUILocation.Left);
var hotbarController = UserInterfaceManager.GetUIController<HotbarUIController>();

hotbarController.Setup(HandContainer);
hotbarController.Setup(HandContainer, StoragePanel);
LayoutContainer.SetGrowVertical(this, LayoutContainer.GrowDirection.Begin);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public ItemGridPiece(Entity<ItemComponent> entity, ItemStorageLocation location,
Location = location;

Visible = true;
MouseFilter = MouseFilterMode.Stop;
MouseFilter = MouseFilterMode.Pass;

TooltipSupplier = SupplyTooltip;

Expand Down Expand Up @@ -105,11 +105,8 @@ protected override void Draw(DrawingHandleScreen handle)
return;
}

if (_storageController.IsDragging && _storageController.DraggingGhost?.Entity == Entity &&
_storageController.DraggingGhost != this)
{
if (_storageController.IsDragging && _storageController.DraggingGhost?.Entity == Entity && _storageController.DraggingGhost != this)
return;
}

var adjustedShape = _entityManager.System<ItemSystem>().GetAdjustedItemShape((Entity, itemComponent), Location.Rotation, Vector2i.Zero);
var boundingGrid = adjustedShape.GetBoundingBox();
Expand Down
Loading

0 comments on commit 6824ab4

Please sign in to comment.