OSDropTarget.cs 4.8 KB

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