BsScriptModalWindow.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsScriptModalWindow.h"
  4. #include "BsScriptMeta.h"
  5. #include "BsMonoField.h"
  6. #include "BsMonoClass.h"
  7. #include "BsMonoMethod.h"
  8. #include "BsMonoManager.h"
  9. #include "BsMonoUtil.h"
  10. #include "BsMonoAssembly.h"
  11. #include "BsScriptObjectManager.h"
  12. #include "BsScriptHString.h"
  13. #include "BsRenderWindow.h"
  14. #include "BsScriptGUILayout.h"
  15. using namespace std::placeholders;
  16. namespace BansheeEngine
  17. {
  18. MonoField* ScriptModalWindow::guiPanelField = nullptr;
  19. ScriptModalWindow::ScriptModalWindow(ManagedModalWindow* window)
  20. :ScriptObject(window->getManagedInstance()), mModalWindow(window)
  21. {
  22. mOnAssemblyRefreshStartedConn = ScriptObjectManager::instance().onRefreshStarted.connect(std::bind(&ScriptModalWindow::onAssemblyRefreshStarted, this));
  23. }
  24. ScriptModalWindow::~ScriptModalWindow()
  25. {
  26. mOnAssemblyRefreshStartedConn.disconnect();
  27. }
  28. void ScriptModalWindow::initRuntimeData()
  29. {
  30. metaData.scriptClass->addInternalCall("Internal_CreateInstance", &ScriptModalWindow::internal_createInstance);
  31. metaData.scriptClass->addInternalCall("Internal_Close", &ScriptModalWindow::internal_close);
  32. metaData.scriptClass->addInternalCall("Internal_GetWidth", &ScriptModalWindow::internal_getWidth);
  33. metaData.scriptClass->addInternalCall("Internal_GetHeight", &ScriptModalWindow::internal_getHeight);
  34. metaData.scriptClass->addInternalCall("Internal_SetWidth", &ScriptModalWindow::internal_setWidth);
  35. metaData.scriptClass->addInternalCall("Internal_SetHeight", &ScriptModalWindow::internal_setHeight);
  36. metaData.scriptClass->addInternalCall("Internal_SetTitle", &ScriptModalWindow::internal_setTitle);
  37. metaData.scriptClass->addInternalCall("Internal_ScreenToWindowPos", &ScriptModalWindow::internal_screenToWindowPos);
  38. metaData.scriptClass->addInternalCall("Internal_WindowToScreenPos", &ScriptModalWindow::internal_windowToScreenPos);
  39. guiPanelField = metaData.scriptClass->getField("GUI");
  40. }
  41. void ScriptModalWindow::internal_createInstance(MonoObject* instance, bool allowCloseButton)
  42. {
  43. ManagedModalWindow* modalWindow = bs_new<ManagedModalWindow>(allowCloseButton, instance);
  44. ScriptModalWindow* nativeInstance = new (bs_alloc<ScriptModalWindow>()) ScriptModalWindow(modalWindow);
  45. modalWindow->setParent(nativeInstance);
  46. }
  47. void ScriptModalWindow::internal_close(ScriptModalWindow* thisPtr)
  48. {
  49. if (thisPtr->mModalWindow != nullptr)
  50. thisPtr->mModalWindow->close();
  51. }
  52. void ScriptModalWindow::internal_setTitle(ScriptModalWindow* thisPtr, MonoObject* title)
  53. {
  54. HString titleStr = HString::dummy();
  55. if (title != nullptr)
  56. {
  57. ScriptHString* textScript = ScriptHString::toNative(title);
  58. titleStr = textScript->getInternalValue();
  59. }
  60. thisPtr->mModalWindow->setTitle(titleStr);
  61. }
  62. void ScriptModalWindow::notifyWindowDestroyed()
  63. {
  64. mModalWindow = nullptr;
  65. }
  66. void ScriptModalWindow::onAssemblyRefreshStarted()
  67. {
  68. if (mModalWindow != nullptr)
  69. mModalWindow->close();
  70. }
  71. UINT32 ScriptModalWindow::internal_getWidth(ScriptModalWindow* thisPtr)
  72. {
  73. if (thisPtr->mModalWindow != nullptr)
  74. return thisPtr->mModalWindow->getWidth();
  75. return 0;
  76. }
  77. UINT32 ScriptModalWindow::internal_getHeight(ScriptModalWindow* thisPtr)
  78. {
  79. if (thisPtr->mModalWindow != nullptr)
  80. return thisPtr->mModalWindow->getHeight();
  81. return 0;
  82. }
  83. void ScriptModalWindow::internal_setWidth(ScriptModalWindow* thisPtr, UINT32 value)
  84. {
  85. if (thisPtr->mModalWindow != nullptr)
  86. thisPtr->mModalWindow->setSize(value, thisPtr->mModalWindow->getHeight());
  87. }
  88. void ScriptModalWindow::internal_setHeight(ScriptModalWindow* thisPtr, UINT32 value)
  89. {
  90. if (thisPtr->mModalWindow != nullptr)
  91. thisPtr->mModalWindow->setSize(thisPtr->mModalWindow->getWidth(), value);
  92. }
  93. void ScriptModalWindow::internal_screenToWindowPos(ScriptModalWindow* thisPtr, Vector2I* screenPos, Vector2I* windowPos)
  94. {
  95. if (thisPtr->mModalWindow != nullptr)
  96. *windowPos = thisPtr->mModalWindow->screenToWindowPos(*screenPos);
  97. else
  98. *windowPos = *screenPos;
  99. }
  100. void ScriptModalWindow::internal_windowToScreenPos(ScriptModalWindow* thisPtr, Vector2I* windowPos, Vector2I* screenPos)
  101. {
  102. if (thisPtr->mModalWindow != nullptr)
  103. *screenPos = thisPtr->mModalWindow->windowToScreenPos(*windowPos);
  104. else
  105. *screenPos = *windowPos;
  106. }
  107. ManagedModalWindow::ManagedModalWindow(bool allowCloseButton, MonoObject* managedInstance)
  108. :ModalWindow(HString::dummy(), allowCloseButton), mUpdateThunk(nullptr), mManagedInstance(managedInstance),
  109. mOnInitializeThunk(nullptr), mOnDestroyThunk(nullptr), mOnWindowResizedMethod(nullptr), mGCHandle(0),
  110. mScriptParent(nullptr), mContentsPanel(nullptr), mIsInitialized(false)
  111. {
  112. mGCHandle = mono_gchandle_new(mManagedInstance, false);
  113. MonoObject* guiPanel = ScriptGUIPanel::createFromExisting(mContents);
  114. mContentsPanel = ScriptGUILayout::toNative(guiPanel);
  115. ScriptModalWindow::guiPanelField->setValue(mManagedInstance, guiPanel);
  116. ::MonoClass* rawMonoClass = mono_object_get_class(mManagedInstance);
  117. MonoClass* monoClass = MonoManager::instance().findClass(rawMonoClass);
  118. mNamespace = monoClass->getNamespace();
  119. mTypename = monoClass->getTypeName();
  120. reloadMonoTypes(monoClass);
  121. }
  122. ManagedModalWindow::~ManagedModalWindow()
  123. {
  124. if (mGCHandle != 0)
  125. close();
  126. }
  127. bool ManagedModalWindow::createManagedInstance()
  128. {
  129. MonoAssembly* assembly = MonoManager::instance().getAssembly(EDITOR_ASSEMBLY);
  130. if (assembly != nullptr)
  131. {
  132. MonoClass* editorWindowClass = assembly->getClass(mNamespace, mTypename);
  133. if (editorWindowClass != nullptr)
  134. {
  135. mManagedInstance = editorWindowClass->createInstance(false);
  136. mGCHandle = mono_gchandle_new(mManagedInstance, false);
  137. MonoObject* guiPanel = ScriptGUIPanel::createFromExisting(mContents);
  138. mContentsPanel = ScriptGUILayout::toNative(guiPanel);
  139. ScriptModalWindow::guiPanelField->setValue(mManagedInstance, guiPanel);
  140. reloadMonoTypes(editorWindowClass);
  141. return true;
  142. }
  143. }
  144. return false;
  145. }
  146. void ManagedModalWindow::releaseManagedInstance()
  147. {
  148. mono_gchandle_free(mGCHandle);
  149. mGCHandle = 0;
  150. }
  151. void ManagedModalWindow::triggerOnInitialize()
  152. {
  153. if (mOnInitializeThunk != nullptr && mManagedInstance != nullptr)
  154. {
  155. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  156. // for some extra speed.
  157. MonoUtil::invokeThunk(mOnInitializeThunk, mManagedInstance);
  158. }
  159. }
  160. void ManagedModalWindow::triggerOnDestroy()
  161. {
  162. if (mOnDestroyThunk != nullptr && mManagedInstance != nullptr)
  163. {
  164. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  165. // for some extra speed.
  166. MonoUtil::invokeThunk(mOnDestroyThunk, mManagedInstance);
  167. }
  168. }
  169. void ManagedModalWindow::setParent(ScriptModalWindow* parent)
  170. {
  171. mScriptParent = parent;
  172. }
  173. void ManagedModalWindow::update()
  174. {
  175. if (!mIsInitialized)
  176. {
  177. triggerOnInitialize();
  178. mIsInitialized = true;
  179. }
  180. if (mUpdateThunk != nullptr && mManagedInstance != nullptr)
  181. {
  182. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  183. // for some extra speed.
  184. MonoUtil::invokeThunk(mUpdateThunk, mManagedInstance);
  185. }
  186. }
  187. void ManagedModalWindow::resized()
  188. {
  189. UINT32 width = getWidth();
  190. UINT32 height = getHeight();
  191. if (mOnWindowResizedMethod != nullptr && mManagedInstance != nullptr)
  192. {
  193. void* params[] = { &width, &height };
  194. mOnWindowResizedMethod->invokeVirtual(mManagedInstance, params);
  195. }
  196. ModalWindow::resized();
  197. }
  198. void ManagedModalWindow::close()
  199. {
  200. triggerOnDestroy();
  201. mContentsPanel->destroy();
  202. mContentsPanel = nullptr;
  203. releaseManagedInstance();
  204. mScriptParent->notifyWindowDestroyed();
  205. ModalWindow::close();
  206. }
  207. void ManagedModalWindow::reloadMonoTypes(MonoClass* windowClass)
  208. {
  209. MonoMethod* updateMethod = windowClass->getMethod("OnEditorUpdate", 0);
  210. if (updateMethod != nullptr)
  211. mUpdateThunk = (UpdateThunkDef)updateMethod->getThunk();
  212. MonoMethod* onInitializeMethod = windowClass->getMethod("OnInitialize", 0);
  213. if (onInitializeMethod != nullptr)
  214. mOnInitializeThunk = (OnInitializeThunkDef)onInitializeMethod->getThunk();
  215. MonoMethod* onDestroyMethod = windowClass->getMethod("OnDestroy", 0);
  216. if (onDestroyMethod != nullptr)
  217. mOnDestroyThunk = (OnDestroyThunkDef)onDestroyMethod->getThunk();
  218. MonoClass* modalWindowClass = windowClass->getBaseClass();
  219. mOnWindowResizedMethod = modalWindowClass->getMethod("OnWindowResized", 2);
  220. }
  221. }