BsScriptModalWindow.cpp 9.7 KB

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