Implement Entity List Display and rework StorageComponent window (#4140)

* Create EntityListDisplay

* Rework ClientStorage window

* Add styling

* Remove unnecessary colors

* Rename list

* Make scrollbar push content

* Change children update a bit

* Add old index

* Localize ClientStorageComponent

* Add size return

* Remove spaces

* Fix usings
This commit is contained in:
ShadowCommander
2021-08-12 10:05:02 -07:00
committed by GitHub
parent fe4bf059b9
commit 6a1ca13111
4 changed files with 389 additions and 133 deletions

View File

@@ -1,8 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Content.Client.Animations;
using Content.Client.Items.Components;
using Content.Client.Hands;
using Content.Client.UserInterface.Controls;
using Content.Shared.DragDrop;
using Content.Shared.Storage;
using Robust.Client.GameObjects;
@@ -13,9 +14,11 @@ using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Maths;
using Robust.Shared.Network;
using Robust.Shared.Players;
using static Robust.Client.UserInterface.Control;
using static Robust.Client.UserInterface.Controls.BoxContainer;
namespace Content.Client.Storage
@@ -47,6 +50,8 @@ namespace Content.Client.Storage
base.OnAdd();
_window = new StorageWindow(this) {Title = Owner.Name};
_window.EntityList.GenerateItem += GenerateButton;
_window.EntityList.ItemPressed += Interact;
}
protected override void OnRemove()
@@ -104,7 +109,7 @@ namespace Content.Client.Storage
_storedEntities = storageState.StoredEntities.Select(id => Owner.EntityManager.GetEntity(id)).ToList();
StorageSizeUsed = storageState.StorageSizeUsed;
StorageCapacityMax = storageState.StorageSizeMax;
_window?.BuildEntityList();
_window?.BuildEntityList(storageState.StoredEntities.ToList());
}
/// <summary>
@@ -172,14 +177,53 @@ namespace Content.Client.Storage
return false;
}
/// <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);
button.AddChild(new HBoxContainer
{
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")
}
}
});
}
/// <summary>
/// GUI class for client storage component
/// </summary>
private class StorageWindow : SS14Window
{
private Control VSplitContainer;
private readonly BoxContainer _entityList;
private Control _vBox;
private readonly Label _information;
public readonly EntityListDisplay EntityList;
public ClientStorageComponent StorageEntity;
private readonly StyleBoxFlat _hoveredBox = new() { BackgroundColor = Color.Black.WithAlpha(0.35f) };
@@ -189,20 +233,21 @@ namespace Content.Client.Storage
{
StorageEntity = storageEntity;
SetSize = (200, 320);
Title = "Storage Item";
Title = Loc.GetString("comp-storage-window-title");
RectClipContent = true;
var containerButton = new ContainerButton
{
Name = "StorageContainerButton",
MouseFilter = MouseFilterMode.Pass,
};
Contents.AddChild(containerButton);
var innerContainerButton = new PanelContainer
{
PanelOverride = _unHoveredBox,
};
containerButton.AddChild(innerContainerButton);
containerButton.OnPressed += args =>
{
@@ -214,42 +259,30 @@ namespace Content.Client.Storage
}
};
VSplitContainer = new BoxContainer
_vBox = new BoxContainer()
{
Orientation = LayoutOrientation.Vertical,
MouseFilter = MouseFilterMode.Ignore,
};
containerButton.AddChild(VSplitContainer);
containerButton.AddChild(_vBox);
_information = new Label
{
Text = "Items: 0 Volume: 0/0 Stuff",
Text = Loc.GetString("comp-storage-window-volume", ("itemCount", 0), ("usedVolume", 0), ("maxVolume", 0)),
VerticalAlignment = VAlignment.Center
};
VSplitContainer.AddChild(_information);
_vBox.AddChild(_information);
var listScrollContainer = new ScrollContainer
EntityList = new EntityListDisplay
{
VerticalExpand = true,
HorizontalExpand = true,
HScrollEnabled = false,
VScrollEnabled = true,
Name = "EntityListContainer",
};
_entityList = new BoxContainer
{
Orientation = LayoutOrientation.Vertical,
HorizontalExpand = true
};
listScrollContainer.AddChild(_entityList);
VSplitContainer.AddChild(listScrollContainer);
Contents.AddChild(containerButton);
listScrollContainer.OnMouseEntered += args =>
_vBox.AddChild(EntityList);
EntityList.OnMouseEntered += args =>
{
innerContainerButton.PanelOverride = _hoveredBox;
};
listScrollContainer.OnMouseExited += args =>
EntityList.OnMouseExited += args =>
{
innerContainerButton.PanelOverride = _unHoveredBox;
};
@@ -264,122 +297,21 @@ namespace Content.Client.Storage
/// <summary>
/// Loops through stored entities creating buttons for each, updates information labels
/// </summary>
public void BuildEntityList()
public void BuildEntityList(List<EntityUid> entityUids)
{
_entityList.DisposeAllChildren();
var storageList = StorageEntity.StoredEntities;
var storedGrouped = storageList.GroupBy(e => e).Select(e => new
{
Entity = e.Key,
Amount = e.Count()
});
foreach (var group in storedGrouped)
{
var entity = group.Entity;
var button = new EntityButton()
{
EntityUid = entity.Uid,
MouseFilter = MouseFilterMode.Stop,
};
button.ActualButton.OnToggled += OnItemButtonToggled;
//Name and Size labels set
button.EntityName.Text = entity.Name;
button.EntitySize.Text = group.Amount.ToString();
//Gets entity sprite and assigns it to button texture
if (entity.TryGetComponent(out ISpriteComponent? sprite))
{
button.EntitySpriteView.Sprite = sprite;
}
_entityList.AddChild(button);
}
EntityList.PopulateList(entityUids);
//Sets information about entire storage container current capacity
if (StorageEntity.StorageCapacityMax != 0)
{
_information.Text = String.Format("Items: {0}, Stored: {1}/{2}", storageList.Count,
StorageEntity.StorageSizeUsed, StorageEntity.StorageCapacityMax);
_information.Text = Loc.GetString("comp-storage-window-volume", ("itemCount", entityUids.Count),
("usedVolume", StorageEntity.StorageSizeUsed), ("maxVolume", StorageEntity.StorageCapacityMax));
}
else
{
_information.Text = String.Format("Items: {0}", storageList.Count);
_information.Text = Loc.GetString("comp-storage-window-volume-unlimited", ("itemCount", entityUids.Count));
}
}
/// <summary>
/// Function assigned to button toggle which removes the entity from storage
/// </summary>
/// <param name="args"></param>
private void OnItemButtonToggled(BaseButton.ButtonToggledEventArgs args)
{
if (args.Button.Parent is not EntityButton button)
{
return;
}
args.Button.Pressed = false;
StorageEntity.Interact(button.EntityUid);
}
}
/// <summary>
/// Button created for each entity that represents that item in the storage UI, with a texture, and name and size label
/// </summary>
private class EntityButton : Control
{
public EntityUid EntityUid { get; set; }
public Button ActualButton { get; }
public SpriteView EntitySpriteView { get; }
public Label EntityName { get; }
public Label EntitySize { get; }
public EntityButton()
{
ActualButton = new Button
{
HorizontalExpand = true,
VerticalExpand = true,
ToggleMode = true,
MouseFilter = MouseFilterMode.Stop
};
AddChild(ActualButton);
var hBoxContainer = new BoxContainer
{
Orientation = LayoutOrientation.Horizontal
};
EntitySpriteView = new SpriteView
{
MinSize = new Vector2(32.0f, 32.0f),
OverrideDirection = Direction.South
};
EntityName = new Label
{
VerticalAlignment = VAlignment.Center,
HorizontalExpand = true,
Margin = new Thickness(0, 0, 6, 0),
Text = "Backpack",
ClipText = true
};
hBoxContainer.AddChild(EntitySpriteView);
hBoxContainer.AddChild(EntityName);
EntitySize = new Label
{
VerticalAlignment = VAlignment.Bottom,
Text = "Size 6",
Align = Label.AlignMode.Right,
};
hBoxContainer.AddChild(EntitySize);
AddChild(hBoxContainer);
}
}
}
}