BsScriptGUIElement.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. MonoUtil::invokeThunk(ScriptGUIElement::onFocusChangedThunk, instance, focus);
  30. }
  31. void ScriptGUIElementBaseTBase::_onManagedInstanceDeleted()
  32. {
  33. destroy();
  34. ScriptObjectBase::_onManagedInstanceDeleted();
  35. }
  36. ScriptGUIElementTBase::ScriptGUIElementTBase(MonoObject* instance)
  37. :ScriptGUIElementBaseTBase(instance)
  38. {
  39. }
  40. void ScriptGUIElementTBase::destroy()
  41. {
  42. if(!mIsDestroyed)
  43. {
  44. if (mParent != nullptr)
  45. mParent->removeChild(this);
  46. if (mElement->_getType() == GUIElementBase::Type::Element)
  47. {
  48. GUIElement::destroy((GUIElement*)mElement);
  49. mElement = nullptr;
  50. mIsDestroyed = true;
  51. }
  52. }
  53. }
  54. ScriptGUIElement::OnFocusChangedThunkDef ScriptGUIElement::onFocusChangedThunk;
  55. ScriptGUIElement::ScriptGUIElement(MonoObject* instance)
  56. :ScriptObject(instance)
  57. {
  58. }
  59. void ScriptGUIElement::initRuntimeData()
  60. {
  61. metaData.scriptClass->addInternalCall("Internal_Destroy", &ScriptGUIElement::internal_destroy);
  62. metaData.scriptClass->addInternalCall("Internal_SetVisible", &ScriptGUIElement::internal_setVisible);
  63. metaData.scriptClass->addInternalCall("Internal_SetActive", &ScriptGUIElement::internal_setActive);
  64. metaData.scriptClass->addInternalCall("Internal_SetDisabled", &ScriptGUIElement::internal_setDisabled);
  65. metaData.scriptClass->addInternalCall("Internal_SetFocus", &ScriptGUIElement::internal_setFocus);
  66. metaData.scriptClass->addInternalCall("Internal_GetParent", &ScriptGUIElement::internal_getParent);
  67. metaData.scriptClass->addInternalCall("Internal_GetBounds", &ScriptGUIElement::internal_getBounds);
  68. metaData.scriptClass->addInternalCall("Internal_SetBounds", &ScriptGUIElement::internal_setBounds);
  69. metaData.scriptClass->addInternalCall("Internal_GetVisibleBounds", &ScriptGUIElement::internal_getVisibleBounds);
  70. metaData.scriptClass->addInternalCall("Internal_SetPosition", &ScriptGUIElement::internal_SetPosition);
  71. metaData.scriptClass->addInternalCall("Internal_SetWidth", &ScriptGUIElement::internal_SetWidth);
  72. metaData.scriptClass->addInternalCall("Internal_SetFlexibleWidth", &ScriptGUIElement::internal_SetFlexibleWidth);
  73. metaData.scriptClass->addInternalCall("Internal_SetHeight", &ScriptGUIElement::internal_SetHeight);
  74. metaData.scriptClass->addInternalCall("Internal_SetFlexibleHeight", &ScriptGUIElement::internal_SetFlexibleHeight);
  75. metaData.scriptClass->addInternalCall("Internal_ResetDimensions", &ScriptGUIElement::internal_ResetDimensions);
  76. metaData.scriptClass->addInternalCall("Internal_SetContextMenu", &ScriptGUIElement::internal_SetContextMenu);
  77. onFocusChangedThunk = (OnFocusChangedThunkDef)metaData.scriptClass->getMethod("InternalOnFocusChanged", 1)->getThunk();
  78. }
  79. void ScriptGUIElement::internal_destroy(ScriptGUIElementBaseTBase* nativeInstance)
  80. {
  81. nativeInstance->destroy();
  82. }
  83. void ScriptGUIElement::internal_setVisible(ScriptGUIElementBaseTBase* nativeInstance, bool visible)
  84. {
  85. if (nativeInstance->isDestroyed())
  86. return;
  87. nativeInstance->getGUIElement()->setVisible(visible);
  88. }
  89. void ScriptGUIElement::internal_setActive(ScriptGUIElementBaseTBase* nativeInstance, bool enabled)
  90. {
  91. if (nativeInstance->isDestroyed())
  92. return;
  93. nativeInstance->getGUIElement()->setActive(enabled);
  94. }
  95. void ScriptGUIElement::internal_setDisabled(ScriptGUIElementBaseTBase* nativeInstance, bool disabled)
  96. {
  97. if (nativeInstance->isDestroyed())
  98. return;
  99. nativeInstance->getGUIElement()->setDisabled(disabled);
  100. }
  101. void ScriptGUIElement::internal_setFocus(ScriptGUIElementBaseTBase* nativeInstance, bool focus)
  102. {
  103. if (nativeInstance->isDestroyed())
  104. return;
  105. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  106. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  107. {
  108. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  109. guiElem->setFocus(focus);
  110. }
  111. }
  112. MonoObject* ScriptGUIElement::internal_getParent(ScriptGUIElementBaseTBase* nativeInstance)
  113. {
  114. if (nativeInstance->isDestroyed())
  115. return nullptr;
  116. if (nativeInstance->getParent() != nullptr)
  117. return nativeInstance->getParent()->getManagedInstance();
  118. return nullptr;
  119. }
  120. Rect2I ScriptGUIElement::internal_getBounds(ScriptGUIElementBaseTBase* nativeInstance)
  121. {
  122. if (nativeInstance->isDestroyed())
  123. return Rect2I();
  124. return nativeInstance->getGUIElement()->getBounds();
  125. }
  126. void ScriptGUIElement::internal_setBounds(ScriptGUIElementBaseTBase* nativeInstance, Rect2I bounds)
  127. {
  128. if (nativeInstance->isDestroyed())
  129. return;
  130. nativeInstance->getGUIElement()->setPosition(bounds.x, bounds.y);
  131. nativeInstance->getGUIElement()->setWidth(bounds.width);
  132. nativeInstance->getGUIElement()->setHeight(bounds.height);
  133. }
  134. Rect2I ScriptGUIElement::internal_getVisibleBounds(ScriptGUIElementBaseTBase* nativeInstance)
  135. {
  136. if (nativeInstance->isDestroyed())
  137. return Rect2I();
  138. return nativeInstance->getGUIElement()->getVisibleBounds();
  139. }
  140. void ScriptGUIElement::internal_SetPosition(ScriptGUIElementBaseTBase* nativeInstance, INT32 x, INT32 y)
  141. {
  142. if (nativeInstance->isDestroyed())
  143. return;
  144. nativeInstance->getGUIElement()->setPosition(x, y);
  145. }
  146. void ScriptGUIElement::internal_SetWidth(ScriptGUIElementBaseTBase* nativeInstance, UINT32 width)
  147. {
  148. if (nativeInstance->isDestroyed())
  149. return;
  150. nativeInstance->getGUIElement()->setWidth(width);
  151. }
  152. void ScriptGUIElement::internal_SetFlexibleWidth(ScriptGUIElementBaseTBase* nativeInstance, UINT32 minWidth, UINT32 maxWidth)
  153. {
  154. if (nativeInstance->isDestroyed())
  155. return;
  156. nativeInstance->getGUIElement()->setFlexibleWidth(minWidth, maxWidth);
  157. }
  158. void ScriptGUIElement::internal_SetHeight(ScriptGUIElementBaseTBase* nativeInstance, UINT32 height)
  159. {
  160. if (nativeInstance->isDestroyed())
  161. return;
  162. nativeInstance->getGUIElement()->setHeight(height);
  163. }
  164. void ScriptGUIElement::internal_SetFlexibleHeight(ScriptGUIElementBaseTBase* nativeInstance, UINT32 minHeight, UINT32 maxHeight)
  165. {
  166. if (nativeInstance->isDestroyed())
  167. return;
  168. nativeInstance->getGUIElement()->setFlexibleHeight(minHeight, maxHeight);
  169. }
  170. void ScriptGUIElement::internal_ResetDimensions(ScriptGUIElementBaseTBase* nativeInstance)
  171. {
  172. if (nativeInstance->isDestroyed())
  173. return;
  174. nativeInstance->getGUIElement()->resetDimensions();
  175. }
  176. void ScriptGUIElement::internal_SetContextMenu(ScriptGUIElementBaseTBase* nativeInstance, ScriptContextMenu* contextMenu)
  177. {
  178. if (nativeInstance->isDestroyed())
  179. return;
  180. GUIElementBase* guiElemBase = nativeInstance->getGUIElement();
  181. if (guiElemBase->_getType() == GUIElementBase::Type::Element)
  182. {
  183. GUIElement* guiElem = static_cast<GUIElement*>(guiElemBase);
  184. GUIContextMenuPtr nativeContextMenu;
  185. if (contextMenu != nullptr)
  186. nativeContextMenu = contextMenu->getInternal();
  187. guiElem->setContextMenu(nativeContextMenu);
  188. }
  189. }
  190. }