BsMacOSDropTarget.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "Math/BsVector2I.h"
  6. #include "Math/BsRect2I.h"
  7. namespace bs
  8. {
  9. class CocoaWindow;
  10. /** Handles Cocoa drag and drop functionality. */
  11. class CocoaDragAndDrop
  12. {
  13. /** Possible states of the DND manager. */
  14. enum class State
  15. {
  16. Inactive,
  17. Entered,
  18. Active
  19. };
  20. /** Type of drag and drop operation. */
  21. enum class DragAndDropOpType
  22. {
  23. Enter,
  24. DragOver,
  25. Drop,
  26. Leave
  27. };
  28. /** Structure describing a drag and drop operation. */
  29. struct DragAndDropOp
  30. {
  31. DragAndDropOp(DragAndDropOpType type, DropTarget* target)
  32. :type(type), target(target)
  33. { }
  34. DragAndDropOp(DragAndDropOpType type, DropTarget* target, const Vector2I& pos)
  35. :type(type), target(target), position(pos)
  36. { }
  37. DragAndDropOp(DragAndDropOpType type, DropTarget* target, const Vector2I& pos,
  38. const Vector<Path>& fileList)
  39. :type(type), target(target), position(pos), fileList(fileList)
  40. { }
  41. DragAndDropOpType type;
  42. DropTarget* target;
  43. Vector2I position;
  44. Vector<Path> fileList;
  45. };
  46. /** Represents a single registered drop area. */
  47. struct DropArea
  48. {
  49. DropArea(DropTarget* target, const Rect2I& area)
  50. :target(target), area(area)
  51. { }
  52. DropTarget* target;
  53. Rect2I area;
  54. };
  55. /** Type of operations that can happen to a DropArea. */
  56. enum class DropAreaOpType
  57. {
  58. Register, /**< New DropArea is being registered. */
  59. Unregister, /**< DropArea is being unregistered. */
  60. Update /**< DropArea was updated. */
  61. };
  62. /** Operation that in some way modifies a DropArea. */
  63. struct DropAreaOp
  64. {
  65. DropAreaOp(DropTarget* target, DropAreaOpType type, const Rect2I& area = Rect2I::EMPTY)
  66. :target(target), area(area), type(type)
  67. { }
  68. DropTarget* target;
  69. Rect2I area;
  70. DropAreaOpType type;
  71. };
  72. public:
  73. /**
  74. * Triggers any drag and drop events.
  75. *
  76. * @note Sim thread only.
  77. */
  78. static void update();
  79. /**
  80. * Transfers information about drop areas to the core thread.
  81. *
  82. * @note Core thread only.
  83. */
  84. static void coreUpdate();
  85. /**
  86. * Registers a new drop target. Any further events processed will take this target into account, trigger its events
  87. * and populate its data if a drop occurs.
  88. *
  89. * @note Thread safe.
  90. */
  91. static void registerDropTarget(DropTarget* target);
  92. /**
  93. * Updates information about previous registered DropTarget. Call this when drop target area changes.
  94. *
  95. * @note Thread safe.
  96. */
  97. static void updateDropTarget(DropTarget* target);
  98. /**
  99. * Unregisters a drop target. Its events will no longer be triggered.
  100. *
  101. * @note Thread safe.
  102. */
  103. static void unregisterDropTarget(DropTarget* target);
  104. /** Triggered by Cocoa window when mouse cursor enters its content area while dragging. */
  105. static bool _notifyDragEntered(CocoaWindow* window, const Vector2I& position);
  106. /** Triggered by Cocoa window when mouse cursor moves within its content area while dragging. */
  107. static bool _notifyDragMoved(CocoaWindow* window, const Vector2I& position);
  108. /** Triggered by Cocoa window when mouse cursor leaves its content area while dragging. */
  109. static void _notifyDragLeft(CocoaWindow* window);
  110. /** Triggered by Cocoa window when the user stops dragging (drops the items) within the window's content area. */
  111. static bool _notifyDragDropped(CocoaWindow* window, const Vector2I& position, const Vector<Path>& paths);
  112. private:
  113. static Vector<DropArea> sDropAreas;
  114. static Mutex sMutex;
  115. static Vector<DragAndDropOp> sQueuedOperations;
  116. static Vector<DropAreaOp> sQueuedAreaOperations;
  117. };
  118. }