BsScriptGUILayout.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include "BsScriptGUILayout.h"
  2. #include "BsScriptMeta.h"
  3. #include "BsMonoField.h"
  4. #include "BsMonoClass.h"
  5. #include "BsMonoManager.h"
  6. #include "BsScriptGUIScrollArea.h"
  7. #include "BsGUILayout.h"
  8. #include "BsGUILayoutX.h"
  9. #include "BsGUILayoutY.h"
  10. #include "BsGUIPanel.h"
  11. #include "BsGUIScrollArea.h"
  12. namespace BansheeEngine
  13. {
  14. ScriptGUILayout::ScriptGUILayout(MonoObject* instance, GUILayout* layout, bool ownsNative)
  15. :TScriptGUIElementBase(instance, layout), mLayout(layout), mIsDestroyed(false), mOwnsNative(ownsNative)
  16. {
  17. }
  18. void ScriptGUILayout::initRuntimeData()
  19. {
  20. metaData.scriptClass->addInternalCall("Internal_CreateInstanceX", &ScriptGUILayout::internal_createInstanceX);
  21. metaData.scriptClass->addInternalCall("Internal_CreateInstanceY", &ScriptGUILayout::internal_createInstanceY);
  22. metaData.scriptClass->addInternalCall("Internal_CreateInstancePanel", &ScriptGUILayout::internal_createInstancePanel);
  23. metaData.scriptClass->addInternalCall("Internal_CreateInstanceYFromScrollArea", &ScriptGUILayout::internal_createInstanceYFromScrollArea);
  24. metaData.scriptClass->addInternalCall("Internal_AddElement", &ScriptGUILayout::internal_addElement);
  25. metaData.scriptClass->addInternalCall("Internal_InsertElement", &ScriptGUILayout::internal_insertElement);
  26. metaData.scriptClass->addInternalCall("Internal_GetChildCount", &ScriptGUILayout::internal_getChildCount);
  27. metaData.scriptClass->addInternalCall("Internal_GetChild", &ScriptGUILayout::internal_getChild);
  28. }
  29. void ScriptGUILayout::destroy()
  30. {
  31. if(!mIsDestroyed)
  32. {
  33. if (mParent != nullptr)
  34. mParent->removeChild(this);
  35. while (mChildren.size() > 0)
  36. {
  37. ChildInfo childInfo = mChildren[0];
  38. childInfo.element->destroy();
  39. }
  40. if (mOwnsNative)
  41. GUILayout::destroy(mLayout);
  42. mLayout = nullptr;
  43. mIsDestroyed = true;
  44. }
  45. }
  46. void ScriptGUILayout::addChild(ScriptGUIElementBaseTBase* element)
  47. {
  48. ChildInfo childInfo;
  49. childInfo.element = element;
  50. childInfo.gcHandle = mono_gchandle_new(element->getManagedInstance(), false);
  51. mChildren.push_back(childInfo);
  52. }
  53. void ScriptGUILayout::insertChild(UINT32 idx, ScriptGUIElementBaseTBase* element)
  54. {
  55. ChildInfo childInfo;
  56. childInfo.element = element;
  57. childInfo.gcHandle = mono_gchandle_new(element->getManagedInstance(), false);
  58. mChildren.insert(mChildren.begin() + idx, childInfo);
  59. }
  60. void ScriptGUILayout::removeChild(ScriptGUIElementBaseTBase* element)
  61. {
  62. auto iterFind = std::find_if(mChildren.begin(), mChildren.end(),
  63. [&](const ChildInfo& x)
  64. {
  65. return x.element == element;
  66. });
  67. if (iterFind != mChildren.end())
  68. {
  69. mono_gchandle_free(iterFind->gcHandle);
  70. mChildren.erase(iterFind);
  71. }
  72. }
  73. void ScriptGUILayout::internal_createInstanceX(MonoObject* instance, MonoArray* guiOptions)
  74. {
  75. GUIOptions options;
  76. UINT32 arrayLen = (UINT32)mono_array_length(guiOptions);
  77. for (UINT32 i = 0; i < arrayLen; i++)
  78. options.addOption(mono_array_get(guiOptions, GUIOption, i));
  79. GUILayout* layout = GUILayoutX::create(options);
  80. ScriptGUILayout* nativeInstance = new (bs_alloc<ScriptGUILayout>()) ScriptGUILayout(instance, layout);
  81. }
  82. void ScriptGUILayout::internal_createInstanceY(MonoObject* instance, MonoArray* guiOptions)
  83. {
  84. GUIOptions options;
  85. UINT32 arrayLen = (UINT32)mono_array_length(guiOptions);
  86. for (UINT32 i = 0; i < arrayLen; i++)
  87. options.addOption(mono_array_get(guiOptions, GUIOption, i));
  88. GUILayout* layout = GUILayoutY::create(options);
  89. ScriptGUILayout* nativeInstance = new (bs_alloc<ScriptGUILayout>()) ScriptGUILayout(instance, layout);
  90. }
  91. void ScriptGUILayout::internal_createInstancePanel(MonoObject* instance, INT16 depth, UINT16 depthRangeMin, UINT32 depthRangeMax, MonoArray* guiOptions)
  92. {
  93. GUIOptions options;
  94. UINT32 arrayLen = (UINT32)mono_array_length(guiOptions);
  95. for (UINT32 i = 0; i < arrayLen; i++)
  96. options.addOption(mono_array_get(guiOptions, GUIOption, i));
  97. GUILayout* layout = GUIPanel::create(depth, depthRangeMin, depthRangeMax, options);
  98. ScriptGUILayout* nativeInstance = new (bs_alloc<ScriptGUILayout>()) ScriptGUILayout(instance, layout);
  99. }
  100. void ScriptGUILayout::internal_createInstanceYFromScrollArea(MonoObject* instance, MonoObject* parentScrollArea)
  101. {
  102. ScriptGUIScrollArea* scriptScrollArea = ScriptGUIScrollArea::toNative(parentScrollArea);
  103. GUIScrollArea* scrollArea = (GUIScrollArea*)scriptScrollArea->getGUIElement();
  104. GUILayout* nativeLayout = &scrollArea->getLayout();
  105. ScriptGUILayout* nativeInstance = new (bs_alloc<ScriptGUILayout>())
  106. ScriptGUILayout(instance, nativeLayout, false);
  107. // This method is expected to be called during GUIScrollArea construction, so we finish its initialization
  108. scriptScrollArea->initialize(nativeInstance);
  109. }
  110. void ScriptGUILayout::internal_addElement(ScriptGUILayout* instance, ScriptGUIElementBaseTBase* element)
  111. {
  112. if (instance->isDestroyed() || element->isDestroyed())
  113. return;
  114. instance->getInternalValue()->addElement(element->getGUIElement());
  115. if (element->getParent() != nullptr)
  116. element->getParent()->removeChild(element);
  117. element->setParent(instance);
  118. instance->addChild(element);
  119. }
  120. void ScriptGUILayout::internal_insertElement(ScriptGUILayout* instance, UINT32 index, ScriptGUIElementBaseTBase* element)
  121. {
  122. if (instance->isDestroyed() || element->isDestroyed())
  123. return;
  124. instance->getInternalValue()->insertElement(index, element->getGUIElement());
  125. if (element->getParent() != nullptr)
  126. element->getParent()->removeChild(element);
  127. element->setParent(instance);
  128. instance->insertChild(index, element);
  129. }
  130. UINT32 ScriptGUILayout::internal_getChildCount(ScriptGUILayout* instance)
  131. {
  132. if (instance->isDestroyed())
  133. return 0;
  134. return instance->mLayout->getNumChildren();
  135. }
  136. MonoObject* ScriptGUILayout::internal_getChild(ScriptGUILayout* instance, UINT32 index)
  137. {
  138. if (instance->isDestroyed() || instance->mChildren.size() >= index)
  139. return nullptr;
  140. return instance->mChildren[index].element->getManagedInstance();
  141. }
  142. ScriptGUIPanel::ScriptGUIPanel(MonoObject* instance)
  143. :ScriptObject(instance)
  144. { }
  145. void ScriptGUIPanel::initRuntimeData()
  146. { }
  147. MonoObject* ScriptGUIPanel::createFromExisting(GUIPanel* panel)
  148. {
  149. MonoObject* managedInstance = metaData.scriptClass->createInstance();
  150. ScriptGUILayout* nativeInstance = new (bs_alloc<ScriptGUILayout>()) ScriptGUILayout(managedInstance, panel, false);
  151. return managedInstance;
  152. }
  153. }