DragDropHelper.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using Content.Shared.CCVar;
  2. using Robust.Client.Input;
  3. using Robust.Shared.Configuration;
  4. using Robust.Shared.Map;
  5. namespace Content.Client.Interaction;
  6. /// <summary>
  7. /// Helper for implementing drag and drop interactions.
  8. ///
  9. /// The basic flow for a drag drop interaction as per this helper is:
  10. /// 1. User presses mouse down on something (using class should communicate this to helper by calling MouseDown()).
  11. /// 2. User continues to hold the mouse down and moves the mouse outside of the defined
  12. /// deadzone. OnBeginDrag is invoked to see if a drag should be initiated. If so, initiates a drag.
  13. /// If user didn't move the mouse beyond the deadzone the drag is not initiated (OnEndDrag invoked).
  14. /// 3. Every Update/FrameUpdate, OnContinueDrag is invoked.
  15. /// 4. User lifts mouse up. This is not handled by DragDropHelper. The using class of the helper should
  16. /// do whatever they want and then end the drag by calling EndDrag() (which invokes OnEndDrag).
  17. ///
  18. /// If for any reason the drag is ended, OnEndDrag is invoked.
  19. /// </summary>
  20. /// <typeparam name="T">thing being dragged and dropped</typeparam>
  21. public sealed class DragDropHelper<T>
  22. {
  23. [Dependency] private readonly IInputManager _inputManager = default!;
  24. [Dependency] private readonly IConfigurationManager _cfg = default!;
  25. private readonly OnBeginDrag _onBeginDrag;
  26. private readonly OnEndDrag _onEndDrag;
  27. private readonly OnContinueDrag _onContinueDrag;
  28. private float _deadzone;
  29. /// <summary>
  30. /// Convenience method, current mouse screen position as provided by inputmanager.
  31. /// </summary>
  32. public ScreenCoordinates MouseScreenPosition => _inputManager.MouseScreenPosition;
  33. /// <summary>
  34. /// True if initiated a drag and currently dragging something.
  35. /// I.e. this will be false if we've just had a mousedown over something but the mouse
  36. /// has not moved outside of the drag deadzone.
  37. /// </summary>
  38. public bool IsDragging => _state == DragState.Dragging;
  39. /// <summary>
  40. /// Current thing being dragged or which mouse button is being held down on.
  41. /// </summary>
  42. public T? Dragged { get; private set; }
  43. // screen pos where the mouse down began for the drag
  44. private ScreenCoordinates _mouseDownScreenPos;
  45. private DragState _state = DragState.NotDragging;
  46. private enum DragState : byte
  47. {
  48. NotDragging,
  49. // not dragging yet, waiting to see
  50. // if they hold for long enough
  51. MouseDown,
  52. // currently dragging something
  53. Dragging,
  54. }
  55. /// <param name="onBeginDrag"><see cref="OnBeginDrag"/></param>
  56. /// <param name="onContinueDrag"><see cref="OnContinueDrag"/></param>
  57. /// <param name="onEndDrag"><see cref="OnEndDrag"/></param>
  58. public DragDropHelper(OnBeginDrag onBeginDrag, OnContinueDrag onContinueDrag, OnEndDrag onEndDrag)
  59. {
  60. IoCManager.InjectDependencies(this);
  61. _onBeginDrag = onBeginDrag;
  62. _onEndDrag = onEndDrag;
  63. _onContinueDrag = onContinueDrag;
  64. _cfg.OnValueChanged(CCVars.DragDropDeadZone, SetDeadZone, true);
  65. }
  66. /// <summary>
  67. /// Tell the helper that the mouse button was pressed down on
  68. /// a target, thus a drag has the possibility to begin for this target.
  69. /// Assumes current mouse screen position is the location the mouse was clicked.
  70. ///
  71. /// EndDrag should be called when the drag is done.
  72. /// </summary>
  73. public void MouseDown(T target)
  74. {
  75. if (_state != DragState.NotDragging)
  76. {
  77. EndDrag();
  78. }
  79. Dragged = target;
  80. _state = DragState.MouseDown;
  81. _mouseDownScreenPos = _inputManager.MouseScreenPosition;
  82. }
  83. /// <summary>
  84. /// Stop the current drag / drop operation no matter what state it is in.
  85. /// </summary>
  86. public void EndDrag()
  87. {
  88. Dragged = default;
  89. _state = DragState.NotDragging;
  90. _onEndDrag.Invoke();
  91. }
  92. private void StartDragging()
  93. {
  94. if (_onBeginDrag.Invoke())
  95. {
  96. _state = DragState.Dragging;
  97. }
  98. else
  99. {
  100. EndDrag();
  101. }
  102. }
  103. /// <summary>
  104. /// Should be invoked by using class every FrameUpdate or Update.
  105. /// </summary>
  106. public void Update(float frameTime)
  107. {
  108. switch (_state)
  109. {
  110. // check if dragging should begin
  111. case DragState.MouseDown:
  112. {
  113. var screenPos = _inputManager.MouseScreenPosition;
  114. if ((_mouseDownScreenPos.Position - screenPos.Position).Length() > _deadzone)
  115. {
  116. StartDragging();
  117. }
  118. break;
  119. }
  120. case DragState.Dragging:
  121. {
  122. if (!_onContinueDrag.Invoke(frameTime))
  123. {
  124. EndDrag();
  125. }
  126. break;
  127. }
  128. }
  129. }
  130. private void SetDeadZone(float value)
  131. {
  132. _deadzone = value;
  133. }
  134. }
  135. /// <summary>
  136. /// Invoked when a drag is confirmed and going to be initiated. Implementation should
  137. /// typically set the drag shadow texture based on the target.
  138. /// </summary>
  139. /// <returns>true if drag should begin, false to end.</returns>
  140. public delegate bool OnBeginDrag();
  141. /// <summary>
  142. /// Invoked every frame when drag is ongoing. Typically implementation should
  143. /// make the drag shadow follow the mouse position.
  144. /// </summary>
  145. /// <returns>true if drag should continue, false to end.</returns>
  146. public delegate bool OnContinueDrag(float frameTime);
  147. /// <summary>
  148. /// invoked when
  149. /// the drag drop is ending for any reason. This
  150. /// should typically just clear the drag shadow.
  151. /// </summary>
  152. public delegate void OnEndDrag();