ProjectDropTarget.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. triggerStartDrag = 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 (triggerStartDrag)
  79. {
  80. isDragInProgress = true;
  81. triggerStartDrag = false;
  82. if (OnStart != null)
  83. OnStart(currentWindowPos);
  84. }
  85. if (triggerEndDrag)
  86. {
  87. triggerEndDrag = false;
  88. if (OnEnd != null)
  89. OnEnd(currentWindowPos);
  90. }
  91. if (isOSDragActive)
  92. return;
  93. if (isDragInProgress)
  94. {
  95. if (lastDragWindowPos != currentWindowPos)
  96. {
  97. if (!isDragInBounds)
  98. {
  99. if (Bounds.Contains(currentWindowPos))
  100. {
  101. isDragInBounds = true;
  102. if (OnEnter != null)
  103. OnEnter(currentWindowPos);
  104. }
  105. }
  106. if (OnDrag != null)
  107. OnDrag(currentWindowPos);
  108. if (isDragInBounds)
  109. {
  110. if (!Bounds.Contains(currentWindowPos))
  111. {
  112. isDragInBounds = false;
  113. if (OnLeave != null)
  114. OnLeave();
  115. }
  116. }
  117. lastDragWindowPos = currentWindowPos;
  118. }
  119. }
  120. if (DragDrop.DropInProgress)
  121. {
  122. if (DragDrop.Type == DragDropType.Resource)
  123. {
  124. if (OnDropResource != null)
  125. {
  126. ResourceDragDropData resourceDragDrop = (ResourceDragDropData) DragDrop.Data;
  127. OnDropResource(currentWindowPos, resourceDragDrop.Paths);
  128. }
  129. }
  130. else if (DragDrop.Type == DragDropType.SceneObject)
  131. {
  132. if (OnDropSceneObject != null)
  133. {
  134. SceneObjectDragDropData sceneDragDrop = (SceneObjectDragDropData) DragDrop.Data;
  135. OnDropSceneObject(currentWindowPos, sceneDragDrop.Objects);
  136. }
  137. }
  138. }
  139. }
  140. public void Destroy()
  141. {
  142. dropTargetOS.Destroy();
  143. dropTargetOS = null;
  144. }
  145. private void DoOnOSDragEnter(int x, int y)
  146. {
  147. isOSDragActive = true;
  148. if (OnEnter != null)
  149. OnEnter(new Vector2I(x, y));
  150. }
  151. private void DoOnOSDragLeave()
  152. {
  153. isOSDragActive = false;
  154. if (OnLeave != null)
  155. OnLeave();
  156. }
  157. private void DoOnOSDrag(int x, int y)
  158. {
  159. if (OnDrag != null)
  160. OnDrag(new Vector2I(x, y));
  161. }
  162. private void DoOnOSDrop(int x, int y)
  163. {
  164. isOSDragActive = false;
  165. if (OnDropResource != null)
  166. OnDropResource(new Vector2I(x, y), dropTargetOS.FilePaths);
  167. }
  168. }
  169. }