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,17 +1,17 @@
#nullable enable
using Content.Client.GameObjects.Components.Disposal;
using Content.Client.GameObjects.Components.MedicalScanner;
using Content.Client.Interfaces.GameObjects.Components.Interaction;
using Content.Shared.GameObjects.Components.Body;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.GameObjects;
namespace Content.Client.GameObjects.Components.Body
{
[RegisterComponent]
[ComponentReference(typeof(IBody))]
public class BodyComponent : SharedBodyComponent, IClientDraggable
public class BodyComponent : SharedBodyComponent, IDraggable
{
public bool ClientCanDropOn(CanDropEventArgs eventArgs)
public bool CanDrop(CanDropEventArgs eventArgs)
{
if (eventArgs.Target.HasComponent<DisposalUnitComponent>() ||
eventArgs.Target.HasComponent<MedicalScannerComponent>())
@@ -21,10 +21,5 @@ namespace Content.Client.GameObjects.Components.Body
return false;
}
public bool ClientCanDrag(CanDragEventArgs eventArgs)
{
return true;
}
}
}

View File

@@ -1,19 +1,24 @@
using Content.Client.GameObjects.Components.Strap;
using Content.Client.Interfaces.GameObjects.Components.Interaction;
using Content.Shared.GameObjects.Components.Buckle;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Client.GameObjects.Components.Buckle
{
[RegisterComponent]
public class BuckleComponent : SharedBuckleComponent, IClientDraggable
public class BuckleComponent : SharedBuckleComponent
{
private bool _buckled;
private int? _originalDrawDepth;
public override bool Buckled => _buckled;
public override bool TryBuckle(IEntity user, IEntity to)
{
// TODO: Prediction
return false;
}
public override void HandleComponentState(ComponentState curState, ComponentState nextState)
{
if (!(curState is BuckleComponentState buckle))
@@ -41,15 +46,5 @@ namespace Content.Client.GameObjects.Components.Buckle
_originalDrawDepth = null;
}
}
bool IClientDraggable.ClientCanDropOn(CanDropEventArgs eventArgs)
{
return eventArgs.Target.HasComponent<StrapComponent>();
}
bool IClientDraggable.ClientCanDrag(CanDragEventArgs eventArgs)
{
return true;
}
}
}

View File

@@ -1,22 +1,17 @@
using Content.Client.GameObjects.Components.Items;
using Content.Client.Interfaces.GameObjects.Components.Interaction;
using Content.Shared.GameObjects.Components.GUI;
using Content.Shared.GameObjects.Components.GUI;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.GameObjects;
namespace Content.Client.GameObjects.Components.GUI
{
[RegisterComponent]
public class StrippableComponent : SharedStrippableComponent, IClientDraggable
[ComponentReference(typeof(SharedStrippableComponent))]
public class StrippableComponent : SharedStrippableComponent
{
public bool ClientCanDropOn(CanDropEventArgs eventArgs)
public override bool Drop(DragDropEventArgs args)
{
return eventArgs.Target.HasComponent<HandsComponent>()
&& eventArgs.Target != eventArgs.Dragged && eventArgs.Target == eventArgs.User;
}
public bool ClientCanDrag(CanDragEventArgs eventArgs)
{
return true;
// TODO: Prediction
return false;
}
}
}

View File

@@ -1,7 +1,7 @@
using Content.Client.GameObjects.Components.Disposal;
using Content.Client.Interfaces.GameObjects.Components.Interaction;
using Content.Shared.GameObjects;
using Content.Shared.GameObjects.Components.Items;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Client.Graphics;
using Robust.Client.Interfaces.ResourceManagement;
using Robust.Client.ResourceManagement;
@@ -19,7 +19,7 @@ namespace Content.Client.GameObjects.Components.Items
{
[RegisterComponent]
[ComponentReference(typeof(IItemComponent))]
public class ItemComponent : Component, IItemComponent, IClientDraggable
public class ItemComponent : Component, IItemComponent, IDraggable
{
public override string Name => "Item";
public override uint? NetID => ContentNetIDs.ITEM;
@@ -85,14 +85,15 @@ namespace Content.Client.GameObjects.Components.Items
EquippedPrefix = itemComponentState.EquippedPrefix;
}
bool IClientDraggable.ClientCanDropOn(CanDropEventArgs eventArgs)
bool IDraggable.CanDrop(CanDropEventArgs args)
{
return eventArgs.Target.HasComponent<DisposalUnitComponent>();
return args.Target.HasComponent<DisposalUnitComponent>();
}
bool IClientDraggable.ClientCanDrag(CanDragEventArgs eventArgs)
public bool Drop(DragDropEventArgs args)
{
return true;
// TODO: Shared item class
return false;
}
}
}

View File

@@ -1,11 +1,10 @@
using Content.Client.Interfaces.GameObjects.Components.Interaction;
using Content.Shared.GameObjects.Components.Movement;
using Content.Shared.GameObjects.Components.Movement;
using Robust.Shared.GameObjects;
namespace Content.Client.GameObjects.Components.Movement
{
[RegisterComponent]
public class ClimbingComponent : SharedClimbingComponent, IClientDraggable
public class ClimbingComponent : SharedClimbingComponent
{
public override void HandleComponentState(ComponentState curState, ComponentState nextState)
{
@@ -14,19 +13,9 @@ namespace Content.Client.GameObjects.Components.Movement
return;
}
IsClimbing = climbModeState.Climbing;
IsClimbing = climbModeState.Climbing;
}
public override bool IsClimbing { get; set; }
bool IClientDraggable.ClientCanDropOn(CanDropEventArgs eventArgs)
{
return eventArgs.Target.HasComponent<IClimbable>();
}
bool IClientDraggable.ClientCanDrag(CanDragEventArgs eventArgs)
{
return true;
}
}
}

View File

@@ -1,11 +1,41 @@
#nullable enable
using Content.Shared.GameObjects.Components;
using Robust.Shared.GameObjects;
namespace Content.Client.GameObjects.Components
{
[RegisterComponent]
[ComponentReference(typeof(SharedPlaceableSurfaceComponent))]
public class PlaceableSurfaceComponent : SharedPlaceableSurfaceComponent
{
private bool _isPlaceable;
public override bool IsPlaceable
{
get => _isPlaceable;
set
{
if (_isPlaceable == value)
{
return;
}
_isPlaceable = value;
Dirty();
}
}
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
base.HandleComponentState(curState, nextState);
if (!(curState is PlaceableSurfaceComponentState state))
{
return;
}
_isPlaceable = state.IsPlaceable;
}
}
}

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;
}
}
}

View File

@@ -5,6 +5,7 @@ using Robust.Shared.GameObjects;
namespace Content.Client.GameObjects.Components.Strap
{
[RegisterComponent]
[ComponentReference(typeof(SharedStrapComponent))]
public class StrapComponent : SharedStrapComponent
{
}