BsScriptOSDropTarget.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. mWidgetMovedConn.disconnect();
  93. setDropTarget(nullptr, 0, 0, 0, 0);
  94. }
  95. EditorWidgetBase* ScriptOSDropTarget::getParentWidget() const
  96. {
  97. EditorWidgetBase* parentWidget = nullptr;
  98. if (mParent != nullptr && !mParent->isDestroyed())
  99. parentWidget = mParent->getEditorWidget();
  100. return parentWidget;
  101. }
  102. Rect2I ScriptOSDropTarget::getDropTargetArea() const
  103. {
  104. Rect2I dropTargetArea = mArea;
  105. dropTargetArea.x += mParentArea.x;
  106. dropTargetArea.y += mParentArea.y;
  107. dropTargetArea.clip(mParentArea);
  108. return dropTargetArea;
  109. }
  110. void ScriptOSDropTarget::setDropTarget(const SPtr<RenderWindow>& parentWindow, INT32 x, INT32 y, UINT32 width, UINT32 height)
  111. {
  112. if (mDropTarget != nullptr)
  113. {
  114. mDropTarget = nullptr;
  115. mDropTargetEnterConn.disconnect();
  116. mDropTargetLeaveConn.disconnect();
  117. mDropTargetMoveConn.disconnect();
  118. mDropTargetDroppedConn.disconnect();
  119. }
  120. if (parentWindow != nullptr)
  121. {
  122. mDropTarget = DropTarget::create(parentWindow.get(), Rect2I(x, y, width, height));
  123. mDropTargetEnterConn = mDropTarget->onEnter.connect(std::bind(&ScriptOSDropTarget::dropTargetDragEnter, this, _1, _2));
  124. mDropTargetMoveConn = mDropTarget->onDragOver.connect(std::bind(&ScriptOSDropTarget::dropTargetDragMove, this, _1, _2));
  125. mDropTargetLeaveConn = mDropTarget->onLeave.connect(std::bind(&ScriptOSDropTarget::dropTargetDragLeave, this));
  126. mDropTargetDroppedConn = mDropTarget->onDrop.connect(std::bind(&ScriptOSDropTarget::dropTargetDragDropped, this, _1, _2));
  127. }
  128. else
  129. mDropTarget = nullptr;
  130. }
  131. void ScriptOSDropTarget::setBounds(const Rect2I& bounds)
  132. {
  133. mArea = bounds;
  134. Rect2I dropTargetArea = getDropTargetArea();
  135. if (mDropTarget != nullptr)
  136. mDropTarget->setArea(dropTargetArea);
  137. }
  138. void ScriptOSDropTarget::dropTargetDragEnter(ScriptOSDropTarget* thisPtr, INT32 x, INT32 y)
  139. {
  140. if (thisPtr->mIsDestroyed)
  141. return;
  142. MonoObject* instance = MonoUtil::getObjectFromGCHandle(thisPtr->mGCHandle);
  143. MonoUtil::invokeThunk(onEnterThunk, instance, x - thisPtr->mParentArea.x, y - thisPtr->mParentArea.y);
  144. }
  145. void ScriptOSDropTarget::dropTargetDragMove(ScriptOSDropTarget* thisPtr, INT32 x, INT32 y)
  146. {
  147. if (thisPtr->mIsDestroyed)
  148. return;
  149. MonoObject* instance = MonoUtil::getObjectFromGCHandle(thisPtr->mGCHandle);
  150. MonoUtil::invokeThunk(onMoveThunk, instance, x - thisPtr->mParentArea.x, y - thisPtr->mParentArea.y);
  151. }
  152. void ScriptOSDropTarget::dropTargetDragLeave(ScriptOSDropTarget* thisPtr)
  153. {
  154. if (thisPtr->mIsDestroyed)
  155. return;
  156. MonoObject* instance = MonoUtil::getObjectFromGCHandle(thisPtr->mGCHandle);
  157. MonoUtil::invokeThunk(onLeaveThunk, instance);
  158. }
  159. void ScriptOSDropTarget::dropTargetDragDropped(ScriptOSDropTarget* thisPtr, INT32 x, INT32 y)
  160. {
  161. if (thisPtr->mIsDestroyed)
  162. return;
  163. MonoObject* instance = MonoUtil::getObjectFromGCHandle(thisPtr->mGCHandle);
  164. MonoUtil::invokeThunk(onDropThunk, instance, x - thisPtr->mParentArea.x, y - thisPtr->mParentArea.y);
  165. }
  166. void ScriptOSDropTarget::widgetParentChanged(EditorWidgetContainer* parent)
  167. {
  168. SPtr<RenderWindow> 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. }