| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- #include "Wrappers/BsScriptOSDropTarget.h"
- #include "BsScriptMeta.h"
- #include "BsMonoField.h"
- #include "BsMonoClass.h"
- #include "BsMonoManager.h"
- #include "BsMonoMethod.h"
- #include "BsMonoUtil.h"
- #include "BsMonoArray.h"
- #include "Reflection/BsRTTIType.h"
- #include "Platform/BsDropTarget.h"
- #include "EditorWindow/BsEditorWidget.h"
- #include "EditorWindow/BsEditorWindowBase.h"
- #include "EditorWindow/BsEditorWidgetContainer.h"
- #include "Wrappers/BsScriptEditorWindow.h"
- using namespace std::placeholders;
- namespace bs
- {
- ScriptOSDropTarget::OnEnterThunkDef ScriptOSDropTarget::onEnterThunk;
- ScriptOSDropTarget::OnMoveDef ScriptOSDropTarget::onMoveThunk;
- ScriptOSDropTarget::OnLeaveDef ScriptOSDropTarget::onLeaveThunk;
- ScriptOSDropTarget::OnDropThunkDef ScriptOSDropTarget::onDropThunk;
- ScriptOSDropTarget::ScriptOSDropTarget(MonoObject* instance, ScriptEditorWindow* parent)
- :ScriptObject(instance), mParent(parent)
- {
- mGCHandle = MonoUtil::newWeakGCHandle(instance);
- EditorWidgetBase* parentWidget = getParentWidget();
- if (parentWidget != nullptr)
- {
- mWidgetParentChangedConn = parentWidget->onParentChanged.connect(std::bind(&ScriptOSDropTarget::widgetParentChanged, this, _1));
- mWidgetResizedConn = parentWidget->onResized.connect(std::bind(&ScriptOSDropTarget::widgetResized, this, _1, _2));
- mWidgetMovedConn = parentWidget->onMoved.connect(std::bind(&ScriptOSDropTarget::widgetMoved, this, _1, _2));
- EditorWindowBase* parentWindow = parentWidget->getParentWindow();
- if (parentWindow != nullptr)
- setDropTarget(parentWindow->getRenderWindow(), 0, 0, 0, 0);
- mParentArea = parentWidget->getBounds();
- }
- }
- ScriptOSDropTarget::~ScriptOSDropTarget()
- {
- if (!mIsDestroyed)
- destroy();
- }
- void ScriptOSDropTarget::initRuntimeData()
- {
- metaData.scriptClass->addInternalCall("Internal_CreateInstance", (void*)&ScriptOSDropTarget::internal_CreateInstance);
- metaData.scriptClass->addInternalCall("Internal_Destroy", (void*)&ScriptOSDropTarget::internal_Destroy);
- metaData.scriptClass->addInternalCall("Internal_SetBounds", (void*)&ScriptOSDropTarget::internal_SetBounds);
- metaData.scriptClass->addInternalCall("Internal_GetFilePaths", (void*)&ScriptOSDropTarget::internal_GetFilePaths);
- onEnterThunk = (OnEnterThunkDef)metaData.scriptClass->getMethod("InternalDoOnEnter", 2)->getThunk();
- onMoveThunk = (OnMoveDef)metaData.scriptClass->getMethod("InternalDoOnDrag", 2)->getThunk();
- onLeaveThunk = (OnLeaveDef)metaData.scriptClass->getMethod("InternalDoOnLeave")->getThunk();
- onDropThunk = (OnDropThunkDef)metaData.scriptClass->getMethod("InternalDoOnDrop", 2)->getThunk();
- }
- void ScriptOSDropTarget::internal_CreateInstance(MonoObject* instance, ScriptEditorWindow* editorWindow)
- {
- new (bs_alloc<ScriptOSDropTarget>()) ScriptOSDropTarget(instance, editorWindow);
- }
- void ScriptOSDropTarget::internal_Destroy(ScriptOSDropTarget* nativeInstance)
- {
- if (nativeInstance->mIsDestroyed)
- return;
- nativeInstance->destroy();
- }
- void ScriptOSDropTarget::internal_SetBounds(ScriptOSDropTarget* nativeInstance, Rect2I* bounds)
- {
- if (nativeInstance->mIsDestroyed)
- return;
- nativeInstance->setBounds(*bounds);
- }
- MonoArray* ScriptOSDropTarget::internal_GetFilePaths(ScriptOSDropTarget* nativeInstance)
- {
- SPtr<DropTarget> dropTarget = nativeInstance->mDropTarget;
- if (nativeInstance->mIsDestroyed || dropTarget == nullptr || dropTarget->getDropType() != DropTargetType::FileList)
- return ScriptArray::create<String>(0).getInternal();
- Vector<Path> fileList = dropTarget->getFileList();
- ScriptArray output = ScriptArray::create<String>((UINT32)fileList.size());
- UINT32 idx = 0;
- for (auto& path : fileList)
- {
- output.set(idx, path.toString());
- idx++;
- }
- return output.getInternal();
- }
- void ScriptOSDropTarget::destroy()
- {
- mIsDestroyed = true;
- mWidgetParentChangedConn.disconnect();
- mWidgetResizedConn.disconnect();
- setDropTarget(nullptr, 0, 0, 0, 0);
- }
- EditorWidgetBase* ScriptOSDropTarget::getParentWidget() const
- {
- EditorWidgetBase* parentWidget = nullptr;
- if (mParent != nullptr && !mParent->isDestroyed())
- parentWidget = mParent->getEditorWidget();
- return parentWidget;
- }
- Rect2I ScriptOSDropTarget::getDropTargetArea() const
- {
- Rect2I dropTargetArea = mArea;
- dropTargetArea.x += mParentArea.x;
- dropTargetArea.y += mParentArea.y;
- dropTargetArea.clip(mParentArea);
- return dropTargetArea;
- }
- void ScriptOSDropTarget::setDropTarget(const SPtr<RenderWindow>& parentWindow, INT32 x, INT32 y, UINT32 width, UINT32 height)
- {
- if (mDropTarget != nullptr)
- {
- mDropTarget = nullptr;
- mDropTargetEnterConn.disconnect();
- mDropTargetLeaveConn.disconnect();
- mDropTargetMoveConn.disconnect();
- mDropTargetDroppedConn.disconnect();
- }
- if (parentWindow != nullptr)
- {
- mDropTarget = DropTarget::create(parentWindow.get(), Rect2I(x, y, width, height));
- mDropTargetEnterConn = mDropTarget->onEnter.connect(std::bind(&ScriptOSDropTarget::dropTargetDragEnter, this, _1, _2));
- mDropTargetMoveConn = mDropTarget->onDragOver.connect(std::bind(&ScriptOSDropTarget::dropTargetDragMove, this, _1, _2));
- mDropTargetLeaveConn = mDropTarget->onLeave.connect(std::bind(&ScriptOSDropTarget::dropTargetDragLeave, this));
- mDropTargetDroppedConn = mDropTarget->onDrop.connect(std::bind(&ScriptOSDropTarget::dropTargetDragDropped, this, _1, _2));
- }
- else
- mDropTarget = nullptr;
- }
- void ScriptOSDropTarget::setBounds(const Rect2I& bounds)
- {
- mArea = bounds;
- Rect2I dropTargetArea = getDropTargetArea();
- if (mDropTarget != nullptr)
- mDropTarget->setArea(dropTargetArea);
- }
- void ScriptOSDropTarget::dropTargetDragEnter(ScriptOSDropTarget* thisPtr, INT32 x, INT32 y)
- {
- if (thisPtr->mIsDestroyed)
- return;
- MonoObject* instance = MonoUtil::getObjectFromGCHandle(thisPtr->mGCHandle);
- MonoUtil::invokeThunk(onEnterThunk, instance, x - thisPtr->mParentArea.x, y - thisPtr->mParentArea.y);
- }
- void ScriptOSDropTarget::dropTargetDragMove(ScriptOSDropTarget* thisPtr, INT32 x, INT32 y)
- {
- if (thisPtr->mIsDestroyed)
- return;
- MonoObject* instance = MonoUtil::getObjectFromGCHandle(thisPtr->mGCHandle);
- MonoUtil::invokeThunk(onMoveThunk, instance, x - thisPtr->mParentArea.x, y - thisPtr->mParentArea.y);
- }
- void ScriptOSDropTarget::dropTargetDragLeave(ScriptOSDropTarget* thisPtr)
- {
- if (thisPtr->mIsDestroyed)
- return;
- MonoObject* instance = MonoUtil::getObjectFromGCHandle(thisPtr->mGCHandle);
- MonoUtil::invokeThunk(onLeaveThunk, instance);
- }
- void ScriptOSDropTarget::dropTargetDragDropped(ScriptOSDropTarget* thisPtr, INT32 x, INT32 y)
- {
- if (thisPtr->mIsDestroyed)
- return;
- MonoObject* instance = MonoUtil::getObjectFromGCHandle(thisPtr->mGCHandle);
- MonoUtil::invokeThunk(onDropThunk, instance, x - thisPtr->mParentArea.x, y - thisPtr->mParentArea.y);
- }
- void ScriptOSDropTarget::widgetParentChanged(EditorWidgetContainer* parent)
- {
- SPtr<RenderWindow> parentRenderWindow;
- if (parent != nullptr)
- {
- EditorWindowBase* parentWindow = parent->getParentWindow();
- if (parentWindow != nullptr)
- parentRenderWindow = parentWindow->getRenderWindow();
- }
- if (parentRenderWindow == nullptr)
- mParentArea = Rect2I();
-
- Rect2I dropTargetArea = getDropTargetArea();
- setDropTarget(parentRenderWindow, dropTargetArea.x, dropTargetArea.y, dropTargetArea.width, dropTargetArea.height);
- }
- void ScriptOSDropTarget::widgetResized(UINT32 width, UINT32 height)
- {
- mParentArea.width = width;
- mParentArea.height = height;
- setBounds(mArea);
- }
- void ScriptOSDropTarget::widgetMoved(INT32 x, INT32 y)
- {
- mParentArea.x = x;
- mParentArea.y = y;
- setBounds(mArea);
- }
- }
|