BsScriptGUIElement.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Wrappers/GUI/BsScriptGUIElement.h"
  4. #include "BsScriptMeta.h"
  5. #include "BsMonoField.h"
  6. #include "BsMonoClass.h"
  7. #include "BsMonoManager.h"
  8. #include "BsMonoMethod.h"
  9. #include "BsMonoUtil.h"
  10. #include "GUI/BsGUIElement.h"
  11. #include "Wrappers/GUI/BsScriptGUILayout.h"
  12. #include "Wrappers/BsScriptContextMenu.h"
  13. #include "GUI/BsGUIElement.h"
  14. using namespace std::placeholders;
  15. namespace bs
  16. {
  17. ScriptGUIElementBaseTBase::ScriptGUIElementBaseTBase(MonoObject* instance)
  18. :ScriptObjectBase(instance), mIsDestroyed(false), mElement(nullptr), mParent(nullptr)
  19. {
  20. mGCHandle = MonoUtil::newWeakGCHandle(instance);
  21. }
  22. void ScriptGUIElementBaseTBase::initialize(GUIElementBase* element)
  23. {
  24. mElement = element;
  25. if (mElement != nullptr && mElement->_getType() == GUIElementBase::Type::Element)
  26. {
  27. GUIElement* guiElem = static_cast<GUIElement*>(element);
  28. guiElem->onFocusChanged.connect(std::bind(&ScriptGUIElementBaseTBase::onFocusChanged, this, _1));
  29. }
  30. }
  31. void ScriptGUIElementBaseTBase::onFocusChanged(ScriptGUIElementBaseTBase* thisPtr, bool focus)
  32. {
  33. MonoObject* instance = MonoUtil::getObjectFromGCHandle(thisPtr->mGCHandle);
  34. if (focus)
  35. MonoUtil::invokeThunk(ScriptGUIElement::onFocusGainedThunk, instance);
  36. else
  37. MonoUtil::invokeThunk(ScriptGUIElement::onFocusLostThunk, instance);
  38. }
  39. MonoObject* ScriptGUIElementBaseTBase::getManagedInstance() const
  40. {
  41. return MonoUtil::getObjectFromGCHandle(mGCHandle);
  42. }
  43. void ScriptGUIElementBaseTBase::_onManagedInstanceDeleted(bool assemblyRefresh)
  44. {
  45. destroy();
  46. ScriptObjectBase::_onManagedInstanceDeleted(assemblyRefresh);
  47. }
  48. void ScriptGUIElementBaseTBase::_clearManagedInstance()
  49. {
  50. // Need to call destroy here because we need to release any GC handles before the domain is unloaded
  51. destroy();
  52. }
  53. ScriptGUIElementTBase::ScriptGUIElementTBase(MonoObject* instance)
  54. :ScriptGUIElementBaseTBase(instance)
  55. {
  56. }
  57. void ScriptGUIElementTBase::destroy()
  58. {
  59. if(!mIsDestroyed)
  60. {
  61. if (mParent != nullptr)
  62. mParent->removeChild(this);
  63. if (mElement->_getType() == GUIElementBase::Type::Element)
  64. {
  65. GUIElement::destroy((GUIElement*)mElement);
  66. mElement = nullptr;
  67. mIsDestroyed = true;
  68. }
  69. }
  70. }
  71. ScriptGUIElement::OnFocusChangedThunkDef ScriptGUIElement::onFocusGainedThunk;
  72. ScriptGUIElement::OnFocusChangedThunkDef ScriptGUIElement::onFocusLostThunk;
  73. ScriptGUIElement::ScriptGUIElement(MonoObject* instance)
  74. :ScriptObject(instance)
  75. {
  76. }
  77. void ScriptGUIElement::initRuntimeData()
  78. {
  79. metaData.scriptClass->addInternalCall("Internal_Destroy", (void*)&ScriptGUIElement::internal_destroy);
  80. metaData.scriptClass->addInternalCall("Internal_SetVisible", (void*)&ScriptGUIElement::internal_setVisible);
  81. metaData.scriptClass->addInternalCall("Internal_SetActive", (void*)&ScriptGUIElement::internal_setActive);
  82. metaData.scriptClass->addInternalCall("Internal_SetDisabled", (void*)&ScriptGUIElement::internal_setDisabled);
  83. metaData.scriptClass->addInternalCall("Internal_GetVisible", (void*)&ScriptGUIElement::internal_getVisible);
  84. metaData.scriptClass->addInternalCall("Internal_GetActive", (void*)&ScriptGUIElement::internal_getActive);
  85. metaData.scriptClass->addInternalCall("Internal_GetDisabled", (void*)&ScriptGUIElement::internal_getDisabled);
  86. metaData.scriptClass->addInternalCall("Internal_SetFocus", (void*)&ScriptGUIElement::internal_setFocus);
  87. metaData.scriptClass->addInternalCall("Internal_SetFocus", (void*)&ScriptGUIElement::internal_setFocus);
  88. metaData.scriptClass->addInternalCall("Internal_GetBlocking", (void*)&ScriptGUIElement::internal_getBlocking);
  89. metaData.scriptClass->addInternalCall("Internal_SetBlocking", (void*)&ScriptGUIElement::internal_setBlocking);
  90. metaData.scriptClass->addInternalCall("Internal_GetBounds", (void*)&ScriptGUIElement::internal_getBounds);
  91. metaData.scriptClass->addInternalCall("Internal_SetBounds", (void*)&ScriptGUIElement::internal_setBounds);
  92. metaData.scriptClass->addInternalCall("Internal_GetVisibleBounds", (void*)&ScriptGUIElement::internal_getVisibleBounds);
  93. metaData.scriptClass->addInternalCall("Internal_SetPosition", (void*)&ScriptGUIElement::internal_SetPosition);
  94. metaData.scriptClass->addInternalCall("Internal_SetWidth", (void*)&ScriptGUIElement::internal_SetWidth);
  95. metaData.scriptClass->addInternalCall("Internal_SetFlexibleWidth", (void*)&ScriptGUIElement::internal_SetFlexibleWidth);
  96. metaData.scriptClass->addInternalCall("Internal_SetHeight", (void*)&ScriptGUIElement::internal_SetHeight);
  97. metaData.scriptClass->addInternalCall("Internal_SetFlexibleHeight", (void*)&ScriptGUIElement::internal_SetFlexibleHeight);
  98. metaData.scriptClass->addInternalCall("Internal_ResetDimensions", (void*)&ScriptGUIElement::internal_ResetDimensions);
  99. metaData.scriptClass->addInternalCall("Internal_SetContextMenu", (void*)&ScriptGUIElement::internal_SetContextMenu);
  100. metaData.scriptClass->addInternalCall("Internal_GetStyle", (void*)&ScriptGUIElement::internal_GetStyle);
  101. metaData.scriptClass->addInternalCall("Internal_SetStyle", (void*)&ScriptGUIElement::internal_SetStyle);
  102. metaData.scriptClass->addInternalCall("Internal_GetParent", (void*)&ScriptGUIElement::internal_getParent);
  103. onFocusGainedThunk = (OnFocusChangedThunkDef)metaData.scriptClass->getMethod("Internal_OnFocusGained", 0)->getThunk();
  104. onFocusLostThunk = (OnFocusChangedThunkDef)metaData.scriptClass->getMethod("Internal_OnFocusLost", 0)->getThunk();
  105. }
  106. void ScriptGUIElement::internal_destroy(ScriptGUIElementBaseTBase* nativeInstance)
  107. {
  108. nativeInstance->destroy();
  109. }
  110. void ScriptGUIElement::internal_setVisible(ScriptGUIElementBaseTBase* nativeInstance, bool visible)
  111. {
  112. if (nativeInstance->isDestroyed())
  113. return;
  114. nativeInstance->getGUIElement()->setVisible(visible);
  115. }
  116. void ScriptGUIElement::internal_setActive(ScriptGUIElementBaseTBase* nativeInstance, bool enabled)
  117. {
  118. if (nativeInstance->isDestroyed())
  119. return;
  120. nativeInstance->getGUIElement()->setActive(enabled);
  121. }
  122. void ScriptGUIElement::internal_setDisabled(ScriptGUIElementBaseTBase* nativeInstance, bool disabled)
  123. {
  124. if (nativeInstance->isDestroyed())
  125. return;
  126. nativeInstance->getGUIElement()->setDisabled(disabled);
  127. }
  128. void ScriptGUIElement::internal_setFocus(ScriptGUIElementBaseTBase* nativeInstance, bool focus)
  129. {
  130. if (nativeInstance->isDestroyed())
  131. return;
  132. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  133. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  134. {
  135. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  136. guiElem->setFocus(focus);
  137. }
  138. }
  139. bool ScriptGUIElement::internal_getVisible(ScriptGUIElementBaseTBase* nativeInstance)
  140. {
  141. if (nativeInstance->isDestroyed())
  142. return false;
  143. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  144. return guiElemBase->_isVisible();
  145. }
  146. bool ScriptGUIElement::internal_getActive(ScriptGUIElementBaseTBase* nativeInstance)
  147. {
  148. if (nativeInstance->isDestroyed())
  149. return false;
  150. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  151. return guiElemBase->_isActive();
  152. }
  153. bool ScriptGUIElement::internal_getDisabled(ScriptGUIElementBaseTBase* nativeInstance)
  154. {
  155. if (nativeInstance->isDestroyed())
  156. return false;
  157. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  158. return guiElemBase->_isDisabled();
  159. }
  160. bool ScriptGUIElement::internal_getBlocking(ScriptGUIElementBaseTBase* nativeInstance)
  161. {
  162. if (nativeInstance->isDestroyed())
  163. return false;
  164. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  165. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  166. {
  167. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  168. return guiElem->getBlockPointerEvents();
  169. }
  170. return false;
  171. }
  172. void ScriptGUIElement::internal_setBlocking(ScriptGUIElementBaseTBase* nativeInstance, bool blocking)
  173. {
  174. if (nativeInstance->isDestroyed())
  175. return;
  176. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  177. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  178. {
  179. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  180. guiElem->setBlockPointerEvents(blocking);
  181. }
  182. }
  183. MonoObject* ScriptGUIElement::internal_getParent(ScriptGUIElementBaseTBase* nativeInstance)
  184. {
  185. if (nativeInstance->isDestroyed())
  186. return nullptr;
  187. if (nativeInstance->getParent() != nullptr)
  188. return nativeInstance->getParent()->getManagedInstance();
  189. return nullptr;
  190. }
  191. void ScriptGUIElement::internal_getBounds(ScriptGUIElementBaseTBase* nativeInstance, Rect2I* bounds)
  192. {
  193. if (nativeInstance->isDestroyed())
  194. {
  195. *bounds = Rect2I();
  196. return;
  197. }
  198. *bounds = nativeInstance->getGUIElement()->getBounds();
  199. }
  200. void ScriptGUIElement::internal_setBounds(ScriptGUIElementBaseTBase* nativeInstance, Rect2I* bounds)
  201. {
  202. if (nativeInstance->isDestroyed())
  203. return;
  204. nativeInstance->getGUIElement()->setPosition(bounds->x, bounds->y);
  205. nativeInstance->getGUIElement()->setWidth(bounds->width);
  206. nativeInstance->getGUIElement()->setHeight(bounds->height);
  207. }
  208. void ScriptGUIElement::internal_getVisibleBounds(ScriptGUIElementBaseTBase* nativeInstance, Rect2I* bounds)
  209. {
  210. if (nativeInstance->isDestroyed())
  211. {
  212. *bounds = Rect2I();
  213. return;
  214. }
  215. *bounds = nativeInstance->getGUIElement()->getVisibleBounds();
  216. }
  217. void ScriptGUIElement::internal_SetPosition(ScriptGUIElementBaseTBase* nativeInstance, INT32 x, INT32 y)
  218. {
  219. if (nativeInstance->isDestroyed())
  220. return;
  221. nativeInstance->getGUIElement()->setPosition(x, y);
  222. }
  223. void ScriptGUIElement::internal_SetWidth(ScriptGUIElementBaseTBase* nativeInstance, UINT32 width)
  224. {
  225. if (nativeInstance->isDestroyed())
  226. return;
  227. nativeInstance->getGUIElement()->setWidth(width);
  228. }
  229. void ScriptGUIElement::internal_SetFlexibleWidth(ScriptGUIElementBaseTBase* nativeInstance, UINT32 minWidth, UINT32 maxWidth)
  230. {
  231. if (nativeInstance->isDestroyed())
  232. return;
  233. nativeInstance->getGUIElement()->setFlexibleWidth(minWidth, maxWidth);
  234. }
  235. void ScriptGUIElement::internal_SetHeight(ScriptGUIElementBaseTBase* nativeInstance, UINT32 height)
  236. {
  237. if (nativeInstance->isDestroyed())
  238. return;
  239. nativeInstance->getGUIElement()->setHeight(height);
  240. }
  241. void ScriptGUIElement::internal_SetFlexibleHeight(ScriptGUIElementBaseTBase* nativeInstance, UINT32 minHeight, UINT32 maxHeight)
  242. {
  243. if (nativeInstance->isDestroyed())
  244. return;
  245. nativeInstance->getGUIElement()->setFlexibleHeight(minHeight, maxHeight);
  246. }
  247. void ScriptGUIElement::internal_ResetDimensions(ScriptGUIElementBaseTBase* nativeInstance)
  248. {
  249. if (nativeInstance->isDestroyed())
  250. return;
  251. nativeInstance->getGUIElement()->resetDimensions();
  252. }
  253. void ScriptGUIElement::internal_SetContextMenu(ScriptGUIElementBaseTBase* nativeInstance, ScriptContextMenu* contextMenu)
  254. {
  255. if (nativeInstance->isDestroyed())
  256. return;
  257. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  258. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  259. {
  260. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  261. SPtr<GUIContextMenu> nativeContextMenu;
  262. if (contextMenu != nullptr)
  263. nativeContextMenu = contextMenu->getInternal();
  264. guiElem->setContextMenu(nativeContextMenu);
  265. }
  266. }
  267. MonoString* ScriptGUIElement::internal_GetStyle(ScriptGUIElementBaseTBase* nativeInstance)
  268. {
  269. if (!nativeInstance->isDestroyed())
  270. {
  271. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  272. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  273. {
  274. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  275. return MonoUtil::stringToMono(guiElem->getStyleName());
  276. }
  277. }
  278. return MonoUtil::stringToMono(StringUtil::BLANK);
  279. }
  280. void ScriptGUIElement::internal_SetStyle(ScriptGUIElementBaseTBase* nativeInstance, MonoString* style)
  281. {
  282. if (!nativeInstance->isDestroyed())
  283. {
  284. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  285. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  286. {
  287. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  288. guiElem->setStyle(MonoUtil::monoToString(style));
  289. }
  290. }
  291. }
  292. }