BsScriptGUIListBoxField.cpp 6.9 KB

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