BsScriptDropDownWindow.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. #include "RenderAPI/BsViewport.h"
  20. #include "RenderAPI/BsRenderWindow.h"
  21. #include "Reflection/BsRTTIType.h"
  22. using namespace std::placeholders;
  23. namespace bs
  24. {
  25. MonoField* ScriptDropDownWindow::guiPanelField = nullptr;
  26. ScriptDropDownWindow::ScriptDropDownWindow(ManagedDropDownWindow* window)
  27. :ScriptObject(window->getManagedInstance()), mDropDownWindow(window)
  28. {
  29. mOnAssemblyRefreshStartedConn = ScriptObjectManager::instance().onRefreshStarted.connect(
  30. std::bind(&ScriptDropDownWindow::onAssemblyRefreshStarted, this));
  31. }
  32. ScriptDropDownWindow::~ScriptDropDownWindow()
  33. {
  34. mOnAssemblyRefreshStartedConn.disconnect();
  35. // Window must have been marked as deleted already
  36. assert(mDropDownWindow == nullptr);
  37. }
  38. void ScriptDropDownWindow::initRuntimeData()
  39. {
  40. metaData.scriptClass->addInternalCall("Internal_CreateInstance", (void*)&ScriptDropDownWindow::internal_CreateInstance);
  41. metaData.scriptClass->addInternalCall("Internal_Close", (void*)&ScriptDropDownWindow::internal_Close);
  42. metaData.scriptClass->addInternalCall("Internal_GetWidth", (void*)&ScriptDropDownWindow::internal_GetWidth);
  43. metaData.scriptClass->addInternalCall("Internal_SetWidth", (void*)&ScriptDropDownWindow::internal_SetWidth);
  44. metaData.scriptClass->addInternalCall("Internal_GetHeight", (void*)&ScriptDropDownWindow::internal_GetHeight);
  45. metaData.scriptClass->addInternalCall("Internal_SetHeight", (void*)&ScriptDropDownWindow::internal_SetHeight);
  46. metaData.scriptClass->addInternalCall("Internal_ScreenToWindowPos", (void*)&ScriptDropDownWindow::internal_ScreenToWindowPos);
  47. metaData.scriptClass->addInternalCall("Internal_WindowToScreenPos", (void*)&ScriptDropDownWindow::internal_WindowToScreenPos);
  48. guiPanelField = metaData.scriptClass->getField("GUI");
  49. }
  50. MonoObject* ScriptDropDownWindow::internal_CreateInstance(MonoString* ns, MonoString* typeName,
  51. ScriptGUIElementBaseTBase* parentElement, Vector2I* position)
  52. {
  53. const String strTypeName = MonoUtil::monoToString(typeName);
  54. const String strNamespace = MonoUtil::monoToString(ns);
  55. String fullName = strNamespace + "." + strTypeName;
  56. MonoClass* windowClass = MonoManager::instance().findClass(strNamespace, strTypeName);
  57. if (windowClass == nullptr)
  58. return nullptr;
  59. MonoAssembly* assembly = MonoManager::instance().getAssembly(EDITOR_ASSEMBLY);
  60. MonoClass* defaultSizeAttrib = assembly->getClass(EDITOR_NS, "DefaultSize");
  61. if (defaultSizeAttrib == nullptr)
  62. BS_EXCEPT(InternalErrorException, "Cannot find DefaultSize managed class.");
  63. MonoField* defaultWidthField = defaultSizeAttrib->getField("width");
  64. MonoField* defaultHeightField = defaultSizeAttrib->getField("height");
  65. int width = 200;
  66. int height = 200;
  67. MonoObject* defaultSizeObj = windowClass->getAttribute(defaultSizeAttrib);
  68. if (defaultSizeObj != nullptr)
  69. {
  70. defaultWidthField->get(defaultSizeObj, &width);
  71. defaultHeightField->get(defaultSizeObj, &height);
  72. }
  73. MonoObject* instance = windowClass->createInstance(false);
  74. GUIElementBase* rootElement = nullptr;
  75. if (!parentElement->isDestroyed())
  76. rootElement = parentElement->getGUIElement();
  77. SPtr<RenderWindow> window;
  78. SPtr<Camera> camera;
  79. if(rootElement)
  80. {
  81. GUIWidget* widget = rootElement->_getParentWidget();
  82. if (widget)
  83. {
  84. Viewport* viewport = widget->getTarget();
  85. if (viewport)
  86. {
  87. SPtr<RenderTarget> target = viewport->getTarget();
  88. if (target && target->getProperties().isWindow)
  89. window = std::static_pointer_cast<RenderWindow>(target);
  90. }
  91. camera = widget->getCamera();
  92. }
  93. }
  94. ManagedDropDownWindow* dropDownWindow = nullptr;
  95. if(window && camera)
  96. {
  97. const Rect2I screenBounds = rootElement->getScreenBounds();
  98. const Vector2I windowPos = window->screenToWindowPos(Vector2I(screenBounds.x, screenBounds.y));
  99. (*position) += windowPos;
  100. dropDownWindow = DropDownWindowManager::instance().open<ManagedDropDownWindow>(
  101. window, camera, *position, instance, width, height);
  102. }
  103. ScriptDropDownWindow* nativeInstance = new (bs_alloc<ScriptDropDownWindow>()) ScriptDropDownWindow(dropDownWindow);
  104. if (dropDownWindow != nullptr)
  105. dropDownWindow->initialize(nativeInstance);
  106. windowClass->construct(instance);
  107. return instance;
  108. }
  109. void ScriptDropDownWindow::internal_Close(ScriptDropDownWindow* thisPtr)
  110. {
  111. if (thisPtr->mDropDownWindow != nullptr)
  112. DropDownWindowManager::instance().close();
  113. }
  114. void ScriptDropDownWindow::onAssemblyRefreshStarted()
  115. {
  116. if (mDropDownWindow != nullptr)
  117. DropDownWindowManager::instance().close();
  118. }
  119. void ScriptDropDownWindow::notifyWindowClosed()
  120. {
  121. mDropDownWindow = nullptr;
  122. }
  123. UINT32 ScriptDropDownWindow::internal_GetWidth(ScriptDropDownWindow* thisPtr)
  124. {
  125. if (thisPtr->mDropDownWindow != nullptr)
  126. return thisPtr->mDropDownWindow->getWidth();
  127. return 0;
  128. }
  129. void ScriptDropDownWindow::internal_SetWidth(ScriptDropDownWindow* thisPtr, UINT32 value)
  130. {
  131. if (thisPtr->mDropDownWindow != nullptr)
  132. thisPtr->mDropDownWindow->setSize(value, thisPtr->mDropDownWindow->getHeight());
  133. }
  134. UINT32 ScriptDropDownWindow::internal_GetHeight(ScriptDropDownWindow* thisPtr)
  135. {
  136. if (thisPtr->mDropDownWindow != nullptr)
  137. return thisPtr->mDropDownWindow->getHeight();
  138. return 0;
  139. }
  140. void ScriptDropDownWindow::internal_SetHeight(ScriptDropDownWindow* thisPtr, UINT32 value)
  141. {
  142. if (thisPtr->mDropDownWindow != nullptr)
  143. thisPtr->mDropDownWindow->setSize(thisPtr->mDropDownWindow->getWidth(), value);
  144. }
  145. void ScriptDropDownWindow::internal_ScreenToWindowPos(ScriptDropDownWindow* thisPtr, Vector2I* screenPos, Vector2I* windowPos)
  146. {
  147. if (thisPtr->mDropDownWindow != nullptr)
  148. *windowPos = thisPtr->mDropDownWindow->screenToWindowPos(*screenPos);
  149. else
  150. *windowPos = *screenPos;
  151. }
  152. void ScriptDropDownWindow::internal_WindowToScreenPos(ScriptDropDownWindow* thisPtr, Vector2I* windowPos, Vector2I* screenPos)
  153. {
  154. if (thisPtr->mDropDownWindow != nullptr)
  155. *screenPos = thisPtr->mDropDownWindow->windowToScreenPos(*windowPos);
  156. else
  157. *screenPos = *windowPos;
  158. }
  159. ManagedDropDownWindow::ManagedDropDownWindow(const SPtr<RenderWindow>& parent, const SPtr<Camera>& camera,
  160. const Vector2I& position, MonoObject* managedInstance, UINT32 width, UINT32 height)
  161. : DropDownWindow(parent, camera, position, width, height), mOnInitializeThunk(nullptr), mOnDestroyThunk(nullptr)
  162. , mUpdateThunk(nullptr), mIsInitialized(false), mManagedInstance(managedInstance), mGCHandle(0)
  163. , mScriptParent(nullptr), mContentsPanel(nullptr)
  164. {
  165. mGCHandle = MonoUtil::newGCHandle(mManagedInstance);
  166. mManagedInstance = MonoUtil::getObjectFromGCHandle(mGCHandle);
  167. MonoObject* guiPanel = ScriptGUIPanel::createFromExisting(mContents);
  168. mContentsPanel = ScriptGUILayout::toNative(guiPanel);
  169. ScriptDropDownWindow::guiPanelField->set(mManagedInstance, guiPanel);
  170. ::MonoClass* rawMonoClass = MonoUtil::getClass(mManagedInstance);
  171. MonoClass* monoClass = MonoManager::instance().findClass(rawMonoClass);
  172. mNamespace = monoClass->getNamespace();
  173. mTypename = monoClass->getTypeName();
  174. reloadMonoTypes(monoClass);
  175. }
  176. ManagedDropDownWindow::~ManagedDropDownWindow()
  177. {
  178. mContentsPanel->destroy();
  179. mContentsPanel = nullptr;
  180. triggerOnDestroy();
  181. mScriptParent->notifyWindowClosed();
  182. MonoUtil::freeGCHandle(mGCHandle);
  183. mGCHandle = 0;
  184. }
  185. void ManagedDropDownWindow::initialize(ScriptDropDownWindow* parent)
  186. {
  187. mScriptParent = parent;
  188. }
  189. void ManagedDropDownWindow::triggerOnInitialize()
  190. {
  191. if (mOnInitializeThunk != nullptr && mManagedInstance != nullptr)
  192. {
  193. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  194. // for some extra speed.
  195. MonoUtil::invokeThunk(mOnInitializeThunk, mManagedInstance);
  196. }
  197. }
  198. void ManagedDropDownWindow::triggerOnDestroy()
  199. {
  200. if (mOnDestroyThunk != nullptr && mManagedInstance != nullptr)
  201. {
  202. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  203. // for some extra speed.
  204. MonoUtil::invokeThunk(mOnDestroyThunk, mManagedInstance);
  205. }
  206. }
  207. void ManagedDropDownWindow::update()
  208. {
  209. if (!mIsInitialized)
  210. {
  211. triggerOnInitialize();
  212. mIsInitialized = true;
  213. }
  214. if (mUpdateThunk != nullptr && mManagedInstance != nullptr)
  215. {
  216. // Note: Not calling virtual methods. Can be easily done if needed but for now doing this
  217. // for some extra speed.
  218. MonoUtil::invokeThunk(mUpdateThunk, mManagedInstance);
  219. }
  220. }
  221. void ManagedDropDownWindow::reloadMonoTypes(MonoClass* windowClass)
  222. {
  223. MonoMethod* updateMethod = windowClass->getMethod("OnEditorUpdate", 0);
  224. if (updateMethod != nullptr)
  225. mUpdateThunk = (UpdateThunkDef)updateMethod->getThunk();
  226. MonoMethod* onInitializeMethod = windowClass->getMethod("OnInitialize", 0);
  227. if (onInitializeMethod != nullptr)
  228. mOnInitializeThunk = (OnInitializeThunkDef)onInitializeMethod->getThunk();
  229. MonoMethod* onDestroyMethod = windowClass->getMethod("OnDestroy", 0);
  230. if (onDestroyMethod != nullptr)
  231. mOnDestroyThunk = (OnDestroyThunkDef)onDestroyMethod->getThunk();
  232. }
  233. }