ProjectDropTarget.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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[]> OnDrop;
  11. public Action<Vector2I> OnStart;
  12. public Action<Vector2I> OnEnter;
  13. public Action OnLeave;
  14. public Action<Vector2I> OnDrag;
  15. private readonly EditorWindow parentWindow;
  16. private OSDropTarget dropTargetOS;
  17. private Rect2I bounds;
  18. private bool isMouseDown;
  19. private bool isDragInProgress;
  20. private bool triggerStartDrag;
  21. private bool isDragInBounds;
  22. private bool isOSDragActive;
  23. private Vector2I mouseDownScreenPos;
  24. private Vector2I lastDragWindowPos;
  25. public Rect2I Bounds
  26. {
  27. get { return bounds; }
  28. set
  29. {
  30. bounds = value;
  31. dropTargetOS.Bounds = bounds;
  32. }
  33. }
  34. public ProjectDropTarget(EditorWindow window)
  35. {
  36. parentWindow = window;
  37. dropTargetOS = new OSDropTarget(window);
  38. dropTargetOS.OnDrag += DoOnOSDrag;
  39. dropTargetOS.OnDrop += DoOnOSDrop;
  40. dropTargetOS.OnEnter += DoOnOSDragEnter;
  41. dropTargetOS.OnLeave += DoOnOSDragLeave;
  42. Input.OnPointerPressed += Input_OnPointerPressed;
  43. Input.OnPointerReleased += Input_OnPointerReleased;
  44. Input.OnPointerMoved += Input_OnPointerMoved;
  45. }
  46. void Input_OnPointerMoved(PointerEvent ev)
  47. {
  48. Vector2I currentWindowPos = parentWindow.ScreenToWindowPos(ev.screenPos);
  49. if (isMouseDown && !isDragInProgress)
  50. {
  51. Debug.Log("MOUSE DOWN");
  52. Vector2I startWindowPos = parentWindow.ScreenToWindowPos(mouseDownScreenPos);
  53. if (!Bounds.Contains(startWindowPos))
  54. return;
  55. Debug.Log("IN BOUNDS");
  56. int distance = Vector2I.Distance(startWindowPos, currentWindowPos);
  57. if (distance >= DragStartDistancePx)
  58. triggerStartDrag = true;
  59. }
  60. }
  61. void Input_OnPointerReleased(PointerEvent ev)
  62. {
  63. isDragInProgress = false;
  64. isMouseDown = false;
  65. isDragInBounds = false;
  66. }
  67. void Input_OnPointerPressed(PointerEvent ev)
  68. {
  69. isMouseDown = true;
  70. mouseDownScreenPos = ev.screenPos;
  71. }
  72. public void Update()
  73. {
  74. Vector2I currentWindowPos = parentWindow.ScreenToWindowPos(Input.PointerPosition);
  75. if (triggerStartDrag)
  76. {
  77. Debug.Log("TRIGGER START");
  78. isDragInProgress = true;
  79. triggerStartDrag = false;
  80. if (OnStart != null)
  81. OnStart(currentWindowPos);
  82. }
  83. if (isOSDragActive)
  84. return;
  85. if (DragDrop.DragInProgress && DragDrop.Type == DragDropType.Resource)
  86. {
  87. if (lastDragWindowPos != currentWindowPos)
  88. {
  89. if (!isDragInBounds)
  90. {
  91. if (Bounds.Contains(currentWindowPos))
  92. {
  93. isDragInBounds = true;
  94. if (OnEnter != null)
  95. OnEnter(currentWindowPos);
  96. }
  97. }
  98. if (OnDrag != null)
  99. OnDrag(currentWindowPos);
  100. if (isDragInBounds)
  101. {
  102. if (!Bounds.Contains(currentWindowPos))
  103. {
  104. isDragInBounds = false;
  105. if (OnLeave != null)
  106. OnLeave();
  107. }
  108. }
  109. lastDragWindowPos = currentWindowPos;
  110. }
  111. if (DragDrop.DropInProgress)
  112. {
  113. if (OnDrop != null)
  114. {
  115. ResourceDragDropData resourceDragDrop = (ResourceDragDropData)DragDrop.Data;
  116. OnDrop(currentWindowPos, resourceDragDrop.Paths);
  117. }
  118. }
  119. }
  120. }
  121. public void Destroy()
  122. {
  123. dropTargetOS.Destroy();
  124. dropTargetOS = null;
  125. }
  126. private void DoOnOSDragEnter(int x, int y)
  127. {
  128. isOSDragActive = true;
  129. if (OnEnter != null)
  130. OnEnter(new Vector2I(x, y));
  131. }
  132. private void DoOnOSDragLeave()
  133. {
  134. isOSDragActive = false;
  135. if (OnLeave != null)
  136. OnLeave();
  137. }
  138. private void DoOnOSDrag(int x, int y)
  139. {
  140. if (OnDrag != null)
  141. OnDrag(new Vector2I(x, y));
  142. }
  143. private void DoOnOSDrop(int x, int y)
  144. {
  145. isOSDragActive = false;
  146. if (OnDrop != null)
  147. OnDrop(new Vector2I(x, y), dropTargetOS.FilePaths);
  148. }
  149. }
  150. }