2019-07-31 13:48:50 +02:00
|
|
|
using System.Collections.Generic;
|
2020-10-14 15:24:07 +02:00
|
|
|
using System.Linq;
|
2021-02-03 22:07:13 +00:00
|
|
|
using Content.Client.Animations;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Client.Hands;
|
2021-09-04 19:42:32 +02:00
|
|
|
using Content.Client.Items.Components;
|
2021-08-12 10:05:02 -07:00
|
|
|
using Content.Client.UserInterface.Controls;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.DragDrop;
|
2021-09-04 19:42:32 +02:00
|
|
|
using Content.Shared.Stacks;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Storage;
|
2021-09-04 19:42:32 +02:00
|
|
|
using Content.Shared.Storage.Components;
|
2021-02-03 22:07:13 +00:00
|
|
|
using Robust.Client.GameObjects;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Client.Graphics;
|
2020-08-13 14:40:27 +02:00
|
|
|
using Robust.Client.Player;
|
2019-04-15 21:11:38 -06:00
|
|
|
using Robust.Client.UserInterface;
|
|
|
|
|
using Robust.Client.UserInterface.Controls;
|
|
|
|
|
using Robust.Client.UserInterface.CustomControls;
|
|
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.IoC;
|
2021-08-12 10:05:02 -07:00
|
|
|
using Robust.Shared.Localization;
|
2019-05-17 00:07:24 -04:00
|
|
|
using Robust.Shared.Maths;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.Network;
|
2020-04-20 10:36:02 +01:00
|
|
|
using Robust.Shared.Players;
|
2021-08-12 10:05:02 -07:00
|
|
|
using static Robust.Client.UserInterface.Control;
|
2021-07-18 18:39:31 +02:00
|
|
|
using static Robust.Client.UserInterface.Controls.BoxContainer;
|
2018-04-22 06:11:38 -05:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Client.Storage
|
2018-04-22 06:11:38 -05:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Client version of item storage containers, contains a UI which displays stored entities and their size
|
|
|
|
|
/// </summary>
|
2019-07-31 15:02:36 +02:00
|
|
|
[RegisterComponent]
|
2020-10-14 15:24:07 +02:00
|
|
|
public class ClientStorageComponent : SharedStorageComponent, IDraggable
|
2018-04-22 06:11:38 -05:00
|
|
|
{
|
2020-11-27 11:00:49 +01:00
|
|
|
private List<IEntity> _storedEntities = new();
|
2018-04-22 06:11:38 -05:00
|
|
|
private int StorageSizeUsed;
|
|
|
|
|
private int StorageCapacityMax;
|
2021-03-10 14:48:29 +01:00
|
|
|
private StorageWindow? _window;
|
2018-04-22 06:11:38 -05:00
|
|
|
|
2020-10-14 15:24:07 +02:00
|
|
|
public override IReadOnlyList<IEntity> StoredEntities => _storedEntities;
|
|
|
|
|
|
2021-06-19 19:41:26 -07:00
|
|
|
protected override void Initialize()
|
2021-02-17 14:02:36 +01:00
|
|
|
{
|
|
|
|
|
base.Initialize();
|
2021-02-21 12:38:56 +01:00
|
|
|
|
2021-02-17 14:02:36 +01:00
|
|
|
// Hide stackVisualizer on start
|
2021-02-28 12:02:08 +01:00
|
|
|
ChangeStorageVisualization(SharedBagState.Close);
|
2021-02-17 14:02:36 +01:00
|
|
|
}
|
|
|
|
|
|
2021-06-19 19:41:26 -07:00
|
|
|
protected override void OnAdd()
|
2018-04-22 06:11:38 -05:00
|
|
|
{
|
|
|
|
|
base.OnAdd();
|
|
|
|
|
|
2021-03-10 14:48:29 +01:00
|
|
|
_window = new StorageWindow(this) {Title = Owner.Name};
|
2021-08-12 10:05:02 -07:00
|
|
|
_window.EntityList.GenerateItem += GenerateButton;
|
|
|
|
|
_window.EntityList.ItemPressed += Interact;
|
2018-04-22 06:11:38 -05:00
|
|
|
}
|
|
|
|
|
|
2021-06-19 19:41:26 -07:00
|
|
|
protected override void OnRemove()
|
2018-04-22 06:11:38 -05:00
|
|
|
{
|
2021-03-10 14:48:29 +01:00
|
|
|
_window?.Dispose();
|
2018-04-22 06:11:38 -05:00
|
|
|
base.OnRemove();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 14:48:29 +01:00
|
|
|
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
|
2020-10-14 15:24:07 +02:00
|
|
|
{
|
|
|
|
|
base.HandleComponentState(curState, nextState);
|
|
|
|
|
|
2020-11-26 14:33:31 +01:00
|
|
|
if (curState is not StorageComponentState state)
|
2020-10-14 15:24:07 +02:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_storedEntities = state.StoredEntities
|
2020-11-18 15:45:53 +01:00
|
|
|
.Select(id => Owner.EntityManager.GetEntity(id))
|
2020-10-14 15:24:07 +02:00
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 14:48:29 +01:00
|
|
|
public override void HandleNetworkMessage(ComponentMessage message, INetChannel channel, ICommonSession? session = null)
|
2018-04-22 06:11:38 -05:00
|
|
|
{
|
2020-04-20 10:36:02 +01:00
|
|
|
base.HandleNetworkMessage(message, channel, session);
|
|
|
|
|
|
2018-04-22 06:11:38 -05:00
|
|
|
switch (message)
|
|
|
|
|
{
|
|
|
|
|
//Updates what we are storing for the UI
|
|
|
|
|
case StorageHeldItemsMessage msg:
|
|
|
|
|
HandleStorageMessage(msg);
|
|
|
|
|
break;
|
|
|
|
|
//Opens the UI
|
2019-11-13 17:37:46 -05:00
|
|
|
case OpenStorageUIMessage _:
|
2020-09-07 01:17:24 -07:00
|
|
|
ToggleUI();
|
2018-04-22 06:11:38 -05:00
|
|
|
break;
|
2019-11-13 17:37:46 -05:00
|
|
|
case CloseStorageUIMessage _:
|
2018-10-25 17:35:34 -07:00
|
|
|
CloseUI();
|
2018-06-20 16:49:13 -04:00
|
|
|
break;
|
2021-02-03 22:07:13 +00:00
|
|
|
case AnimateInsertingEntitiesMessage msg:
|
|
|
|
|
HandleAnimatingInsertingEntities(msg);
|
|
|
|
|
break;
|
2018-04-22 06:11:38 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Copies received values from server about contents of storage container
|
|
|
|
|
/// </summary>
|
2020-09-13 14:23:52 +02:00
|
|
|
/// <param name="storageState"></param>
|
|
|
|
|
private void HandleStorageMessage(StorageHeldItemsMessage storageState)
|
2018-04-22 06:11:38 -05:00
|
|
|
{
|
2020-11-18 15:45:53 +01:00
|
|
|
_storedEntities = storageState.StoredEntities.Select(id => Owner.EntityManager.GetEntity(id)).ToList();
|
2020-09-13 14:23:52 +02:00
|
|
|
StorageSizeUsed = storageState.StorageSizeUsed;
|
|
|
|
|
StorageCapacityMax = storageState.StorageSizeMax;
|
2021-08-12 10:05:02 -07:00
|
|
|
_window?.BuildEntityList(storageState.StoredEntities.ToList());
|
2018-04-22 06:11:38 -05:00
|
|
|
}
|
|
|
|
|
|
2021-02-03 22:07:13 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Animate the newly stored entities in <paramref name="msg"/> flying towards this storage's position
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="msg"></param>
|
|
|
|
|
private void HandleAnimatingInsertingEntities(AnimateInsertingEntitiesMessage msg)
|
|
|
|
|
{
|
|
|
|
|
for (var i = 0; msg.StoredEntities.Count > i; i++)
|
|
|
|
|
{
|
|
|
|
|
var entityId = msg.StoredEntities[i];
|
2021-08-06 19:20:27 +02:00
|
|
|
var initialPosition = msg.EntityPositions[i];
|
2021-02-03 22:07:13 +00:00
|
|
|
|
|
|
|
|
if (Owner.EntityManager.TryGetEntity(entityId, out var entity))
|
|
|
|
|
{
|
2021-08-06 19:20:27 +02:00
|
|
|
ReusableAnimations.AnimateEntityPickup(entity, initialPosition, Owner.Transform.LocalPosition);
|
2021-02-03 22:07:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-22 06:11:38 -05:00
|
|
|
/// <summary>
|
2020-09-07 01:17:24 -07:00
|
|
|
/// Opens the storage UI if closed. Closes it if opened.
|
2018-04-22 06:11:38 -05:00
|
|
|
/// </summary>
|
2020-09-07 01:17:24 -07:00
|
|
|
private void ToggleUI()
|
2018-04-22 06:11:38 -05:00
|
|
|
{
|
2021-03-10 14:48:29 +01:00
|
|
|
if (_window == null) return;
|
2021-02-21 12:38:56 +01:00
|
|
|
|
2021-03-10 14:48:29 +01:00
|
|
|
if (_window.IsOpen)
|
2021-09-04 19:42:32 +02:00
|
|
|
{
|
2021-03-10 14:48:29 +01:00
|
|
|
_window.Close();
|
2021-09-04 19:42:32 +02:00
|
|
|
ChangeStorageVisualization(SharedBagState.Close);
|
|
|
|
|
}
|
2020-09-07 01:17:24 -07:00
|
|
|
else
|
2021-09-04 19:42:32 +02:00
|
|
|
{
|
2021-03-27 08:01:12 +00:00
|
|
|
_window.OpenCentered();
|
2021-09-04 19:42:32 +02:00
|
|
|
ChangeStorageVisualization(SharedBagState.Open);
|
|
|
|
|
}
|
2018-04-22 06:11:38 -05:00
|
|
|
}
|
|
|
|
|
|
2018-10-25 17:35:34 -07:00
|
|
|
private void CloseUI()
|
|
|
|
|
{
|
2021-09-04 19:42:32 +02:00
|
|
|
if (_window == null) return;
|
|
|
|
|
|
|
|
|
|
_window.Close();
|
|
|
|
|
ChangeStorageVisualization(SharedBagState.Close);
|
|
|
|
|
|
2018-10-25 17:35:34 -07:00
|
|
|
}
|
2021-02-21 12:38:56 +01:00
|
|
|
|
2021-02-17 14:02:36 +01:00
|
|
|
private void ChangeStorageVisualization(SharedBagState state)
|
|
|
|
|
{
|
|
|
|
|
if (Owner.TryGetComponent<AppearanceComponent>(out var appearanceComponent))
|
|
|
|
|
{
|
|
|
|
|
appearanceComponent.SetData(SharedBagOpenVisuals.BagState, state);
|
2021-09-04 19:42:32 +02:00
|
|
|
if (Owner.HasComponent<ItemCounterComponent>())
|
|
|
|
|
{
|
|
|
|
|
appearanceComponent.SetData(StackVisuals.Hide, state == SharedBagState.Close);
|
|
|
|
|
}
|
2021-02-17 14:02:36 +01:00
|
|
|
}
|
|
|
|
|
}
|
2018-10-25 17:35:34 -07:00
|
|
|
|
2018-04-22 06:11:38 -05:00
|
|
|
/// <summary>
|
|
|
|
|
/// Function for clicking one of the stored entity buttons in the UI, tells server to remove that entity
|
|
|
|
|
/// </summary>
|
2020-09-13 14:23:52 +02:00
|
|
|
/// <param name="entityUid"></param>
|
|
|
|
|
private void Interact(EntityUid entityUid)
|
2018-04-22 06:11:38 -05:00
|
|
|
{
|
2020-09-13 14:23:52 +02:00
|
|
|
SendNetworkMessage(new RemoveEntityMessage(entityUid));
|
2018-04-22 06:11:38 -05:00
|
|
|
}
|
|
|
|
|
|
2020-10-14 15:24:07 +02:00
|
|
|
public override bool Remove(IEntity entity)
|
|
|
|
|
{
|
|
|
|
|
if (_storedEntities.Remove(entity))
|
|
|
|
|
{
|
|
|
|
|
Dirty();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 10:05:02 -07:00
|
|
|
/// <summary>
|
|
|
|
|
/// Button created for each entity that represents that item in the storage UI, with a texture, and name and size label
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void GenerateButton(EntityUid entityUid, Control button)
|
|
|
|
|
{
|
|
|
|
|
if (!Owner.EntityManager.TryGetEntity(entityUid, out var entity))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
entity.TryGetComponent(out ISpriteComponent? sprite);
|
|
|
|
|
entity.TryGetComponent(out ItemComponent? item);
|
|
|
|
|
|
2021-09-19 19:56:04 +02:00
|
|
|
button.AddChild(new BoxContainer
|
2021-08-12 10:05:02 -07:00
|
|
|
{
|
2021-09-19 19:56:04 +02:00
|
|
|
Orientation = LayoutOrientation.Horizontal,
|
2021-08-12 10:05:02 -07:00
|
|
|
SeparationOverride = 2,
|
|
|
|
|
Children =
|
|
|
|
|
{
|
|
|
|
|
new SpriteView
|
|
|
|
|
{
|
|
|
|
|
HorizontalAlignment = HAlignment.Left,
|
|
|
|
|
VerticalAlignment = VAlignment.Center,
|
|
|
|
|
MinSize = new Vector2(32.0f, 32.0f),
|
|
|
|
|
OverrideDirection = Direction.South,
|
|
|
|
|
Sprite = sprite
|
|
|
|
|
},
|
|
|
|
|
new Label
|
|
|
|
|
{
|
|
|
|
|
HorizontalExpand = true,
|
|
|
|
|
ClipText = true,
|
|
|
|
|
Text = entity.Name
|
|
|
|
|
},
|
|
|
|
|
new Label
|
|
|
|
|
{
|
|
|
|
|
Align = Label.AlignMode.Right,
|
|
|
|
|
Text = item?.Size.ToString() ?? Loc.GetString("no-item-size")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-22 06:11:38 -05:00
|
|
|
/// <summary>
|
|
|
|
|
/// GUI class for client storage component
|
|
|
|
|
/// </summary>
|
|
|
|
|
private class StorageWindow : SS14Window
|
|
|
|
|
{
|
2021-08-12 10:05:02 -07:00
|
|
|
private Control _vBox;
|
2020-11-21 14:02:00 +01:00
|
|
|
private readonly Label _information;
|
2021-08-12 10:05:02 -07:00
|
|
|
public readonly EntityListDisplay EntityList;
|
2018-04-22 06:11:38 -05:00
|
|
|
public ClientStorageComponent StorageEntity;
|
|
|
|
|
|
2020-11-27 11:00:49 +01:00
|
|
|
private readonly StyleBoxFlat _hoveredBox = new() { BackgroundColor = Color.Black.WithAlpha(0.35f) };
|
|
|
|
|
private readonly StyleBoxFlat _unHoveredBox = new() { BackgroundColor = Color.Black.WithAlpha(0.0f) };
|
2020-05-11 18:38:49 -05:00
|
|
|
|
2021-03-10 14:48:29 +01:00
|
|
|
public StorageWindow(ClientStorageComponent storageEntity)
|
2019-05-24 16:24:32 -04:00
|
|
|
{
|
2021-03-10 14:48:29 +01:00
|
|
|
StorageEntity = storageEntity;
|
2021-03-15 01:38:08 -07:00
|
|
|
SetSize = (200, 320);
|
2021-08-12 10:05:02 -07:00
|
|
|
Title = Loc.GetString("comp-storage-window-title");
|
2019-05-17 00:07:24 -04:00
|
|
|
RectClipContent = true;
|
|
|
|
|
|
2020-05-10 21:38:50 -05:00
|
|
|
var containerButton = new ContainerButton
|
|
|
|
|
{
|
2021-08-12 10:05:02 -07:00
|
|
|
Name = "StorageContainerButton",
|
2020-05-11 18:38:49 -05:00
|
|
|
MouseFilter = MouseFilterMode.Pass,
|
2020-05-10 21:38:50 -05:00
|
|
|
};
|
2021-08-12 10:05:02 -07:00
|
|
|
Contents.AddChild(containerButton);
|
2020-05-11 18:38:49 -05:00
|
|
|
|
|
|
|
|
var innerContainerButton = new PanelContainer
|
|
|
|
|
{
|
|
|
|
|
PanelOverride = _unHoveredBox,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
containerButton.AddChild(innerContainerButton);
|
2020-05-10 21:38:50 -05:00
|
|
|
containerButton.OnPressed += args =>
|
|
|
|
|
{
|
2021-03-10 14:48:29 +01:00
|
|
|
var controlledEntity = IoCManager.Resolve<IPlayerManager>().LocalPlayer?.ControlledEntity;
|
2020-05-10 21:38:50 -05:00
|
|
|
|
2021-03-10 14:48:29 +01:00
|
|
|
if (controlledEntity?.TryGetComponent(out HandsComponent? hands) ?? false)
|
2020-05-10 21:38:50 -05:00
|
|
|
{
|
|
|
|
|
StorageEntity.SendNetworkMessage(new InsertEntityMessage());
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-05-10 21:41:03 -05:00
|
|
|
|
2021-08-12 10:05:02 -07:00
|
|
|
_vBox = new BoxContainer()
|
2020-05-11 18:38:49 -05:00
|
|
|
{
|
2021-07-18 18:39:31 +02:00
|
|
|
Orientation = LayoutOrientation.Vertical,
|
2020-05-11 18:38:49 -05:00
|
|
|
MouseFilter = MouseFilterMode.Ignore,
|
|
|
|
|
};
|
2021-08-12 10:05:02 -07:00
|
|
|
containerButton.AddChild(_vBox);
|
2020-11-21 14:02:00 +01:00
|
|
|
_information = new Label
|
2019-05-17 00:07:24 -04:00
|
|
|
{
|
2021-08-12 10:05:02 -07:00
|
|
|
Text = Loc.GetString("comp-storage-window-volume", ("itemCount", 0), ("usedVolume", 0), ("maxVolume", 0)),
|
2021-02-21 12:38:56 +01:00
|
|
|
VerticalAlignment = VAlignment.Center
|
2019-05-17 00:07:24 -04:00
|
|
|
};
|
2021-08-12 10:05:02 -07:00
|
|
|
_vBox.AddChild(_information);
|
2018-04-22 06:11:38 -05:00
|
|
|
|
2021-08-12 10:05:02 -07:00
|
|
|
EntityList = new EntityListDisplay
|
2019-05-17 00:07:24 -04:00
|
|
|
{
|
2021-08-12 10:05:02 -07:00
|
|
|
Name = "EntityListContainer",
|
2019-05-17 00:07:24 -04:00
|
|
|
};
|
2021-08-12 10:05:02 -07:00
|
|
|
_vBox.AddChild(EntityList);
|
|
|
|
|
EntityList.OnMouseEntered += args =>
|
2020-05-11 18:38:49 -05:00
|
|
|
{
|
2020-11-21 14:02:00 +01:00
|
|
|
innerContainerButton.PanelOverride = _hoveredBox;
|
2020-05-11 18:38:49 -05:00
|
|
|
};
|
|
|
|
|
|
2021-08-12 10:05:02 -07:00
|
|
|
EntityList.OnMouseExited += args =>
|
2020-05-11 18:38:49 -05:00
|
|
|
{
|
|
|
|
|
innerContainerButton.PanelOverride = _unHoveredBox;
|
|
|
|
|
};
|
2018-04-22 06:11:38 -05:00
|
|
|
}
|
2018-10-25 17:35:34 -07:00
|
|
|
|
|
|
|
|
public override void Close()
|
|
|
|
|
{
|
|
|
|
|
StorageEntity.SendNetworkMessage(new CloseStorageUIMessage());
|
|
|
|
|
base.Close();
|
|
|
|
|
}
|
2018-04-22 06:11:38 -05:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Loops through stored entities creating buttons for each, updates information labels
|
|
|
|
|
/// </summary>
|
2021-08-12 10:05:02 -07:00
|
|
|
public void BuildEntityList(List<EntityUid> entityUids)
|
2018-04-22 06:11:38 -05:00
|
|
|
{
|
2021-08-12 10:05:02 -07:00
|
|
|
EntityList.PopulateList(entityUids);
|
2018-04-22 06:11:38 -05:00
|
|
|
|
|
|
|
|
//Sets information about entire storage container current capacity
|
2018-05-08 08:20:15 +02:00
|
|
|
if (StorageEntity.StorageCapacityMax != 0)
|
2018-04-22 06:11:38 -05:00
|
|
|
{
|
2021-08-12 10:05:02 -07:00
|
|
|
_information.Text = Loc.GetString("comp-storage-window-volume", ("itemCount", entityUids.Count),
|
|
|
|
|
("usedVolume", StorageEntity.StorageSizeUsed), ("maxVolume", StorageEntity.StorageCapacityMax));
|
2018-04-22 06:11:38 -05:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-08-12 10:05:02 -07:00
|
|
|
_information.Text = Loc.GetString("comp-storage-window-volume-unlimited", ("itemCount", entityUids.Count));
|
2021-03-10 14:48:29 +01:00
|
|
|
}
|
2018-04-22 06:11:38 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|