Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grabbag of newinv cherrypicks #2954

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 53 additions & 10 deletions Content.Client/Storage/StorageBoundUserInterface.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,80 @@
using Content.Client.Storage.Systems;
using Content.Client.UserInterface.Systems.Storage;
using Content.Client.UserInterface.Systems.Storage.Controls;
using Content.Shared.Storage;
using JetBrains.Annotations;
using Robust.Client.UserInterface;

namespace Content.Client.Storage;

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

private readonly StorageSystem _storage;
private StorageWindow? _window;

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

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

if (_entManager.TryGetComponent<StorageComponent>(Owner, out var comp))
_storage.OpenStorageWindow((Owner, comp));
_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;
}

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

Reclaim();
}

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

_storage.CloseStorageWindow(Owner);
_window.Visible = false;
}

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

_window.Visible = true;
}

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

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

Expand All @@ -13,114 +14,91 @@ 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 readonly List<Entity<StorageComponent>> _openStorages = new();
public int OpenStorageAmount => _openStorages.Count;

public event Action<Entity<StorageComponent>>? StorageUpdated;
public event Action<Entity<StorageComponent>?>? StorageOrderChanged;
private Dictionary<EntityUid, ItemStorageLocation> _oldStoredItems = new();

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

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

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

public void OpenStorageWindow(Entity<StorageComponent> entity)
{
if (_openStorages.Contains(entity))
{
if (_openStorages.LastOrDefault() == entity)
{
CloseStorageWindow((entity, entity.Comp));
}
else
{
var storages = new ValueList<Entity<StorageComponent>>(_openStorages);
var reverseStorages = storages.Reverse();
component.Grid.Clear();
component.Grid.AddRange(state.Grid);
component.MaxItemSize = state.MaxItemSize;
component.Whitelist = state.Whitelist;
component.Blacklist = state.Blacklist;

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

CloseStorageBoundUserInterface(storageEnt.Owner);
_openStorages.Remove(entity);
}
}
return;
foreach (var item in component.StoredItems)
{
_oldStoredItems.Add(item.Key, item.Value);
}

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

public void CloseStorageWindow(Entity<StorageComponent?> entity)
{
if (!Resolve(entity, ref entity.Comp, false))
return;

if (!_openStorages.Contains((entity, entity.Comp)))
return;
foreach (var (nent, location) in state.StoredItems)
{
var ent = EnsureEntity<StorageComponent>(nent, uid);
component.StoredItems[ent] = location;
}

var storages = new ValueList<Entity<StorageComponent>>(_openStorages);
var reverseStorages = storages.Reverse();
component.SavedLocations.Clear();

foreach (var storage in reverseStorages)
foreach (var loc in state.SavedLocations)
{
CloseStorageBoundUserInterface(storage.Owner);
_openStorages.Remove(storage);
if (storage.Owner == entity.Owner)
break;
component.SavedLocations[loc.Key] = new(loc.Value);
}

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

private void ClearNonParentStorages(EntityUid uid)
{
var storages = new ValueList<Entity<StorageComponent>>(_openStorages);
var reverseStorages = storages.Reverse();
var uiDirty = !component.StoredItems.SequenceEqual(_oldStoredItems);

foreach (var storage in reverseStorages)
if (uiDirty && UI.TryGetOpenUi<StorageBoundUserInterface>(uid, StorageComponent.StorageUiKey.Key, out var storageBui))
{
if (storage.Comp.Container.Contains(uid))
break;
storageBui.Refresh();
// Make sure nesting still updated.
var player = _player.LocalEntity;

CloseStorageBoundUserInterface(storage.Owner);
_openStorages.Remove(storage);
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();
}
}
}

private void CloseStorageBoundUserInterface(Entity<UserInterfaceComponent?> entity)
public override void UpdateUI(Entity<StorageComponent?> entity)
{
if (!Resolve(entity, ref entity.Comp, false))
return;

if (entity.Comp.ClientOpenInterfaces.GetValueOrDefault(StorageComponent.StorageUiKey.Key) is not { } bui)
return;
if (UI.TryGetOpenUi<StorageBoundUserInterface>(entity.Owner, StorageComponent.StorageUiKey.Key, out var sBui))
{
sBui.Refresh();
}
}

bui.Close();
protected override void HideStorageWindow(EntityUid uid, EntityUid actor)
{
if (UI.TryGetOpenUi<StorageBoundUserInterface>(uid, StorageComponent.StorageUiKey.Key, out var storageBui))
{
storageBui.Hide();
}
}

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

/// <inheritdoc />
Expand All @@ -142,7 +120,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 @@ -74,7 +74,7 @@ private void OnKeyBindDown(Control control)
/// internal recentlyInteractedWindows tracking.
/// </summary>
/// <param name="window"></param>
private void SetMostRecentlyInteractedWindow(BaseWindow window)
public void SetMostRecentlyInteractedWindow(BaseWindow window)
{
// Search through the list and see if already added.
// (This search is backwards since it's fairly common that the user is clicking the same
Expand Down Expand Up @@ -134,7 +134,6 @@ public bool HasClosableWindow()
if (window.IsOpen)
return true;

recentlyInteractedWindows.RemoveAt(i);
// continue going down the list, hoping to find a still-open window
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ private void OnScreenLoad()
ReloadHotbar();
}

public void Setup(HandsContainer handsContainer, StorageContainer storageContainer)
public void Setup(HandsContainer handsContainer)
{
_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,7 +1,6 @@
<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 @@ -10,13 +9,31 @@
Orientation="Vertical"
HorizontalAlignment="Center">
<BoxContainer Orientation="Vertical">
<BoxContainer Name="StorageContainer"
<BoxContainer Name="SingleStorageContainer"
Access="Public"
HorizontalAlignment="Center"
HorizontalExpand="True"
Margin="10">
<storage:StorageContainer
Name="StoragePanel"
Visible="False"/>
</BoxContainer>
<BoxContainer Name="DoubleStorageContainer"
Access="Public"
HorizontalAlignment="Stretch"
HorizontalExpand="True"
Margin="10">
<BoxContainer Name="LeftStorageContainer"
Access="Public"
HorizontalAlignment="Left"
HorizontalExpand="True"
VerticalAlignment="Bottom"
Margin="10">
</BoxContainer>
<BoxContainer Name="RightStorageContainer"
Access="Public"
HorizontalAlignment="Right"
HorizontalExpand="True"
VerticalAlignment="Bottom"
Margin="10">
</BoxContainer>
</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, StoragePanel);
hotbarController.Setup(HandContainer);
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.Pass;
MouseFilter = MouseFilterMode.Stop;

TooltipSupplier = SupplyTooltip;

Expand Down Expand Up @@ -105,8 +105,11 @@ 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
Loading