BsScriptGUIElement.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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_GetAcceptsKeyFocus", (void*)&ScriptGUIElement::internal_getAcceptsKeyFocus);
  91. metaData.scriptClass->addInternalCall("Internal_SetAcceptsKeyFocus", (void*)&ScriptGUIElement::internal_setAcceptsKeyFocus);
  92. metaData.scriptClass->addInternalCall("Internal_GetBounds", (void*)&ScriptGUIElement::internal_getBounds);
  93. metaData.scriptClass->addInternalCall("Internal_SetBounds", (void*)&ScriptGUIElement::internal_setBounds);
  94. metaData.scriptClass->addInternalCall("Internal_GetVisibleBounds", (void*)&ScriptGUIElement::internal_getVisibleBounds);
  95. metaData.scriptClass->addInternalCall("Internal_SetPosition", (void*)&ScriptGUIElement::internal_SetPosition);
  96. metaData.scriptClass->addInternalCall("Internal_SetWidth", (void*)&ScriptGUIElement::internal_SetWidth);
  97. metaData.scriptClass->addInternalCall("Internal_SetFlexibleWidth", (void*)&ScriptGUIElement::internal_SetFlexibleWidth);
  98. metaData.scriptClass->addInternalCall("Internal_SetHeight", (void*)&ScriptGUIElement::internal_SetHeight);
  99. metaData.scriptClass->addInternalCall("Internal_SetFlexibleHeight", (void*)&ScriptGUIElement::internal_SetFlexibleHeight);
  100. metaData.scriptClass->addInternalCall("Internal_ResetDimensions", (void*)&ScriptGUIElement::internal_ResetDimensions);
  101. metaData.scriptClass->addInternalCall("Internal_SetContextMenu", (void*)&ScriptGUIElement::internal_SetContextMenu);
  102. metaData.scriptClass->addInternalCall("Internal_GetStyle", (void*)&ScriptGUIElement::internal_GetStyle);
  103. metaData.scriptClass->addInternalCall("Internal_SetStyle", (void*)&ScriptGUIElement::internal_SetStyle);
  104. metaData.scriptClass->addInternalCall("Internal_GetParent", (void*)&ScriptGUIElement::internal_getParent);
  105. onFocusGainedThunk = (OnFocusChangedThunkDef)metaData.scriptClass->getMethod("Internal_OnFocusGained", 0)->getThunk();
  106. onFocusLostThunk = (OnFocusChangedThunkDef)metaData.scriptClass->getMethod("Internal_OnFocusLost", 0)->getThunk();
  107. }
  108. void ScriptGUIElement::internal_destroy(ScriptGUIElementBaseTBase* nativeInstance)
  109. {
  110. nativeInstance->destroy();
  111. }
  112. void ScriptGUIElement::internal_setVisible(ScriptGUIElementBaseTBase* nativeInstance, bool visible)
  113. {
  114. if (nativeInstance->isDestroyed())
  115. return;
  116. nativeInstance->getGUIElement()->setVisible(visible);
  117. }
  118. void ScriptGUIElement::internal_setActive(ScriptGUIElementBaseTBase* nativeInstance, bool enabled)
  119. {
  120. if (nativeInstance->isDestroyed())
  121. return;
  122. nativeInstance->getGUIElement()->setActive(enabled);
  123. }
  124. void ScriptGUIElement::internal_setDisabled(ScriptGUIElementBaseTBase* nativeInstance, bool disabled)
  125. {
  126. if (nativeInstance->isDestroyed())
  127. return;
  128. nativeInstance->getGUIElement()->setDisabled(disabled);
  129. }
  130. void ScriptGUIElement::internal_setFocus(ScriptGUIElementBaseTBase* nativeInstance, bool focus)
  131. {
  132. if (nativeInstance->isDestroyed())
  133. return;
  134. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  135. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  136. {
  137. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  138. guiElem->setFocus(focus);
  139. }
  140. }
  141. bool ScriptGUIElement::internal_getVisible(ScriptGUIElementBaseTBase* nativeInstance)
  142. {
  143. if (nativeInstance->isDestroyed())
  144. return false;
  145. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  146. return guiElemBase->_isVisible();
  147. }
  148. bool ScriptGUIElement::internal_getActive(ScriptGUIElementBaseTBase* nativeInstance)
  149. {
  150. if (nativeInstance->isDestroyed())
  151. return false;
  152. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  153. return guiElemBase->_isActive();
  154. }
  155. bool ScriptGUIElement::internal_getDisabled(ScriptGUIElementBaseTBase* nativeInstance)
  156. {
  157. if (nativeInstance->isDestroyed())
  158. return false;
  159. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  160. return guiElemBase->_isDisabled();
  161. }
  162. bool ScriptGUIElement::internal_getBlocking(ScriptGUIElementBaseTBase* nativeInstance)
  163. {
  164. if (nativeInstance->isDestroyed())
  165. return false;
  166. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  167. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  168. {
  169. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  170. return !guiElem->getOptionFlags().isSet(GUIElementOption::ClickThrough);
  171. }
  172. return false;
  173. }
  174. void ScriptGUIElement::internal_setBlocking(ScriptGUIElementBaseTBase* nativeInstance, bool blocking)
  175. {
  176. if (nativeInstance->isDestroyed())
  177. return;
  178. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  179. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  180. {
  181. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  182. GUIElementOptions options = guiElem->getOptionFlags();
  183. if(blocking)
  184. options.unset(GUIElementOption::ClickThrough);
  185. else
  186. options.set(GUIElementOption::ClickThrough);
  187. guiElem->setOptionFlags(options);
  188. }
  189. }
  190. bool ScriptGUIElement::internal_getAcceptsKeyFocus(ScriptGUIElementBaseTBase* nativeInstance)
  191. {
  192. if (nativeInstance->isDestroyed())
  193. return false;
  194. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  195. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  196. {
  197. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  198. return guiElem->getOptionFlags().isSet(GUIElementOption::AcceptsKeyFocus);
  199. }
  200. return false;
  201. }
  202. void ScriptGUIElement::internal_setAcceptsKeyFocus(ScriptGUIElementBaseTBase* nativeInstance, bool accepts)
  203. {
  204. if (nativeInstance->isDestroyed())
  205. return;
  206. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  207. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  208. {
  209. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  210. GUIElementOptions options = guiElem->getOptionFlags();
  211. if(accepts)
  212. options.set(GUIElementOption::AcceptsKeyFocus);
  213. else
  214. options.unset(GUIElementOption::AcceptsKeyFocus);
  215. guiElem->setOptionFlags(options);
  216. }
  217. }
  218. MonoObject* ScriptGUIElement::internal_getParent(ScriptGUIElementBaseTBase* nativeInstance)
  219. {
  220. if (nativeInstance->isDestroyed())
  221. return nullptr;
  222. if (nativeInstance->getParent() != nullptr)
  223. return nativeInstance->getParent()->getManagedInstance();
  224. return nullptr;
  225. }
  226. void ScriptGUIElement::internal_getBounds(ScriptGUIElementBaseTBase* nativeInstance, Rect2I* bounds)
  227. {
  228. if (nativeInstance->isDestroyed())
  229. {
  230. *bounds = Rect2I();
  231. return;
  232. }
  233. *bounds = nativeInstance->getGUIElement()->getBounds();
  234. }
  235. void ScriptGUIElement::internal_setBounds(ScriptGUIElementBaseTBase* nativeInstance, Rect2I* bounds)
  236. {
  237. if (nativeInstance->isDestroyed())
  238. return;
  239. nativeInstance->getGUIElement()->setPosition(bounds->x, bounds->y);
  240. nativeInstance->getGUIElement()->setWidth(bounds->width);
  241. nativeInstance->getGUIElement()->setHeight(bounds->height);
  242. }
  243. void ScriptGUIElement::internal_getVisibleBounds(ScriptGUIElementBaseTBase* nativeInstance, Rect2I* bounds)
  244. {
  245. if (nativeInstance->isDestroyed())
  246. {
  247. *bounds = Rect2I();
  248. return;
  249. }
  250. *bounds = nativeInstance->getGUIElement()->getVisibleBounds();
  251. }
  252. void ScriptGUIElement::internal_SetPosition(ScriptGUIElementBaseTBase* nativeInstance, INT32 x, INT32 y)
  253. {
  254. if (nativeInstance->isDestroyed())
  255. return;
  256. nativeInstance->getGUIElement()->setPosition(x, y);
  257. }
  258. void ScriptGUIElement::internal_SetWidth(ScriptGUIElementBaseTBase* nativeInstance, UINT32 width)
  259. {
  260. if (nativeInstance->isDestroyed())
  261. return;
  262. nativeInstance->getGUIElement()->setWidth(width);
  263. }
  264. void ScriptGUIElement::internal_SetFlexibleWidth(ScriptGUIElementBaseTBase* nativeInstance, UINT32 minWidth, UINT32 maxWidth)
  265. {
  266. if (nativeInstance->isDestroyed())
  267. return;
  268. nativeInstance->getGUIElement()->setFlexibleWidth(minWidth, maxWidth);
  269. }
  270. void ScriptGUIElement::internal_SetHeight(ScriptGUIElementBaseTBase* nativeInstance, UINT32 height)
  271. {
  272. if (nativeInstance->isDestroyed())
  273. return;
  274. nativeInstance->getGUIElement()->setHeight(height);
  275. }
  276. void ScriptGUIElement::internal_SetFlexibleHeight(ScriptGUIElementBaseTBase* nativeInstance, UINT32 minHeight, UINT32 maxHeight)
  277. {
  278. if (nativeInstance->isDestroyed())
  279. return;
  280. nativeInstance->getGUIElement()->setFlexibleHeight(minHeight, maxHeight);
  281. }
  282. void ScriptGUIElement::internal_ResetDimensions(ScriptGUIElementBaseTBase* nativeInstance)
  283. {
  284. if (nativeInstance->isDestroyed())
  285. return;
  286. nativeInstance->getGUIElement()->resetDimensions();
  287. }
  288. void ScriptGUIElement::internal_SetContextMenu(ScriptGUIElementBaseTBase* nativeInstance, ScriptContextMenu* contextMenu)
  289. {
  290. if (nativeInstance->isDestroyed())
  291. return;
  292. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  293. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  294. {
  295. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  296. SPtr<GUIContextMenu> nativeContextMenu;
  297. if (contextMenu != nullptr)
  298. nativeContextMenu = contextMenu->getInternal();
  299. guiElem->setContextMenu(nativeContextMenu);
  300. }
  301. }
  302. MonoString* ScriptGUIElement::internal_GetStyle(ScriptGUIElementBaseTBase* nativeInstance)
  303. {
  304. if (!nativeInstance->isDestroyed())
  305. {
  306. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  307. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  308. {
  309. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  310. return MonoUtil::stringToMono(guiElem->getStyleName());
  311. }
  312. }
  313. return MonoUtil::stringToMono(StringUtil::BLANK);
  314. }
  315. void ScriptGUIElement::internal_SetStyle(ScriptGUIElementBaseTBase* nativeInstance, MonoString* style)
  316. {
  317. if (!nativeInstance->isDestroyed())
  318. {
  319. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  320. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  321. {
  322. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  323. guiElem->setStyle(MonoUtil::monoToString(style));
  324. }
  325. }
  326. }
  327. }