Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

OptionButton

Inherits: Button < BaseButton < Control < CanvasItem < Node < Object

A button that brings up a dropdown with selectable options when pressed.

描述

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.

Note: The ID values used for items are limited to 32 bits, not full 64 bits of int. This has a range of -2^32 to 2^32 - 1, i.e. -2147483648 to 2147483647.

Note: The Button.text and Button.icon properties are set automatically based on the selected item. They shouldn't be changed manually.

属性

ActionMode

action_mode

0 (overrides BaseButton)

HorizontalAlignment

alignment

0 (overrides Button)

bool

allow_reselect

false

bool

fit_to_longest_item

true

int

item_count

0

int

selected

-1

bool

toggle_mode

true (overrides BaseButton)

Methods

void

add_icon_item ( Texture2D texture, String label, int id=-1 )

void

add_item ( String label, int id=-1 )

void

add_separator ( String text="" )

void

clear ( )

Texture2D

get_item_icon ( int idx ) const

int

get_item_id ( int idx ) const

int

get_item_index ( int id ) const

Variant

get_item_metadata ( int idx ) const

String

get_item_text ( int idx ) const

String

get_item_tooltip ( int idx ) const

PopupMenu

get_popup ( ) const

int

get_selectable_item ( bool from_last=false ) const

int

get_selected_id ( ) const

Variant

get_selected_metadata ( ) const

bool

has_selectable_items ( ) const

bool

is_item_disabled ( int idx ) const

bool

is_item_separator ( int idx ) const

void

remove_item ( int idx )

void

select ( int idx )

void

set_disable_shortcuts ( bool disabled )

void

set_item_disabled ( int idx, bool disabled )

void

set_item_icon ( int idx, Texture2D texture )

void

set_item_id ( int idx, int id )

void

set_item_metadata ( int idx, Variant metadata )

void

set_item_text ( int idx, String text )

void

set_item_tooltip ( int idx, String tooltip )

void

show_popup ( )

Theme Properties

Color

font_color

Color(0.875, 0.875, 0.875, 1)

Color

font_disabled_color

Color(0.875, 0.875, 0.875, 0.5)

Color

font_focus_color

Color(0.95, 0.95, 0.95, 1)

Color

font_hover_color

Color(0.95, 0.95, 0.95, 1)

Color

font_hover_pressed_color

Color(1, 1, 1, 1)

Color

font_outline_color

Color(1, 1, 1, 1)

Color

font_pressed_color

Color(1, 1, 1, 1)

int

arrow_margin

4

int

h_separation

4

int

modulate_arrow

0

int

outline_size

0

Font

font

int

font_size

Texture2D

arrow

StyleBox

disabled

StyleBox

disabled_mirrored

StyleBox

focus

StyleBox

hover

StyleBox

hover_mirrored

StyleBox

normal

StyleBox

normal_mirrored

StyleBox

pressed

StyleBox

pressed_mirrored


信号

item_focused ( int index )

Emitted when the user navigates to an item using the ProjectSettings.input/ui_up or ProjectSettings.input/ui_down input actions. The index of the item selected is passed as argument.


item_selected ( int index )

Emitted when the current item has been changed by the user. The index of the item selected is passed as argument.

allow_reselect must be enabled to reselect an item.


Property Descriptions

bool allow_reselect = false

  • void set_allow_reselect ( bool value )

  • bool get_allow_reselect ( )

If true, the currently selected item can be selected again.


bool fit_to_longest_item = true

  • void set_fit_to_longest_item ( bool value )

  • bool is_fit_to_longest_item ( )

If true, minimum size will be determined by the longest item's text, instead of the currently selected one's.

Note: For performance reasons, the minimum size doesn't update immediately when adding, removing or modifying items.


int item_count = 0

  • void set_item_count ( int value )

  • int get_item_count ( )

The number of items to select from.


int selected = -1

  • int get_selected ( )

The index of the currently selected item, or -1 if no item is selected.


Method Descriptions

void add_icon_item ( Texture2D texture, String label, int id=-1 )

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.


void add_item ( String label, int id=-1 )

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.


void add_separator ( String text="" )

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.


void clear ( )

Clears all the items in the OptionButton.


Texture2D get_item_icon ( int idx ) const

Returns the icon of the item at index idx.


int get_item_id ( int idx ) const

Returns the ID of the item at index idx.


int get_item_index ( int id ) const

Returns the index of the item with the given id.


Variant get_item_metadata ( int idx ) const

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.


String get_item_text ( int idx ) const

Returns the text of the item at index idx.


String get_item_tooltip ( int idx ) const

