LibraryDropTarget.cs 10 KB

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