BsScriptOSDropTarget.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include "BsScriptOSDropTarget.h"
  2. #include "BsScriptMeta.h"
  3. #include "BsMonoField.h"
  4. #include "BsMonoClass.h"
  5. #include "BsMonoManager.h"
  6. #include "BsMonoMethod.h"
  7. #include "BsMonoUtil.h"
  8. #include "BsMonoArray.h"
  9. #include "BsRTTIType.h"
  10. #include "BsPlatform.h"
  11. #include "BsEditorWidget.h"
  12. #include "BsEditorWindowBase.h"
  13. #include "BsEditorWidgetContainer.h"
  14. #include "BsScriptEditorWindow.h"
  15. using namespace std::placeholders;
  16. namespace BansheeEngine
  17. {
  18. ScriptOSDropTarget::OnEnterThunkDef ScriptOSDropTarget::onEnterThunk;
  19. ScriptOSDropTarget::OnMoveDef ScriptOSDropTarget::onMoveThunk;
  20. ScriptOSDropTarget::OnLeaveDef ScriptOSDropTarget::onLeaveThunk;
  21. ScriptOSDropTarget::OnDropThunkDef ScriptOSDropTarget::onDropThunk;
  22. ScriptOSDropTarget::ScriptOSDropTarget(MonoObject* instance, ScriptEditorWindow* parent)
  23. :ScriptObject(instance), mDropTarget(nullptr), mIsDestroyed(false), mParent(parent)
  24. {
  25. EditorWidgetBase* parentWidget = getParentWidget();
  26. if (parentWidget != nullptr)
  27. {
  28. mWidgetParentChangedConn = parentWidget->onParentChanged.connect(std::bind(&ScriptOSDropTarget::widgetParentChanged, this, _1));
  29. mWidgetResizedConn = parentWidget->onResized.connect(std::bind(&ScriptOSDropTarget::widgetResized, this, _1, _2));
  30. EditorWindowBase* parentWindow = parentWidget->getParentWindow();
  31. if (parentWindow != nullptr)
  32. setDropTarget(parentWindow->getRenderWindow(), 0, 0, 0, 0);
  33. }
  34. }
  35. ScriptOSDropTarget::~ScriptOSDropTarget()
  36. {
  37. if (!mIsDestroyed)
  38. destroy();
  39. }
  40. void ScriptOSDropTarget::initRuntimeData()
  41. {
  42. metaData.scriptClass->addInternalCall("Internal_CreateInstance", &ScriptOSDropTarget::internal_CreateInstance);
  43. metaData.scriptClass->addInternalCall("Internal_Destroy", &ScriptOSDropTarget::internal_Destroy);
  44. metaData.scriptClass->addInternalCall("Internal_SetBounds", &ScriptOSDropTarget::internal_SetBounds);
  45. metaData.scriptClass->addInternalCall("Internal_GetFilePaths", &ScriptOSDropTarget::internal_GetFilePaths);
  46. onEnterThunk = (OnEnterThunkDef)metaData.scriptClass->getMethod("InternalDoOnEnter", 2)->getThunk();
  47. onMoveThunk = (OnMoveDef)metaData.scriptClass->getMethod("InternalDoOnDrag", 2)->getThunk();
  48. onLeaveThunk = (OnLeaveDef)metaData.scriptClass->getMethod("InternalDoOnLeave")->getThunk();
  49. onDropThunk = (OnDropThunkDef)metaData.scriptClass->getMethod("InternalDoOnDrop", 2)->getThunk();
  50. }
  51. void ScriptOSDropTarget::internal_CreateInstance(MonoObject* instance, ScriptEditorWindow* editorWindow)
  52. {
  53. ScriptOSDropTarget* nativeInstance = new (bs_alloc<ScriptOSDropTarget>()) ScriptOSDropTarget(instance, editorWindow);
  54. }
  55. void ScriptOSDropTarget::internal_Destroy(ScriptOSDropTarget* nativeInstance)
  56. {
  57. if (nativeInstance->mIsDestroyed)
  58. return;
  59. nativeInstance->destroy();
  60. }
  61. void ScriptOSDropTarget::internal_SetBounds(ScriptOSDropTarget* nativeInstance, Rect2I bounds)
  62. {
  63. if (nativeInstance->mIsDestroyed)
  64. return;
  65. nativeInstance->setBounds(bounds);
  66. }
  67. MonoArray* ScriptOSDropTarget::internal_GetFilePaths(ScriptOSDropTarget* nativeInstance)
  68. {
  69. OSDropTarget* dropTarget = nativeInstance->mDropTarget;
  70. if (nativeInstance->mIsDestroyed || dropTarget == nullptr || dropTarget->getDropType() != OSDropType::FileList)
  71. return ScriptArray::create<String>(0).getInternal();
  72. Vector<WString> fileList = dropTarget->getFileList();
  73. ScriptArray output = ScriptArray::create<WString>((UINT32)fileList.size());
  74. UINT32 idx = 0;
  75. for (auto& path : fileList)
  76. {
  77. output.set(idx, path);
  78. idx++;
  79. }
  80. return output.getInternal();
  81. }
  82. void ScriptOSDropTarget::destroy()
  83. {
  84. mIsDestroyed = true;
  85. mWidgetParentChangedConn.disconnect();
  86. mWidgetResizedConn.disconnect();
  87. setDropTarget(nullptr, 0, 0, 0, 0);
  88. }
  89. EditorWidgetBase* ScriptOSDropTarget::getParentWidget() const
  90. {
  91. EditorWidgetBase* parentWidget = nullptr;
  92. if (mParent != nullptr && mParent->isDestroyed())
  93. parentWidget = mParent->getEditorWidget();
  94. return parentWidget;
  95. }
  96. Rect2I ScriptOSDropTarget::getDropTargetArea() const
  97. {
  98. Rect2I dropTargetArea = mArea;
  99. dropTargetArea.clip(mParentArea);
  100. EditorWidgetBase* parentWidget = getParentWidget();
  101. if (parentWidget != nullptr)
  102. {
  103. dropTargetArea.x += parentWidget->getX();
  104. dropTargetArea.y += parentWidget->getY();
  105. }
  106. return dropTargetArea;
  107. }
  108. void ScriptOSDropTarget::setDropTarget(const RenderWindowPtr& parentWindow, INT32 x, INT32 y, UINT32 width, UINT32 height)
  109. {
  110. if (mDropTarget != nullptr)
  111. {
  112. Platform::destroyDropTarget(*mDropTarget);
  113. mDropTargetEnterConn.disconnect();
  114. mDropTargetLeaveConn.disconnect();
  115. mDropTargetMoveConn.disconnect();
  116. mDropTargetDroppedConn.disconnect();
  117. }
  118. if (parentWindow != nullptr)
  119. {
  120. mDropTarget = &Platform::createDropTarget(parentWindow.get(), x, y, width, height);
  121. mDropTargetEnterConn = mDropTarget->onEnter.connect(std::bind(&ScriptOSDropTarget::dropTargetDragMove, this, _1, _2));
  122. mDropTargetMoveConn = mDropTarget->onDragOver.connect(std::bind(&ScriptOSDropTarget::dropTargetDragMove, this, _1, _2));
  123. mDropTargetLeaveConn = mDropTarget->onLeave.connect(std::bind(&ScriptOSDropTarget::dropTargetDragLeave, this));
  124. mDropTargetDroppedConn = mDropTarget->onDrop.connect(std::bind(&ScriptOSDropTarget::dropTargetDragDropped, this, _1, _2));
  125. }
  126. else
  127. mDropTarget = nullptr;
  128. }
  129. void ScriptOSDropTarget::setBounds(const Rect2I& bounds)
  130. {
  131. mArea = bounds;
  132. Rect2I dropTargetArea = getDropTargetArea();
  133. if (mDropTarget != nullptr)
  134. mDropTarget->setArea(dropTargetArea.x, dropTargetArea.y, dropTargetArea.width, dropTargetArea.height);
  135. }
  136. void ScriptOSDropTarget::dropTargetDragEnter(ScriptOSDropTarget* thisPtr, INT32 x, INT32 y)
  137. {
  138. if (thisPtr->mIsDestroyed)
  139. return;
  140. EditorWidgetBase* parentWidget = thisPtr->getParentWidget();
  141. if (parentWidget == nullptr)
  142. return;
  143. MonoException* exception = nullptr;
  144. onEnterThunk(thisPtr->getManagedInstance(), x - parentWidget->getX(), y - parentWidget->getY(), &exception);
  145. MonoUtil::throwIfException(exception);
  146. }
  147. void ScriptOSDropTarget::dropTargetDragMove(ScriptOSDropTarget* thisPtr, INT32 x, INT32 y)
  148. {
  149. if (thisPtr->mIsDestroyed)
  150. return;
  151. EditorWidgetBase* parentWidget = thisPtr->getParentWidget();
  152. if (parentWidget == nullptr)
  153. return;
  154. MonoException* exception = nullptr;
  155. onMoveThunk(thisPtr->getManagedInstance(), x - parentWidget->getX(), y - parentWidget->getY(), &exception);
  156. MonoUtil::throwIfException(exception);
  157. }
  158. void ScriptOSDropTarget::dropTargetDragLeave(ScriptOSDropTarget* thisPtr)
  159. {
  160. if (thisPtr->mIsDestroyed)
  161. return;
  162. MonoException* exception = nullptr;
  163. onLeaveThunk(thisPtr->getManagedInstance(), &exception);
  164. MonoUtil::throwIfException(exception);
  165. }
  166. void ScriptOSDropTarget::dropTargetDragDropped(ScriptOSDropTarget* thisPtr, INT32 x, INT32 y)
  167. {
  168. if (thisPtr->mIsDestroyed)
  169. return;
  170. EditorWidgetBase* parentWidget = thisPtr->getParentWidget();
  171. if (parentWidget == nullptr)
  172. return;
  173. MonoException* exception = nullptr;
  174. onDropThunk(thisPtr->getManagedInstance(), x - parentWidget->getX(), y - parentWidget->getY(), &exception);
  175. MonoUtil::throwIfException(exception);
  176. }
  177. void ScriptOSDropTarget::widgetParentChanged(EditorWidgetContainer* parent)
  178. {
  179. RenderWindowPtr parentRenderWindow;
  180. if (parent != nullptr)
  181. {
  182. EditorWindowBase* parentWindow = parent->getParentWindow();
  183. if (parentWindow != nullptr)
  184. parentRenderWindow = parentWindow->getRenderWindow();
  185. }
  186. if (parentRenderWindow == nullptr)
  187. mParentArea = Rect2I();
  188. Rect2I dropTargetArea = getDropTargetArea();
  189. setDropTarget(parentRenderWindow, dropTargetArea.x, dropTargetArea.y, dropTargetArea.width, dropTargetArea.height);
  190. }
  191. void ScriptOSDropTarget::widgetResized(UINT32 width, UINT32 height)
  192. {
  193. mParentArea.width = width;
  194. mParentArea.height = height;
  195. setBounds(mArea);
  196. }
  197. }