BsScriptGUIListBoxField.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "BsScriptGUIListBoxField.h"
  2. #include "BsScriptMeta.h"
  3. #include "BsMonoField.h"
  4. #include "BsMonoClass.h"
  5. #include "BsMonoManager.h"
  6. #include "BsMonoMethod.h"
  7. #include "BsMonoUtil.h"
  8. #include "BsGUIListBoxField.h"
  9. #include "BsGUIOptions.h"
  10. #include "BsGUIContent.h"
  11. #include "BsScriptHString.h"
  12. #include "BsScriptGUIContent.h"
  13. using namespace std::placeholders;
  14. namespace BansheeEngine
  15. {
  16. ScriptGUIListBoxField::OnSelectionChangedThunkDef ScriptGUIListBoxField::onSelectionChangedThunk;
  17. ScriptGUIListBoxField::ScriptGUIListBoxField(MonoObject* instance, GUIListBoxField* listBoxField)
  18. :TScriptGUIElement(instance, listBoxField)
  19. {
  20. }
  21. void ScriptGUIListBoxField::initRuntimeData()
  22. {
  23. metaData.scriptClass->addInternalCall("Internal_CreateInstance", &ScriptGUIListBoxField::internal_createInstance);
  24. metaData.scriptClass->addInternalCall("Internal_SetElements", &ScriptGUIListBoxField::internal_setElements);
  25. metaData.scriptClass->addInternalCall("Internal_GetValue", &ScriptGUIListBoxField::internal_getValue);
  26. metaData.scriptClass->addInternalCall("Internal_SetValue", &ScriptGUIListBoxField::internal_setValue);
  27. metaData.scriptClass->addInternalCall("Internal_SetTint", &ScriptGUIListBoxField::internal_setTint);
  28. onSelectionChangedThunk = (OnSelectionChangedThunkDef)metaData.scriptClass->getMethod("DoOnSelectionChanged", 1)->getThunk();
  29. }
  30. void ScriptGUIListBoxField::internal_createInstance(MonoObject* instance, MonoArray* elements, MonoObject* title,
  31. UINT32 titleWidth, MonoString* style, MonoArray* guiOptions, bool withTitle)
  32. {
  33. GUIOptions options;
  34. UINT32 arrayLen = (UINT32)mono_array_length(guiOptions);
  35. for (UINT32 i = 0; i < arrayLen; i++)
  36. options.addOption(mono_array_get(guiOptions, GUIOption, i));
  37. String styleName = toString(MonoUtil::monoToWString(style));
  38. UINT32 elementsArrayLen = (UINT32)mono_array_length(elements);
  39. Vector<HString> nativeElements;
  40. for (UINT32 i = 0; i < elementsArrayLen; i++)
  41. {
  42. MonoObject* stringManaged = (MonoObject*)mono_array_get(elements, MonoObject*, i);
  43. if (stringManaged == nullptr)
  44. nativeElements.push_back(HString::dummy());
  45. else
  46. {
  47. ScriptHString* textScript = ScriptHString::toNative(stringManaged);
  48. nativeElements.push_back(textScript->getInternalValue());
  49. }
  50. }
  51. GUIListBoxField* guiField = nullptr;
  52. if (withTitle)
  53. {
  54. GUIContent nativeContent(ScriptGUIContent::getText(title), ScriptGUIContent::getImage(title), ScriptGUIContent::getTooltip(title));
  55. guiField = GUIListBoxField::create(nativeElements, nativeContent, titleWidth, options, styleName);
  56. }
  57. else
  58. {
  59. guiField = GUIListBoxField::create(nativeElements, options, styleName);
  60. }
  61. guiField->onSelectionChanged.connect(std::bind(&ScriptGUIListBoxField::onSelectionChanged, instance, _1));
  62. ScriptGUIListBoxField* nativeInstance = new (bs_alloc<ScriptGUIListBoxField>()) ScriptGUIListBoxField(instance, guiField);
  63. }
  64. UINT32 ScriptGUIListBoxField::internal_getValue(ScriptGUIListBoxField* nativeInstance)
  65. {
  66. GUIListBoxField* field = static_cast<GUIListBoxField*>(nativeInstance->getGUIElement());
  67. return field->getIndex();
  68. }
  69. void ScriptGUIListBoxField::internal_setValue(ScriptGUIListBoxField* nativeInstance, UINT32 index)
  70. {
  71. GUIListBoxField* field = static_cast<GUIListBoxField*>(nativeInstance->getGUIElement());
  72. return field->setIndex(index);
  73. }
  74. void ScriptGUIListBoxField::internal_setElements(ScriptGUIListBoxField* nativeInstance, MonoArray* elements)
  75. {
  76. UINT32 elementsArrayLen = (UINT32)mono_array_length(elements);
  77. Vector<HString> 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. GUIListBoxField* field = static_cast<GUIListBoxField*>(nativeInstance->getGUIElement());
  90. field->setElements(nativeElements);
  91. }
  92. void ScriptGUIListBoxField::internal_setTint(ScriptGUIListBoxField* nativeInstance, Color color)
  93. {
  94. GUIListBoxField* field = (GUIListBoxField*)nativeInstance->getGUIElement();
  95. field->setTint(color);
  96. }
  97. void ScriptGUIListBoxField::onSelectionChanged(MonoObject* instance, UINT32 newIndex)
  98. {
  99. MonoUtil::invokeThunk(onSelectionChangedThunk, instance, newIndex);
  100. }
  101. }