LibraryDropTarget.cs 10 KB

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