BsScriptDropDownWindow.cpp 7.1 KB

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