ProjectDropTarget.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. Vector2I startWindowPos = parentWindow.ScreenToWindowPos(mouseDownScreenPos);
  52. if (!Bounds.Contains(startWindowPos))
  53. return;
  54. int distance = Vector2I.Distance(startWindowPos, currentWindowPos);
  55. if (distance >= DragStartDistancePx)
  56. triggerStartDrag = true;
  57. }
  58. }
  59. void Input_OnPointerReleased(PointerEvent ev)
  60. {
  61. isDragInProgress = false;
  62. isMouseDown = false;
  63. isDragInBounds = false;
  64. }
  65. void Input_OnPointerPressed(PointerEvent ev)
  66. {
  67. isMouseDown = true;
  68. mouseDownScreenPos = ev.screenPos;
  69. }
  70. public void Update()
  71. {
  72. Vector2I currentWindowPos = parentWindow.ScreenToWindowPos(Input.PointerPosition);
  73. if (triggerStartDrag)
  74. {
  75. isDragInProgress = true;
  76. triggerStartDrag = false;
  77. if (OnStart != null)
  78. OnStart(currentWindowPos);
  79. }
  80. if (isOSDragActive)
  81. return;
  82. if (DragDrop.DragInProgress && DragDrop.Type == DragDropType.Resource)
  83. {
  84. if (lastDragWindowPos != currentWindowPos)
  85. {
  86. if (!isDragInBounds)
  87. {
  88. if (Bounds.Contains(currentWindowPos))
  89. {
  90. isDragInBounds = true;
  91. if (OnEnter != null)
  92. OnEnter(currentWindowPos);
  93. }
  94. }
  95. if (OnDrag != null)
  96. OnDrag(currentWindowPos);
  97. if (isDragInBounds)
  98. {
  99. if (!Bounds.Contains(currentWindowPos))
  100. {
  101. isDragInBounds = false;
  102. if (OnLeave != null)
  103. OnLeave();
  104. }
  105. }
  106. lastDragWindowPos = currentWindowPos;
  107. }
  108. }
  109. if (DragDrop.DropInProgress && DragDrop.Type == DragDropType.Resource)
  110. {
  111. if (OnDrop != null)
  112. {
  113. ResourceDragDropData resourceDragDrop = (ResourceDragDropData)DragDrop.Data;
  114. OnDrop(currentWindowPos, resourceDragDrop.Paths);
  115. }
  116. }
  117. }
  118. public void Destroy()
  119. {
  120. dropTargetOS.Destroy();
  121. dropTargetOS = null;
  122. }
  123. private void DoOnOSDragEnter(int x, int y)
  124. {
  125. isOSDragActive = true;
  126. if (OnEnter != null)
  127. OnEnter(new Vector2I(x, y));
  128. }
  129. private void DoOnOSDragLeave()
  130. {
  131. isOSDragActive = false;
  132. if (OnLeave != null)
  133. OnLeave();
  134. }
  135. private void DoOnOSDrag(int x, int y)
  136. {
  137. if (OnDrag != null)
  138. OnDrag(new Vector2I(x, y));
  139. }
  140. private void DoOnOSDrop(int x, int y)
  141. {
  142. isOSDragActive = false;
  143. if (OnDrop != null)
  144. OnDrop(new Vector2I(x, y), dropTargetOS.FilePaths);
  145. }
  146. }
  147. }