OSDropTarget.cs 4.8 KB

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