BsScriptGUIListBox.cpp 4.0 KB

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