BsScriptGUILayout.cpp 6.4 KB

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