BsScriptGUIElement.cpp 10 KB

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