BsScriptOSDropTarget.cpp 7.5 KB

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