BsScriptGUIListBox.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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(MonoObject* instance, GUIListBox* listBox)
  21. :TScriptGUIElement(instance, listBox)
  22. {
  23. }
  24. void ScriptGUIListBox::initRuntimeData()
  25. {
  26. metaData.scriptClass->addInternalCall("Internal_CreateInstance", &ScriptGUIListBox::internal_createInstance);
  27. metaData.scriptClass->addInternalCall("Internal_SetElements", &ScriptGUIListBox::internal_setElements);
  28. onSelectionChangedThunk = (OnSelectionChangedThunkDef)metaData.scriptClass->getMethod("DoOnSelectionChanged", 1)->getThunk();
  29. }
  30. void ScriptGUIListBox::internal_createInstance(MonoObject* instance, MonoArray* elements, MonoString* style, MonoArray* guiOptions)
  31. {
  32. GUIOptions options;
  33. UINT32 optionsArrayLen = (UINT32)mono_array_length(guiOptions);
  34. for(UINT32 i = 0; i < optionsArrayLen; i++)
  35. options.addOption(mono_array_get(guiOptions, GUIOption, i));
  36. UINT32 elementsArrayLen = (UINT32)mono_array_length(elements);
  37. Vector<HString> nativeElements;
  38. for(UINT32 i = 0; i < elementsArrayLen; i++)
  39. {
  40. MonoObject* stringManaged = (MonoObject*)mono_array_get(elements, MonoObject*, i);
  41. if(stringManaged == nullptr)
  42. nativeElements.push_back(HString::dummy());
  43. else
  44. {
  45. ScriptHString* textScript = ScriptHString::toNative(stringManaged);
  46. nativeElements.push_back(textScript->getInternalValue());
  47. }
  48. }
  49. GUIListBox* guiListBox = GUIListBox::create(nativeElements, options, toString(MonoUtil::monoToWString(style)));
  50. guiListBox->onSelectionChanged.connect(std::bind(&ScriptGUIListBox::onSelectionChanged, instance, std::placeholders::_1));
  51. ScriptGUIListBox* nativeInstance = new (bs_alloc<ScriptGUIListBox>()) ScriptGUIListBox(instance, guiListBox);
  52. }
  53. void ScriptGUIListBox::internal_setElements(ScriptGUIListBox* nativeInstance, MonoArray* elements)
  54. {
  55. UINT32 elementsArrayLen = (UINT32)mono_array_length(elements);
  56. Vector<HString> nativeElements;
  57. for(UINT32 i = 0; i < elementsArrayLen; i++)
  58. {
  59. MonoObject* stringManaged = (MonoObject*)mono_array_get(elements, MonoObject*, i);
  60. if(stringManaged == nullptr)
  61. nativeElements.push_back(HString::dummy());
  62. else
  63. {
  64. ScriptHString* textScript = ScriptHString::toNative(stringManaged);
  65. nativeElements.push_back(textScript->getInternalValue());
  66. }
  67. }
  68. GUIListBox* listBox = (GUIListBox*)nativeInstance->getGUIElement();
  69. listBox->setElements(nativeElements);
  70. }
  71. void ScriptGUIListBox::onSelectionChanged(MonoObject* instance, UINT32 index)
  72. {
  73. MonoException* exception = nullptr;
  74. onSelectionChangedThunk(instance, index, &exception);
  75. MonoUtil::throwIfException(exception);
  76. }
  77. }