LibraryDropTarget.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. /// <summary>
  8. /// Handles various types of drag and drop operations used by the library window.
  9. /// </summary>
  10. public class LibraryDropTarget
  11. {
  12. private const int DragStartDistancePx = 3;
  13. /// <summary>
  14. /// Triggered when a resource drag and drop operation occured over the drop target.
  15. /// </summary>
  16. public Action<Vector2I, string[]> OnDropResource;
  17. /// <summary>
  18. /// Triggered when a scene object drag and drop operation occured over the drop target.
  19. /// </summary>
  20. public Action<Vector2I, SceneObject[]> OnDropSceneObject;
  21. /// <summary>
  22. /// Triggered when a local drag and drop operation is starting (pointer has been pressed and is being dragged while
  23. /// within the drop target).
  24. /// </summary>
  25. public Action<Vector2I> OnStart;
  26. /// <summary>
  27. /// Triggered when a pointer enters the drop target area while a drag operation is in progress.
  28. /// </summary>
  29. public Action<Vector2I> OnEnter;
  30. /// <summary>
  31. /// Triggered when a pointer leaves the drop target area while a drag operation is in progress.
  32. /// </summary>
  33. public Action OnLeave;
  34. /// <summary>
  35. /// Triggered when a pointer moves within the drop target area while a drag operation is in progress.
  36. /// </summary>
  37. public Action<Vector2I> OnDrag;
  38. /// <summary>
  39. /// Triggered when a locally initiated (started within the drop target) drag and drop operation ends.
  40. /// </summary>
  41. public Action<Vector2I> OnEnd;
  42. private readonly EditorWindow parentWindow;
  43. private OSDropTarget dropTargetOS;
  44. private Rect2I bounds;
  45. private bool isMouseDown;
  46. private bool isLocalDragInProgress;
  47. private bool triggerStartLocalDrag;
  48. private bool triggerEndLocalDrag;
  49. private bool isDragInBounds;
  50. private bool isOSDragActive;
  51. private Vector2I mouseDownScreenPos;
  52. private Vector2I lastDragWindowPos;
  53. /// <summary>
  54. /// Area inside which drag and drop operations are allowed to occurr. Relative to the parent library window.
  55. /// </summary>
  56. public Rect2I Bounds
  57. {
  58. get { return bounds; }
  59. set
  60. {
  61. bounds = value;
  62. dropTargetOS.Bounds = bounds;
  63. }
  64. }
  65. /// <summary>
  66. /// Creates a new library drop target.
  67. /// </summary>
  68. /// <param name="window">Window the drop target is part of.</param>
  69. public LibraryDropTarget(EditorWindow window)
  70. {
  71. parentWindow = window;
  72. dropTargetOS = new OSDropTarget(window);
  73. dropTargetOS.OnDrag += DoOnOSDrag;
  74. dropTargetOS.OnDrop += DoOnOSDrop;
  75. dropTargetOS.OnEnter += DoOnOSDragEnter;
  76. dropTargetOS.OnLeave += DoOnOSDragLeave;
  77. Input.OnPointerPressed += Input_OnPointerPressed;
  78. Input.OnPointerReleased += Input_OnPointerReleased;
  79. Input.OnPointerMoved += Input_OnPointerMoved;
  80. }
  81. /// <summary>
  82. /// Triggered when the pointer moved.
  83. /// </summary>
  84. /// <param name="ev">Data about the pointer move event.</param>
  85. void Input_OnPointerMoved(PointerEvent ev)
  86. {
  87. Vector2I currentWindowPos = parentWindow.ScreenToWindowPos(ev.ScreenPos);
  88. if (isMouseDown && !isLocalDragInProgress)
  89. {
  90. Vector2I startWindowPos = parentWindow.ScreenToWindowPos(mouseDownScreenPos);
  91. if (!Bounds.Contains(startWindowPos))
  92. return;
  93. int distance = Vector2I.Distance(startWindowPos, currentWindowPos);
  94. if (distance >= DragStartDistancePx)
  95. triggerStartLocalDrag = true;
  96. }
  97. }
  98. /// <summary>
  99. /// Triggered when the pointer is released.
  100. /// </summary>
  101. /// <param name="ev">Data about the pointer release event.</param>
  102. void Input_OnPointerReleased(PointerEvent ev)
  103. {
  104. if (isLocalDragInProgress)
  105. triggerEndLocalDrag = true;
  106. isLocalDragInProgress = false;
  107. isMouseDown = false;
  108. isDragInBounds = false;
  109. triggerStartLocalDrag = false;
  110. }
  111. /// <summary>
  112. /// Triggered when the pointer is pressed.
  113. /// </summary>
  114. /// <param name="ev">Data about the pointer press event.</param>
  115. void Input_OnPointerPressed(PointerEvent ev)
  116. {
  117. Vector2I currentWindowPos = parentWindow.ScreenToWindowPos(ev.ScreenPos);
  118. if (Bounds.Contains(currentWindowPos))
  119. {
  120. isMouseDown = true;
  121. mouseDownScreenPos = ev.ScreenPos;
  122. }
  123. }
  124. /// <summary>
  125. /// Triggers events and queries for changes in drag and drop operations. Should be called every frame.
  126. /// </summary>
  127. public void Update()
  128. {
  129. Vector2I currentWindowPos = parentWindow.ScreenToWindowPos(Input.PointerPosition);
  130. if (triggerStartLocalDrag)
  131. {
  132. isLocalDragInProgress = true;
  133. triggerStartLocalDrag = false;
  134. if (OnStart != null)
  135. OnStart(currentWindowPos);
  136. }
  137. if (triggerEndLocalDrag)
  138. {
  139. triggerEndLocalDrag = false;
  140. if (OnEnd != null)
  141. OnEnd(currentWindowPos);
  142. }
  143. if (isOSDragActive)
  144. return;
  145. bool externalDragInProgress = DragDrop.DragInProgress && DragDrop.Type == DragDropType.SceneObject;
  146. if (isLocalDragInProgress || externalDragInProgress)
  147. {
  148. if (lastDragWindowPos != currentWindowPos)
  149. {
  150. if (!isDragInBounds)
  151. {
  152. if (Bounds.Contains(currentWindowPos))
  153. {
  154. isDragInBounds = true;
  155. if (OnEnter != null)
  156. OnEnter(currentWindowPos);
  157. }
  158. }
  159. if (OnDrag != null)
  160. OnDrag(currentWindowPos);
  161. if (isDragInBounds)
  162. {
  163. if (!Bounds.Contains(currentWindowPos))
  164. {
  165. isDragInBounds = false;
  166. if (OnLeave != null)
  167. OnLeave();
  168. }
  169. }
  170. lastDragWindowPos = currentWindowPos;
  171. }
  172. }
  173. if (DragDrop.DropInProgress && Bounds.Contains(currentWindowPos))
  174. {
  175. if (DragDrop.Type == DragDropType.Resource)
  176. {
  177. if (OnDropResource != null)
  178. {
  179. ResourceDragDropData resourceDragDrop = (ResourceDragDropData)DragDrop.Data;
  180. OnDropResource(currentWindowPos, resourceDragDrop.Paths);
  181. }
  182. }
  183. else if (DragDrop.Type == DragDropType.SceneObject)
  184. {
  185. if (OnDropSceneObject != null)
  186. {
  187. SceneObjectDragDropData sceneDragDrop = (SceneObjectDragDropData)DragDrop.Data;
  188. OnDropSceneObject(currentWindowPos, sceneDragDrop.Objects);
  189. }
  190. }
  191. isDragInBounds = false;
  192. }
  193. }
  194. /// <summary>
  195. /// Destroy the drop target. You should call this when done with the drop target.
  196. /// </summary>
  197. public void Destroy()
  198. {
  199. dropTargetOS.Destroy();
  200. dropTargetOS = null;
  201. }
  202. /// <summary>
  203. /// Triggered when an OS drag event has entered the drop target area.
  204. /// </summary>
  205. /// <param name="x">X coordinate of the pointer relative to the drop target area.</param>
  206. /// <param name="y">Y coordinate of the pointer relative to the drop target area.</param>
  207. private void DoOnOSDragEnter(int x, int y)
  208. {
  209. isOSDragActive = true;
  210. if (OnEnter != null)
  211. OnEnter(new Vector2I(x, y));
  212. }
  213. /// <summary>
  214. /// Triggered when an OS drag event has left the drop target area.
  215. /// </summary>
  216. private void DoOnOSDragLeave()
  217. {
  218. isOSDragActive = false;
  219. if (OnLeave != null)
  220. OnLeave();
  221. }
  222. /// <summary>
  223. /// Triggered when a pointer moves while an OS drag event is occuring over the drop target area.
  224. /// </summary>
  225. /// <param name="x">X coordinate of the pointer relative to the drop target area.</param>
  226. /// <param name="y">Y coordinate of the pointer relative to the drop target area.</param>
  227. private void DoOnOSDrag(int x, int y)
  228. {
  229. if (OnDrag != null)
  230. OnDrag(new Vector2I(x, y));
  231. }
  232. /// <summary>
  233. /// Triggered when the pointer was released during an OS drag and drop operation while over the drop target area.
  234. /// </summary>
  235. /// <param name="x">X coordinate of the pointer relative to the drop target area.</param>
  236. /// <param name="y">Y coordinate of the pointer relative to the drop target area.</param>
  237. private void DoOnOSDrop(int x, int y)
  238. {
  239. isOSDragActive = false;
  240. if (OnDropResource != null)
  241. OnDropResource(new Vector2I(x, y), dropTargetOS.FilePaths);
  242. }
  243. }
  244. }