BsScriptGUIListBoxField.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsScriptGUIListBoxField.h"
  4. #include "BsScriptMeta.h"
  5. #include "BsMonoField.h"
  6. #include "BsMonoClass.h"
  7. #include "BsMonoManager.h"
  8. #include "BsMonoMethod.h"
  9. #include "BsMonoUtil.h"
  10. #include "BsGUIListBoxField.h"
  11. #include "BsGUIOptions.h"
  12. #include "BsGUIContent.h"
  13. #include "BsScriptHString.h"
  14. #include "BsScriptGUIContent.h"
  15. using namespace std::placeholders;
  16. namespace BansheeEngine
  17. {
  18. ScriptGUIListBoxField::OnSelectionChangedThunkDef ScriptGUIListBoxField::onSelectionChangedThunk;
  19. ScriptGUIListBoxField::ScriptGUIListBoxField(MonoObject* instance, GUIListBoxField* listBoxField)
  20. :TScriptGUIElement(instance, listBoxField)
  21. {
  22. }
  23. void ScriptGUIListBoxField::initRuntimeData()
  24. {
  25. metaData.scriptClass->addInternalCall("Internal_CreateInstance", &ScriptGUIListBoxField::internal_createInstance);
  26. metaData.scriptClass->addInternalCall("Internal_SetElements", &ScriptGUIListBoxField::internal_setElements);
  27. metaData.scriptClass->addInternalCall("Internal_GetValue", &ScriptGUIListBoxField::internal_getValue);
  28. metaData.scriptClass->addInternalCall("Internal_SetValue", &ScriptGUIListBoxField::internal_setValue);
  29. metaData.scriptClass->addInternalCall("Internal_SetTint", &ScriptGUIListBoxField::internal_setTint);
  30. metaData.scriptClass->addInternalCall("Internal_SelectElement", &ScriptGUIListBoxField::internal_selectElement);
  31. metaData.scriptClass->addInternalCall("Internal_DeselectElement", &ScriptGUIListBoxField::internal_deselectElement);
  32. metaData.scriptClass->addInternalCall("Internal_GetElementStates", &ScriptGUIListBoxField::internal_getElementStates);
  33. metaData.scriptClass->addInternalCall("Internal_SetElementStates", &ScriptGUIListBoxField::internal_setElementStates);
  34. onSelectionChangedThunk = (OnSelectionChangedThunkDef)metaData.scriptClass->getMethod("DoOnSelectionChanged", 1)->getThunk();
  35. }
  36. void ScriptGUIListBoxField::internal_createInstance(MonoObject* instance, MonoArray* elements, bool multiselect, MonoObject* title,
  37. UINT32 titleWidth, MonoString* style, MonoArray* guiOptions, bool withTitle)
  38. {
  39. GUIOptions options;
  40. UINT32 arrayLen = (UINT32)mono_array_length(guiOptions);
  41. for (UINT32 i = 0; i < arrayLen; i++)
  42. options.addOption(mono_array_get(guiOptions, GUIOption, i));
  43. String styleName = toString(MonoUtil::monoToWString(style));
  44. UINT32 elementsArrayLen = (UINT32)mono_array_length(elements);
  45. Vector<HString> nativeElements;
  46. for (UINT32 i = 0; i < elementsArrayLen; i++)
  47. {
  48. MonoObject* stringManaged = (MonoObject*)mono_array_get(elements, MonoObject*, i);
  49. if (stringManaged == nullptr)
  50. nativeElements.push_back(HString::dummy());
  51. else
  52. {
  53. ScriptHString* textScript = ScriptHString::toNative(stringManaged);
  54. nativeElements.push_back(textScript->getInternalValue());
  55. }
  56. }
  57. GUIListBoxField* guiField = nullptr;
  58. if (withTitle)
  59. {
  60. GUIContent nativeContent(ScriptGUIContent::getText(title), ScriptGUIContent::getImage(title), ScriptGUIContent::getTooltip(title));
  61. guiField = GUIListBoxField::create(nativeElements, multiselect, nativeContent, titleWidth, options, styleName);
  62. }
  63. else
  64. {
  65. guiField = GUIListBoxField::create(nativeElements, multiselect, options, styleName);
  66. }
  67. guiField->onSelectionChanged.connect(std::bind(&ScriptGUIListBoxField::onSelectionChanged, instance, _1));
  68. ScriptGUIListBoxField* nativeInstance = new (bs_alloc<ScriptGUIListBoxField>()) ScriptGUIListBoxField(instance, guiField);
  69. }
  70. UINT32 ScriptGUIListBoxField::internal_getValue(ScriptGUIListBoxField* nativeInstance)
  71. {
  72. GUIListBoxField* field = static_cast<GUIListBoxField*>(nativeInstance->getGUIElement());
  73. const Vector<bool>& states = field->getElementStates();
  74. for (UINT32 i = 0; i < (UINT32)states.size(); i++)
  75. {
  76. if (states[i])
  77. return i;
  78. }
  79. return UINT_MAX;
  80. }
  81. void ScriptGUIListBoxField::internal_setValue(ScriptGUIListBoxField* nativeInstance, UINT32 index)
  82. {
  83. GUIListBoxField* field = static_cast<GUIListBoxField*>(nativeInstance->getGUIElement());
  84. Vector<bool> states = field->getElementStates();
  85. UINT32 numElements = (UINT32)states.size();
  86. if (index >= numElements)
  87. return;
  88. for (UINT32 i = 0; i < numElements; i++)
  89. {
  90. if (states[i])
  91. states[i] = true;
  92. else
  93. states[i] = false;
  94. }
  95. field->setElementStates(states);
  96. }
  97. void ScriptGUIListBoxField::internal_setElements(ScriptGUIListBoxField* nativeInstance, MonoArray* elements)
  98. {
  99. UINT32 elementsArrayLen = (UINT32)mono_array_length(elements);
  100. Vector<HString> nativeElements;
  101. for (UINT32 i = 0; i < elementsArrayLen; i++)
  102. {
  103. MonoObject* stringManaged = (MonoObject*)mono_array_get(elements, MonoObject*, i);
  104. if (stringManaged == nullptr)
  105. nativeElements.push_back(HString::dummy());
  106. else
  107. {
  108. ScriptHString* textScript = ScriptHString::toNative(stringManaged);
  109. nativeElements.push_back(textScript->getInternalValue());
  110. }
  111. }
  112. GUIListBoxField* field = static_cast<GUIListBoxField*>(nativeInstance->getGUIElement());
  113. field->setElements(nativeElements);
  114. }
  115. void ScriptGUIListBoxField::internal_selectElement(ScriptGUIListBoxField* nativeInstance, int idx)
  116. {
  117. GUIListBoxField* listBox = (GUIListBoxField*)nativeInstance->getGUIElement();
  118. listBox->selectElement(idx);
  119. }
  120. void ScriptGUIListBoxField::internal_deselectElement(ScriptGUIListBoxField* nativeInstance, int idx)
  121. {
  122. GUIListBoxField* listBox = (GUIListBoxField*)nativeInstance->getGUIElement();
  123. listBox->deselectElement(idx);
  124. }
  125. MonoArray* ScriptGUIListBoxField::internal_getElementStates(ScriptGUIListBoxField* nativeInstance)
  126. {
  127. GUIListBoxField* listBox = (GUIListBoxField*)nativeInstance->getGUIElement();
  128. const Vector<bool>& states = listBox->getElementStates();
  129. UINT32 numElements = (UINT32)states.size();
  130. ScriptArray outStates = ScriptArray::create<bool>(numElements);
  131. for (UINT32 i = 0; i < numElements; i++)
  132. outStates.set(i, states[i]);
  133. return outStates.getInternal();
  134. }
  135. void ScriptGUIListBoxField::internal_setElementStates(ScriptGUIListBoxField* nativeInstance, MonoArray* monoStates)
  136. {
  137. if (monoStates == nullptr)
  138. return;
  139. ScriptArray inStates(monoStates);
  140. UINT32 numElements = inStates.size();
  141. Vector<bool> states(numElements);
  142. for (UINT32 i = 0; i < numElements; i++)
  143. states[i] = inStates.get<bool>(i);
  144. GUIListBoxField* listBox = (GUIListBoxField*)nativeInstance->getGUIElement();
  145. listBox->setElementStates(states);
  146. }
  147. void ScriptGUIListBoxField::internal_setTint(ScriptGUIListBoxField* nativeInstance, Color* color)
  148. {
  149. GUIListBoxField* field = (GUIListBoxField*)nativeInstance->getGUIElement();
  150. field->setTint(*color);
  151. }
  152. void ScriptGUIListBoxField::onSelectionChanged(MonoObject* instance, UINT32 newIndex)
  153. {
  154. MonoUtil::invokeThunk(onSelectionChangedThunk, instance, newIndex);
  155. }
  156. }