BsScriptModalWindow.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include "BsScriptModalWindow.h"
  2. #include "BsScriptMeta.h"
  3. #include "BsScriptGUIPanel.h"
  4. #include "BsMonoField.h"
  5. #include "BsMonoClass.h"
  6. #include "BsMonoMethod.h"
  7. #include "BsMonoManager.h"
  8. #include "BsMonoUtil.h"
  9. #include "BsMonoAssembly.h"
  10. #include "BsScriptObjectManager.h"
  11. using namespace std::placeholders;
  12. namespace BansheeEngine
  13. {
  14. MonoMethod* ScriptModalWindow::onInitializedInternalMethod = nullptr;
  15. MonoMethod* ScriptModalWindow::onDestroyInternalMethod = nullptr;
  16. ScriptModalWindow::ScriptModalWindow(ManagedModalWindow* window)
  17. :ScriptObject(window->getManagedInstance()), mModalWindow(window), mRefreshInProgress(false)
  18. {
  19. mOnAssemblyRefreshStartedConn = ScriptObjectManager::instance().onRefreshStarted.connect(std::bind(&ScriptModalWindow::onAssemblyRefreshStarted, this));
  20. }
  21. ScriptModalWindow::~ScriptModalWindow()
  22. {
  23. mOnAssemblyRefreshStartedConn.disconnect();
  24. }
  25. void ScriptModalWindow::initRuntimeData()
  26. {
  27. metaData.scriptClass->addInternalCall("Internal_CreateInstance", &ScriptModalWindow::internal_createInstance);
  28. metaData.scriptClass->addInternalCall("Internal_Close", &ScriptModalWindow::internal_close);
  29. metaData.scriptClass->addInternalCall("Internal_InitializeGUIPanel", &ScriptModalWindow::internal_initializeGUIPanel);
  30. metaData.scriptClass->addInternalCall("Internal_GetWidth", &ScriptModalWindow::internal_getWidth);
  31. metaData.scriptClass->addInternalCall("Internal_GetHeight", &ScriptModalWindow::internal_getHeight);
  32. onInitializedInternalMethod = metaData.scriptClass->getMethod("OnInitializeInternal", 0);
  33. onDestroyInternalMethod = metaData.scriptClass->getMethod("OnDestroyInternal", 0);
  34. }
  35. MonoObject* ScriptModalWindow::internal_createInstance(MonoString* ns, MonoString* typeName)
  36. {
  37. String strTypeName = toString(MonoUtil::monoToWString(typeName));
  38. String strNamespace = toString(MonoUtil::monoToWString(ns));
  39. String fullName = strNamespace + "." + strTypeName;
  40. ManagedModalWindow* modalWindow = bs_new<ManagedModalWindow>(strNamespace, strTypeName);
  41. ScriptModalWindow* nativeInstance = new (bs_alloc<ScriptModalWindow>()) ScriptModalWindow(modalWindow);
  42. mono_runtime_object_init(modalWindow->getManagedInstance()); // Construct it
  43. modalWindow->triggerOnInitialize();
  44. return modalWindow->getManagedInstance();
  45. }
  46. void ScriptModalWindow::internal_close(ScriptModalWindow* thisPtr)
  47. {
  48. if (thisPtr->mModalWindow == nullptr)
  49. return;
  50. thisPtr->mModalWindow->triggerOnDestroy();
  51. thisPtr->mModalWindow->releaseManagedInstance();
  52. thisPtr->mModalWindow->close();
  53. thisPtr->mModalWindow = nullptr;
  54. }
  55. void ScriptModalWindow::_onManagedInstanceDeleted()
  56. {
  57. if (!mRefreshInProgress)
  58. {
  59. // Note: This should only ever get triggered after "internal_close" is called
  60. ScriptObject::_onManagedInstanceDeleted();
  61. }
  62. else
  63. mManagedInstance = nullptr;
  64. }
  65. ScriptObjectBackup ScriptModalWindow::beginRefresh()
  66. {
  67. mRefreshInProgress = true;
  68. if (mModalWindow != nullptr)
  69. mModalWindow->releaseManagedInstance();
  70. return PersistentScriptObjectBase::beginRefresh();
  71. }
  72. void ScriptModalWindow::endRefresh(const ScriptObjectBackup& backupData)
  73. {
  74. mRefreshInProgress = false;
  75. if (mModalWindow != nullptr)
  76. mManagedInstance = mModalWindow->getManagedInstance();
  77. else
  78. mManagedInstance = nullptr;
  79. if (mManagedInstance != nullptr)
  80. {
  81. mModalWindow->triggerOnInitialize();
  82. }
  83. else
  84. {
  85. // We couldn't restore managed instance because window class cannot be found
  86. _onManagedInstanceDeleted();
  87. }
  88. PersistentScriptObjectBase::endRefresh(backupData);
  89. }
  90. void ScriptModalWindow::onAssemblyRefreshStarted()
  91. {
  92. if (mModalWindow != nullptr)
  93. mModalWindow->triggerOnDestroy();
  94. }
  95. MonoObject* ScriptModalWindow::_createManagedInstance(bool construct)
  96. {
  97. if (mModalWindow != nullptr)
  98. {
  99. mModalWindow->createManagedInstance();
  100. return mModalWindow->getManagedInstance();
  101. }
  102. else
  103. return nullptr;
  104. }
  105. UINT32 ScriptModalWindow::internal_getWidth(ScriptModalWindow* thisPtr)
  106. {
  107. if (thisPtr->mModalWindow != nullptr)
  108. return thisPtr->mModalWindow->getWidth();
  109. return 0;
  110. }
  111. UINT32 ScriptModalWindow::internal_getHeight(ScriptModalWindow* thisPtr)
  112. {
  113. if (thisPtr->mModalWindow != nullptr)
  114. return thisPtr->mModalWindow->getHeight();
  115. return 0;
  116. }
  117. void ScriptModalWindow::internal_initializeGUIPanel(ScriptModalWindow* thisPtr, MonoObject* panel)
  118. {
  119. ScriptGUIPanel* scriptGUIPanel = ScriptGUIPanel::toNative(panel);
  120. thisPtr->mGUIPanel = scriptGUIPanel;
  121. // TODO - Handle TitleBar when I add it
  122. scriptGUIPanel->setParentArea(0, 0,
  123. thisPtr->mModalWindow->getWidth(), thisPtr->mModalWindow->getHeight());
  124. scriptGUIPanel->setParentWidget(thisPtr->mModalWindow->getGUIWidget().get());
  125. }
  126. ManagedModalWindow::ManagedModalWindow(const String& ns, const String& type)
  127. :ModalWindow(), mNamespace(ns), mTypename(type), mUpdateThunk(nullptr), mManagedInstance(nullptr),
  128. mOnInitializeThunk(nullptr), mOnDestroyThunk(nullptr), mOnWindowResizedMethod(nullptr), mGCHandle(0)
  129. {
  130. createManagedInstance();
  131. }
  132. ManagedModalWindow::~ManagedModalWindow()
  133. {
  134. }
  135. bool ManagedModalWindow::createManagedInstance()
  136. {
  137. MonoAssembly* assembly = MonoManager::instance().getAssembly(BansheeEditorAssemblyName);
  138. if (assembly != nullptr)
  139. {
  140. MonoClass* editorWindowClass = assembly->getClass(mNamespace, mTypename);
  141. if (editorWindowClass != nullptr)
  142. {
  143. mManagedInstance = editorWindowClass->createInstance();
  144. mGCHandle = mono_gchandle_new(mManagedInstance, false);
  145. reloadMonoTypes(editorWindowClass);
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. void ManagedModalWindow::releaseManagedInstance()
  152. {
  153. mono_gchandle_free(mGCHandle);
  154. mGCHandle = 0;
  155. }
  156. void ManagedModalWindow::triggerOnInitialize()
  157. {
  158. if (mManagedInstance != nullptr)
  159. ScriptModalWindow::onInitializedInternalMethod->invoke(mManagedInstance, nullptr);
  160. if (mOnInitializeThunk != nullptr && mManagedInstance != nullptr)
  161. {
  162. MonoException* exception = nullptr;
  163. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  164. // for some extra speed.
  165. mOnInitializeThunk(mManagedInstance, &exception);
  166. MonoUtil::throwIfException(exception);
  167. }
  168. }
  169. void ManagedModalWindow::triggerOnDestroy()
  170. {
  171. if (mManagedInstance != nullptr)
  172. ScriptModalWindow::onDestroyInternalMethod->invoke(mManagedInstance, nullptr);
  173. if (mOnDestroyThunk != nullptr && mManagedInstance != nullptr)
  174. {
  175. MonoException* exception = nullptr;
  176. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  177. // for some extra speed.
  178. mOnDestroyThunk(mManagedInstance, &exception);
  179. MonoUtil::throwIfException(exception);
  180. }
  181. }
  182. void ManagedModalWindow::update()
  183. {
  184. if (mUpdateThunk != nullptr && mManagedInstance != nullptr)
  185. {
  186. MonoException* exception = nullptr;
  187. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  188. // for some extra speed.
  189. mUpdateThunk(mManagedInstance, &exception);
  190. MonoUtil::throwIfException(exception);
  191. }
  192. }
  193. void ManagedModalWindow::resized()
  194. {
  195. UINT32 width = getWidth();
  196. UINT32 height = getHeight();
  197. // TODO - Handle TitleBar when I add it
  198. mScriptParent->mGUIPanel->setParentArea(0, 0, width, height);
  199. if (mOnWindowResizedMethod != nullptr && mManagedInstance != nullptr)
  200. {
  201. void* params[] = { &width, &height };
  202. mOnWindowResizedMethod->invokeVirtual(mManagedInstance, params);
  203. }
  204. }
  205. void ManagedModalWindow::reloadMonoTypes(MonoClass* windowClass)
  206. {
  207. MonoMethod* updateMethod = windowClass->getMethod("OnEditorUpdate", 0);
  208. if (updateMethod != nullptr)
  209. mUpdateThunk = (UpdateThunkDef)updateMethod->getThunk();
  210. MonoMethod* onInitializeMethod = windowClass->getMethod("OnInitialize", 0);
  211. if (onInitializeMethod != nullptr)
  212. mOnInitializeThunk = (OnInitializeThunkDef)onInitializeMethod->getThunk();
  213. MonoMethod* onDestroyMethod = windowClass->getMethod("OnDestroy", 0);
  214. if (onDestroyMethod != nullptr)
  215. mOnDestroyThunk = (OnDestroyThunkDef)onDestroyMethod->getThunk();
  216. mOnWindowResizedMethod = windowClass->getMethod("OnWindowResized", 2);
  217. }
  218. }