BsScriptOSDropTarget.cpp 7.8 KB

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