1
0

SharedDragDropSystem.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using Content.Shared.ActionBlocker;
  2. using Content.Shared.Interaction;
  3. namespace Content.Shared.DragDrop;
  4. public abstract class SharedDragDropSystem : EntitySystem
  5. {
  6. [Dependency] private readonly ActionBlockerSystem _actionBlockerSystem = default!;
  7. [Dependency] private readonly SharedInteractionSystem _interaction = default!;
  8. public override void Initialize()
  9. {
  10. base.Initialize();
  11. SubscribeAllEvent<DragDropRequestEvent>(OnDragDropRequestEvent);
  12. }
  13. private void OnDragDropRequestEvent(DragDropRequestEvent msg, EntitySessionEventArgs args)
  14. {
  15. var dragged = GetEntity(msg.Dragged);
  16. var target = GetEntity(msg.Target);
  17. if (Deleted(dragged) || Deleted(target))
  18. return;
  19. var user = args.SenderSession.AttachedEntity;
  20. if (user == null || !_actionBlockerSystem.CanInteract(user.Value, target))
  21. return;
  22. // must be in range of both the target and the object they are drag / dropping
  23. // Client also does this check but ya know we gotta validate it.
  24. if (!_interaction.InRangeUnobstructed(user.Value, dragged, popup: true)
  25. || !_interaction.InRangeUnobstructed(user.Value, target, popup: true))
  26. {
  27. return;
  28. }
  29. var dragArgs = new DragDropDraggedEvent(user.Value, target);
  30. // trigger dragdrops on the dropped entity
  31. RaiseLocalEvent(dragged, ref dragArgs);
  32. if (dragArgs.Handled)
  33. return;
  34. var dropArgs = new DragDropTargetEvent(user.Value, dragged);
  35. // trigger dragdrops on the target entity (what you are dropping onto)
  36. RaiseLocalEvent(GetEntity(msg.Target), ref dropArgs);
  37. }
  38. }