BsScriptGUIListBox.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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), mIsDestroyed(false)
  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_SetVisible", &ScriptGUIListBox::internal_setVisible);
  37. metaData.scriptClass->addInternalCall("Internal_SetParent", &ScriptGUIListBox::internal_setParent);
  38. onSelectionChangedThunk = (OnSelectionChangedThunkDef)metaData.scriptClass->getMethod("DoOnSelectionChanged", 1).getThunk();
  39. }
  40. void ScriptGUIListBox::destroy()
  41. {
  42. if(!mIsDestroyed)
  43. {
  44. GUIElement::destroy(mListBox);
  45. mListBox = nullptr;
  46. mIsDestroyed = true;
  47. }
  48. }
  49. void ScriptGUIListBox::internal_createInstance(MonoObject* instance, MonoArray* elements, MonoString* style, MonoArray* guiOptions)
  50. {
  51. GUIOptions options;
  52. UINT32 optionsArrayLen = (UINT32)mono_array_length(guiOptions);
  53. for(UINT32 i = 0; i < optionsArrayLen; i++)
  54. options.addOption(mono_array_get(guiOptions, GUIOption, i));
  55. UINT32 elementsArrayLen = (UINT32)mono_array_length(elements);
  56. Vector<HString>::type 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* guiListBox = GUIListBox::create(nativeElements, options, toString(MonoUtil::monoToWString(style)));
  69. guiListBox->onSelectionChanged.connect(std::bind(&ScriptGUIListBox::onSelectionChanged, instance, std::placeholders::_1));
  70. ScriptGUIListBox* nativeInstance = new (cm_alloc<ScriptGUIListBox>()) ScriptGUIListBox(guiListBox);
  71. nativeInstance->createInstance(instance);
  72. metaData.thisPtrField->setValue(instance, &nativeInstance);
  73. }
  74. void ScriptGUIListBox::internal_setElements(ScriptGUIListBox* nativeInstance, MonoArray* elements)
  75. {
  76. UINT32 elementsArrayLen = (UINT32)mono_array_length(elements);
  77. Vector<HString>::type nativeElements;
  78. for(UINT32 i = 0; i < elementsArrayLen; i++)
  79. {
  80. MonoObject* stringManaged = (MonoObject*)mono_array_get(elements, MonoObject*, i);
  81. if(stringManaged == nullptr)
  82. nativeElements.push_back(HString::dummy());
  83. else
  84. {
  85. ScriptHString* textScript = ScriptHString::toNative(stringManaged);
  86. nativeElements.push_back(textScript->getInternalValue());
  87. }
  88. }
  89. nativeInstance->getInternalValue()->setElements(nativeElements);
  90. }
  91. void ScriptGUIListBox::internal_destroy(ScriptGUIListBox* nativeInstance)
  92. {
  93. nativeInstance->destroy();
  94. }
  95. void ScriptGUIListBox::internal_destroyInstance(ScriptGUIListBox* nativeInstance)
  96. {
  97. nativeInstance->destroy();
  98. cm_delete(nativeInstance);
  99. }
  100. void ScriptGUIListBox::internal_setVisible(ScriptGUIListBox* nativeInstance, bool visible)
  101. {
  102. if(visible)
  103. nativeInstance->getInternalValue()->enableRecursively();
  104. else
  105. nativeInstance->getInternalValue()->disableRecursively();
  106. }
  107. void ScriptGUIListBox::internal_setParent(ScriptGUIListBox* nativeInstance, MonoObject* parentLayout)
  108. {
  109. ScriptGUILayout* scriptLayout = ScriptGUILayout::toNative(parentLayout);
  110. GUILayout* nativeLayout = scriptLayout->getInternalValue();
  111. nativeLayout->addElement(nativeInstance->getInternalValue());
  112. }
  113. void ScriptGUIListBox::onSelectionChanged(MonoObject* instance, CM::UINT32 index)
  114. {
  115. MonoException* exception = nullptr;
  116. onSelectionChangedThunk(instance, index, &exception);
  117. MonoUtil::throwIfException(exception);
  118. }
  119. }