guiListBoxCtrl_ScriptBinding.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. ConsoleMethodGroupBeginWithDocs(GuiListBoxCtrl, GuiControl)
  23. /*! If true, allows the user to select multiple items.
  24. @param allowMultiSelection True to allow multi-selection. False to allow single selection.
  25. @return No return value.
  26. */
  27. ConsoleMethodWithDocs(GuiListBoxCtrl, setMultiSelection, ConsoleVoid, 3, 3, "(bool allowMultiSelection)")
  28. {
  29. object->setMultipleSelection(dAtob(argv[2]));
  30. }
  31. /*! Reports if multi-selection is enabled.
  32. @return Returns true if multi-selection is enabled. False otherwise.
  33. */
  34. ConsoleMethodWithDocs(GuiListBoxCtrl, getMultiSelection, ConsoleVoid, 2, 2, "()")
  35. {
  36. object->getMultipleSelection();
  37. }
  38. /*! Removes all items from the listbox.
  39. @return No return value.
  40. */
  41. ConsoleMethodWithDocs(GuiListBoxCtrl, clearItems, ConsoleVoid, 2, 2, "()")
  42. {
  43. object->clearItems();
  44. }
  45. /*! Unselects any items that have been selected.
  46. @return No return value.
  47. */
  48. ConsoleMethodWithDocs(GuiListBoxCtrl, clearSelection, ConsoleVoid, 2, 2, "()")
  49. {
  50. object->clearSelection();
  51. }
  52. /*! Sets an item to selected or unselected using an index.
  53. @param index The zero-based index of the item that should be selected.
  54. @param isSelected If true, the item is selected. If false, it is unselected. If omitted, will default to true.
  55. @return No return value.
  56. */
  57. ConsoleMethodWithDocs(GuiListBoxCtrl, setSelected, ConsoleVoid, 3, 4, "(S32 index, [bool isSelected])")
  58. {
  59. bool value = true;
  60. if (argc == 4)
  61. value = dAtob(argv[3]);
  62. if (value == true)
  63. object->addSelection(dAtoi(argv[2]));
  64. else
  65. object->removeSelection(dAtoi(argv[2]));
  66. }
  67. /*! Returns the number of items in the list.
  68. @return The number of items.
  69. */
  70. ConsoleMethodWithDocs(GuiListBoxCtrl, getItemCount, ConsoleInt, 2, 2, "()")
  71. {
  72. return object->getItemCount();
  73. }
  74. /*! Returns the number of selected items in the list.
  75. @return The number of items currently selected.
  76. */
  77. ConsoleMethodWithDocs(GuiListBoxCtrl, getSelCount, ConsoleInt, 2, 2, "()")
  78. {
  79. return object->getSelCount();
  80. }
  81. /*! Returns the index of the first selected item.
  82. @return The index of the first selected item or -1 if the item does not belong to the listbox.
  83. */
  84. ConsoleMethodWithDocs(GuiListBoxCtrl, getSelectedItem, ConsoleInt, 2, 2, "()")
  85. {
  86. return object->getSelectedItem();
  87. }
  88. /*! Returns a list of selected item indexes.
  89. @return A space-delimited list of selected item indexes or -1 if no items are selected.
  90. */
  91. ConsoleMethodWithDocs(GuiListBoxCtrl, getSelectedItems, ConsoleString, 2, 2, "()")
  92. {
  93. S32 selCount = object->getSelCount();
  94. if (selCount == -1 || selCount == 0)
  95. return StringTable->lookup("-1");
  96. else if (selCount == 1)
  97. return Con::getIntArg(object->getSelectedItem());
  98. Vector<S32> selItems;
  99. object->getSelectedItems(selItems);
  100. if (selItems.empty())
  101. return StringTable->lookup("-1");
  102. UTF8 *retBuffer = Con::getReturnBuffer(selItems.size() * 4);
  103. dMemset(retBuffer, 0, selItems.size() * 4);
  104. Vector<S32>::iterator i = selItems.begin();
  105. for (; i != selItems.end(); i++)
  106. {
  107. UTF8 retFormat[12];
  108. dSprintf(retFormat, 12, "%d ", (*i));
  109. dStrcat(retBuffer, retFormat);
  110. }
  111. return retBuffer;
  112. }
  113. /*! Finds all items with the given text.
  114. @param itemText The text to search for.
  115. @param caseSensitive If true, a case-sensitive search will be performed.
  116. @return A space-delimited list of matching item indexes or -1 if no items are found.
  117. */
  118. ConsoleMethodWithDocs(GuiListBoxCtrl, findItemText, ConsoleInt, 3, 4, "(string itemText, bool caseSensitive)")
  119. {
  120. bool bCaseSensitive = false;
  121. if (argc == 4)
  122. bCaseSensitive = dAtob(argv[3]);
  123. return object->findItemText(argv[2], bCaseSensitive);
  124. }
  125. /*! Selects the item at the given index.
  126. @param index The zero-based index of the item that should be selected. Passing a -1 clears the selection.
  127. @return No return value.
  128. */
  129. ConsoleMethodWithDocs(GuiListBoxCtrl, setCurSel, ConsoleVoid, 3, 3, "(S32 index)")
  130. {
  131. object->setCurSel(dAtoi(argv[2]));
  132. }
  133. /*! Selects a range of items from a start index to the end or an ending index.
  134. @param start The zero-based index of the start of the selected range.
  135. @param stop The optional, zero-based index of the end of the selected range. If no stop is given, the selection will go to the end of the list.
  136. @return No return value.
  137. */
  138. ConsoleMethodWithDocs(GuiListBoxCtrl, setCurSelRange, ConsoleVoid, 3, 4, "(S32 start, [S32 stop])")
  139. {
  140. if (argc == 4)
  141. object->setCurSelRange(dAtoi(argv[2]), dAtoi(argv[3]));
  142. else
  143. object->setCurSelRange(dAtoi(argv[2]), 999999);
  144. }
  145. /*! Adds an item to the end of the list with an optional color box.
  146. @param text The text of the new item.
  147. @param color The optional color of a color box that will appear to the left of the text. Values range between 0 and 255.
  148. @return No return value.
  149. */
  150. ConsoleMethodWithDocs(GuiListBoxCtrl, addItem, ConsoleVoid, 3, 4, "(string text, [color red / green / blue / [alpha]])")
  151. {
  152. if (argc == 3)
  153. {
  154. object->addItem(argv[2]);
  155. }
  156. else if (argc == 4)
  157. {
  158. const U32 colorCount = Utility::mGetStringElementCount(argv[3]);
  159. if (colorCount != 4 && colorCount != 3)
  160. {
  161. Con::warnf("GuiListBoxCtrl::addItem() - Invalid color! Colors require three or four values (red / green / blue / [alpha])!");
  162. return;
  163. }
  164. F32 red, green, blue, alpha;
  165. red = dAtof(Utility::mGetStringElement(argv[3], 0));
  166. green = dAtof(Utility::mGetStringElement(argv[3], 1));
  167. blue = dAtof(Utility::mGetStringElement(argv[3], 2));
  168. if (colorCount == 3)
  169. {
  170. alpha = 255;
  171. }
  172. else
  173. {
  174. alpha = dAtof(Utility::mGetStringElement(argv[3], 3));
  175. }
  176. object->addItemWithColor(argv[2], ColorI(red, green, blue, alpha));
  177. }
  178. else
  179. {
  180. Con::warnf("GuiListBoxCtrl::addItem() - Invalid number of parameters!");
  181. }
  182. }
  183. /*! Sets the color of the color box at the given index.
  184. @param index The zero-based index of the item that should have a color box.
  185. @param color The color of a color box that will appear to the left of the text. Values range between 0 and 255.
  186. @return No return value.
  187. */
  188. ConsoleMethodWithDocs(GuiListBoxCtrl, setItemColor, ConsoleVoid, 4, 4, "(S32 index, color red / green / blue / [alpha])")
  189. {
  190. if (argc == 4)
  191. {
  192. const U32 colorCount = Utility::mGetStringElementCount(argv[3]);
  193. if (colorCount != 4 && colorCount != 3)
  194. {
  195. Con::warnf("GuiListBoxCtrl::setItemColor() - Invalid color! Colors require three or four values (red / green / blue / [alpha])!");
  196. return;
  197. }
  198. F32 red, green, blue, alpha;
  199. red = dAtof(Utility::mGetStringElement(argv[3], 0));
  200. green = dAtof(Utility::mGetStringElement(argv[3], 1));
  201. blue = dAtof(Utility::mGetStringElement(argv[3], 2));
  202. if (colorCount == 3)
  203. {
  204. alpha = 255;
  205. }
  206. else
  207. {
  208. alpha = dAtof(Utility::mGetStringElement(argv[3], 3));
  209. }
  210. object->setItemColor(dAtoi(argv[2]), ColorI(red, green, blue, alpha));
  211. }
  212. else
  213. {
  214. Con::warnf("GuiListBoxCtrl::setItemColor() - Invalid number of parameters!");
  215. }
  216. }
  217. /*! Clears the color of the color box at the given index.
  218. @param index The zero-based index of the item's color box.
  219. @return No return value.
  220. */
  221. ConsoleMethodWithDocs(GuiListBoxCtrl, clearItemColor, ConsoleVoid, 3, 3, "(S32 index)")
  222. {
  223. object->clearItemColor(dAtoi(argv[2]));
  224. }
  225. /*! Clears the color boxes on all items.
  226. @return No return value.
  227. */
  228. ConsoleMethodWithDocs(GuiListBoxCtrl, clearAllColors, ConsoleVoid, 2, 2, "()")
  229. {
  230. object->clearAllColors();
  231. }
  232. /*! Inserts an item into the list at the specified index.
  233. @param index The zero-based index of the new location of the item.
  234. @param text The text of the new item.
  235. @return Returns the index assigned or -1 on error.
  236. */
  237. ConsoleMethodWithDocs(GuiListBoxCtrl, insertItem, ConsoleVoid, 4, 4, "(S32 index, string text)")
  238. {
  239. object->insertItem(dAtoi(argv[2]), argv[3]);
  240. }
  241. /*! Removes the item at the given index.
  242. @param index The zero-based index of the item to delete.
  243. @return No return value.
  244. */
  245. ConsoleMethodWithDocs(GuiListBoxCtrl, deleteItem, ConsoleVoid, 3, 3, "(S32 index)")
  246. {
  247. object->deleteItem(dAtoi(argv[2]));
  248. }
  249. /*! Returns the text of the given item index.
  250. @param index The zero-based index of the item to inspect.
  251. @return The text of the inspected item.
  252. */
  253. ConsoleMethodWithDocs(GuiListBoxCtrl, getItemText, ConsoleString, 3, 3, "(S32 index)")
  254. {
  255. return object->getItemText(dAtoi(argv[2]));
  256. }
  257. /*! Sets the text of the item at the given item index.
  258. @param index The zero-based index of the item that will be updated.
  259. @param text The text value to update with.
  260. @return No return value.
  261. */
  262. ConsoleMethodWithDocs(GuiListBoxCtrl, setItemText, ConsoleVoid, 4, 4, "(S32 index, string text)")
  263. {
  264. object->setItemText(dAtoi(argv[2]), argv[3]);
  265. }
  266. ConsoleMethodGroupEndWithDocs(GuiListBoxCtrl)