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:
@@ -1,12 +1,14 @@
|
||||
using System;
|
||||
using Content.Shared.GameObjects.Components.Strap;
|
||||
using Content.Shared.GameObjects.EntitySystems;
|
||||
using Content.Shared.Interfaces.GameObjects.Components;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.GameObjects.Components.Buckle
|
||||
{
|
||||
public abstract class SharedBuckleComponent : Component, IActionBlocker, IEffectBlocker
|
||||
public abstract class SharedBuckleComponent : Component, IActionBlocker, IEffectBlocker, IDraggable
|
||||
{
|
||||
public sealed override string Name => "Buckle";
|
||||
|
||||
@@ -17,6 +19,8 @@ namespace Content.Shared.GameObjects.Components.Buckle
|
||||
/// </summary>
|
||||
public abstract bool Buckled { get; }
|
||||
|
||||
public abstract bool TryBuckle(IEntity user, IEntity to);
|
||||
|
||||
bool IActionBlocker.CanMove()
|
||||
{
|
||||
return !Buckled;
|
||||
@@ -31,6 +35,16 @@ namespace Content.Shared.GameObjects.Components.Buckle
|
||||
{
|
||||
return !Buckled;
|
||||
}
|
||||
|
||||
bool IDraggable.CanDrop(CanDropEventArgs args)
|
||||
{
|
||||
return args.Target.HasComponent<SharedStrapComponent>();
|
||||
}
|
||||
|
||||
public bool Drop(DragDropEventArgs args)
|
||||
{
|
||||
return TryBuckle(args.User, args.Dragged);
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
|
||||
@@ -1,16 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Content.Shared.GameObjects.Components.Items;
|
||||
using Content.Shared.GameObjects.EntitySystems;
|
||||
using Content.Shared.Interfaces.GameObjects.Components;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameObjects.Components.UserInterface;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Serialization;
|
||||
using static Content.Shared.GameObjects.Components.Inventory.EquipmentSlotDefines;
|
||||
|
||||
namespace Content.Shared.GameObjects.Components.GUI
|
||||
{
|
||||
public class SharedStrippableComponent : Component
|
||||
public abstract class SharedStrippableComponent : Component, IDraggable
|
||||
{
|
||||
public override string Name => "Strippable";
|
||||
|
||||
public bool CanBeStripped(IEntity by)
|
||||
{
|
||||
return by != Owner
|
||||
&& by.HasComponent<ISharedHandsComponent>()
|
||||
&& ActionBlockerSystem.CanInteract(by);
|
||||
}
|
||||
|
||||
bool IDraggable.CanDrop(CanDropEventArgs args)
|
||||
{
|
||||
return args.Target != args.Dragged
|
||||
&& args.Target == args.User
|
||||
&& CanBeStripped(args.User);
|
||||
}
|
||||
|
||||
public abstract bool Drop(DragDropEventArgs args);
|
||||
|
||||
[NetSerializable, Serializable]
|
||||
public enum StrippingUiKey
|
||||
{
|
||||
|
||||
@@ -5,10 +5,11 @@ using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameObjects.Components;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Serialization;
|
||||
using Content.Shared.Interfaces.GameObjects.Components;
|
||||
|
||||
namespace Content.Shared.GameObjects.Components.Movement
|
||||
{
|
||||
public abstract class SharedClimbingComponent : Component, IActionBlocker, ICollideSpecial
|
||||
public abstract class SharedClimbingComponent : Component, IActionBlocker, ICollideSpecial, IDraggable
|
||||
{
|
||||
public sealed override string Name => "Climbing";
|
||||
public sealed override uint? NetID => ContentNetIDs.CLIMBING;
|
||||
@@ -45,6 +46,16 @@ namespace Content.Shared.GameObjects.Components.Movement
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IDraggable.CanDrop(CanDropEventArgs args)
|
||||
{
|
||||
return args.Target.HasComponent<IClimbable>();
|
||||
}
|
||||
|
||||
bool IDraggable.Drop(DragDropEventArgs args)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
@@ -1,9 +1,25 @@
|
||||
using System;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.GameObjects.Components
|
||||
{
|
||||
public abstract class SharedPlaceableSurfaceComponent : Component
|
||||
{
|
||||
public override string Name => "PlaceableSurface";
|
||||
public override uint? NetID => ContentNetIDs.PLACEABLE_SURFACE;
|
||||
|
||||
public virtual bool IsPlaceable { get; set; }
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public class PlaceableSurfaceComponentState : ComponentState
|
||||
{
|
||||
public readonly bool IsPlaceable;
|
||||
|
||||
public PlaceableSurfaceComponentState(bool placeable) : base(ContentNetIDs.PLACEABLE_SURFACE)
|
||||
{
|
||||
IsPlaceable = placeable;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.GameObjects.Components.Storage
|
||||
{
|
||||
public abstract class SharedStorableComponent : Component
|
||||
{
|
||||
public override string Name => "Storable";
|
||||
public override uint? NetID => ContentNetIDs.STORABLE;
|
||||
|
||||
public virtual int Size { get; set; }
|
||||
|
||||
public override void ExposeData(ObjectSerializer serializer)
|
||||
{
|
||||
base.ExposeData(serializer);
|
||||
|
||||
serializer.DataField(this, s => s.Size, "size", 1);
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public class StorableComponentState : ComponentState
|
||||
{
|
||||
public readonly int Size;
|
||||
|
||||
public StorableComponentState(int size) : base(ContentNetIDs.STORABLE)
|
||||
{
|
||||
Size = size;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,71 @@
|
||||
using System;
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Content.Shared.GameObjects.EntitySystems;
|
||||
using Content.Shared.Interfaces.GameObjects.Components;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.GameObjects.Components.Storage
|
||||
{
|
||||
public abstract class SharedStorageComponent : Component
|
||||
public abstract class SharedStorageComponent : Component, IDraggable
|
||||
{
|
||||
public override string Name => "Storage";
|
||||
public override uint? NetID => ContentNetIDs.INVENTORY;
|
||||
|
||||
public abstract IReadOnlyList<IEntity>? StoredEntities { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Removes from the storage container and updates the stored value
|
||||
/// </summary>
|
||||
/// <param name="entity">The entity to remove</param>
|
||||
/// <returns>True if no longer in storage, false otherwise</returns>
|
||||
public abstract bool Remove(IEntity entity);
|
||||
|
||||
public bool CanDrop(CanDropEventArgs args)
|
||||
{
|
||||
return args.Target.TryGetComponent(out SharedPlaceableSurfaceComponent? placeable) &&
|
||||
placeable.IsPlaceable;
|
||||
}
|
||||
|
||||
public bool Drop(DragDropEventArgs eventArgs)
|
||||
{
|
||||
if (!ActionBlockerSystem.CanInteract(eventArgs.User))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var storedEntities = StoredEntities?.ToArray();
|
||||
|
||||
if (storedEntities == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// empty everything out
|
||||
foreach (var storedEntity in storedEntities)
|
||||
{
|
||||
if (Remove(storedEntity))
|
||||
{
|
||||
storedEntity.Transform.WorldPosition = eventArgs.DropLocation.Position;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public class StorageComponentState : ComponentState
|
||||
{
|
||||
public readonly EntityUid[] StoredEntities;
|
||||
|
||||
public StorageComponentState(EntityUid[] storedEntities) : base(ContentNetIDs.INVENTORY)
|
||||
{
|
||||
StoredEntities = storedEntities;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -19,14 +76,14 @@ namespace Content.Shared.GameObjects.Components.Storage
|
||||
{
|
||||
public readonly int StorageSizeMax;
|
||||
public readonly int StorageSizeUsed;
|
||||
public Dictionary<EntityUid, int> StoredEntities;
|
||||
public readonly EntityUid[] StoredEntities;
|
||||
|
||||
public StorageHeldItemsMessage(Dictionary<EntityUid, int> storedentities, int storageused, int storagemaxsize)
|
||||
public StorageHeldItemsMessage(EntityUid[] storedEntities, int storageUsed, int storageMaxSize)
|
||||
{
|
||||
Directed = true;
|
||||
StorageSizeMax = storagemaxsize;
|
||||
StorageSizeUsed = storageused;
|
||||
StoredEntities = storedentities;
|
||||
StorageSizeMax = storageMaxSize;
|
||||
StorageSizeUsed = storageUsed;
|
||||
StoredEntities = storedEntities;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -79,6 +79,8 @@
|
||||
public const uint BLOCKGAME_ARCADE = 1073;
|
||||
public const uint BODY_PART = 1074;
|
||||
public const uint CRAYONS = 1075;
|
||||
public const uint PLACEABLE_SURFACE = 1076;
|
||||
public const uint STORABLE = 1077;
|
||||
|
||||
// Net IDs for integration tests.
|
||||
public const uint PREDICTION_TEST = 10001;
|
||||
|
||||
@@ -445,7 +445,7 @@ namespace Content.Shared.GameObjects.EntitySystems
|
||||
bool popup = false)
|
||||
{
|
||||
var user = args.User;
|
||||
var dropped = args.Dropped;
|
||||
var dropped = args.Dragged;
|
||||
var target = args.Target;
|
||||
|
||||
if (!InRangeUnobstructed(user, target, range, collisionMask, predicate, ignoreInsideBlocker))
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
using System;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Map;
|
||||
|
||||
namespace Content.Shared.Interfaces.GameObjects.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// This interface allows the component's entity to be dragged and dropped
|
||||
/// by mouse onto another entity and gives it behavior when that occurs.
|
||||
/// </summary>
|
||||
public interface IDragDrop
|
||||
{
|
||||
/// <summary>
|
||||
/// Invoked server-side when this component's entity is being dragged
|
||||
/// and dropped on another before invoking <see cref="DragDrop"/>.
|
||||
/// Note that other drag and drop interactions may be attempted if
|
||||
/// this one fails.
|
||||
/// </summary>
|
||||
/// <param name="eventArgs"></param>
|
||||
/// <returns>true if <see cref="eventArgs"/> is valid, false otherwise.</returns>
|
||||
bool CanDragDrop(DragDropEventArgs eventArgs);
|
||||
|
||||
/// <summary>
|
||||
/// Invoked server-side when this component's entity is being dragged
|
||||
/// and dropped on another.
|
||||
/// Note that other drag and drop interactions may be attempted if
|
||||
/// this one fails.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// true if an interaction occurred and no further interaction should
|
||||
/// be processed for this drop.
|
||||
/// </returns>
|
||||
bool DragDrop(DragDropEventArgs eventArgs);
|
||||
}
|
||||
|
||||
public class DragDropEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="DragDropEventArgs"/>.
|
||||
/// </summary>
|
||||
/// <param name="user">The entity doing the drag and drop.</param>
|
||||
/// <param name="dropLocation">The location where <see cref="dropped"/> is being dropped.</param>
|
||||
/// <param name="dropped">The entity that is being dragged and dropped.</param>
|
||||
/// <param name="target">The entity that <see cref="dropped"/> is being dropped onto.</param>
|
||||
public DragDropEventArgs(IEntity user, EntityCoordinates dropLocation, IEntity dropped, IEntity target)
|
||||
{
|
||||
User = user;
|
||||
DropLocation = dropLocation;
|
||||
Dropped = dropped;
|
||||
Target = target;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The entity doing the drag and drop.
|
||||
/// </summary>
|
||||
public IEntity User { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The location where <see cref="Dropped"/> is being dropped.
|
||||
/// </summary>
|
||||
public EntityCoordinates DropLocation { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The entity that is being dragged and dropped.
|
||||
/// </summary>
|
||||
public IEntity Dropped { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The entity that <see cref="Dropped"/> is being dropped onto.
|
||||
/// </summary>
|
||||
public IEntity Target { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
using System;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Map;
|
||||
|
||||
namespace Content.Shared.Interfaces.GameObjects.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// This interface allows a local client to initiate dragging of the component's
|
||||
/// entity by mouse, for drag and drop interactions.
|
||||
/// </summary>
|
||||
public interface IDraggable
|
||||
{
|
||||
/// <summary>
|
||||
/// Invoked when an user is attempting to initiate a drag with
|
||||
/// this component's entity in range. It's fine to return true even if there
|
||||
/// wouldn't be any valid targets - just return true if this entity is in a
|
||||
/// "draggable" state.
|
||||
/// </summary>
|
||||
/// <param name="args">
|
||||
/// The information about the drag, such as who is doing it.
|
||||
/// </param>
|
||||
/// <returns>True if the drag should be initiated, false otherwise.</returns>
|
||||
bool CanStartDrag(StartDragDropEventArgs args)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invoked on entities visible to the user to check if this component's
|
||||
/// entity can be dropped on the indicated target entity.
|
||||
/// No need to check range / reachability in here.
|
||||
/// Returning true will cause the target entity to be highlighted as
|
||||
/// a potential target and allow dropping when in range.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// True if target is a valid target to be dropped on by this component's
|
||||
/// entity, false otherwise.
|
||||
/// </returns>
|
||||
bool CanDrop(CanDropEventArgs args);
|
||||
|
||||
/// <summary>
|
||||
/// Invoked when this component's entity is being dropped on another.
|
||||
/// Other drag and drop interactions may be attempted if this one fails.
|
||||
/// </summary>
|
||||
/// <param name="args">
|
||||
/// The information about the drag, such as who is doing it.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// True if an interaction occurred and no further interaction should
|
||||
/// be processed for this drop, false otherwise.
|
||||
/// </returns>
|
||||
bool Drop(DragDropEventArgs args)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public class StartDragDropEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="StartDragDropEventArgs"/>.
|
||||
/// </summary>
|
||||
/// <param name="user">The entity doing the drag and drop.</param>
|
||||
/// <param name="dragged">The entity that is being dragged and dropped.</param>
|
||||
public StartDragDropEventArgs(IEntity user, IEntity dragged)
|
||||
{
|
||||
User = user;
|
||||
Dragged = dragged;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The entity doing the drag and drop.
|
||||
/// </summary>
|
||||
public IEntity User { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The entity that is being dragged.
|
||||
/// </summary>
|
||||
public IEntity Dragged { get; }
|
||||
}
|
||||
|
||||
public class CanDropEventArgs : StartDragDropEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="CanDropEventArgs"/>.
|
||||
/// </summary>
|
||||
/// <param name="user">The entity doing the drag and drop.</param>
|
||||
/// <param name="dragged">The entity that is being dragged and dropped.</param>
|
||||
/// <param name="target">The entity that <see cref="dropped"/> is being dropped onto.</param>
|
||||
public CanDropEventArgs(IEntity user, IEntity dragged, IEntity target) : base(user, dragged)
|
||||
{
|
||||
Target = target;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The entity that <see cref="StartDragDropEventArgs.Dragged"/>
|
||||
/// is being dropped onto.
|
||||
/// </summary>
|
||||
public IEntity Target { get; }
|
||||
}
|
||||
|
||||
public class DragDropEventArgs : CanDropEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new instance of <see cref="DragDropEventArgs"/>.
|
||||
/// </summary>
|
||||
/// <param name="user">The entity doing the drag and drop.</param>
|
||||
/// <param name="dropLocation">The location where <see cref="dropped"/> is being dropped.</param>
|
||||
/// <param name="dragged">The entity that is being dragged and dropped.</param>
|
||||
/// <param name="target">The entity that <see cref="dropped"/> is being dropped onto.</param>
|
||||
public DragDropEventArgs(IEntity user, EntityCoordinates dropLocation, IEntity dragged, IEntity target) : base(user, dragged, target)
|
||||
{
|
||||
DropLocation = dropLocation;
|
||||
}
|
||||
/// <summary>
|
||||
/// The location where <see cref="StartDragDropEventArgs.Dragged"/>
|
||||
/// is being dropped.
|
||||
/// </summary>
|
||||
public EntityCoordinates DropLocation { get; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user