ProjectDropTarget.cs 5.9 KB

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