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. namespace BansheeEngine
  18. {
  19. ScriptGUIListBox::OnSelectionChangedThunkDef ScriptGUIListBox::onSelectionChangedThunk;
  20. ScriptGUIListBox::ScriptGUIListBox(GUIListBox* listBox)
  21. :mListBox(listBox)
  22. {
  23. }
  24. void ScriptGUIListBox::initMetaData()
  25. {
  26. metaData = ScriptMeta("MBansheeEngine", "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_Enable", &ScriptGUIListBox::internal_enable);
  36. metaData.scriptClass->addInternalCall("Internal_Disable", &ScriptGUIListBox::internal_disable);
  37. onSelectionChangedThunk = (OnSelectionChangedThunkDef)metaData.scriptClass->getMethod("DoOnSelectionChanged", 1).getThunk();
  38. }
  39. void ScriptGUIListBox::internal_createInstance(MonoObject* instance, MonoObject* parentLayout, MonoArray* elements, MonoObject* style, MonoArray* guiOptions)
  40. {
  41. ScriptGUILayout* scriptLayout = ScriptGUILayout::toNative(parentLayout);
  42. GUIOptions options;
  43. UINT32 optionsArrayLen = (UINT32)mono_array_length(guiOptions);
  44. for(UINT32 i = 0; i < optionsArrayLen; i++)
  45. options.addOption(mono_array_get(guiOptions, GUIOption, i));
  46. GUIElementStyle* elemStyle = nullptr;
  47. if(style != nullptr)
  48. elemStyle = ScriptGUIElementStyle::toNative(style)->getInternalValue();
  49. UINT32 elementsArrayLen = (UINT32)mono_array_length(elements);
  50. Vector<HString>::type nativeElements;
  51. for(UINT32 i = 0; i < elementsArrayLen; i++)
  52. {
  53. MonoObject* stringManaged = (MonoObject*)mono_array_get(elements, MonoObject*, i);
  54. if(stringManaged == nullptr)
  55. nativeElements.push_back(HString::dummy());
  56. else
  57. {
  58. ScriptHString* textScript = ScriptHString::toNative(stringManaged);
  59. nativeElements.push_back(textScript->getInternalValue());
  60. }
  61. }
  62. GUIListBox* guiListBox = GUIListBox::create(scriptLayout->getParentWidget(), nativeElements, options, elemStyle);
  63. guiListBox->onSelectionChanged.connect(std::bind(&ScriptGUIListBox::onSelectionChanged, instance, std::placeholders::_1));
  64. GUILayout* nativeLayout = scriptLayout->getInternalValue();
  65. nativeLayout->addElement(guiListBox);
  66. ScriptGUIListBox* nativeInstance = new (cm_alloc<ScriptGUIListBox>()) ScriptGUIListBox(guiListBox);
  67. nativeInstance->createInstance(instance);
  68. metaData.thisPtrField->setValue(instance, nativeInstance);
  69. }
  70. void ScriptGUIListBox::internal_destroyInstance(ScriptGUIListBox* nativeInstance)
  71. {
  72. nativeInstance->destroyInstance();
  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. }