BsScriptGUIListBox.cpp 4.8 KB

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