BsScriptDropDownWindow.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "BsScriptDropDownWindow.h"
  2. #include "BsScriptMeta.h"
  3. #include "BsMonoField.h"
  4. #include "BsMonoClass.h"
  5. #include "BsMonoMethod.h"
  6. #include "BsMonoManager.h"
  7. #include "BsMonoUtil.h"
  8. #include "BsScriptGUILayout.h"
  9. #include "BsScriptEditorWindow.h"
  10. #include "BsEditorWidget.h"
  11. #include "BsEditorWindow.h"
  12. #include "BsEditorWidgetContainer.h"
  13. #include "BsCGUIWidget.h"
  14. #include "BsDropDownWindowManager.h"
  15. #include <BsScriptObjectManager.h>
  16. using namespace std::placeholders;
  17. namespace BansheeEngine
  18. {
  19. MonoField* ScriptDropDownWindow::guiPanelField = nullptr;
  20. ScriptDropDownWindow::ScriptDropDownWindow(ManagedDropDownWindow* window)
  21. :ScriptObject(window->getManagedInstance()), mDropDownWindow(window)
  22. {
  23. mOnAssemblyRefreshStartedConn = ScriptObjectManager::instance().onRefreshStarted.connect(std::bind(&ScriptDropDownWindow::onAssemblyRefreshStarted, this));
  24. }
  25. ScriptDropDownWindow::~ScriptDropDownWindow()
  26. {
  27. mOnAssemblyRefreshStartedConn.disconnect();
  28. // Window must have been marked as deleted already
  29. assert(mDropDownWindow == nullptr);
  30. }
  31. void ScriptDropDownWindow::initRuntimeData()
  32. {
  33. metaData.scriptClass->addInternalCall("Internal_CreateInstance", &ScriptDropDownWindow::internal_CreateInstance);
  34. metaData.scriptClass->addInternalCall("Internal_Close", &ScriptDropDownWindow::internal_Close);
  35. metaData.scriptClass->addInternalCall("Internal_SetWidth", &ScriptDropDownWindow::internal_SetWidth);
  36. metaData.scriptClass->addInternalCall("Internal_SetHeight", &ScriptDropDownWindow::internal_SetHeight);
  37. metaData.scriptClass->addInternalCall("Internal_ScreenToWindowPos", &ScriptDropDownWindow::internal_ScreenToWindowPos);
  38. metaData.scriptClass->addInternalCall("Internal_WindowToScreenPos", &ScriptDropDownWindow::internal_WindowToScreenPos);
  39. guiPanelField = metaData.scriptClass->getField("GUI");
  40. }
  41. void ScriptDropDownWindow::internal_CreateInstance(MonoObject* instance, ScriptEditorWindow* parentWindow,
  42. Vector2I* position, int width, int height)
  43. {
  44. ManagedDropDownWindow* dropDownWindow = nullptr;
  45. if (parentWindow != nullptr && !parentWindow->isDestroyed())
  46. {
  47. EditorWidgetBase* editorWidget = parentWindow->getEditorWidget();
  48. EditorWidgetContainer* parentContainer = editorWidget->_getParent();
  49. if (parentContainer != nullptr)
  50. {
  51. RenderWindowPtr parentRenderWindow = parentContainer->getParentWindow()->getRenderWindow();
  52. CameraPtr parentCamera = parentContainer->getParentWidget().getCamera();
  53. position->x += editorWidget->getX();
  54. position->y += editorWidget->getY();
  55. dropDownWindow = DropDownWindowManager::instance().open<ManagedDropDownWindow>(
  56. parentRenderWindow, parentCamera, *position, instance, width, height);
  57. }
  58. }
  59. ScriptDropDownWindow* nativeInstance = new (bs_alloc<ScriptDropDownWindow>()) ScriptDropDownWindow(dropDownWindow);
  60. if (dropDownWindow != nullptr)
  61. dropDownWindow->initialize(nativeInstance);
  62. }
  63. void ScriptDropDownWindow::internal_Close(ScriptDropDownWindow* thisPtr)
  64. {
  65. if (thisPtr->mDropDownWindow != nullptr)
  66. DropDownWindowManager::instance().close();
  67. }
  68. void ScriptDropDownWindow::onAssemblyRefreshStarted()
  69. {
  70. if (mDropDownWindow != nullptr)
  71. DropDownWindowManager::instance().close();
  72. }
  73. void ScriptDropDownWindow::notifyWindowClosed()
  74. {
  75. mDropDownWindow = nullptr;
  76. }
  77. void ScriptDropDownWindow::internal_SetWidth(ScriptDropDownWindow* thisPtr, UINT32 value)
  78. {
  79. if (thisPtr->mDropDownWindow != nullptr)
  80. thisPtr->mDropDownWindow->setSize(value, thisPtr->mDropDownWindow->getHeight());
  81. }
  82. void ScriptDropDownWindow::internal_SetHeight(ScriptDropDownWindow* thisPtr, UINT32 value)
  83. {
  84. if (thisPtr->mDropDownWindow != nullptr)
  85. thisPtr->mDropDownWindow->setSize(thisPtr->mDropDownWindow->getWidth(), value);
  86. }
  87. void ScriptDropDownWindow::internal_ScreenToWindowPos(ScriptDropDownWindow* thisPtr, Vector2I* screenPos, Vector2I* windowPos)
  88. {
  89. if (thisPtr->mDropDownWindow != nullptr)
  90. *windowPos = thisPtr->mDropDownWindow->screenToWindowPos(*screenPos);
  91. else
  92. *windowPos = *screenPos;
  93. }
  94. void ScriptDropDownWindow::internal_WindowToScreenPos(ScriptDropDownWindow* thisPtr, Vector2I* windowPos, Vector2I* screenPos)
  95. {
  96. if (thisPtr->mDropDownWindow != nullptr)
  97. *screenPos = thisPtr->mDropDownWindow->windowToScreenPos(*windowPos);
  98. else
  99. *screenPos = *windowPos;
  100. }
  101. ManagedDropDownWindow::ManagedDropDownWindow(const RenderWindowPtr& parent, const CameraPtr& camera,
  102. const Vector2I& position, MonoObject* managedInstance, UINT32 width, UINT32 height)
  103. :DropDownWindow(parent, camera, position, width, height), mUpdateThunk(nullptr), mManagedInstance(managedInstance),
  104. mOnInitializeThunk(nullptr), mOnDestroyThunk(nullptr), mGCHandle(0), mScriptParent(nullptr),
  105. mContentsPanel(nullptr), mIsInitialized(false)
  106. {
  107. mGCHandle = mono_gchandle_new(mManagedInstance, false);
  108. MonoObject* guiPanel = ScriptGUIPanel::createFromExisting(mContents);
  109. mContentsPanel = ScriptGUILayout::toNative(guiPanel);
  110. ScriptDropDownWindow::guiPanelField->setValue(mManagedInstance, guiPanel);
  111. ::MonoClass* rawMonoClass = mono_object_get_class(mManagedInstance);
  112. MonoClass* monoClass = MonoManager::instance().findClass(rawMonoClass);
  113. mNamespace = monoClass->getNamespace();
  114. mTypename = monoClass->getTypeName();
  115. reloadMonoTypes(monoClass);
  116. }
  117. ManagedDropDownWindow::~ManagedDropDownWindow()
  118. {
  119. mContentsPanel->destroy();
  120. mContentsPanel = nullptr;
  121. triggerOnDestroy();
  122. mScriptParent->notifyWindowClosed();
  123. mono_gchandle_free(mGCHandle);
  124. mGCHandle = 0;
  125. }
  126. void ManagedDropDownWindow::initialize(ScriptDropDownWindow* parent)
  127. {
  128. mScriptParent = parent;
  129. }
  130. void ManagedDropDownWindow::triggerOnInitialize()
  131. {
  132. if (mOnInitializeThunk != nullptr && mManagedInstance != nullptr)
  133. {
  134. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  135. // for some extra speed.
  136. MonoUtil::invokeThunk(mOnInitializeThunk, mManagedInstance);
  137. }
  138. }
  139. void ManagedDropDownWindow::triggerOnDestroy()
  140. {
  141. if (mOnDestroyThunk != nullptr && mManagedInstance != nullptr)
  142. {
  143. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  144. // for some extra speed.
  145. MonoUtil::invokeThunk(mOnDestroyThunk, mManagedInstance);
  146. }
  147. }
  148. void ManagedDropDownWindow::update()
  149. {
  150. if (!mIsInitialized)
  151. {
  152. triggerOnInitialize();
  153. mIsInitialized = true;
  154. }
  155. if (mUpdateThunk != nullptr && mManagedInstance != nullptr)
  156. {
  157. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  158. // for some extra speed.
  159. MonoUtil::invokeThunk(mUpdateThunk, mManagedInstance);
  160. }
  161. }
  162. void ManagedDropDownWindow::reloadMonoTypes(MonoClass* windowClass)
  163. {
  164. MonoMethod* updateMethod = windowClass->getMethod("OnEditorUpdate", 0);
  165. if (updateMethod != nullptr)
  166. mUpdateThunk = (UpdateThunkDef)updateMethod->getThunk();
  167. MonoMethod* onInitializeMethod = windowClass->getMethod("OnInitialize", 0);
  168. if (onInitializeMethod != nullptr)
  169. mOnInitializeThunk = (OnInitializeThunkDef)onInitializeMethod->getThunk();
  170. MonoMethod* onDestroyMethod = windowClass->getMethod("OnDestroy", 0);
  171. if (onDestroyMethod != nullptr)
  172. mOnDestroyThunk = (OnDestroyThunkDef)onDestroyMethod->getThunk();
  173. }
  174. }