BsScriptModalWindow.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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)
  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::onAssemblyRefreshStarted()
  66. {
  67. if (mModalWindow != nullptr)
  68. mModalWindow->close();
  69. }
  70. UINT32 ScriptModalWindow::internal_getWidth(ScriptModalWindow* thisPtr)
  71. {
  72. if (thisPtr->mModalWindow != nullptr)
  73. return thisPtr->mModalWindow->getWidth();
  74. return 0;
  75. }
  76. UINT32 ScriptModalWindow::internal_getHeight(ScriptModalWindow* thisPtr)
  77. {
  78. if (thisPtr->mModalWindow != nullptr)
  79. return thisPtr->mModalWindow->getHeight();
  80. return 0;
  81. }
  82. void ScriptModalWindow::internal_setWidth(ScriptModalWindow* thisPtr, UINT32 value)
  83. {
  84. if (thisPtr->mModalWindow != nullptr)
  85. thisPtr->mModalWindow->setSize(value, thisPtr->mModalWindow->getHeight());
  86. }
  87. void ScriptModalWindow::internal_setHeight(ScriptModalWindow* thisPtr, UINT32 value)
  88. {
  89. if (thisPtr->mModalWindow != nullptr)
  90. thisPtr->mModalWindow->setSize(thisPtr->mModalWindow->getWidth(), value);
  91. }
  92. void ScriptModalWindow::internal_screenToWindowPos(ScriptModalWindow* thisPtr, Vector2I screenPos, Vector2I* windowPos)
  93. {
  94. if (thisPtr->mModalWindow != nullptr)
  95. *windowPos = thisPtr->mModalWindow->screenToWindowPos(screenPos);
  96. else
  97. *windowPos = screenPos;
  98. }
  99. void ScriptModalWindow::internal_windowToScreenPos(ScriptModalWindow* thisPtr, Vector2I windowPos, Vector2I* screenPos)
  100. {
  101. if (thisPtr->mModalWindow != nullptr)
  102. *screenPos = thisPtr->mModalWindow->windowToScreenPos(windowPos);
  103. else
  104. *screenPos = windowPos;
  105. }
  106. ManagedModalWindow::ManagedModalWindow(bool allowCloseButton, MonoObject* managedInstance)
  107. :ModalWindow(HString::dummy(), allowCloseButton), mUpdateThunk(nullptr), mManagedInstance(managedInstance),
  108. mOnInitializeThunk(nullptr), mOnDestroyThunk(nullptr), mOnWindowResizedMethod(nullptr), mGCHandle(0),
  109. mScriptParent(nullptr), mContentsPanel(nullptr)
  110. {
  111. mGCHandle = mono_gchandle_new(mManagedInstance, false);
  112. MonoObject* guiPanel = ScriptGUIPanel::createFromExisting(mContents);
  113. mContentsPanel = ScriptGUILayout::toNative(guiPanel);
  114. ScriptModalWindow::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. ManagedModalWindow::~ManagedModalWindow()
  122. {
  123. assert(mGCHandle == 0); // We expect "close" to be called either from C++ or C# before destruction
  124. }
  125. bool ManagedModalWindow::createManagedInstance()
  126. {
  127. MonoAssembly* assembly = MonoManager::instance().getAssembly(EDITOR_ASSEMBLY);
  128. if (assembly != nullptr)
  129. {
  130. MonoClass* editorWindowClass = assembly->getClass(mNamespace, mTypename);
  131. if (editorWindowClass != nullptr)
  132. {
  133. mManagedInstance = editorWindowClass->createInstance(false);
  134. mGCHandle = mono_gchandle_new(mManagedInstance, false);
  135. MonoObject* guiPanel = ScriptGUIPanel::createFromExisting(mContents);
  136. mContentsPanel = ScriptGUILayout::toNative(guiPanel);
  137. ScriptModalWindow::guiPanelField->setValue(mManagedInstance, guiPanel);
  138. reloadMonoTypes(editorWindowClass);
  139. return true;
  140. }
  141. }
  142. return false;
  143. }
  144. void ManagedModalWindow::releaseManagedInstance()
  145. {
  146. mono_gchandle_free(mGCHandle);
  147. mGCHandle = 0;
  148. }
  149. void ManagedModalWindow::triggerOnInitialize()
  150. {
  151. if (mOnInitializeThunk != nullptr && mManagedInstance != nullptr)
  152. {
  153. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  154. // for some extra speed.
  155. MonoUtil::invokeThunk(mOnInitializeThunk, mManagedInstance);
  156. }
  157. }
  158. void ManagedModalWindow::triggerOnDestroy()
  159. {
  160. if (mOnDestroyThunk != nullptr && mManagedInstance != nullptr)
  161. {
  162. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  163. // for some extra speed.
  164. MonoUtil::invokeThunk(mOnDestroyThunk, mManagedInstance);
  165. }
  166. }
  167. void ManagedModalWindow::setParent(ScriptModalWindow* parent)
  168. {
  169. mScriptParent = parent;
  170. }
  171. void ManagedModalWindow::update()
  172. {
  173. if (mUpdateThunk != nullptr && mManagedInstance != nullptr)
  174. {
  175. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  176. // for some extra speed.
  177. MonoUtil::invokeThunk(mUpdateThunk, mManagedInstance);
  178. }
  179. }
  180. void ManagedModalWindow::resized()
  181. {
  182. UINT32 width = getWidth();
  183. UINT32 height = getHeight();
  184. if (mOnWindowResizedMethod != nullptr && mManagedInstance != nullptr)
  185. {
  186. void* params[] = { &width, &height };
  187. mOnWindowResizedMethod->invokeVirtual(mManagedInstance, params);
  188. }
  189. ModalWindow::resized();
  190. }
  191. void ManagedModalWindow::close()
  192. {
  193. triggerOnDestroy();
  194. mContentsPanel->destroy();
  195. mContentsPanel = nullptr;
  196. releaseManagedInstance();
  197. mScriptParent->notifyWindowDestroyed();
  198. ModalWindow::close();
  199. }
  200. void ManagedModalWindow::reloadMonoTypes(MonoClass* windowClass)
  201. {
  202. MonoMethod* updateMethod = windowClass->getMethod("OnEditorUpdate", 0);
  203. if (updateMethod != nullptr)
  204. mUpdateThunk = (UpdateThunkDef)updateMethod->getThunk();
  205. MonoMethod* onInitializeMethod = windowClass->getMethod("OnInitialize", 0);
  206. if (onInitializeMethod != nullptr)
  207. mOnInitializeThunk = (OnInitializeThunkDef)onInitializeMethod->getThunk();
  208. MonoMethod* onDestroyMethod = windowClass->getMethod("OnDestroy", 0);
  209. if (onDestroyMethod != nullptr)
  210. mOnDestroyThunk = (OnDestroyThunkDef)onDestroyMethod->getThunk();
  211. MonoClass* modalWindowClass = windowClass->getBaseClass();
  212. mOnWindowResizedMethod = modalWindowClass->getMethod("OnWindowResized", 2);
  213. }
  214. }