ProjectDropTarget.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. public class ProjectDropTarget
  8. {
  9. private const int DragStartDistancePx = 3;
  10. public Action<Vector2I, string[]> OnDrop;
  11. public Action<Vector2I> OnStart;
  12. public Action<Vector2I> OnEnter;
  13. public Action OnLeave;
  14. public Action<Vector2I> OnDrag;
  15. public Action<Vector2I> OnEnd;
  16. private readonly EditorWindow parentWindow;
  17. private OSDropTarget dropTargetOS;
  18. private Rect2I bounds;
  19. private bool isMouseDown;
  20. private bool isDragInProgress;
  21. private bool triggerStartDrag;
  22. private bool triggerEndDrag;
  23. private bool isDragInBounds;
  24. private bool isOSDragActive;
  25. private Vector2I mouseDownScreenPos;
  26. private Vector2I lastDragWindowPos;
  27. public Rect2I Bounds
  28. {
  29. get { return bounds; }
  30. set
  31. {
  32. bounds = value;
  33. dropTargetOS.Bounds = bounds;
  34. }
  35. }
  36. public ProjectDropTarget(EditorWindow window)
  37. {
  38. parentWindow = window;
  39. dropTargetOS = new OSDropTarget(window);
  40. dropTargetOS.OnDrag += DoOnOSDrag;
  41. dropTargetOS.OnDrop += DoOnOSDrop;
  42. dropTargetOS.OnEnter += DoOnOSDragEnter;
  43. dropTargetOS.OnLeave += DoOnOSDragLeave;
  44. Input.OnPointerPressed += Input_OnPointerPressed;
  45. Input.OnPointerReleased += Input_OnPointerReleased;
  46. Input.OnPointerMoved += Input_OnPointerMoved;
  47. }
  48. void Input_OnPointerMoved(PointerEvent ev)
  49. {
  50. Vector2I currentWindowPos = parentWindow.ScreenToWindowPos(ev.screenPos);
  51. if (isMouseDown && !isDragInProgress)
  52. {
  53. Vector2I startWindowPos = parentWindow.ScreenToWindowPos(mouseDownScreenPos);
  54. if (!Bounds.Contains(startWindowPos))
  55. return;
  56. int distance = Vector2I.Distance(startWindowPos, currentWindowPos);
  57. if (distance >= DragStartDistancePx)
  58. triggerStartDrag = true;
  59. }
  60. }
  61. void Input_OnPointerReleased(PointerEvent ev)
  62. {
  63. isDragInProgress = false;
  64. isMouseDown = false;
  65. isDragInBounds = false;
  66. triggerEndDrag = true;
  67. }
  68. void Input_OnPointerPressed(PointerEvent ev)
  69. {
  70. isMouseDown = true;
  71. mouseDownScreenPos = ev.screenPos;
  72. }
  73. public void Update()
  74. {
  75. Vector2I currentWindowPos = parentWindow.ScreenToWindowPos(Input.PointerPosition);
  76. if (triggerStartDrag)
  77. {
  78. isDragInProgress = true;
  79. triggerStartDrag = false;
  80. if (OnStart != null)
  81. OnStart(currentWindowPos);
  82. }
  83. if (triggerEndDrag)
  84. {
  85. triggerEndDrag = false;
  86. if (OnEnd != null)
  87. OnEnd(currentWindowPos);
  88. }
  89. if (isOSDragActive)
  90. return;
  91. if (isDragInProgress)
  92. {
  93. if (lastDragWindowPos != currentWindowPos)
  94. {
  95. if (!isDragInBounds)
  96. {
  97. if (Bounds.Contains(currentWindowPos))
  98. {
  99. isDragInBounds = true;
  100. if (OnEnter != null)
  101. OnEnter(currentWindowPos);
  102. }
  103. }
  104. if (OnDrag != null)
  105. OnDrag(currentWindowPos);
  106. if (isDragInBounds)
  107. {
  108. if (!Bounds.Contains(currentWindowPos))
  109. {
  110. isDragInBounds = false;
  111. if (OnLeave != null)
  112. OnLeave();
  113. }
  114. }
  115. lastDragWindowPos = currentWindowPos;
  116. }
  117. }
  118. if (DragDrop.DropInProgress && DragDrop.Type == DragDropType.Resource)
  119. {
  120. if (OnDrop != null)
  121. {
  122. ResourceDragDropData resourceDragDrop = (ResourceDragDropData)DragDrop.Data;
  123. OnDrop(currentWindowPos, resourceDragDrop.Paths);
  124. }
  125. }
  126. }
  127. public void Destroy()
  128. {
  129. dropTargetOS.Destroy();
  130. dropTargetOS = null;
  131. }
  132. private void DoOnOSDragEnter(int x, int y)
  133. {
  134. isOSDragActive = true;
  135. if (OnEnter != null)
  136. OnEnter(new Vector2I(x, y));
  137. }
  138. private void DoOnOSDragLeave()
  139. {
  140. isOSDragActive = false;
  141. if (OnLeave != null)
  142. OnLeave();
  143. }
  144. private void DoOnOSDrag(int x, int y)
  145. {
  146. if (OnDrag != null)
  147. OnDrag(new Vector2I(x, y));
  148. }
  149. private void DoOnOSDrop(int x, int y)
  150. {
  151. isOSDragActive = false;
  152. if (OnDrop != null)
  153. OnDrop(new Vector2I(x, y), dropTargetOS.FilePaths);
  154. }
  155. }
  156. }