BsScriptGUILayout.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. destroyChildren();
  36. if (mOwnsNative)
  37. GUILayout::destroy(mLayout);
  38. mLayout = nullptr;
  39. mIsDestroyed = true;
  40. }
  41. }
  42. void ScriptGUILayout::destroyChildren()
  43. {
  44. while (mChildren.size() > 0)
  45. {
  46. ChildInfo childInfo = mChildren[0];
  47. childInfo.element->destroy();
  48. }
  49. }
  50. void ScriptGUILayout::markAsDestroyed()
  51. {
  52. mLayout = nullptr;
  53. mIsDestroyed = true;
  54. }
  55. void ScriptGUILayout::addChild(ScriptGUIElementBaseTBase* element)
  56. {
  57. ChildInfo childInfo;
  58. childInfo.element = element;
  59. childInfo.gcHandle = mono_gchandle_new(element->getManagedInstance(), false);
  60. mChildren.push_back(childInfo);
  61. }
  62. void ScriptGUILayout::insertChild(UINT32 idx, ScriptGUIElementBaseTBase* element)
  63. {
  64. ChildInfo childInfo;
  65. childInfo.element = element;
  66. childInfo.gcHandle = mono_gchandle_new(element->getManagedInstance(), false);
  67. mChildren.insert(mChildren.begin() + idx, childInfo);
  68. }
  69. void ScriptGUILayout::removeChild(ScriptGUIElementBaseTBase* element)
  70. {
  71. auto iterFind = std::find_if(mChildren.begin(), mChildren.end(),
  72. [&](const ChildInfo& x)
  73. {
  74. return x.element == element;
  75. });
  76. if (iterFind != mChildren.end())
  77. {
  78. mono_gchandle_free(iterFind->gcHandle);
  79. mChildren.erase(iterFind);
  80. }
  81. }
  82. void ScriptGUILayout::internal_createInstanceX(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 = GUILayoutX::create(options);
  89. ScriptGUILayout* nativeInstance = new (bs_alloc<ScriptGUILayout>()) ScriptGUILayout(instance, layout);
  90. }
  91. void ScriptGUILayout::internal_createInstanceY(MonoObject* instance, 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 = GUILayoutY::create(options);
  98. ScriptGUILayout* nativeInstance = new (bs_alloc<ScriptGUILayout>()) ScriptGUILayout(instance, layout);
  99. }
  100. void ScriptGUILayout::internal_createInstancePanel(MonoObject* instance, INT16 depth, UINT16 depthRangeMin, UINT32 depthRangeMax, MonoArray* guiOptions)
  101. {
  102. GUIOptions options;
  103. UINT32 arrayLen = (UINT32)mono_array_length(guiOptions);
  104. for (UINT32 i = 0; i < arrayLen; i++)
  105. options.addOption(mono_array_get(guiOptions, GUIOption, i));
  106. GUILayout* layout = GUIPanel::create(depth, depthRangeMin, depthRangeMax, options);
  107. ScriptGUILayout* nativeInstance = new (bs_alloc<ScriptGUILayout>()) ScriptGUILayout(instance, layout);
  108. }
  109. void ScriptGUILayout::internal_createInstanceYFromScrollArea(MonoObject* instance, MonoObject* parentScrollArea)
  110. {
  111. ScriptGUIScrollArea* scriptScrollArea = ScriptGUIScrollArea::toNative(parentScrollArea);
  112. GUIScrollArea* scrollArea = (GUIScrollArea*)scriptScrollArea->getGUIElement();
  113. GUILayout* nativeLayout = &scrollArea->getLayout();
  114. ScriptGUILayout* nativeInstance = new (bs_alloc<ScriptGUILayout>())
  115. ScriptGUILayout(instance, nativeLayout);
  116. }
  117. void ScriptGUILayout::internal_addElement(ScriptGUILayout* instance, ScriptGUIElementBaseTBase* element)
  118. {
  119. if (instance->isDestroyed() || element->isDestroyed())
  120. return;
  121. instance->getInternalValue()->addElement(element->getGUIElement());
  122. if (element->getParent() != nullptr)
  123. element->getParent()->removeChild(element);
  124. element->setParent(instance);
  125. instance->addChild(element);
  126. }
  127. void ScriptGUILayout::internal_insertElement(ScriptGUILayout* instance, UINT32 index, ScriptGUIElementBaseTBase* element)
  128. {
  129. if (instance->isDestroyed() || element->isDestroyed())
  130. return;
  131. instance->getInternalValue()->insertElement(index, element->getGUIElement());
  132. if (element->getParent() != nullptr)
  133. element->getParent()->removeChild(element);
  134. element->setParent(instance);
  135. instance->insertChild(index, element);
  136. }
  137. UINT32 ScriptGUILayout::internal_getChildCount(ScriptGUILayout* instance)
  138. {
  139. if (instance->isDestroyed())
  140. return 0;
  141. return instance->mLayout->getNumChildren();
  142. }
  143. MonoObject* ScriptGUILayout::internal_getChild(ScriptGUILayout* instance, UINT32 index)
  144. {
  145. if (instance->isDestroyed() || instance->mChildren.size() >= index)
  146. return nullptr;
  147. return instance->mChildren[index].element->getManagedInstance();
  148. }
  149. ScriptGUIPanel::ScriptGUIPanel(MonoObject* instance)
  150. :ScriptObject(instance)
  151. { }
  152. void ScriptGUIPanel::initRuntimeData()
  153. { }
  154. MonoObject* ScriptGUIPanel::createFromExisting(GUIPanel* panel)
  155. {
  156. MonoObject* managedInstance = metaData.scriptClass->createInstance();
  157. ScriptGUILayout* nativeInstance = new (bs_alloc<ScriptGUILayout>()) ScriptGUILayout(managedInstance, panel, false);
  158. return managedInstance;
  159. }
  160. }