BsScriptGUIListBoxField.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. metaData.scriptClass->addInternalCall("Internal_SelectElement", &ScriptGUIListBoxField::internal_selectElement);
  29. metaData.scriptClass->addInternalCall("Internal_DeselectElement", &ScriptGUIListBoxField::internal_deselectElement);
  30. metaData.scriptClass->addInternalCall("Internal_GetElementStates", &ScriptGUIListBoxField::internal_getElementStates);
  31. metaData.scriptClass->addInternalCall("Internal_SetElementStates", &ScriptGUIListBoxField::internal_setElementStates);
  32. onSelectionChangedThunk = (OnSelectionChangedThunkDef)metaData.scriptClass->getMethod("DoOnSelectionChanged", 1)->getThunk();
  33. }
  34. void ScriptGUIListBoxField::internal_createInstance(MonoObject* instance, MonoArray* elements, bool multiselect, MonoObject* title,
  35. UINT32 titleWidth, MonoString* style, MonoArray* guiOptions, bool withTitle)
  36. {
  37. GUIOptions options;
  38. UINT32 arrayLen = (UINT32)mono_array_length(guiOptions);
  39. for (UINT32 i = 0; i < arrayLen; i++)
  40. options.addOption(mono_array_get(guiOptions, GUIOption, i));
  41. String styleName = toString(MonoUtil::monoToWString(style));
  42. UINT32 elementsArrayLen = (UINT32)mono_array_length(elements);
  43. Vector<HString> nativeElements;
  44. for (UINT32 i = 0; i < elementsArrayLen; i++)
  45. {
  46. MonoObject* stringManaged = (MonoObject*)mono_array_get(elements, MonoObject*, i);
  47. if (stringManaged == nullptr)
  48. nativeElements.push_back(HString::dummy());
  49. else
  50. {
  51. ScriptHString* textScript = ScriptHString::toNative(stringManaged);
  52. nativeElements.push_back(textScript->getInternalValue());
  53. }
  54. }
  55. GUIListBoxField* guiField = nullptr;
  56. if (withTitle)
  57. {
  58. GUIContent nativeContent(ScriptGUIContent::getText(title), ScriptGUIContent::getImage(title), ScriptGUIContent::getTooltip(title));
  59. guiField = GUIListBoxField::create(nativeElements, multiselect, nativeContent, titleWidth, options, styleName);
  60. }
  61. else
  62. {
  63. guiField = GUIListBoxField::create(nativeElements, multiselect, options, styleName);
  64. }
  65. guiField->onSelectionChanged.connect(std::bind(&ScriptGUIListBoxField::onSelectionChanged, instance, _1));
  66. ScriptGUIListBoxField* nativeInstance = new (bs_alloc<ScriptGUIListBoxField>()) ScriptGUIListBoxField(instance, guiField);
  67. }
  68. UINT32 ScriptGUIListBoxField::internal_getValue(ScriptGUIListBoxField* nativeInstance)
  69. {
  70. GUIListBoxField* field = static_cast<GUIListBoxField*>(nativeInstance->getGUIElement());
  71. const Vector<bool>& states = field->getElementStates();
  72. for (UINT32 i = 0; i < (UINT32)states.size(); i++)
  73. {
  74. if (states[i])
  75. return i;
  76. }
  77. return UINT_MAX;
  78. }
  79. void ScriptGUIListBoxField::internal_setValue(ScriptGUIListBoxField* nativeInstance, UINT32 index)
  80. {
  81. GUIListBoxField* field = static_cast<GUIListBoxField*>(nativeInstance->getGUIElement());
  82. Vector<bool> states = field->getElementStates();
  83. UINT32 numElements = (UINT32)states.size();
  84. if (index >= numElements)
  85. return;
  86. for (UINT32 i = 0; i < numElements; i++)
  87. {
  88. if (states[i])
  89. states[i] = true;
  90. else
  91. states[i] = false;
  92. }
  93. field->setElementStates(states);
  94. }
  95. void ScriptGUIListBoxField::internal_setElements(ScriptGUIListBoxField* nativeInstance, MonoArray* elements)
  96. {
  97. UINT32 elementsArrayLen = (UINT32)mono_array_length(elements);
  98. Vector<HString> nativeElements;
  99. for (UINT32 i = 0; i < elementsArrayLen; i++)
  100. {
  101. MonoObject* stringManaged = (MonoObject*)mono_array_get(elements, MonoObject*, i);
  102. if (stringManaged == nullptr)
  103. nativeElements.push_back(HString::dummy());
  104. else
  105. {
  106. ScriptHString* textScript = ScriptHString::toNative(stringManaged);
  107. nativeElements.push_back(textScript->getInternalValue());
  108. }
  109. }
  110. GUIListBoxField* field = static_cast<GUIListBoxField*>(nativeInstance->getGUIElement());
  111. field->setElements(nativeElements);
  112. }
  113. void ScriptGUIListBoxField::internal_selectElement(ScriptGUIListBoxField* nativeInstance, int idx)
  114. {
  115. GUIListBoxField* listBox = (GUIListBoxField*)nativeInstance->getGUIElement();
  116. listBox->selectElement(idx);
  117. }
  118. void ScriptGUIListBoxField::internal_deselectElement(ScriptGUIListBoxField* nativeInstance, int idx)
  119. {
  120. GUIListBoxField* listBox = (GUIListBoxField*)nativeInstance->getGUIElement();
  121. listBox->deselectElement(idx);
  122. }
  123. MonoArray* ScriptGUIListBoxField::internal_getElementStates(ScriptGUIListBoxField* nativeInstance)
  124. {
  125. GUIListBoxField* listBox = (GUIListBoxField*)nativeInstance->getGUIElement();
  126. const Vector<bool>& states = listBox->getElementStates();
  127. UINT32 numElements = (UINT32)states.size();
  128. ScriptArray outStates = ScriptArray::create<bool>(numElements);
  129. for (UINT32 i = 0; i < numElements; i++)
  130. outStates.set(i, states[i]);
  131. return outStates.getInternal();
  132. }
  133. void ScriptGUIListBoxField::internal_setElementStates(ScriptGUIListBoxField* nativeInstance, MonoArray* monoStates)
  134. {
  135. if (monoStates == nullptr)
  136. return;
  137. ScriptArray inStates(monoStates);
  138. UINT32 numElements = inStates.size();
  139. Vector<bool> states(numElements);
  140. for (UINT32 i = 0; i < numElements; i++)
  141. states[i] = inStates.get<bool>(i);
  142. GUIListBoxField* listBox = (GUIListBoxField*)nativeInstance->getGUIElement();
  143. listBox->setElementStates(states);
  144. }
  145. void ScriptGUIListBoxField::internal_setTint(ScriptGUIListBoxField* nativeInstance, Color color)
  146. {
  147. GUIListBoxField* field = (GUIListBoxField*)nativeInstance->getGUIElement();
  148. field->setTint(color);
  149. }
  150. void ScriptGUIListBoxField::onSelectionChanged(MonoObject* instance, UINT32 newIndex)
  151. {
  152. MonoUtil::invokeThunk(onSelectionChangedThunk, instance, newIndex);
  153. }
  154. }