BsScriptGUIListBox.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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)
  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_Enable", &ScriptGUIListBox::internal_enable);
  37. metaData.scriptClass->addInternalCall("Internal_Disable", &ScriptGUIListBox::internal_disable);
  38. onSelectionChangedThunk = (OnSelectionChangedThunkDef)metaData.scriptClass->getMethod("DoOnSelectionChanged", 1).getThunk();
  39. }
  40. void ScriptGUIListBox::internal_createInstance(MonoObject* instance, MonoObject* parentLayout, MonoArray* elements, MonoObject* style, MonoArray* guiOptions)
  41. {
  42. ScriptGUILayout* scriptLayout = ScriptGUILayout::toNative(parentLayout);
  43. GUIOptions options;
  44. UINT32 optionsArrayLen = (UINT32)mono_array_length(guiOptions);
  45. for(UINT32 i = 0; i < optionsArrayLen; i++)
  46. options.addOption(mono_array_get(guiOptions, GUIOption, i));
  47. GUIElementStyle* elemStyle = nullptr;
  48. if(style != nullptr)
  49. elemStyle = ScriptGUIElementStyle::toNative(style)->getInternalValue();
  50. UINT32 elementsArrayLen = (UINT32)mono_array_length(elements);
  51. Vector<HString>::type nativeElements;
  52. for(UINT32 i = 0; i < elementsArrayLen; i++)
  53. {
  54. MonoObject* stringManaged = (MonoObject*)mono_array_get(elements, MonoObject*, i);
  55. if(stringManaged == nullptr)
  56. nativeElements.push_back(HString::dummy());
  57. else
  58. {
  59. ScriptHString* textScript = ScriptHString::toNative(stringManaged);
  60. nativeElements.push_back(textScript->getInternalValue());
  61. }
  62. }
  63. GUIListBox* guiListBox = GUIListBox::create(scriptLayout->getParentWidget(), nativeElements, options, elemStyle);
  64. guiListBox->onSelectionChanged.connect(std::bind(&ScriptGUIListBox::onSelectionChanged, instance, std::placeholders::_1));
  65. GUILayout* nativeLayout = scriptLayout->getInternalValue();
  66. nativeLayout->addElement(guiListBox);
  67. ScriptGUIListBox* nativeInstance = new (cm_alloc<ScriptGUIListBox>()) ScriptGUIListBox(guiListBox);
  68. nativeInstance->createInstance(instance);
  69. metaData.thisPtrField->setValue(instance, &nativeInstance);
  70. }
  71. void ScriptGUIListBox::internal_destroyInstance(ScriptGUIListBox* nativeInstance)
  72. {
  73. cm_delete(nativeInstance);
  74. }
  75. void ScriptGUIListBox::internal_setElements(ScriptGUIListBox* nativeInstance, MonoArray* elements)
  76. {
  77. UINT32 elementsArrayLen = (UINT32)mono_array_length(elements);
  78. Vector<HString>::type nativeElements;
  79. for(UINT32 i = 0; i < elementsArrayLen; i++)
  80. {
  81. MonoObject* stringManaged = (MonoObject*)mono_array_get(elements, MonoObject*, i);
  82. if(stringManaged == nullptr)
  83. nativeElements.push_back(HString::dummy());
  84. else
  85. {
  86. ScriptHString* textScript = ScriptHString::toNative(stringManaged);
  87. nativeElements.push_back(textScript->getInternalValue());
  88. }
  89. }
  90. nativeInstance->getInternalValue()->setElements(nativeElements);
  91. }
  92. void ScriptGUIListBox::internal_destroy(ScriptGUIListBox* nativeInstance)
  93. {
  94. GUIElement::destroy(nativeInstance->getInternalValue());
  95. }
  96. void ScriptGUIListBox::internal_disable(ScriptGUIListBox* nativeInstance)
  97. {
  98. nativeInstance->getInternalValue()->disableRecursively();
  99. }
  100. void ScriptGUIListBox::internal_enable(ScriptGUIListBox* nativeInstance)
  101. {
  102. nativeInstance->getInternalValue()->enableRecursively();
  103. }
  104. void ScriptGUIListBox::onSelectionChanged(MonoObject* instance, CM::UINT32 index)
  105. {
  106. MonoException* exception = nullptr;
  107. onSelectionChangedThunk(instance, index, &exception);
  108. MonoUtil::throwIfException(exception);
  109. }
  110. }