BsScriptModalWindow.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Wrappers/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 "RenderAPI/BsRenderWindow.h"
  13. #include "Wrappers/GUI/BsScriptGUILayout.h"
  14. #include "Generated/BsScriptHString.generated.h"
  15. using namespace std::placeholders;
  16. namespace bs
  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", (void*)&ScriptModalWindow::internal_createInstance);
  31. metaData.scriptClass->addInternalCall("Internal_Close", (void*)&ScriptModalWindow::internal_close);
  32. metaData.scriptClass->addInternalCall("Internal_GetWidth", (void*)&ScriptModalWindow::internal_getWidth);
  33. metaData.scriptClass->addInternalCall("Internal_GetHeight", (void*)&ScriptModalWindow::internal_getHeight);
  34. metaData.scriptClass->addInternalCall("Internal_SetWidth", (void*)&ScriptModalWindow::internal_setWidth);
  35. metaData.scriptClass->addInternalCall("Internal_SetHeight", (void*)&ScriptModalWindow::internal_setHeight);
  36. metaData.scriptClass->addInternalCall("Internal_SetTitle", (void*)&ScriptModalWindow::internal_setTitle);
  37. metaData.scriptClass->addInternalCall("Internal_ScreenToWindowPos", (void*)&ScriptModalWindow::internal_screenToWindowPos);
  38. metaData.scriptClass->addInternalCall("Internal_WindowToScreenPos", (void*)&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->getInternal();
  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), mOnInitializeThunk(nullptr), mOnDestroyThunk(nullptr)
  109. , mUpdateThunk(nullptr), mOnWindowResizedMethod(nullptr), mIsInitialized(false), mManagedInstance(managedInstance)
  110. , mGCHandle(0), mScriptParent(nullptr), mContentsPanel(nullptr)
  111. {
  112. mGCHandle = MonoUtil::newGCHandle(mManagedInstance);
  113. mManagedInstance = MonoUtil::getObjectFromGCHandle(mGCHandle);
  114. MonoObject* guiPanel = ScriptGUIPanel::createFromExisting(mContents);
  115. mContentsPanel = ScriptGUILayout::toNative(guiPanel);
  116. ScriptModalWindow::guiPanelField->set(mManagedInstance, guiPanel);
  117. ::MonoClass* rawMonoClass = MonoUtil::getClass(mManagedInstance);
  118. MonoClass* monoClass = MonoManager::instance().findClass(rawMonoClass);
  119. mNamespace = monoClass->getNamespace();
  120. mTypename = monoClass->getTypeName();
  121. reloadMonoTypes(monoClass);
  122. }
  123. ManagedModalWindow::~ManagedModalWindow()
  124. {
  125. if (mGCHandle != 0)
  126. close();
  127. }
  128. bool ManagedModalWindow::createManagedInstance()
  129. {
  130. MonoAssembly* assembly = MonoManager::instance().getAssembly(EDITOR_ASSEMBLY);
  131. if (assembly != nullptr)
  132. {
  133. MonoClass* editorWindowClass = assembly->getClass(mNamespace, mTypename);
  134. if (editorWindowClass != nullptr)
  135. {
  136. mManagedInstance = editorWindowClass->createInstance(false);
  137. mGCHandle = MonoUtil::newGCHandle(mManagedInstance);
  138. mManagedInstance = MonoUtil::getObjectFromGCHandle(mGCHandle);
  139. MonoObject* guiPanel = ScriptGUIPanel::createFromExisting(mContents);
  140. mContentsPanel = ScriptGUILayout::toNative(guiPanel);
  141. ScriptModalWindow::guiPanelField->set(mManagedInstance, guiPanel);
  142. reloadMonoTypes(editorWindowClass);
  143. return true;
  144. }
  145. }
  146. return false;
  147. }
  148. void ManagedModalWindow::releaseManagedInstance()
  149. {
  150. MonoUtil::freeGCHandle(mGCHandle);
  151. mGCHandle = 0;
  152. }
  153. void ManagedModalWindow::triggerOnInitialize()
  154. {
  155. if (mOnInitializeThunk != nullptr && mManagedInstance != nullptr)
  156. {
  157. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  158. // for some extra speed.
  159. MonoUtil::invokeThunk(mOnInitializeThunk, mManagedInstance);
  160. }
  161. }
  162. void ManagedModalWindow::triggerOnDestroy()
  163. {
  164. if (mOnDestroyThunk != nullptr && mManagedInstance != nullptr)
  165. {
  166. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  167. // for some extra speed.
  168. MonoUtil::invokeThunk(mOnDestroyThunk, mManagedInstance);
  169. }
  170. }
  171. void ManagedModalWindow::setParent(ScriptModalWindow* parent)
  172. {
  173. mScriptParent = parent;
  174. }
  175. void ManagedModalWindow::update()
  176. {
  177. if (!mIsInitialized)
  178. {
  179. triggerOnInitialize();
  180. mIsInitialized = true;
  181. }
  182. if (mUpdateThunk != nullptr && mManagedInstance != nullptr)
  183. {
  184. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  185. // for some extra speed.
  186. MonoUtil::invokeThunk(mUpdateThunk, mManagedInstance);
  187. }
  188. }
  189. void ManagedModalWindow::resized()
  190. {
  191. UINT32 width = getWidth();
  192. UINT32 height = getHeight();
  193. if (mOnWindowResizedMethod != nullptr && mManagedInstance != nullptr)
  194. {
  195. void* params[] = { &width, &height };
  196. mOnWindowResizedMethod->invokeVirtual(mManagedInstance, params);
  197. }
  198. ModalWindow::resized();
  199. }
  200. void ManagedModalWindow::close()
  201. {
  202. triggerOnDestroy();
  203. mContentsPanel->destroy();
  204. mContentsPanel = nullptr;
  205. releaseManagedInstance();
  206. mScriptParent->notifyWindowDestroyed();
  207. ModalWindow::close();
  208. }
  209. void ManagedModalWindow::reloadMonoTypes(MonoClass* windowClass)
  210. {
  211. MonoMethod* updateMethod = windowClass->getMethod("OnEditorUpdate", 0);
  212. if (updateMethod != nullptr)
  213. mUpdateThunk = (UpdateThunkDef)updateMethod->getThunk();
  214. MonoMethod* onInitializeMethod = windowClass->getMethod("OnInitialize", 0);
  215. if (onInitializeMethod != nullptr)
  216. mOnInitializeThunk = (OnInitializeThunkDef)onInitializeMethod->getThunk();
  217. MonoMethod* onDestroyMethod = windowClass->getMethod("OnDestroy", 0);
  218. if (onDestroyMethod != nullptr)
  219. mOnDestroyThunk = (OnDestroyThunkDef)onDestroyMethod->getThunk();
  220. MonoClass* modalWindowClass = windowClass->getBaseClass();
  221. mOnWindowResizedMethod = modalWindowClass->getMethod("OnWindowResized", 2);
  222. }
  223. }