BsScriptGUIElement.cpp 11 KB

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