BsScriptOSDropTarget.cpp 7.6 KB

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