BsScriptDropDownWindow.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Wrappers/BsScriptDropDownWindow.h"
  4. #include "BsScriptMeta.h"
  5. #include "BsMonoField.h"
  6. #include "BsMonoClass.h"
  7. #include "BsMonoMethod.h"
  8. #include "BsMonoAssembly.h"
  9. #include "BsMonoManager.h"
  10. #include "BsMonoUtil.h"
  11. #include "Wrappers/GUI/BsScriptGUILayout.h"
  12. #include "Wrappers/BsScriptEditorWindow.h"
  13. #include "EditorWindow/BsEditorWidget.h"
  14. #include "EditorWindow/BsEditorWindow.h"
  15. #include "EditorWindow/BsEditorWidgetContainer.h"
  16. #include "GUI/BsCGUIWidget.h"
  17. #include "EditorWindow/BsDropDownWindowManager.h"
  18. #include "BsScriptObjectManager.h"
  19. using namespace std::placeholders;
  20. namespace bs
  21. {
  22. MonoField* ScriptDropDownWindow::guiPanelField = nullptr;
  23. ScriptDropDownWindow::ScriptDropDownWindow(ManagedDropDownWindow* window)
  24. :ScriptObject(window->getManagedInstance()), mDropDownWindow(window)
  25. {
  26. mOnAssemblyRefreshStartedConn = ScriptObjectManager::instance().onRefreshStarted.connect(
  27. std::bind(&ScriptDropDownWindow::onAssemblyRefreshStarted, this));
  28. }
  29. ScriptDropDownWindow::~ScriptDropDownWindow()
  30. {
  31. mOnAssemblyRefreshStartedConn.disconnect();
  32. // Window must have been marked as deleted already
  33. assert(mDropDownWindow == nullptr);
  34. }
  35. void ScriptDropDownWindow::initRuntimeData()
  36. {
  37. metaData.scriptClass->addInternalCall("Internal_CreateInstance", (void*)&ScriptDropDownWindow::internal_CreateInstance);
  38. metaData.scriptClass->addInternalCall("Internal_Close", (void*)&ScriptDropDownWindow::internal_Close);
  39. metaData.scriptClass->addInternalCall("Internal_GetWidth", (void*)&ScriptDropDownWindow::internal_GetWidth);
  40. metaData.scriptClass->addInternalCall("Internal_SetWidth", (void*)&ScriptDropDownWindow::internal_SetWidth);
  41. metaData.scriptClass->addInternalCall("Internal_GetHeight", (void*)&ScriptDropDownWindow::internal_GetHeight);
  42. metaData.scriptClass->addInternalCall("Internal_SetHeight", (void*)&ScriptDropDownWindow::internal_SetHeight);
  43. metaData.scriptClass->addInternalCall("Internal_ScreenToWindowPos", (void*)&ScriptDropDownWindow::internal_ScreenToWindowPos);
  44. metaData.scriptClass->addInternalCall("Internal_WindowToScreenPos", (void*)&ScriptDropDownWindow::internal_WindowToScreenPos);
  45. guiPanelField = metaData.scriptClass->getField("GUI");
  46. }
  47. MonoObject* ScriptDropDownWindow::internal_CreateInstance(MonoString* ns, MonoString* typeName,
  48. ScriptEditorWindow* parentWindow, Vector2I* position)
  49. {
  50. String strTypeName = MonoUtil::monoToString(typeName);
  51. String strNamespace = MonoUtil::monoToString(ns);
  52. String fullName = strNamespace + "." + strTypeName;
  53. MonoClass* windowClass = MonoManager::instance().findClass(strNamespace, strTypeName);
  54. if (windowClass == nullptr)
  55. return nullptr;
  56. MonoAssembly* assembly = MonoManager::instance().getAssembly(EDITOR_ASSEMBLY);
  57. MonoClass* defaultSizeAttrib = assembly->getClass("BansheeEditor", "DefaultSize");
  58. if (defaultSizeAttrib == nullptr)
  59. BS_EXCEPT(InternalErrorException, "Cannot find DefaultSize managed class.");
  60. MonoField* defaultWidthField = defaultSizeAttrib->getField("width");
  61. MonoField* defaultHeightField = defaultSizeAttrib->getField("height");
  62. int width = 200;
  63. int height = 200;
  64. MonoObject* defaultSizeObj = windowClass->getAttribute(defaultSizeAttrib);
  65. if (defaultSizeObj != nullptr)
  66. {
  67. defaultWidthField->get(defaultSizeObj, &width);
  68. defaultHeightField->get(defaultSizeObj, &height);
  69. }
  70. MonoObject* instance = windowClass->createInstance(false);
  71. ManagedDropDownWindow* dropDownWindow = nullptr;
  72. if (parentWindow != nullptr && !parentWindow->isDestroyed())
  73. {
  74. EditorWidgetBase* editorWidget = parentWindow->getEditorWidget();
  75. EditorWidgetContainer* parentContainer = editorWidget->_getParent();
  76. if (parentContainer != nullptr)
  77. {
  78. SPtr<RenderWindow> parentRenderWindow = parentContainer->getParentWindow()->getRenderWindow();
  79. SPtr<Camera> parentCamera = parentContainer->getParentWidget().getCamera();
  80. position->x += editorWidget->getX();
  81. position->y += editorWidget->getY();
  82. dropDownWindow = DropDownWindowManager::instance().open<ManagedDropDownWindow>(
  83. parentRenderWindow, parentCamera, *position, instance, width, height);
  84. }
  85. }
  86. ScriptDropDownWindow* nativeInstance = new (bs_alloc<ScriptDropDownWindow>()) ScriptDropDownWindow(dropDownWindow);
  87. if (dropDownWindow != nullptr)
  88. dropDownWindow->initialize(nativeInstance);
  89. windowClass->construct(instance);
  90. return instance;
  91. }
  92. void ScriptDropDownWindow::internal_Close(ScriptDropDownWindow* thisPtr)
  93. {
  94. if (thisPtr->mDropDownWindow != nullptr)
  95. DropDownWindowManager::instance().close();
  96. }
  97. void ScriptDropDownWindow::onAssemblyRefreshStarted()
  98. {
  99. if (mDropDownWindow != nullptr)
  100. DropDownWindowManager::instance().close();
  101. }
  102. void ScriptDropDownWindow::notifyWindowClosed()
  103. {
  104. mDropDownWindow = nullptr;
  105. }
  106. UINT32 ScriptDropDownWindow::internal_GetWidth(ScriptDropDownWindow* thisPtr)
  107. {
  108. if (thisPtr->mDropDownWindow != nullptr)
  109. return thisPtr->mDropDownWindow->getWidth();
  110. return 0;
  111. }
  112. void ScriptDropDownWindow::internal_SetWidth(ScriptDropDownWindow* thisPtr, UINT32 value)
  113. {
  114. if (thisPtr->mDropDownWindow != nullptr)
  115. thisPtr->mDropDownWindow->setSize(value, thisPtr->mDropDownWindow->getHeight());
  116. }
  117. UINT32 ScriptDropDownWindow::internal_GetHeight(ScriptDropDownWindow* thisPtr)
  118. {
  119. if (thisPtr->mDropDownWindow != nullptr)
  120. return thisPtr->mDropDownWindow->getHeight();
  121. return 0;
  122. }
  123. void ScriptDropDownWindow::internal_SetHeight(ScriptDropDownWindow* thisPtr, UINT32 value)
  124. {
  125. if (thisPtr->mDropDownWindow != nullptr)
  126. thisPtr->mDropDownWindow->setSize(thisPtr->mDropDownWindow->getWidth(), value);
  127. }
  128. void ScriptDropDownWindow::internal_ScreenToWindowPos(ScriptDropDownWindow* thisPtr, Vector2I* screenPos, Vector2I* windowPos)
  129. {
  130. if (thisPtr->mDropDownWindow != nullptr)
  131. *windowPos = thisPtr->mDropDownWindow->screenToWindowPos(*screenPos);
  132. else
  133. *windowPos = *screenPos;
  134. }
  135. void ScriptDropDownWindow::internal_WindowToScreenPos(ScriptDropDownWindow* thisPtr, Vector2I* windowPos, Vector2I* screenPos)
  136. {
  137. if (thisPtr->mDropDownWindow != nullptr)
  138. *screenPos = thisPtr->mDropDownWindow->windowToScreenPos(*windowPos);
  139. else
  140. *screenPos = *windowPos;
  141. }
  142. ManagedDropDownWindow::ManagedDropDownWindow(const SPtr<RenderWindow>& parent, const SPtr<Camera>& camera,
  143. const Vector2I& position, MonoObject* managedInstance, UINT32 width, UINT32 height)
  144. : DropDownWindow(parent, camera, position, width, height), mOnInitializeThunk(nullptr), mOnDestroyThunk(nullptr)
  145. , mUpdateThunk(nullptr), mIsInitialized(false), mManagedInstance(managedInstance), mGCHandle(0)
  146. , mScriptParent(nullptr), mContentsPanel(nullptr)
  147. {
  148. mGCHandle = MonoUtil::newGCHandle(mManagedInstance);
  149. mManagedInstance = MonoUtil::getObjectFromGCHandle(mGCHandle);
  150. MonoObject* guiPanel = ScriptGUIPanel::createFromExisting(mContents);
  151. mContentsPanel = ScriptGUILayout::toNative(guiPanel);
  152. ScriptDropDownWindow::guiPanelField->set(mManagedInstance, guiPanel);
  153. ::MonoClass* rawMonoClass = MonoUtil::getClass(mManagedInstance);
  154. MonoClass* monoClass = MonoManager::instance().findClass(rawMonoClass);
  155. mNamespace = monoClass->getNamespace();
  156. mTypename = monoClass->getTypeName();
  157. reloadMonoTypes(monoClass);
  158. }
  159. ManagedDropDownWindow::~ManagedDropDownWindow()
  160. {
  161. mContentsPanel->destroy();
  162. mContentsPanel = nullptr;
  163. triggerOnDestroy();
  164. mScriptParent->notifyWindowClosed();
  165. MonoUtil::freeGCHandle(mGCHandle);
  166. mGCHandle = 0;
  167. }
  168. void ManagedDropDownWindow::initialize(ScriptDropDownWindow* parent)
  169. {
  170. mScriptParent = parent;
  171. }
  172. void ManagedDropDownWindow::triggerOnInitialize()
  173. {
  174. if (mOnInitializeThunk != nullptr && mManagedInstance != nullptr)
  175. {
  176. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  177. // for some extra speed.
  178. MonoUtil::invokeThunk(mOnInitializeThunk, mManagedInstance);
  179. }
  180. }
  181. void ManagedDropDownWindow::triggerOnDestroy()
  182. {
  183. if (mOnDestroyThunk != nullptr && mManagedInstance != nullptr)
  184. {
  185. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  186. // for some extra speed.
  187. MonoUtil::invokeThunk(mOnDestroyThunk, mManagedInstance);
  188. }
  189. }
  190. void ManagedDropDownWindow::update()
  191. {
  192. if (!mIsInitialized)
  193. {
  194. triggerOnInitialize();
  195. mIsInitialized = true;
  196. }
  197. if (mUpdateThunk != nullptr && mManagedInstance != nullptr)
  198. {
  199. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  200. // for some extra speed.
  201. MonoUtil::invokeThunk(mUpdateThunk, mManagedInstance);
  202. }
  203. }
  204. void ManagedDropDownWindow::reloadMonoTypes(MonoClass* windowClass)
  205. {
  206. MonoMethod* updateMethod = windowClass->getMethod("OnEditorUpdate", 0);
  207. if (updateMethod != nullptr)
  208. mUpdateThunk = (UpdateThunkDef)updateMethod->getThunk();
  209. MonoMethod* onInitializeMethod = windowClass->getMethod("OnInitialize", 0);
  210. if (onInitializeMethod != nullptr)
  211. mOnInitializeThunk = (OnInitializeThunkDef)onInitializeMethod->getThunk();
  212. MonoMethod* onDestroyMethod = windowClass->getMethod("OnDestroy", 0);
  213. if (onDestroyMethod != nullptr)
  214. mOnDestroyThunk = (OnDestroyThunkDef)onDestroyMethod->getThunk();
  215. }
  216. }