BsScriptModalWindow.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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_GetContentWidth", (void*)&ScriptModalWindow::internal_getContentWidth);
  37. metaData.scriptClass->addInternalCall("Internal_GetContentHeight", (void*)&ScriptModalWindow::internal_getContentHeight);
  38. metaData.scriptClass->addInternalCall("Internal_SetContentWidth", (void*)&ScriptModalWindow::internal_setContentWidth);
  39. metaData.scriptClass->addInternalCall("Internal_SetContentHeight", (void*)&ScriptModalWindow::internal_setContentHeight);
  40. metaData.scriptClass->addInternalCall("Internal_SetTitle", (void*)&ScriptModalWindow::internal_setTitle);
  41. metaData.scriptClass->addInternalCall("Internal_ScreenToWindowPos", (void*)&ScriptModalWindow::internal_screenToWindowPos);
  42. metaData.scriptClass->addInternalCall("Internal_WindowToScreenPos", (void*)&ScriptModalWindow::internal_windowToScreenPos);
  43. guiPanelField = metaData.scriptClass->getField("GUI");
  44. }
  45. void ScriptModalWindow::internal_createInstance(MonoObject* instance, bool allowCloseButton)
  46. {
  47. ::MonoClass* rawMonoClass = MonoUtil::getClass(instance);
  48. MonoClass* monoClass = MonoManager::instance().findClass(rawMonoClass);
  49. MonoAssembly* assembly = MonoManager::instance().getAssembly(EDITOR_ASSEMBLY);
  50. MonoClass* defaultSizeAttrib = assembly->getClass(EDITOR_NS, "DefaultSize");
  51. if (defaultSizeAttrib == nullptr)
  52. BS_EXCEPT(InternalErrorException, "Cannot find DefaultSize managed class.");
  53. MonoField* defaultWidthField = defaultSizeAttrib->getField("width");
  54. MonoField* defaultHeightField = defaultSizeAttrib->getField("height");
  55. int width = 200;
  56. int height = 200;
  57. MonoObject* defaultSizeObj = monoClass->getAttribute(defaultSizeAttrib);
  58. if (defaultSizeObj != nullptr)
  59. {
  60. defaultWidthField->get(defaultSizeObj, &width);
  61. defaultHeightField->get(defaultSizeObj, &height);
  62. }
  63. ManagedModalWindow* modalWindow = bs_new<ManagedModalWindow>(allowCloseButton, width, height, instance);
  64. ScriptModalWindow* nativeInstance = new (bs_alloc<ScriptModalWindow>()) ScriptModalWindow(modalWindow);
  65. modalWindow->setParent(nativeInstance);
  66. }
  67. void ScriptModalWindow::internal_close(ScriptModalWindow* thisPtr)
  68. {
  69. if (thisPtr->mModalWindow != nullptr)
  70. thisPtr->mModalWindow->close();
  71. }
  72. void ScriptModalWindow::internal_setTitle(ScriptModalWindow* thisPtr, MonoObject* title)
  73. {
  74. HString titleStr = HString::dummy();
  75. if (title != nullptr)
  76. {
  77. ScriptHString* textScript = ScriptHString::toNative(title);
  78. titleStr = *textScript->getInternal();
  79. }
  80. thisPtr->mModalWindow->setTitle(titleStr);
  81. }
  82. void ScriptModalWindow::notifyWindowDestroyed()
  83. {
  84. mModalWindow = nullptr;
  85. }
  86. void ScriptModalWindow::onAssemblyRefreshStarted()
  87. {
  88. if (mModalWindow != nullptr)
  89. mModalWindow->close();
  90. }
  91. UINT32 ScriptModalWindow::internal_getWidth(ScriptModalWindow* thisPtr)
  92. {
  93. if (thisPtr->mModalWindow != nullptr)
  94. return thisPtr->mModalWindow->getWidth();
  95. return 0;
  96. }
  97. UINT32 ScriptModalWindow::internal_getHeight(ScriptModalWindow* thisPtr)
  98. {
  99. if (thisPtr->mModalWindow != nullptr)
  100. return thisPtr->mModalWindow->getHeight();
  101. return 0;
  102. }
  103. void ScriptModalWindow::internal_setWidth(ScriptModalWindow* thisPtr, UINT32 value)
  104. {
  105. if (thisPtr->mModalWindow != nullptr)
  106. thisPtr->mModalWindow->setSize(value, thisPtr->mModalWindow->getHeight());
  107. }
  108. void ScriptModalWindow::internal_setHeight(ScriptModalWindow* thisPtr, UINT32 value)
  109. {
  110. if (thisPtr->mModalWindow != nullptr)
  111. thisPtr->mModalWindow->setSize(thisPtr->mModalWindow->getWidth(), value);
  112. }
  113. UINT32 ScriptModalWindow::internal_getContentWidth(ScriptModalWindow* thisPtr)
  114. {
  115. if (thisPtr->mModalWindow != nullptr)
  116. return thisPtr->mModalWindow->getContentWidth();
  117. return 0;
  118. }
  119. UINT32 ScriptModalWindow::internal_getContentHeight(ScriptModalWindow* thisPtr)
  120. {
  121. if (thisPtr->mModalWindow != nullptr)
  122. return thisPtr->mModalWindow->getContentHeight();
  123. return 0;
  124. }
  125. void ScriptModalWindow::internal_setContentWidth(ScriptModalWindow* thisPtr, UINT32 value)
  126. {
  127. if (thisPtr->mModalWindow != nullptr)
  128. thisPtr->mModalWindow->setSize(value, thisPtr->mModalWindow->getHeight());
  129. }
  130. void ScriptModalWindow::internal_setContentHeight(ScriptModalWindow* thisPtr, UINT32 value)
  131. {
  132. if (thisPtr->mModalWindow != nullptr)
  133. thisPtr->mModalWindow->setSize(thisPtr->mModalWindow->getWidth(), value);
  134. }
  135. void ScriptModalWindow::internal_screenToWindowPos(ScriptModalWindow* thisPtr, Vector2I* screenPos, Vector2I* windowPos)
  136. {
  137. if (thisPtr->mModalWindow != nullptr)
  138. *windowPos = thisPtr->mModalWindow->screenToWindowPos(*screenPos);
  139. else
  140. *windowPos = *screenPos;
  141. }
  142. void ScriptModalWindow::internal_windowToScreenPos(ScriptModalWindow* thisPtr, Vector2I* windowPos, Vector2I* screenPos)
  143. {
  144. if (thisPtr->mModalWindow != nullptr)
  145. *screenPos = thisPtr->mModalWindow->windowToScreenPos(*windowPos);
  146. else
  147. *screenPos = *windowPos;
  148. }
  149. ManagedModalWindow::ManagedModalWindow(bool allowCloseButton, UINT32 width, UINT32 height, MonoObject* managedInstance)
  150. : ModalWindow(HString::dummy(), allowCloseButton, width, height), mManagedInstance(managedInstance)
  151. {
  152. mGCHandle = MonoUtil::newGCHandle(mManagedInstance);
  153. mManagedInstance = MonoUtil::getObjectFromGCHandle(mGCHandle);
  154. MonoObject* guiPanel = ScriptGUIPanel::createFromExisting(mContents);
  155. mContentsPanel = ScriptGUILayout::toNative(guiPanel);
  156. ScriptModalWindow::guiPanelField->set(mManagedInstance, guiPanel);
  157. ::MonoClass* rawMonoClass = MonoUtil::getClass(mManagedInstance);
  158. MonoClass* monoClass = MonoManager::instance().findClass(rawMonoClass);
  159. mNamespace = monoClass->getNamespace();
  160. mTypename = monoClass->getTypeName();
  161. reloadMonoTypes(monoClass);
  162. }
  163. ManagedModalWindow::~ManagedModalWindow()
  164. {
  165. if (mGCHandle != 0)
  166. close();
  167. }
  168. bool ManagedModalWindow::createManagedInstance()
  169. {
  170. MonoAssembly* assembly = MonoManager::instance().getAssembly(EDITOR_ASSEMBLY);
  171. if (assembly != nullptr)
  172. {
  173. MonoClass* editorWindowClass = assembly->getClass(mNamespace, mTypename);
  174. if (editorWindowClass != nullptr)
  175. {
  176. mManagedInstance = editorWindowClass->createInstance(false);
  177. mGCHandle = MonoUtil::newGCHandle(mManagedInstance);
  178. mManagedInstance = MonoUtil::getObjectFromGCHandle(mGCHandle);
  179. MonoObject* guiPanel = ScriptGUIPanel::createFromExisting(mContents);
  180. mContentsPanel = ScriptGUILayout::toNative(guiPanel);
  181. ScriptModalWindow::guiPanelField->set(mManagedInstance, guiPanel);
  182. reloadMonoTypes(editorWindowClass);
  183. return true;
  184. }
  185. }
  186. return false;
  187. }
  188. void ManagedModalWindow::releaseManagedInstance()
  189. {
  190. MonoUtil::freeGCHandle(mGCHandle);
  191. mGCHandle = 0;
  192. }
  193. void ManagedModalWindow::triggerOnInitialize()
  194. {
  195. if (mOnInitializeThunk != nullptr && mManagedInstance != nullptr)
  196. {
  197. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  198. // for some extra speed.
  199. MonoUtil::invokeThunk(mOnInitializeThunk, mManagedInstance);
  200. }
  201. }
  202. void ManagedModalWindow::triggerOnDestroy()
  203. {
  204. if (mOnDestroyThunk != nullptr && mManagedInstance != nullptr)
  205. {
  206. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  207. // for some extra speed.
  208. MonoUtil::invokeThunk(mOnDestroyThunk, mManagedInstance);
  209. }
  210. }
  211. void ManagedModalWindow::setParent(ScriptModalWindow* parent)
  212. {
  213. mScriptParent = parent;
  214. }
  215. void ManagedModalWindow::update()
  216. {
  217. if (!mIsInitialized)
  218. {
  219. triggerOnInitialize();
  220. mIsInitialized = true;
  221. }
  222. if (mUpdateThunk != nullptr && mManagedInstance != nullptr)
  223. {
  224. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  225. // for some extra speed.
  226. MonoUtil::invokeThunk(mUpdateThunk, mManagedInstance);
  227. }
  228. }
  229. void ManagedModalWindow::resized()
  230. {
  231. UINT32 width = getWidth();
  232. UINT32 height = getHeight();
  233. if (mOnWindowResizedMethod != nullptr && mManagedInstance != nullptr)
  234. {
  235. void* params[] = { &width, &height };
  236. mOnWindowResizedMethod->invokeVirtual(mManagedInstance, params);
  237. }
  238. ModalWindow::resized();
  239. }
  240. void ManagedModalWindow::close()
  241. {
  242. triggerOnDestroy();
  243. mContentsPanel->destroy();
  244. mContentsPanel = nullptr;
  245. releaseManagedInstance();
  246. mScriptParent->notifyWindowDestroyed();
  247. ModalWindow::close();
  248. }
  249. void ManagedModalWindow::reloadMonoTypes(MonoClass* windowClass)
  250. {
  251. MonoMethod* updateMethod = windowClass->getMethod("OnEditorUpdate", 0);
  252. if (updateMethod != nullptr)
  253. mUpdateThunk = (UpdateThunkDef)updateMethod->getThunk();
  254. MonoMethod* onInitializeMethod = windowClass->getMethod("OnInitialize", 0);
  255. if (onInitializeMethod != nullptr)
  256. mOnInitializeThunk = (OnInitializeThunkDef)onInitializeMethod->getThunk();
  257. MonoMethod* onDestroyMethod = windowClass->getMethod("OnDestroy", 0);
  258. if (onDestroyMethod != nullptr)
  259. mOnDestroyThunk = (OnDestroyThunkDef)onDestroyMethod->getThunk();
  260. MonoClass* modalWindowClass = windowClass->getBaseClass();
  261. mOnWindowResizedMethod = modalWindowClass->getMethod("OnWindowResized", 2);
  262. }
  263. }