BsScriptGUIElement.cpp 14 KB

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