Refactor drag and drop to use a shared interface (#2012)

* WIP in progress hours

* Cleanup

* Fix bugle

* Fix nullable error

* Merge fixes

* Merge fixes

* Merge fixes
This commit is contained in:
DrSmugleaf
2020-10-14 15:24:07 +02:00
committed by GitHub
parent f715eed63c
commit cdedaeb12e
37 changed files with 527 additions and 377 deletions

View File

@@ -1,8 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Content.Client.GameObjects.Components.Items;
using Content.Client.Interfaces.GameObjects.Components.Interaction;
using Content.Shared.GameObjects.Components.Storage;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Client.Graphics.Drawing;
using Robust.Client.Interfaces.GameObjects.Components;
using Robust.Client.Player;
@@ -22,13 +23,17 @@ namespace Content.Client.GameObjects.Components.Storage
/// Client version of item storage containers, contains a UI which displays stored entities and their size
/// </summary>
[RegisterComponent]
public class ClientStorageComponent : SharedStorageComponent, IClientDraggable
public class ClientStorageComponent : SharedStorageComponent, IDraggable
{
private Dictionary<EntityUid, int> StoredEntities { get; set; } = new Dictionary<EntityUid, int>();
[Dependency] private readonly IEntityManager _entityManager = default!;
private List<IEntity> _storedEntities = new List<IEntity>();
private int StorageSizeUsed;
private int StorageCapacityMax;
private StorageWindow Window;
public override IReadOnlyList<IEntity> StoredEntities => _storedEntities;
public override void OnAdd()
{
base.OnAdd();
@@ -43,6 +48,20 @@ namespace Content.Client.GameObjects.Components.Storage
base.OnRemove();
}
public override void HandleComponentState(ComponentState curState, ComponentState nextState)
{
base.HandleComponentState(curState, nextState);
if (!(curState is StorageComponentState state))
{
return;
}
_storedEntities = state.StoredEntities
.Select(id => _entityManager.GetEntity(id))
.ToList();
}
public override void HandleNetworkMessage(ComponentMessage message, INetChannel channel, ICommonSession session = null)
{
base.HandleNetworkMessage(message, channel, session);
@@ -69,7 +88,7 @@ namespace Content.Client.GameObjects.Components.Storage
/// <param name="storageState"></param>
private void HandleStorageMessage(StorageHeldItemsMessage storageState)
{
StoredEntities = new Dictionary<EntityUid, int>(storageState.StoredEntities);
_storedEntities = storageState.StoredEntities.Select(id => _entityManager.GetEntity(id)).ToList();
StorageSizeUsed = storageState.StorageSizeUsed;
StorageCapacityMax = storageState.StorageSizeMax;
Window.BuildEntityList();
@@ -100,6 +119,17 @@ namespace Content.Client.GameObjects.Components.Storage
SendNetworkMessage(new RemoveEntityMessage(entityUid));
}
public override bool Remove(IEntity entity)
{
if (_storedEntities.Remove(entity))
{
Dirty();
return true;
}
return false;
}
/// <summary>
/// GUI class for client storage component
/// </summary>
@@ -200,19 +230,25 @@ namespace Content.Client.GameObjects.Components.Storage
var storageList = StorageEntity.StoredEntities;
foreach (var entityUid in storageList)
var storedGrouped = storageList.GroupBy(e => e).Select(e => new
{
var entity = IoCManager.Resolve<IEntityManager>().GetEntity(entityUid.Key);
Entity = e.Key,
Amount = e.Count()
});
foreach (var group in storedGrouped)
{
var entity = group.Entity;
var button = new EntityButton()
{
EntityUid = entityUid.Key,
EntityUid = entity.Uid,
MouseFilter = MouseFilterMode.Stop,
};
button.ActualButton.OnToggled += OnItemButtonToggled;
//Name and Size labels set
button.EntityName.Text = entity.Name;
button.EntitySize.Text = string.Format("{0}", entityUid.Value);
button.EntitySize.Text = group.Amount.ToString();
//Gets entity sprite and assigns it to button texture
if (entity.TryGetComponent(out ISpriteComponent sprite))
@@ -320,17 +356,5 @@ namespace Content.Client.GameObjects.Components.Storage
AddChild(hBoxContainer);
}
}
public bool ClientCanDropOn(CanDropEventArgs eventArgs)
{
//can only drop on placeable surfaces to empty out contents
return eventArgs.Target.HasComponent<PlaceableSurfaceComponent>();
}
public bool ClientCanDrag(CanDragEventArgs eventArgs)
{
//always draggable, at least for now
return true;
}
}
}

View File

@@ -0,0 +1,41 @@
#nullable enable
using Content.Shared.GameObjects.Components.Storage;
using Robust.Shared.GameObjects;
namespace Content.Client.GameObjects.Components.Storage
{
[RegisterComponent]
[ComponentReference(typeof(SharedStorableComponent))]
public class StorableComponent : SharedStorableComponent
{
private int _size;
public override int Size
{
get => _size;
set
{
if (_size == value)
{
return;
}
_size = value;
Dirty();
}
}
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
base.HandleComponentState(curState, nextState);
if (!(curState is StorableComponentState state))
{
return;
}
_size = state.Size;
}
}
}