2
0

OSDropTarget.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. /// <summary>
  7. /// Drop targets allow you to register a certain portion of a window as a drop target that accepts file drops from the
  8. /// OS (platform) specific drag and drop system. You will receive events with the specified drop area as long as it is
  9. /// active.
  10. /// </summary>
  11. public class OSDropTarget : ScriptObject
  12. {
  13. /// <summary>
  14. /// Triggered when the user completes a drop while pointer is over the drop area. Provides window coordinates of
  15. /// the pointer position.
  16. /// </summary>
  17. public Action<int, int> OnDrop;
  18. /// <summary>
  19. /// Triggered when a pointer enters the drop area. Provides window coordinates of the pointer position.
  20. /// </summary>
  21. public Action<int, int> OnEnter;
  22. /// <summary>
  23. /// Triggered when a pointer leaves the drop area.
  24. /// </summary>
  25. public Action OnLeave;
  26. /// <summary>
  27. /// Triggered when a pointer is being dragged over the drop area. Provides window coordinates of the pointer
  28. /// position.
  29. /// </summary>
  30. public Action<int, int> OnDrag;
  31. /// <summary>
  32. /// Creates a new OS drop target.
  33. /// </summary>
  34. /// <param name="window">Window on which the drop target is on.</param>
  35. public OSDropTarget(EditorWindow window)
  36. {
  37. IntPtr nativeWindow = window.GetCachedPtr();;
  38. Internal_CreateInstance(this, nativeWindow);
  39. }
  40. /// <summary>
  41. /// Destroys the drop target, stopping any further events.
  42. /// </summary>
  43. public void Destroy()
  44. {
  45. Internal_Destroy(mCachedPtr);
  46. }
  47. /// <summary>
  48. /// Allows you to set the drop target bounds, relative to the parent window.
  49. /// </summary>
  50. public Rect2I Bounds
  51. {
  52. set { Internal_SetBounds(mCachedPtr, value); }
  53. }
  54. /// <summary>
  55. /// Returns a list of files received by the drop target. Only valid after a drop has been triggered.
  56. /// </summary>
  57. public string[] FilePaths
  58. {
  59. get { return Internal_GetFilePaths(mCachedPtr); }
  60. }
  61. /// <summary>
  62. /// Triggered internally by the runtime when a pointer enters the drop area.
  63. /// </summary>
  64. /// <param name="x">X coordinate of the pointer position, relative to the parent window.</param>
  65. /// <param name="y">Y coordinate of the pointer position, relative to the parent window.</param>
  66. private void InternalDoOnEnter(int x, int y)
  67. {
  68. if (OnEnter != null)
  69. OnEnter(x, y);
  70. }
  71. /// <summary>
  72. /// Triggered internally by the runtime when a pointer leaves the drop area.
  73. /// </summary>
  74. private void InternalDoOnLeave()
  75. {
  76. if (OnLeave != null)
  77. OnLeave();
  78. }
  79. /// <summary>
  80. /// Triggered internally by the runtime when a pointer is being dragged over the drop area.
  81. /// </summary>
  82. /// <param name="x">X coordinate of the pointer position, relative to the parent window.</param>
  83. /// <param name="y">Y coordinate of the pointer position, relative to the parent window.</param>
  84. private void InternalDoOnDrag(int x, int y)
  85. {
  86. if (OnDrag != null)
  87. OnDrag(x, y);
  88. }
  89. /// <summary>
  90. /// Triggered internally by the runtime when the user completes a drop while pointer is over the drop area.
  91. /// </summary>
  92. /// <param name="x">X coordinate of the pointer position, relative to the parent window.</param>
  93. /// <param name="y">Y coordinate of the pointer position, relative to the parent window.</param>
  94. private void InternalDoOnDrop(int x, int y)
  95. {
  96. if (OnDrop != null)
  97. OnDrop(x, y);
  98. }
  99. [MethodImpl(MethodImplOptions.InternalCall)]
  100. private static extern void Internal_CreateInstance(OSDropTarget instance, IntPtr editorWindow);
  101. [MethodImpl(MethodImplOptions.InternalCall)]
  102. private static extern void Internal_Destroy(IntPtr nativeInstance);
  103. [MethodImpl(MethodImplOptions.InternalCall)]
  104. private static extern void Internal_SetBounds(IntPtr nativeInstance, Rect2I bounds);
  105. [MethodImpl(MethodImplOptions.InternalCall)]
  106. private static extern string[] Internal_GetFilePaths(IntPtr nativeInstance);
  107. }
  108. }