BsScriptModalWindow.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. #include "BsScriptHString.h"
  12. using namespace std::placeholders;
  13. namespace BansheeEngine
  14. {
  15. MonoMethod* ScriptModalWindow::onInitializedInternalMethod = nullptr;
  16. MonoMethod* ScriptModalWindow::onDestroyInternalMethod = nullptr;
  17. ScriptModalWindow::ScriptModalWindow(ManagedModalWindow* window)
  18. :ScriptObject(window->getManagedInstance()), mModalWindow(window), mRefreshInProgress(false)
  19. {
  20. mOnAssemblyRefreshStartedConn = ScriptObjectManager::instance().onRefreshStarted.connect(std::bind(&ScriptModalWindow::onAssemblyRefreshStarted, this));
  21. }
  22. ScriptModalWindow::~ScriptModalWindow()
  23. {
  24. mOnAssemblyRefreshStartedConn.disconnect();
  25. }
  26. void ScriptModalWindow::initRuntimeData()
  27. {
  28. metaData.scriptClass->addInternalCall("Internal_CreateInstance", &ScriptModalWindow::internal_createInstance);
  29. metaData.scriptClass->addInternalCall("Internal_Close", &ScriptModalWindow::internal_close);
  30. metaData.scriptClass->addInternalCall("Internal_InitializeGUIPanel", &ScriptModalWindow::internal_initializeGUIPanel);
  31. metaData.scriptClass->addInternalCall("Internal_GetWidth", &ScriptModalWindow::internal_getWidth);
  32. metaData.scriptClass->addInternalCall("Internal_GetHeight", &ScriptModalWindow::internal_getHeight);
  33. metaData.scriptClass->addInternalCall("Internal_SetWidth", &ScriptModalWindow::internal_setWidth);
  34. metaData.scriptClass->addInternalCall("Internal_SetHeight", &ScriptModalWindow::internal_setHeight);
  35. metaData.scriptClass->addInternalCall("Internal_SetTitle", &ScriptModalWindow::internal_setTitle);
  36. onInitializedInternalMethod = metaData.scriptClass->getMethod("OnInitializeInternal", 0);
  37. onDestroyInternalMethod = metaData.scriptClass->getMethod("OnDestroyInternal", 0);
  38. }
  39. void ScriptModalWindow::internal_createInstance(MonoObject* instance, bool allowCloseButton)
  40. {
  41. ManagedModalWindow* modalWindow = bs_new<ManagedModalWindow>(allowCloseButton, instance);
  42. ScriptModalWindow* nativeInstance = new (bs_alloc<ScriptModalWindow>()) ScriptModalWindow(modalWindow);
  43. modalWindow->initialize(nativeInstance);
  44. modalWindow->triggerOnInitialize();
  45. }
  46. void ScriptModalWindow::internal_close(ScriptModalWindow* thisPtr)
  47. {
  48. thisPtr->closeWindow();
  49. }
  50. void ScriptModalWindow::internal_setTitle(ScriptModalWindow* thisPtr, MonoObject* title)
  51. {
  52. HString titleStr = HString::dummy();
  53. if (title != nullptr)
  54. {
  55. ScriptHString* textScript = ScriptHString::toNative(title);
  56. titleStr = textScript->getInternalValue();
  57. }
  58. thisPtr->mModalWindow->setTitle(titleStr);
  59. }
  60. void ScriptModalWindow::closeWindow()
  61. {
  62. if (mModalWindow == nullptr)
  63. return;
  64. mModalWindow->triggerOnDestroy();
  65. mModalWindow->releaseManagedInstance();
  66. mModalWindow->close();
  67. mModalWindow = nullptr;
  68. }
  69. void ScriptModalWindow::_onManagedInstanceDeleted()
  70. {
  71. if (!mRefreshInProgress)
  72. {
  73. // Note: This should only ever get triggered after "internal_close" is called
  74. ScriptObject::_onManagedInstanceDeleted();
  75. }
  76. else
  77. mManagedInstance = nullptr;
  78. }
  79. ScriptObjectBackup ScriptModalWindow::beginRefresh()
  80. {
  81. mRefreshInProgress = true;
  82. if (mModalWindow != nullptr)
  83. mModalWindow->releaseManagedInstance();
  84. return PersistentScriptObjectBase::beginRefresh();
  85. }
  86. void ScriptModalWindow::endRefresh(const ScriptObjectBackup& backupData)
  87. {
  88. mRefreshInProgress = false;
  89. if (mModalWindow != nullptr)
  90. mManagedInstance = mModalWindow->getManagedInstance();
  91. else
  92. mManagedInstance = nullptr;
  93. if (mManagedInstance != nullptr)
  94. {
  95. mModalWindow->triggerOnInitialize();
  96. }
  97. else
  98. {
  99. // We couldn't restore managed instance because window class cannot be found
  100. _onManagedInstanceDeleted();
  101. }
  102. PersistentScriptObjectBase::endRefresh(backupData);
  103. }
  104. void ScriptModalWindow::onAssemblyRefreshStarted()
  105. {
  106. if (mModalWindow != nullptr)
  107. mModalWindow->triggerOnDestroy();
  108. }
  109. MonoObject* ScriptModalWindow::_createManagedInstance(bool construct)
  110. {
  111. if (mModalWindow != nullptr)
  112. {
  113. mModalWindow->createManagedInstance();
  114. return mModalWindow->getManagedInstance();
  115. }
  116. else
  117. return nullptr;
  118. }
  119. UINT32 ScriptModalWindow::internal_getWidth(ScriptModalWindow* thisPtr)
  120. {
  121. if (thisPtr->mModalWindow != nullptr)
  122. return thisPtr->mModalWindow->getWidth();
  123. return 0;
  124. }
  125. UINT32 ScriptModalWindow::internal_getHeight(ScriptModalWindow* thisPtr)
  126. {
  127. if (thisPtr->mModalWindow != nullptr)
  128. return thisPtr->mModalWindow->getHeight();
  129. return 0;
  130. }
  131. void ScriptModalWindow::internal_setWidth(ScriptModalWindow* thisPtr, UINT32 value)
  132. {
  133. if (thisPtr->mModalWindow != nullptr)
  134. thisPtr->mModalWindow->setSize(value, thisPtr->mModalWindow->getHeight());
  135. }
  136. void ScriptModalWindow::internal_setHeight(ScriptModalWindow* thisPtr, UINT32 value)
  137. {
  138. if (thisPtr->mModalWindow != nullptr)
  139. thisPtr->mModalWindow->setSize(thisPtr->mModalWindow->getWidth(), value);
  140. }
  141. void ScriptModalWindow::internal_initializeGUIPanel(ScriptModalWindow* thisPtr, MonoObject* panel)
  142. {
  143. ScriptGUIPanel* scriptGUIPanel = ScriptGUIPanel::toNative(panel);
  144. thisPtr->mGUIPanel = scriptGUIPanel;
  145. Rect2I contentArea = thisPtr->mModalWindow->getContentArea();
  146. scriptGUIPanel->setParentArea(contentArea.x, contentArea.y, contentArea.width, contentArea.height);
  147. scriptGUIPanel->setParentWidget(thisPtr->mModalWindow->getGUIWidget().get());
  148. }
  149. ManagedModalWindow::ManagedModalWindow(bool allowCloseButton, MonoObject* managedInstance)
  150. :ModalWindow(HString::dummy(), allowCloseButton), mUpdateThunk(nullptr), mManagedInstance(managedInstance),
  151. mOnInitializeThunk(nullptr), mOnDestroyThunk(nullptr), mOnWindowResizedMethod(nullptr), mGCHandle(0),
  152. mScriptParent(nullptr)
  153. {
  154. mGCHandle = mono_gchandle_new(mManagedInstance, false);
  155. ::MonoClass* rawMonoClass = mono_object_get_class(mManagedInstance);
  156. MonoClass* monoClass = MonoManager::instance().findClass(rawMonoClass);
  157. mNamespace = monoClass->getNamespace();
  158. mTypename = monoClass->getTypeName();
  159. reloadMonoTypes(monoClass);
  160. }
  161. ManagedModalWindow::~ManagedModalWindow()
  162. {
  163. assert(mGCHandle == 0); // We expect "close" to be called either from C++ or C# before destruction
  164. }
  165. bool ManagedModalWindow::createManagedInstance()
  166. {
  167. MonoAssembly* assembly = MonoManager::instance().getAssembly(BansheeEditorAssemblyName);
  168. if (assembly != nullptr)
  169. {
  170. MonoClass* editorWindowClass = assembly->getClass(mNamespace, mTypename);
  171. if (editorWindowClass != nullptr)
  172. {
  173. mManagedInstance = editorWindowClass->createInstance();
  174. mGCHandle = mono_gchandle_new(mManagedInstance, false);
  175. reloadMonoTypes(editorWindowClass);
  176. return true;
  177. }
  178. }
  179. return false;
  180. }
  181. void ManagedModalWindow::releaseManagedInstance()
  182. {
  183. mono_gchandle_free(mGCHandle);
  184. mGCHandle = 0;
  185. }
  186. void ManagedModalWindow::triggerOnInitialize()
  187. {
  188. if (mManagedInstance != nullptr)
  189. ScriptModalWindow::onInitializedInternalMethod->invoke(mManagedInstance, nullptr);
  190. if (mOnInitializeThunk != nullptr && mManagedInstance != nullptr)
  191. {
  192. MonoException* exception = nullptr;
  193. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  194. // for some extra speed.
  195. mOnInitializeThunk(mManagedInstance, &exception);
  196. MonoUtil::throwIfException(exception);
  197. }
  198. }
  199. void ManagedModalWindow::triggerOnDestroy()
  200. {
  201. if (mManagedInstance != nullptr)
  202. ScriptModalWindow::onDestroyInternalMethod->invoke(mManagedInstance, nullptr);
  203. if (mOnDestroyThunk != nullptr && mManagedInstance != nullptr)
  204. {
  205. MonoException* exception = nullptr;
  206. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  207. // for some extra speed.
  208. mOnDestroyThunk(mManagedInstance, &exception);
  209. MonoUtil::throwIfException(exception);
  210. }
  211. }
  212. void ManagedModalWindow::initialize(ScriptModalWindow* parent)
  213. {
  214. mScriptParent = parent;
  215. }
  216. void ManagedModalWindow::update()
  217. {
  218. if (mUpdateThunk != nullptr && mManagedInstance != nullptr)
  219. {
  220. MonoException* exception = nullptr;
  221. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  222. // for some extra speed.
  223. mUpdateThunk(mManagedInstance, &exception);
  224. MonoUtil::throwIfException(exception);
  225. }
  226. }
  227. void ManagedModalWindow::resized()
  228. {
  229. UINT32 width = getWidth();
  230. UINT32 height = getHeight();
  231. Rect2I contentArea = getContentArea();
  232. mScriptParent->mGUIPanel->setParentArea(contentArea.x, contentArea.y, contentArea.width, contentArea.height);
  233. if (mOnWindowResizedMethod != nullptr && mManagedInstance != nullptr)
  234. {
  235. void* params[] = { &width, &height };
  236. mOnWindowResizedMethod->invokeVirtual(mManagedInstance, params);
  237. }
  238. }
  239. void ManagedModalWindow::close()
  240. {
  241. mScriptParent->closeWindow();
  242. }
  243. void ManagedModalWindow::reloadMonoTypes(MonoClass* windowClass)
  244. {
  245. MonoMethod* updateMethod = windowClass->getMethod("OnEditorUpdate", 0);
  246. if (updateMethod != nullptr)
  247. mUpdateThunk = (UpdateThunkDef)updateMethod->getThunk();
  248. MonoMethod* onInitializeMethod = windowClass->getMethod("OnInitialize", 0);
  249. if (onInitializeMethod != nullptr)
  250. mOnInitializeThunk = (OnInitializeThunkDef)onInitializeMethod->getThunk();
  251. MonoMethod* onDestroyMethod = windowClass->getMethod("OnDestroy", 0);
  252. if (onDestroyMethod != nullptr)
  253. mOnDestroyThunk = (OnDestroyThunkDef)onDestroyMethod->getThunk();
  254. mOnWindowResizedMethod = windowClass->getMethod("OnWindowResized", 2);
  255. }
  256. }