LibraryDropTarget.cs 6.4 KB

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