class_optionbutton.rst 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/make_rst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the OptionButton.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_OptionButton:
  6. OptionButton
  7. ============
  8. **Inherits:** :ref:`Button<class_Button>` **<** :ref:`BaseButton<class_BaseButton>` **<** :ref:`Control<class_Control>` **<** :ref:`CanvasItem<class_CanvasItem>` **<** :ref:`Node<class_Node>` **<** :ref:`Object<class_Object>`
  9. Button control that provides selectable options when pressed.
  10. Description
  11. -----------
  12. OptionButton is a type button that provides a selectable list of items when pressed. The item selected becomes the "current" item and is displayed as the button text.
  13. See also :ref:`BaseButton<class_BaseButton>` which contains common properties and methods associated with this node.
  14. Properties
  15. ----------
  16. +-----------------------------------------------+-------------------------------------------------------+------------------------------+
  17. | :ref:`ActionMode<enum_BaseButton_ActionMode>` | action_mode | ``0`` *(parent override)* |
  18. +-----------------------------------------------+-------------------------------------------------------+------------------------------+
  19. | :ref:`TextAlign<enum_Button_TextAlign>` | align | ``0`` *(parent override)* |
  20. +-----------------------------------------------+-------------------------------------------------------+------------------------------+
  21. | :ref:`int<class_int>` | :ref:`selected<class_OptionButton_property_selected>` | ``-1`` |
  22. +-----------------------------------------------+-------------------------------------------------------+------------------------------+
  23. | :ref:`bool<class_bool>` | toggle_mode | ``true`` *(parent override)* |
  24. +-----------------------------------------------+-------------------------------------------------------+------------------------------+
  25. Methods
  26. -------
  27. +-----------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  28. | void | :ref:`add_icon_item<class_OptionButton_method_add_icon_item>` **(** :ref:`Texture<class_Texture>` texture, :ref:`String<class_String>` label, :ref:`int<class_int>` id=-1 **)** |
  29. +-----------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  30. | void | :ref:`add_item<class_OptionButton_method_add_item>` **(** :ref:`String<class_String>` label, :ref:`int<class_int>` id=-1 **)** |
  31. +-----------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  32. | void | :ref:`add_separator<class_OptionButton_method_add_separator>` **(** **)** |
  33. +-----------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  34. | void | :ref:`clear<class_OptionButton_method_clear>` **(** **)** |
  35. +-----------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  36. | :ref:`int<class_int>` | :ref:`get_item_count<class_OptionButton_method_get_item_count>` **(** **)** |const| |
  37. +-----------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  38. | :ref:`Texture<class_Texture>` | :ref:`get_item_icon<class_OptionButton_method_get_item_icon>` **(** :ref:`int<class_int>` idx **)** |const| |
  39. +-----------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  40. | :ref:`int<class_int>` | :ref:`get_item_id<class_OptionButton_method_get_item_id>` **(** :ref:`int<class_int>` idx **)** |const| |
  41. +-----------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  42. | :ref:`int<class_int>` | :ref:`get_item_index<class_OptionButton_method_get_item_index>` **(** :ref:`int<class_int>` id **)** |const| |
  43. +-----------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  44. | :ref:`Variant<class_Variant>` | :ref:`get_item_metadata<class_OptionButton_method_get_item_metadata>` **(** :ref:`int<class_int>` idx **)** |const| |
  45. +-----------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  46. | :ref:`String<class_String>` | :ref:`get_item_text<class_OptionButton_method_get_item_text>` **(** :ref:`int<class_int>` idx **)** |const| |
  47. +-----------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  48. | :ref:`PopupMenu<class_PopupMenu>` | :ref:`get_popup<class_OptionButton_method_get_popup>` **(** **)** |const| |
  49. +-----------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  50. | :ref:`int<class_int>` | :ref:`get_selected_id<class_OptionButton_method_get_selected_id>` **(** **)** |const| |
  51. +-----------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  52. | :ref:`Variant<class_Variant>` | :ref:`get_selected_metadata<class_OptionButton_method_get_selected_metadata>` **(** **)** |const| |
  53. +-----------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  54. | :ref:`bool<class_bool>` | :ref:`is_item_disabled<class_OptionButton_method_is_item_disabled>` **(** :ref:`int<class_int>` idx **)** |const| |
  55. +-----------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  56. | void | :ref:`remove_item<class_OptionButton_method_remove_item>` **(** :ref:`int<class_int>` idx **)** |
  57. +-----------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  58. | void | :ref:`select<class_OptionButton_method_select>` **(** :ref:`int<class_int>` idx **)** |
  59. +-----------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  60. | void | :ref:`set_item_disabled<class_OptionButton_method_set_item_disabled>` **(** :ref:`int<class_int>` idx, :ref:`bool<class_bool>` disabled **)** |
  61. +-----------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  62. | void | :ref:`set_item_icon<class_OptionButton_method_set_item_icon>` **(** :ref:`int<class_int>` idx, :ref:`Texture<class_Texture>` texture **)** |
  63. +-----------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  64. | void | :ref:`set_item_id<class_OptionButton_method_set_item_id>` **(** :ref:`int<class_int>` idx, :ref:`int<class_int>` id **)** |
  65. +-----------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  66. | void | :ref:`set_item_metadata<class_OptionButton_method_set_item_metadata>` **(** :ref:`int<class_int>` idx, :ref:`Variant<class_Variant>` metadata **)** |
  67. +-----------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  68. | void | :ref:`set_item_text<class_OptionButton_method_set_item_text>` **(** :ref:`int<class_int>` idx, :ref:`String<class_String>` text **)** |
  69. +-----------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  70. Theme Properties
  71. ----------------
  72. +---------------------------------+--------------------------------------------------------------------------------+----------------------------------+
  73. | :ref:`Color<class_Color>` | :ref:`font_color<class_OptionButton_theme_color_font_color>` | ``Color( 0.88, 0.88, 0.88, 1 )`` |
  74. +---------------------------------+--------------------------------------------------------------------------------+----------------------------------+
  75. | :ref:`Color<class_Color>` | :ref:`font_color_disabled<class_OptionButton_theme_color_font_color_disabled>` | ``Color( 0.9, 0.9, 0.9, 0.2 )`` |
  76. +---------------------------------+--------------------------------------------------------------------------------+----------------------------------+
  77. | :ref:`Color<class_Color>` | :ref:`font_color_focus<class_OptionButton_theme_color_font_color_focus>` | ``Color( 0.94, 0.94, 0.94, 1 )`` |
  78. +---------------------------------+--------------------------------------------------------------------------------+----------------------------------+
  79. | :ref:`Color<class_Color>` | :ref:`font_color_hover<class_OptionButton_theme_color_font_color_hover>` | ``Color( 0.94, 0.94, 0.94, 1 )`` |
  80. +---------------------------------+--------------------------------------------------------------------------------+----------------------------------+
  81. | :ref:`Color<class_Color>` | :ref:`font_color_pressed<class_OptionButton_theme_color_font_color_pressed>` | ``Color( 1, 1, 1, 1 )`` |
  82. +---------------------------------+--------------------------------------------------------------------------------+----------------------------------+
  83. | :ref:`int<class_int>` | :ref:`arrow_margin<class_OptionButton_theme_constant_arrow_margin>` | ``2`` |
  84. +---------------------------------+--------------------------------------------------------------------------------+----------------------------------+
  85. | :ref:`int<class_int>` | :ref:`hseparation<class_OptionButton_theme_constant_hseparation>` | ``2`` |
  86. +---------------------------------+--------------------------------------------------------------------------------+----------------------------------+
  87. | :ref:`Font<class_Font>` | :ref:`font<class_OptionButton_theme_font_font>` | |
  88. +---------------------------------+--------------------------------------------------------------------------------+----------------------------------+
  89. | :ref:`Texture<class_Texture>` | :ref:`arrow<class_OptionButton_theme_icon_arrow>` | |
  90. +---------------------------------+--------------------------------------------------------------------------------+----------------------------------+
  91. | :ref:`StyleBox<class_StyleBox>` | :ref:`disabled<class_OptionButton_theme_style_disabled>` | |
  92. +---------------------------------+--------------------------------------------------------------------------------+----------------------------------+
  93. | :ref:`StyleBox<class_StyleBox>` | :ref:`focus<class_OptionButton_theme_style_focus>` | |
  94. +---------------------------------+--------------------------------------------------------------------------------+----------------------------------+
  95. | :ref:`StyleBox<class_StyleBox>` | :ref:`hover<class_OptionButton_theme_style_hover>` | |
  96. +---------------------------------+--------------------------------------------------------------------------------+----------------------------------+
  97. | :ref:`StyleBox<class_StyleBox>` | :ref:`normal<class_OptionButton_theme_style_normal>` | |
  98. +---------------------------------+--------------------------------------------------------------------------------+----------------------------------+
  99. | :ref:`StyleBox<class_StyleBox>` | :ref:`pressed<class_OptionButton_theme_style_pressed>` | |
  100. +---------------------------------+--------------------------------------------------------------------------------+----------------------------------+
  101. Signals
  102. -------
  103. .. _class_OptionButton_signal_item_focused:
  104. - **item_focused** **(** :ref:`int<class_int>` index **)**
  105. Emitted when the user navigates to an item using the ``ui_up`` or ``ui_down`` actions. The index of the item selected is passed as argument.
  106. ----
  107. .. _class_OptionButton_signal_item_selected:
  108. - **item_selected** **(** :ref:`int<class_int>` index **)**
  109. Emitted when the current item has been changed by the user. The index of the item selected is passed as argument.
  110. Property Descriptions
  111. ---------------------
  112. .. _class_OptionButton_property_selected:
  113. - :ref:`int<class_int>` **selected**
  114. +-----------+----------------+
  115. | *Default* | ``-1`` |
  116. +-----------+----------------+
  117. | *Getter* | get_selected() |
  118. +-----------+----------------+
  119. The index of the currently selected item, or ``-1`` if no item is selected.
  120. Method Descriptions
  121. -------------------
  122. .. _class_OptionButton_method_add_icon_item:
  123. - void **add_icon_item** **(** :ref:`Texture<class_Texture>` texture, :ref:`String<class_String>` label, :ref:`int<class_int>` id=-1 **)**
  124. Adds an item, with a ``texture`` icon, text ``label`` and (optionally) ``id``. If no ``id`` is passed, the item index will be used as the item's ID. New items are appended at the end.
  125. ----
  126. .. _class_OptionButton_method_add_item:
  127. - void **add_item** **(** :ref:`String<class_String>` label, :ref:`int<class_int>` id=-1 **)**
  128. Adds an item, with text ``label`` and (optionally) ``id``. If no ``id`` is passed, the item index will be used as the item's ID. New items are appended at the end.
  129. ----
  130. .. _class_OptionButton_method_add_separator:
  131. - void **add_separator** **(** **)**
  132. Adds a separator to the list of items. Separators help to group items. Separator also takes up an index and is appended at the end.
  133. ----
  134. .. _class_OptionButton_method_clear:
  135. - void **clear** **(** **)**
  136. Clears all the items in the ``OptionButton``.
  137. ----
  138. .. _class_OptionButton_method_get_item_count:
  139. - :ref:`int<class_int>` **get_item_count** **(** **)** |const|
  140. Returns the amount of items in the OptionButton, including separators.
  141. ----
  142. .. _class_OptionButton_method_get_item_icon:
  143. - :ref:`Texture<class_Texture>` **get_item_icon** **(** :ref:`int<class_int>` idx **)** |const|
  144. Returns the icon of the item at index ``idx``.
  145. ----
  146. .. _class_OptionButton_method_get_item_id:
  147. - :ref:`int<class_int>` **get_item_id** **(** :ref:`int<class_int>` idx **)** |const|
  148. Returns the ID of the item at index ``idx``.
  149. ----
  150. .. _class_OptionButton_method_get_item_index:
  151. - :ref:`int<class_int>` **get_item_index** **(** :ref:`int<class_int>` id **)** |const|
  152. Returns the index of the item with the given ``id``.
  153. ----
  154. .. _class_OptionButton_method_get_item_metadata:
  155. - :ref:`Variant<class_Variant>` **get_item_metadata** **(** :ref:`int<class_int>` idx **)** |const|
  156. Retrieves the metadata of an item. Metadata may be any type and can be used to store extra information about an item, such as an external string ID.
  157. ----
  158. .. _class_OptionButton_method_get_item_text:
  159. - :ref:`String<class_String>` **get_item_text** **(** :ref:`int<class_int>` idx **)** |const|
  160. Returns the text of the item at index ``idx``.
  161. ----
  162. .. _class_OptionButton_method_get_popup:
  163. - :ref:`PopupMenu<class_PopupMenu>` **get_popup** **(** **)** |const|
  164. Returns the :ref:`PopupMenu<class_PopupMenu>` contained in this button.
  165. **Warning:** This is a required internal node, removing and freeing it may cause a crash. If you wish to hide it or any of its children, use their :ref:`CanvasItem.visible<class_CanvasItem_property_visible>` property.
  166. ----
  167. .. _class_OptionButton_method_get_selected_id:
  168. - :ref:`int<class_int>` **get_selected_id** **(** **)** |const|
  169. Returns the ID of the selected item, or ``0`` if no item is selected.
  170. ----
  171. .. _class_OptionButton_method_get_selected_metadata:
  172. - :ref:`Variant<class_Variant>` **get_selected_metadata** **(** **)** |const|
  173. Gets the metadata of the selected item. Metadata for items can be set using :ref:`set_item_metadata<class_OptionButton_method_set_item_metadata>`.
  174. ----
  175. .. _class_OptionButton_method_is_item_disabled:
  176. - :ref:`bool<class_bool>` **is_item_disabled** **(** :ref:`int<class_int>` idx **)** |const|
  177. Returns ``true`` if the item at index ``idx`` is disabled.
  178. ----
  179. .. _class_OptionButton_method_remove_item:
  180. - void **remove_item** **(** :ref:`int<class_int>` idx **)**
  181. Removes the item at index ``idx``.
  182. ----
  183. .. _class_OptionButton_method_select:
  184. - void **select** **(** :ref:`int<class_int>` idx **)**
  185. Selects an item by index and makes it the current item. This will work even if the item is disabled.
  186. ----
  187. .. _class_OptionButton_method_set_item_disabled:
  188. - void **set_item_disabled** **(** :ref:`int<class_int>` idx, :ref:`bool<class_bool>` disabled **)**
  189. Sets whether the item at index ``idx`` is disabled.
  190. Disabled items are drawn differently in the dropdown and are not selectable by the user. If the current selected item is set as disabled, it will remain selected.
  191. ----
  192. .. _class_OptionButton_method_set_item_icon:
  193. - void **set_item_icon** **(** :ref:`int<class_int>` idx, :ref:`Texture<class_Texture>` texture **)**
  194. Sets the icon of the item at index ``idx``.
  195. ----
  196. .. _class_OptionButton_method_set_item_id:
  197. - void **set_item_id** **(** :ref:`int<class_int>` idx, :ref:`int<class_int>` id **)**
  198. Sets the ID of the item at index ``idx``.
  199. ----
  200. .. _class_OptionButton_method_set_item_metadata:
  201. - void **set_item_metadata** **(** :ref:`int<class_int>` idx, :ref:`Variant<class_Variant>` metadata **)**
  202. Sets the metadata of an item. Metadata may be of any type and can be used to store extra information about an item, such as an external string ID.
  203. ----
  204. .. _class_OptionButton_method_set_item_text:
  205. - void **set_item_text** **(** :ref:`int<class_int>` idx, :ref:`String<class_String>` text **)**
  206. Sets the text of the item at index ``idx``.
  207. Theme Property Descriptions
  208. ---------------------------
  209. .. _class_OptionButton_theme_color_font_color:
  210. - :ref:`Color<class_Color>` **font_color**
  211. +-----------+----------------------------------+
  212. | *Default* | ``Color( 0.88, 0.88, 0.88, 1 )`` |
  213. +-----------+----------------------------------+
  214. Default text :ref:`Color<class_Color>` of the ``OptionButton``.
  215. ----
  216. .. _class_OptionButton_theme_color_font_color_disabled:
  217. - :ref:`Color<class_Color>` **font_color_disabled**
  218. +-----------+---------------------------------+
  219. | *Default* | ``Color( 0.9, 0.9, 0.9, 0.2 )`` |
  220. +-----------+---------------------------------+
  221. Text :ref:`Color<class_Color>` used when the ``OptionButton`` is disabled.
  222. ----
  223. .. _class_OptionButton_theme_color_font_color_focus:
  224. - :ref:`Color<class_Color>` **font_color_focus**
  225. +-----------+----------------------------------+
  226. | *Default* | ``Color( 0.94, 0.94, 0.94, 1 )`` |
  227. +-----------+----------------------------------+
  228. Text :ref:`Color<class_Color>` used when the ``OptionButton`` is focused. Only replaces the normal text color of the button. Disabled, hovered, and pressed states take precedence over this color.
  229. ----
  230. .. _class_OptionButton_theme_color_font_color_hover:
  231. - :ref:`Color<class_Color>` **font_color_hover**
  232. +-----------+----------------------------------+
  233. | *Default* | ``Color( 0.94, 0.94, 0.94, 1 )`` |
  234. +-----------+----------------------------------+
  235. Text :ref:`Color<class_Color>` used when the ``OptionButton`` is being hovered.
  236. ----
  237. .. _class_OptionButton_theme_color_font_color_pressed:
  238. - :ref:`Color<class_Color>` **font_color_pressed**
  239. +-----------+-------------------------+
  240. | *Default* | ``Color( 1, 1, 1, 1 )`` |
  241. +-----------+-------------------------+
  242. Text :ref:`Color<class_Color>` used when the ``OptionButton`` is being pressed.
  243. ----
  244. .. _class_OptionButton_theme_constant_arrow_margin:
  245. - :ref:`int<class_int>` **arrow_margin**
  246. +-----------+-------+
  247. | *Default* | ``2`` |
  248. +-----------+-------+
  249. The horizontal space between the arrow icon and the right edge of the button.
  250. ----
  251. .. _class_OptionButton_theme_constant_hseparation:
  252. - :ref:`int<class_int>` **hseparation**
  253. +-----------+-------+
  254. | *Default* | ``2`` |
  255. +-----------+-------+
  256. The horizontal space between ``OptionButton``'s icon and text.
  257. ----
  258. .. _class_OptionButton_theme_font_font:
  259. - :ref:`Font<class_Font>` **font**
  260. :ref:`Font<class_Font>` of the ``OptionButton``'s text.
  261. ----
  262. .. _class_OptionButton_theme_icon_arrow:
  263. - :ref:`Texture<class_Texture>` **arrow**
  264. The arrow icon to be drawn on the right end of the button.
  265. ----
  266. .. _class_OptionButton_theme_style_disabled:
  267. - :ref:`StyleBox<class_StyleBox>` **disabled**
  268. :ref:`StyleBox<class_StyleBox>` used when the ``OptionButton`` is disabled.
  269. ----
  270. .. _class_OptionButton_theme_style_focus:
  271. - :ref:`StyleBox<class_StyleBox>` **focus**
  272. :ref:`StyleBox<class_StyleBox>` used when the ``OptionButton`` is focused. It is displayed over the current :ref:`StyleBox<class_StyleBox>`, so using :ref:`StyleBoxEmpty<class_StyleBoxEmpty>` will just disable the focus visual effect.
  273. ----
  274. .. _class_OptionButton_theme_style_hover:
  275. - :ref:`StyleBox<class_StyleBox>` **hover**
  276. :ref:`StyleBox<class_StyleBox>` used when the ``OptionButton`` is being hovered.
  277. ----
  278. .. _class_OptionButton_theme_style_normal:
  279. - :ref:`StyleBox<class_StyleBox>` **normal**
  280. Default :ref:`StyleBox<class_StyleBox>` for the ``OptionButton``.
  281. ----
  282. .. _class_OptionButton_theme_style_pressed:
  283. - :ref:`StyleBox<class_StyleBox>` **pressed**
  284. :ref:`StyleBox<class_StyleBox>` used when the ``OptionButton`` is being pressed.
  285. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  286. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  287. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`