OptionButton
A button that brings up a dropdown with selectable options when pressed.
OptionButton.swift:24class OptionButton
OptionButton
is a type of button that brings up a dropdown with selectable items when pressed. The item selected becomes the “current” item and is displayed as the button text.
See also BaseButton
which contains common properties and methods associated with this node.
This object emits the following signals:
Superclasses
class Button
A themed button that can contain text and an icon.
Citizens in SwiftGodot
Conformances
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol Identifiable<ID>
A class of types whose instances hold the value of an entity with stable identity.
protocol VariantRepresentable
Types that conform to VariantRepresentable can be stored directly in
Variant
with no conversion. These include all of the Variant types from Godot (for exampleGString
,Rect
,Plane
), Godot objects (those that subclass SwiftGodot.Object) as well as the built-in Swift types UInt8, Int64 and Double.protocol VariantStorable
Types that conform to VariantStorable can be stored in a Variant and can be extracted back out of a Variant.
Types
class Signal1
Signal support.
class Signal2
Signal support.
Type members
Instance members
var allowReselect: Bool
If
true
, the currently selected item can be selected again.var fitToLongestItem: Bool
If
true
, minimum size will be determined by the longest item’s text, instead of the currently selected one’s.var itemCount: Int32
The number of items to select from.
var itemFocused: Signal2
Emitted when the user navigates to an item using the
ProjectSettings/input/uiUp
orProjectSettings/input/uiDown
input actions. The index of the item selected is passed as argument.var itemSelected: Signal1
Emitted when the current item has been changed by the user. The index of the item selected is passed as argument.
func addIconItem(texture: Texture2D?, label: String, id: Int32
) Adds an item, with a
texture
icon, textlabel
and (optionally)id
. If noid
is passed, the item index will be used as the item’s ID. New items are appended at the end.func addItem(label: String, id: Int32
) Adds an item, with text
label
and (optionally)id
. If noid
is passed, the item index will be used as the item’s ID. New items are appended at the end.func addSeparator(text: String
) Adds a separator to the list of items. Separators help to group items, and can optionally be given a
text
header. A separator also gets an index assigned, and is appended at the end of the item list.func clear(
) Clears all the items in the
OptionButton
.func getItemIcon(idx: Int32
) -> Texture2D? Returns the icon of the item at index
idx
.func getItemId(idx: Int32
) -> Int32 Returns the ID of the item at index
idx
.func getItemIndex(id: Int32
) -> Int32 Returns the index of the item with the given
id
.func getItemMetadata(idx: Int32
) -> Variant 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.
func getItemText(idx: Int32
) -> String Returns the text of the item at index
idx
.func getItemTooltip(idx: Int32
) -> String Returns the tooltip of the item at index
idx
.func getPopup(
) -> PopupMenu? Returns the
PopupMenu
contained in this button.func getSelectableItem(fromLast: Bool
) -> Int32 Returns the index of the first item which is not disabled, or marked as a separator. If
fromLast
istrue
, the items will be searched in reverse order.func getSelected(
) -> Int32 func getSelectedId(
) -> Int32 Returns the ID of the selected item, or
-1
if no item is selected.func getSelectedMetadata(
) -> Variant Gets the metadata of the selected item. Metadata for items can be set using
setItemMetadata(idx:metadata:)
.func hasSelectableItems(
) -> Bool Returns
true
if this button contains at least one item which is not disabled, or marked as a separator.func isItemDisabled(idx: Int32
) -> Bool Returns
true
if the item at indexidx
is disabled.func isItemSeparator(idx: Int32
) -> Bool Returns
true
if the item at indexidx
is marked as a separator.func removeItem(idx: Int32
) Removes the item at index
idx
.func select(idx: Int32
) Selects an item by index and makes it the current item. This will work even if the item is disabled.
func setDisableShortcuts(disabled: Bool
) If
true
, shortcuts are disabled and cannot be used to trigger the button.func setItemDisabled(idx: Int32, disabled: Bool
) Sets whether the item at index
idx
is disabled.func setItemIcon(idx: Int32, texture: Texture2D?
) Sets the icon of the item at index
idx
.func setItemId(idx: Int32, id: Int32
) Sets the ID of the item at index
idx
.func setItemMetadata(idx: Int32, metadata: Variant
) 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.
func setItemText(idx: Int32, text: String
) Sets the text of the item at index
idx
.func setItemTooltip(idx: Int32, tooltip: String
) Sets the tooltip of the item at index
idx
.func showPopup(
) Adjusts popup position and sizing for the
OptionButton
, then shows thePopupMenu
. Prefer this over usingget_popup().popup()
.