Returns the tooltip of the item at index idx.


PopupMenu get_popup ( ) const

Returns the PopupMenu contained in this button.

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 Window.visible property.


int get_selectable_item ( bool from_last=false ) const

Returns the index of the first item which is not disabled, or marked as a separator. If from_last is true, the items will be searched in reverse order.

Returns -1 if no item is found.


int get_selected_id ( ) const

Returns the ID of the selected item, or -1 if no item is selected.


Variant get_selected_metadata ( ) const

Gets the metadata of the selected item. Metadata for items can be set using set_item_metadata.


bool has_selectable_items ( ) const

Returns true if this button contains at least one item which is not disabled, or marked as a separator.


bool is_item_disabled ( int idx ) const

Returns true if the item at index idx is disabled.


bool is_item_separator ( int idx ) const

Returns true if the item at index idx is marked as a separator.


void remove_item ( int idx )

Removes the item at index idx.


void select ( int idx )

Selects an item by index and makes it the current item. This will work even if the item is disabled.

Passing -1 as the index deselects any currently selected item.


void set_disable_shortcuts ( bool disabled )

If true, shortcuts are disabled and cannot be used to trigger the button.


void set_item_disabled ( int idx, bool disabled )

Sets whether the item at index idx is disabled.

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.


void set_item_icon ( int idx, Texture2D texture )

Sets the icon of the item at index idx.


void set_item_id ( int idx, int id )

Sets the ID of the item at index idx.


void set_item_metadata ( int idx, Variant metadata )

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.


void set_item_text ( int idx, String text )

Sets the text of the item at index idx.


void set_item_tooltip ( int idx, String tooltip )

Sets the tooltip of the item at index idx.


void show_popup ( )

Adjusts popup position and sizing for the OptionButton, then shows the PopupMenu. Prefer this over using get_popup().popup().


Theme Property Descriptions

Color font_color = Color(0.875, 0.875, 0.875, 1)

Default text Color of the OptionButton.


Color font_disabled_color = Color(0.875, 0.875, 0.875, 0.5)

Text Color used when the OptionButton is disabled.


Color font_focus_color = Color(0.95, 0.95, 0.95, 1)

Text 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.


Color font_hover_color = Color(0.95, 0.95, 0.95, 1)

Text Color used when the OptionButton is being hovered.


Color font_hover_pressed_color = Color(1, 1, 1, 1)

Text Color used when the OptionButton is being hovered and pressed.


Color font_outline_color = Color(1, 1, 1, 1)

The tint of text outline of the OptionButton.


Color font_pressed_color = Color(1, 1, 1, 1)

Text Color used when the OptionButton is being pressed.


int arrow_margin = 4

The horizontal space between the arrow icon and the right edge of the button.


int h_separation = 4

The horizontal space between OptionButton's icon and text. Negative values will be treated as 0 when used.


int modulate_arrow = 0

If different than 0, the arrow icon will be modulated to the font color.


int outline_size = 0

The size of the text outline.

Note: If using a font with FontFile.multichannel_signed_distance_field enabled, its FontFile.msdf_pixel_range must be set to at least twice the value of outline_size for outline rendering to look correct. Otherwise, the outline may appear to be cut off earlier than intended.


Font font

Font of the OptionButton's text.


int font_size

Font size of the OptionButton's text.


Texture2D arrow

The arrow icon to be drawn on the right end of the button.


StyleBox disabled

StyleBox used when the OptionButton is disabled (for left-to-right layouts).


StyleBox disabled_mirrored

StyleBox used when the OptionButton is disabled (for right-to-left layouts).


StyleBox focus

StyleBox used when the OptionButton is focused. The focus StyleBox is displayed over the base StyleBox, so a partially transparent StyleBox should be used to ensure the base StyleBox remains visible. A StyleBox that represents an outline or an underline works well for this purpose. To disable the focus visual effect, assign a StyleBoxEmpty resource. Note that disabling the focus visual effect will harm keyboard/controller navigation usability, so this is not recommended for accessibility reasons.


StyleBox hover

StyleBox used when the OptionButton is being hovered (for left-to-right layouts).


StyleBox hover_mirrored

StyleBox used when the OptionButton is being hovered (for right-to-left layouts).


StyleBox normal

Default StyleBox for the OptionButton (for left-to-right layouts).


StyleBox normal_mirrored

Default StyleBox for the OptionButton (for right-to-left layouts).


StyleBox pressed

StyleBox used when the OptionButton is being pressed (for left-to-right layouts).


StyleBox pressed_mirrored

StyleBox used when the OptionButton is being pressed (for right-to-left layouts).