2
0

BsScriptGUIListBox.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 "BsScriptHString.h"
  15. using namespace std::placeholders;
  16. namespace BansheeEngine
  17. {
  18. ScriptGUIListBox::OnSelectionChangedThunkDef ScriptGUIListBox::onSelectionChangedThunk;
  19. ScriptGUIListBox::ScriptGUIListBox(MonoObject* instance, GUIListBox* listBox)
  20. :TScriptGUIElement(instance, listBox)
  21. {
  22. }
  23. void ScriptGUIListBox::initRuntimeData()
  24. {
  25. metaData.scriptClass->addInternalCall("Internal_CreateInstance", &ScriptGUIListBox::internal_createInstance);
  26. metaData.scriptClass->addInternalCall("Internal_SetElements", &ScriptGUIListBox::internal_setElements);
  27. metaData.scriptClass->addInternalCall("Internal_SetTint", &ScriptGUIListBox::internal_setTint);
  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::internal_setTint(ScriptGUIListBox* nativeInstance, Color color)
  72. {
  73. GUIListBox* listBox = (GUIListBox*)nativeInstance->getGUIElement();
  74. listBox->setTint(color);
  75. }
  76. void ScriptGUIListBox::onSelectionChanged(MonoObject* instance, UINT32 index)
  77. {
  78. MonoUtil::invokeThunk(onSelectionChangedThunk, instance, index);
  79. }
  80. }