LibraryDropTarget.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. isLocalDragInProgress = false;
  65. isMouseDown = false;
  66. isDragInBounds = false;
  67. triggerEndLocalDrag = true;
  68. triggerStartLocalDrag = false;
  69. }
  70. void Input_OnPointerPressed(PointerEvent ev)
  71. {
  72. isMouseDown = true;
  73. mouseDownScreenPos = ev.screenPos;
  74. }
  75. public void Update()
  76. {
  77. Vector2I currentWindowPos = parentWindow.ScreenToWindowPos(Input.PointerPosition);
  78. if (triggerStartLocalDrag)
  79. {
  80. isLocalDragInProgress = true;
  81. triggerStartLocalDrag = false;
  82. if (OnStart != null)
  83. OnStart(currentWindowPos);
  84. }
  85. if (triggerEndLocalDrag)
  86. {
  87. triggerEndLocalDrag = false;
  88. if (OnEnd != null)
  89. OnEnd(currentWindowPos);
  90. }
  91. if (isOSDragActive)
  92. return;
  93. bool externalDragInProgress = DragDrop.DragInProgress && DragDrop.Type == DragDropType.SceneObject;
  94. if (isLocalDragInProgress || externalDragInProgress)
  95. {
  96. if (lastDragWindowPos != currentWindowPos)
  97. {
  98. if (!isDragInBounds)
  99. {
  100. if (Bounds.Contains(currentWindowPos))
  101. {
  102. isDragInBounds = true;
  103. if (OnEnter != null)
  104. OnEnter(currentWindowPos);
  105. }
  106. }
  107. if (OnDrag != null)
  108. OnDrag(currentWindowPos);
  109. if (isDragInBounds)
  110. {
  111. if (!Bounds.Contains(currentWindowPos))
  112. {
  113. isDragInBounds = false;
  114. if (OnLeave != null)
  115. OnLeave();
  116. }
  117. }
  118. lastDragWindowPos = currentWindowPos;
  119. }
  120. }
  121. if (DragDrop.DropInProgress && Bounds.Contains(currentWindowPos))
  122. {
  123. if (DragDrop.Type == DragDropType.Resource)
  124. {
  125. if (OnDropResource != null)
  126. {
  127. ResourceDragDropData resourceDragDrop = (ResourceDragDropData)DragDrop.Data;
  128. OnDropResource(currentWindowPos, resourceDragDrop.Paths);
  129. }
  130. }
  131. else if (DragDrop.Type == DragDropType.SceneObject)
  132. {
  133. if (OnDropSceneObject != null)
  134. {
  135. SceneObjectDragDropData sceneDragDrop = (SceneObjectDragDropData)DragDrop.Data;
  136. OnDropSceneObject(currentWindowPos, sceneDragDrop.Objects);
  137. }
  138. }
  139. isDragInBounds = false;
  140. }
  141. }
  142. public void Destroy()
  143. {
  144. dropTargetOS.Destroy();
  145. dropTargetOS = null;
  146. }
  147. private void DoOnOSDragEnter(int x, int y)
  148. {
  149. isOSDragActive = true;
  150. if (OnEnter != null)
  151. OnEnter(new Vector2I(x, y));
  152. }
  153. private void DoOnOSDragLeave()
  154. {
  155. isOSDragActive = false;
  156. if (OnLeave != null)
  157. OnLeave();
  158. }
  159. private void DoOnOSDrag(int x, int y)
  160. {
  161. if (OnDrag != null)
  162. OnDrag(new Vector2I(x, y));
  163. }
  164. private void DoOnOSDrop(int x, int y)
  165. {
  166. isOSDragActive = false;
  167. if (OnDropResource != null)
  168. OnDropResource(new Vector2I(x, y), dropTargetOS.FilePaths);
  169. }
  170. }
  171. }