BsScriptGUIListBox.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "BsScriptGUIListBox.h"
  2. #include "BsScriptMeta.h"
  3. #include "BsMonoField.h"
  4. #include "BsMonoClass.h"
  5. #include "BsMonoMethod.h"
  6. #include "BsMonoManager.h"
  7. #include "BsSpriteTexture.h"
  8. #include "BsMonoUtil.h"
  9. #include "BsGUILayout.h"
  10. #include "BsGUIListBox.h"
  11. #include "BsGUIOptions.h"
  12. #include "BsScriptGUIElementStyle.h"
  13. #include "BsScriptGUILayout.h"
  14. #include "BsScriptGUIArea.h"
  15. #include "BsScriptHString.h"
  16. using namespace CamelotFramework;
  17. using namespace std::placeholders;
  18. namespace BansheeEngine
  19. {
  20. ScriptGUIListBox::OnSelectionChangedThunkDef ScriptGUIListBox::onSelectionChangedThunk;
  21. ScriptGUIListBox::ScriptGUIListBox(GUIListBox* listBox)
  22. :mListBox(listBox), mIsDestroyed(false)
  23. {
  24. }
  25. void ScriptGUIListBox::initMetaData()
  26. {
  27. metaData = ScriptMeta(BansheeEngineAssemblyName, "BansheeEngine", "GUIListBox", &ScriptGUIListBox::initRuntimeData);
  28. MonoManager::registerScriptType(&metaData);
  29. }
  30. void ScriptGUIListBox::initRuntimeData()
  31. {
  32. metaData.scriptClass->addInternalCall("Internal_CreateInstance", &ScriptGUIListBox::internal_createInstance);
  33. metaData.scriptClass->addInternalCall("Internal_DestroyInstance", &ScriptGUIListBox::internal_destroyInstance);
  34. metaData.scriptClass->addInternalCall("Internal_SetElements", &ScriptGUIListBox::internal_setElements);
  35. metaData.scriptClass->addInternalCall("Internal_Destroy", &ScriptGUIListBox::internal_destroy);
  36. metaData.scriptClass->addInternalCall("Internal_SetVisible", &ScriptGUIListBox::internal_setVisible);
  37. metaData.scriptClass->addInternalCall("Internal_SetParent", &ScriptGUIListBox::internal_setParent);
  38. onSelectionChangedThunk = (OnSelectionChangedThunkDef)metaData.scriptClass->getMethod("DoOnSelectionChanged", 1).getThunk();
  39. }
  40. void ScriptGUIListBox::destroy()
  41. {
  42. if(!mIsDestroyed)
  43. {
  44. GUIElement::destroy(mListBox);
  45. mListBox = nullptr;
  46. mIsDestroyed = true;
  47. }
  48. }
  49. void ScriptGUIListBox::internal_createInstance(MonoObject* instance, MonoArray* elements, MonoObject* style, MonoArray* guiOptions)
  50. {
  51. GUIOptions options;
  52. UINT32 optionsArrayLen = (UINT32)mono_array_length(guiOptions);
  53. for(UINT32 i = 0; i < optionsArrayLen; i++)
  54. options.addOption(mono_array_get(guiOptions, GUIOption, i));
  55. GUIElementStyle* elemStyle = nullptr;
  56. if(style != nullptr)
  57. elemStyle = ScriptGUIElementStyle::toNative(style)->getInternalValue();
  58. UINT32 elementsArrayLen = (UINT32)mono_array_length(elements);
  59. Vector<HString>::type nativeElements;
  60. for(UINT32 i = 0; i < elementsArrayLen; i++)
  61. {
  62. MonoObject* stringManaged = (MonoObject*)mono_array_get(elements, MonoObject*, i);
  63. if(stringManaged == nullptr)
  64. nativeElements.push_back(HString::dummy());
  65. else
  66. {
  67. ScriptHString* textScript = ScriptHString::toNative(stringManaged);
  68. nativeElements.push_back(textScript->getInternalValue());
  69. }
  70. }
  71. GUIListBox* guiListBox = GUIListBox::create(nativeElements, options, elemStyle);
  72. guiListBox->onSelectionChanged.connect(std::bind(&ScriptGUIListBox::onSelectionChanged, instance, std::placeholders::_1));
  73. ScriptGUIListBox* nativeInstance = new (cm_alloc<ScriptGUIListBox>()) ScriptGUIListBox(guiListBox);
  74. nativeInstance->createInstance(instance);
  75. metaData.thisPtrField->setValue(instance, &nativeInstance);
  76. }
  77. void ScriptGUIListBox::internal_setElements(ScriptGUIListBox* nativeInstance, MonoArray* elements)
  78. {
  79. UINT32 elementsArrayLen = (UINT32)mono_array_length(elements);
  80. Vector<HString>::type nativeElements;
  81. for(UINT32 i = 0; i < elementsArrayLen; i++)
  82. {
  83. MonoObject* stringManaged = (MonoObject*)mono_array_get(elements, MonoObject*, i);
  84. if(stringManaged == nullptr)
  85. nativeElements.push_back(HString::dummy());
  86. else
  87. {
  88. ScriptHString* textScript = ScriptHString::toNative(stringManaged);
  89. nativeElements.push_back(textScript->getInternalValue());
  90. }
  91. }
  92. nativeInstance->getInternalValue()->setElements(nativeElements);
  93. }
  94. void ScriptGUIListBox::internal_destroy(ScriptGUIListBox* nativeInstance)
  95. {
  96. nativeInstance->destroy();
  97. }
  98. void ScriptGUIListBox::internal_destroyInstance(ScriptGUIListBox* nativeInstance)
  99. {
  100. nativeInstance->destroy();
  101. cm_delete(nativeInstance);
  102. }
  103. void ScriptGUIListBox::internal_setVisible(ScriptGUIListBox* nativeInstance, bool visible)
  104. {
  105. if(visible)
  106. nativeInstance->getInternalValue()->enableRecursively();
  107. else
  108. nativeInstance->getInternalValue()->disableRecursively();
  109. }
  110. void ScriptGUIListBox::internal_setParent(ScriptGUIListBox* nativeInstance, MonoObject* parentLayout)
  111. {
  112. ScriptGUILayout* scriptLayout = ScriptGUILayout::toNative(parentLayout);
  113. GUILayout* nativeLayout = scriptLayout->getInternalValue();
  114. nativeLayout->addElement(nativeInstance->getInternalValue());
  115. }
  116. void ScriptGUIListBox::onSelectionChanged(MonoObject* instance, CM::UINT32 index)
  117. {
  118. MonoException* exception = nullptr;
  119. onSelectionChangedThunk(instance, index, &exception);
  120. MonoUtil::throwIfException(exception);
  121. }
  122. }