BsScriptGUIElement.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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()
  44. {
  45. destroy();
  46. ScriptObjectBase::_onManagedInstanceDeleted();
  47. }
  48. ScriptGUIElementTBase::ScriptGUIElementTBase(MonoObject* instance)
  49. :ScriptGUIElementBaseTBase(instance)
  50. {
  51. }
  52. void ScriptGUIElementTBase::destroy()
  53. {
  54. if(!mIsDestroyed)
  55. {
  56. if (mParent != nullptr)
  57. mParent->removeChild(this);
  58. if (mElement->_getType() == GUIElementBase::Type::Element)
  59. {
  60. GUIElement::destroy((GUIElement*)mElement);
  61. mElement = nullptr;
  62. mIsDestroyed = true;
  63. }
  64. }
  65. }
  66. ScriptGUIElement::OnFocusChangedThunkDef ScriptGUIElement::onFocusGainedThunk;
  67. ScriptGUIElement::OnFocusChangedThunkDef ScriptGUIElement::onFocusLostThunk;
  68. ScriptGUIElement::ScriptGUIElement(MonoObject* instance)
  69. :ScriptObject(instance)
  70. {
  71. }
  72. void ScriptGUIElement::initRuntimeData()
  73. {
  74. metaData.scriptClass->addInternalCall("Internal_Destroy", (void*)&ScriptGUIElement::internal_destroy);
  75. metaData.scriptClass->addInternalCall("Internal_SetVisible", (void*)&ScriptGUIElement::internal_setVisible);
  76. metaData.scriptClass->addInternalCall("Internal_SetActive", (void*)&ScriptGUIElement::internal_setActive);
  77. metaData.scriptClass->addInternalCall("Internal_SetDisabled", (void*)&ScriptGUIElement::internal_setDisabled);
  78. metaData.scriptClass->addInternalCall("Internal_GetVisible", (void*)&ScriptGUIElement::internal_getVisible);
  79. metaData.scriptClass->addInternalCall("Internal_GetActive", (void*)&ScriptGUIElement::internal_getActive);
  80. metaData.scriptClass->addInternalCall("Internal_GetDisabled", (void*)&ScriptGUIElement::internal_getDisabled);
  81. metaData.scriptClass->addInternalCall("Internal_SetFocus", (void*)&ScriptGUIElement::internal_setFocus);
  82. metaData.scriptClass->addInternalCall("Internal_SetFocus", (void*)&ScriptGUIElement::internal_setFocus);
  83. metaData.scriptClass->addInternalCall("Internal_GetBlocking", (void*)&ScriptGUIElement::internal_getBlocking);
  84. metaData.scriptClass->addInternalCall("Internal_SetBlocking", (void*)&ScriptGUIElement::internal_setBlocking);
  85. metaData.scriptClass->addInternalCall("Internal_GetBounds", (void*)&ScriptGUIElement::internal_getBounds);
  86. metaData.scriptClass->addInternalCall("Internal_SetBounds", (void*)&ScriptGUIElement::internal_setBounds);
  87. metaData.scriptClass->addInternalCall("Internal_GetVisibleBounds", (void*)&ScriptGUIElement::internal_getVisibleBounds);
  88. metaData.scriptClass->addInternalCall("Internal_SetPosition", (void*)&ScriptGUIElement::internal_SetPosition);
  89. metaData.scriptClass->addInternalCall("Internal_SetWidth", (void*)&ScriptGUIElement::internal_SetWidth);
  90. metaData.scriptClass->addInternalCall("Internal_SetFlexibleWidth", (void*)&ScriptGUIElement::internal_SetFlexibleWidth);
  91. metaData.scriptClass->addInternalCall("Internal_SetHeight", (void*)&ScriptGUIElement::internal_SetHeight);
  92. metaData.scriptClass->addInternalCall("Internal_SetFlexibleHeight", (void*)&ScriptGUIElement::internal_SetFlexibleHeight);
  93. metaData.scriptClass->addInternalCall("Internal_ResetDimensions", (void*)&ScriptGUIElement::internal_ResetDimensions);
  94. metaData.scriptClass->addInternalCall("Internal_SetContextMenu", (void*)&ScriptGUIElement::internal_SetContextMenu);
  95. metaData.scriptClass->addInternalCall("Internal_GetStyle", (void*)&ScriptGUIElement::internal_GetStyle);
  96. metaData.scriptClass->addInternalCall("Internal_SetStyle", (void*)&ScriptGUIElement::internal_SetStyle);
  97. metaData.scriptClass->addInternalCall("Internal_GetParent", (void*)&ScriptGUIElement::internal_getParent);
  98. onFocusGainedThunk = (OnFocusChangedThunkDef)metaData.scriptClass->getMethod("Internal_OnFocusGained", 0)->getThunk();
  99. onFocusLostThunk = (OnFocusChangedThunkDef)metaData.scriptClass->getMethod("Internal_OnFocusLost", 0)->getThunk();
  100. }
  101. void ScriptGUIElement::internal_destroy(ScriptGUIElementBaseTBase* nativeInstance)
  102. {
  103. nativeInstance->destroy();
  104. }
  105. void ScriptGUIElement::internal_setVisible(ScriptGUIElementBaseTBase* nativeInstance, bool visible)
  106. {
  107. if (nativeInstance->isDestroyed())
  108. return;
  109. nativeInstance->getGUIElement()->setVisible(visible);
  110. }
  111. void ScriptGUIElement::internal_setActive(ScriptGUIElementBaseTBase* nativeInstance, bool enabled)
  112. {
  113. if (nativeInstance->isDestroyed())
  114. return;
  115. nativeInstance->getGUIElement()->setActive(enabled);
  116. }
  117. void ScriptGUIElement::internal_setDisabled(ScriptGUIElementBaseTBase* nativeInstance, bool disabled)
  118. {
  119. if (nativeInstance->isDestroyed())
  120. return;
  121. nativeInstance->getGUIElement()->setDisabled(disabled);
  122. }
  123. void ScriptGUIElement::internal_setFocus(ScriptGUIElementBaseTBase* nativeInstance, bool focus)
  124. {
  125. if (nativeInstance->isDestroyed())
  126. return;
  127. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  128. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  129. {
  130. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  131. guiElem->setFocus(focus);
  132. }
  133. }
  134. bool ScriptGUIElement::internal_getVisible(ScriptGUIElementBaseTBase* nativeInstance)
  135. {
  136. if (nativeInstance->isDestroyed())
  137. return false;
  138. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  139. return guiElemBase->_isVisible();
  140. }
  141. bool ScriptGUIElement::internal_getActive(ScriptGUIElementBaseTBase* nativeInstance)
  142. {
  143. if (nativeInstance->isDestroyed())
  144. return false;
  145. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  146. return guiElemBase->_isActive();
  147. }
  148. bool ScriptGUIElement::internal_getDisabled(ScriptGUIElementBaseTBase* nativeInstance)
  149. {
  150. if (nativeInstance->isDestroyed())
  151. return false;
  152. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  153. return guiElemBase->_isDisabled();
  154. }
  155. bool ScriptGUIElement::internal_getBlocking(ScriptGUIElementBaseTBase* nativeInstance)
  156. {
  157. if (nativeInstance->isDestroyed())
  158. return false;
  159. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  160. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  161. {
  162. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  163. return guiElem->getBlockPointerEvents();
  164. }
  165. return false;
  166. }
  167. void ScriptGUIElement::internal_setBlocking(ScriptGUIElementBaseTBase* nativeInstance, bool blocking)
  168. {
  169. if (nativeInstance->isDestroyed())
  170. return;
  171. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  172. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  173. {
  174. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  175. guiElem->setBlockPointerEvents(blocking);
  176. }
  177. }
  178. MonoObject* ScriptGUIElement::internal_getParent(ScriptGUIElementBaseTBase* nativeInstance)
  179. {
  180. if (nativeInstance->isDestroyed())
  181. return nullptr;
  182. if (nativeInstance->getParent() != nullptr)
  183. return nativeInstance->getParent()->getManagedInstance();
  184. return nullptr;
  185. }
  186. void ScriptGUIElement::internal_getBounds(ScriptGUIElementBaseTBase* nativeInstance, Rect2I* bounds)
  187. {
  188. if (nativeInstance->isDestroyed())
  189. {
  190. *bounds = Rect2I();
  191. return;
  192. }
  193. *bounds = nativeInstance->getGUIElement()->getBounds();
  194. }
  195. void ScriptGUIElement::internal_setBounds(ScriptGUIElementBaseTBase* nativeInstance, Rect2I* bounds)
  196. {
  197. if (nativeInstance->isDestroyed())
  198. return;
  199. nativeInstance->getGUIElement()->setPosition(bounds->x, bounds->y);
  200. nativeInstance->getGUIElement()->setWidth(bounds->width);
  201. nativeInstance->getGUIElement()->setHeight(bounds->height);
  202. }
  203. void ScriptGUIElement::internal_getVisibleBounds(ScriptGUIElementBaseTBase* nativeInstance, Rect2I* bounds)
  204. {
  205. if (nativeInstance->isDestroyed())
  206. {
  207. *bounds = Rect2I();
  208. return;
  209. }
  210. *bounds = nativeInstance->getGUIElement()->getVisibleBounds();
  211. }
  212. void ScriptGUIElement::internal_SetPosition(ScriptGUIElementBaseTBase* nativeInstance, INT32 x, INT32 y)
  213. {
  214. if (nativeInstance->isDestroyed())
  215. return;
  216. nativeInstance->getGUIElement()->setPosition(x, y);
  217. }
  218. void ScriptGUIElement::internal_SetWidth(ScriptGUIElementBaseTBase* nativeInstance, UINT32 width)
  219. {
  220. if (nativeInstance->isDestroyed())
  221. return;
  222. nativeInstance->getGUIElement()->setWidth(width);
  223. }
  224. void ScriptGUIElement::internal_SetFlexibleWidth(ScriptGUIElementBaseTBase* nativeInstance, UINT32 minWidth, UINT32 maxWidth)
  225. {
  226. if (nativeInstance->isDestroyed())
  227. return;
  228. nativeInstance->getGUIElement()->setFlexibleWidth(minWidth, maxWidth);
  229. }
  230. void ScriptGUIElement::internal_SetHeight(ScriptGUIElementBaseTBase* nativeInstance, UINT32 height)
  231. {
  232. if (nativeInstance->isDestroyed())
  233. return;
  234. nativeInstance->getGUIElement()->setHeight(height);
  235. }
  236. void ScriptGUIElement::internal_SetFlexibleHeight(ScriptGUIElementBaseTBase* nativeInstance, UINT32 minHeight, UINT32 maxHeight)
  237. {
  238. if (nativeInstance->isDestroyed())
  239. return;
  240. nativeInstance->getGUIElement()->setFlexibleHeight(minHeight, maxHeight);
  241. }
  242. void ScriptGUIElement::internal_ResetDimensions(ScriptGUIElementBaseTBase* nativeInstance)
  243. {
  244. if (nativeInstance->isDestroyed())
  245. return;
  246. nativeInstance->getGUIElement()->resetDimensions();
  247. }
  248. void ScriptGUIElement::internal_SetContextMenu(ScriptGUIElementBaseTBase* nativeInstance, ScriptContextMenu* contextMenu)
  249. {
  250. if (nativeInstance->isDestroyed())
  251. return;
  252. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  253. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  254. {
  255. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  256. SPtr<GUIContextMenu> nativeContextMenu;
  257. if (contextMenu != nullptr)
  258. nativeContextMenu = contextMenu->getInternal();
  259. guiElem->setContextMenu(nativeContextMenu);
  260. }
  261. }
  262. MonoString* ScriptGUIElement::internal_GetStyle(ScriptGUIElementBaseTBase* nativeInstance)
  263. {
  264. if (!nativeInstance->isDestroyed())
  265. {
  266. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  267. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  268. {
  269. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  270. return MonoUtil::stringToMono(guiElem->getStyleName());
  271. }
  272. }
  273. return MonoUtil::stringToMono(StringUtil::BLANK);
  274. }
  275. void ScriptGUIElement::internal_SetStyle(ScriptGUIElementBaseTBase* nativeInstance, MonoString* style)
  276. {
  277. if (!nativeInstance->isDestroyed())
  278. {
  279. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  280. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  281. {
  282. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  283. guiElem->setStyle(MonoUtil::monoToString(style));
  284. }
  285. }
  286. }
  287. }