BsScriptGUIElement.cpp 6.6 KB

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