BsScriptGUIElement.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 bs
  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. metaData.scriptClass->addInternalCall("Internal_GetParent", &ScriptGUIElement::internal_getParent);
  91. onFocusGainedThunk = (OnFocusChangedThunkDef)metaData.scriptClass->getMethod("Internal_OnFocusGained", 0)->getThunk();
  92. onFocusLostThunk = (OnFocusChangedThunkDef)metaData.scriptClass->getMethod("Internal_OnFocusLost", 0)->getThunk();
  93. }
  94. void ScriptGUIElement::internal_destroy(ScriptGUIElementBaseTBase* nativeInstance)
  95. {
  96. nativeInstance->destroy();
  97. }
  98. void ScriptGUIElement::internal_setVisible(ScriptGUIElementBaseTBase* nativeInstance, bool visible)
  99. {
  100. if (nativeInstance->isDestroyed())
  101. return;
  102. nativeInstance->getGUIElement()->setVisible(visible);
  103. }
  104. void ScriptGUIElement::internal_setActive(ScriptGUIElementBaseTBase* nativeInstance, bool enabled)
  105. {
  106. if (nativeInstance->isDestroyed())
  107. return;
  108. nativeInstance->getGUIElement()->setActive(enabled);
  109. }
  110. void ScriptGUIElement::internal_setDisabled(ScriptGUIElementBaseTBase* nativeInstance, bool disabled)
  111. {
  112. if (nativeInstance->isDestroyed())
  113. return;
  114. nativeInstance->getGUIElement()->setDisabled(disabled);
  115. }
  116. void ScriptGUIElement::internal_setFocus(ScriptGUIElementBaseTBase* nativeInstance, bool focus)
  117. {
  118. if (nativeInstance->isDestroyed())
  119. return;
  120. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  121. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  122. {
  123. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  124. guiElem->setFocus(focus);
  125. }
  126. }
  127. bool ScriptGUIElement::internal_getVisible(ScriptGUIElementBaseTBase* nativeInstance)
  128. {
  129. if (nativeInstance->isDestroyed())
  130. return false;
  131. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  132. return guiElemBase->_isVisible();
  133. }
  134. bool ScriptGUIElement::internal_getActive(ScriptGUIElementBaseTBase* nativeInstance)
  135. {
  136. if (nativeInstance->isDestroyed())
  137. return false;
  138. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  139. return guiElemBase->_isActive();
  140. }
  141. bool ScriptGUIElement::internal_getDisabled(ScriptGUIElementBaseTBase* nativeInstance)
  142. {
  143. if (nativeInstance->isDestroyed())
  144. return false;
  145. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  146. return guiElemBase->_isDisabled();
  147. }
  148. bool ScriptGUIElement::internal_getBlocking(ScriptGUIElementBaseTBase* nativeInstance)
  149. {
  150. if (nativeInstance->isDestroyed())
  151. return false;
  152. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  153. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  154. {
  155. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  156. return guiElem->getBlockPointerEvents();
  157. }
  158. return false;
  159. }
  160. void ScriptGUIElement::internal_setBlocking(ScriptGUIElementBaseTBase* nativeInstance, bool blocking)
  161. {
  162. if (nativeInstance->isDestroyed())
  163. return;
  164. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  165. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  166. {
  167. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  168. guiElem->setBlockPointerEvents(blocking);
  169. }
  170. }
  171. MonoObject* ScriptGUIElement::internal_getParent(ScriptGUIElementBaseTBase* nativeInstance)
  172. {
  173. if (nativeInstance->isDestroyed())
  174. return nullptr;
  175. if (nativeInstance->getParent() != nullptr)
  176. return nativeInstance->getParent()->getManagedInstance();
  177. return nullptr;
  178. }
  179. void ScriptGUIElement::internal_getBounds(ScriptGUIElementBaseTBase* nativeInstance, Rect2I* bounds)
  180. {
  181. if (nativeInstance->isDestroyed())
  182. {
  183. *bounds = Rect2I();
  184. return;
  185. }
  186. *bounds = nativeInstance->getGUIElement()->getBounds();
  187. }
  188. void ScriptGUIElement::internal_setBounds(ScriptGUIElementBaseTBase* nativeInstance, Rect2I* bounds)
  189. {
  190. if (nativeInstance->isDestroyed())
  191. return;
  192. nativeInstance->getGUIElement()->setPosition(bounds->x, bounds->y);
  193. nativeInstance->getGUIElement()->setWidth(bounds->width);
  194. nativeInstance->getGUIElement()->setHeight(bounds->height);
  195. }
  196. void ScriptGUIElement::internal_getVisibleBounds(ScriptGUIElementBaseTBase* nativeInstance, Rect2I* bounds)
  197. {
  198. if (nativeInstance->isDestroyed())
  199. {
  200. *bounds = Rect2I();
  201. return;
  202. }
  203. *bounds = nativeInstance->getGUIElement()->getVisibleBounds();
  204. }
  205. void ScriptGUIElement::internal_SetPosition(ScriptGUIElementBaseTBase* nativeInstance, INT32 x, INT32 y)
  206. {
  207. if (nativeInstance->isDestroyed())
  208. return;
  209. nativeInstance->getGUIElement()->setPosition(x, y);
  210. }
  211. void ScriptGUIElement::internal_SetWidth(ScriptGUIElementBaseTBase* nativeInstance, UINT32 width)
  212. {
  213. if (nativeInstance->isDestroyed())
  214. return;
  215. nativeInstance->getGUIElement()->setWidth(width);
  216. }
  217. void ScriptGUIElement::internal_SetFlexibleWidth(ScriptGUIElementBaseTBase* nativeInstance, UINT32 minWidth, UINT32 maxWidth)
  218. {
  219. if (nativeInstance->isDestroyed())
  220. return;
  221. nativeInstance->getGUIElement()->setFlexibleWidth(minWidth, maxWidth);
  222. }
  223. void ScriptGUIElement::internal_SetHeight(ScriptGUIElementBaseTBase* nativeInstance, UINT32 height)
  224. {
  225. if (nativeInstance->isDestroyed())
  226. return;
  227. nativeInstance->getGUIElement()->setHeight(height);
  228. }
  229. void ScriptGUIElement::internal_SetFlexibleHeight(ScriptGUIElementBaseTBase* nativeInstance, UINT32 minHeight, UINT32 maxHeight)
  230. {
  231. if (nativeInstance->isDestroyed())
  232. return;
  233. nativeInstance->getGUIElement()->setFlexibleHeight(minHeight, maxHeight);
  234. }
  235. void ScriptGUIElement::internal_ResetDimensions(ScriptGUIElementBaseTBase* nativeInstance)
  236. {
  237. if (nativeInstance->isDestroyed())
  238. return;
  239. nativeInstance->getGUIElement()->resetDimensions();
  240. }
  241. void ScriptGUIElement::internal_SetContextMenu(ScriptGUIElementBaseTBase* nativeInstance, ScriptContextMenu* contextMenu)
  242. {
  243. if (nativeInstance->isDestroyed())
  244. return;
  245. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  246. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  247. {
  248. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  249. SPtr<GUIContextMenu> nativeContextMenu;
  250. if (contextMenu != nullptr)
  251. nativeContextMenu = contextMenu->getInternal();
  252. guiElem->setContextMenu(nativeContextMenu);
  253. }
  254. }
  255. MonoString* ScriptGUIElement::internal_GetStyle(ScriptGUIElementBaseTBase* nativeInstance)
  256. {
  257. if (!nativeInstance->isDestroyed())
  258. {
  259. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  260. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  261. {
  262. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  263. return MonoUtil::stringToMono(guiElem->getStyleName());
  264. }
  265. }
  266. return MonoUtil::stringToMono(StringUtil::BLANK);
  267. }
  268. void ScriptGUIElement::internal_SetStyle(ScriptGUIElementBaseTBase* nativeInstance, MonoString* style)
  269. {
  270. if (!nativeInstance->isDestroyed())
  271. {
  272. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  273. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  274. {
  275. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  276. guiElem->setStyle(MonoUtil::monoToString(style));
  277. }
  278. }
  279. }
  280. }