BsScriptGUIElement.cpp 12 KB

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