Button
Inherits:BaseButton<Control<CanvasItem<Node<Object
Inherited By:CheckBox,CheckButton,ColorPickerButton,MenuButton,OptionButton
A themed button that can contain text and an icon.
Description
Button is the standard themed button. It can contain text and an icon, and it will display them according to the currentTheme.
Example: Create a button and connect a method that will be called when the button is pressed:
func_ready():varbutton=Button.new()button.text="Click me"button.pressed.connect(_button_pressed)add_child(button)func_button_pressed():print("Hello world!")
publicoverridevoid_Ready(){varbutton=newButton();button.Text="Click me";button.Pressed+=ButtonPressed;AddChild(button);}privatevoidButtonPressed(){GD.Print("Hello world!");}
See alsoBaseButton which contains common properties and methods associated with this node.
Note: Buttons do not detect touch input and therefore don't support multitouch, since mouse emulation can only press one button at a given time. UseTouchScreenButton for buttons that trigger gameplay movement or actions.
Tutorials
Properties
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
|
Theme Properties
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
Property Descriptions
HorizontalAlignmentalignment =1
🔗
HorizontalAlignmentget_text_alignment()
Text alignment policy for the button's text, use one of theHorizontalAlignment constants.
AutowrapModeautowrap_mode =0
🔗
AutowrapModeget_autowrap_mode()
If set to something other thanTextServer.AUTOWRAP_OFF, the text gets wrapped inside the node's bounding rectangle.
boolget_clip_text()
Iftrue
, text that is too large to fit the button is clipped horizontally. Iffalse
, the button will always be wide enough to hold the text. The text is not vertically clipped, and the button's height is not affected by this property.
boolis_expand_icon()
When enabled, the button's icon will expand/shrink to fit the button's size while keeping its aspect. See alsoicon_max_width.
boolis_flat()
Flat buttons don't display decoration.
Texture2Dget_button_icon()
Button's icon, if text is present the icon will be placed before the text.
To edit margin and spacing of the icon, useh_separation theme property andcontent_margin_*
properties of the usedStyleBoxes.
HorizontalAlignmenticon_alignment =0
🔗
HorizontalAlignmentget_icon_alignment()
Specifies if the icon should be aligned horizontally to the left, right, or center of a button. Uses the sameHorizontalAlignment constants as the text alignment. If centered horizontally and vertically, text will draw on top of the icon.
Stringget_language()
Language code used for line-breaking and text shaping algorithms, if left empty current locale is used instead.
Stringget_text()
The button's text that will be displayed inside the button's area.
TextDirectiontext_direction =0
🔗
TextDirectionget_text_direction()
Base text writing direction.
OverrunBehaviortext_overrun_behavior =0
🔗
OverrunBehaviorget_text_overrun_behavior()
Sets the clipping behavior when the text exceeds the node's bounding rectangle. SeeOverrunBehavior for a description of all modes.
VerticalAlignmentvertical_icon_alignment =1
🔗
VerticalAlignmentget_vertical_icon_alignment()
Specifies if the icon should be aligned vertically to the top, bottom, or center of a button. Uses the sameVerticalAlignment constants as the text alignment. If centered horizontally and vertically, text will draw on top of the icon.
Theme Property Descriptions
Colorfont_color =Color(0.875,0.875,0.875,1)
🔗
Default textColor of theButton.
Colorfont_disabled_color =Color(0.875,0.875,0.875,0.5)
🔗
TextColor used when theButton is disabled.
Colorfont_focus_color =Color(0.95,0.95,0.95,1)
🔗
TextColor used when theButton is focused. Only replaces the normal text color of the button. Disabled, hovered, and pressed states take precedence over this color.
Colorfont_hover_color =Color(0.95,0.95,0.95,1)
🔗
TextColor used when theButton is being hovered.
Colorfont_hover_pressed_color =Color(1,1,1,1)
🔗
TextColor used when theButton is being hovered and pressed.
Colorfont_outline_color =Color(0,0,0,1)
🔗
The tint of text outline of theButton.
Colorfont_pressed_color =Color(1,1,1,1)
🔗
TextColor used when theButton is being pressed.
Coloricon_disabled_color =Color(1,1,1,0.4)
🔗
Icon modulateColor used when theButton is disabled.
Coloricon_focus_color =Color(1,1,1,1)
🔗
Icon modulateColor used when theButton is focused. Only replaces the normal modulate color of the button. Disabled, hovered, and pressed states take precedence over this color.
Coloricon_hover_color =Color(1,1,1,1)
🔗
Icon modulateColor used when theButton is being hovered.
Coloricon_hover_pressed_color =Color(1,1,1,1)
🔗
Icon modulateColor used when theButton is being hovered and pressed.
Coloricon_normal_color =Color(1,1,1,1)
🔗
Default icon modulateColor of theButton.
Coloricon_pressed_color =Color(1,1,1,1)
🔗
Icon modulateColor used when theButton is being pressed.
intalign_to_largest_stylebox =0
🔗
This constant acts as a boolean. Iftrue
, the minimum size of the button and text/icon alignment is always based on the largest stylebox margins, otherwise it's based on the current button state stylebox margins.
The horizontal space betweenButton's icon and text. Negative values will be treated as0
when used.
The maximum allowed width of theButton's icon. This limit is applied on top of the default size of the icon, or its expanded size ifexpand_icon istrue
. The height is adjusted according to the icon's ratio. If the button has additional icons (e.g.CheckBox), they will also be limited.
Additional vertical spacing between lines (in pixels), spacing is added to line descent. This value can be negative.
The size of the text outline.
Note: If using a font withFontFile.multichannel_signed_distance_field enabled, itsFontFile.msdf_pixel_range must be set to at leasttwice the value ofoutline_size for outline rendering to look correct. Otherwise, the outline may appear to be cut off earlier than intended.
Font of theButton's text.
Font size of theButton's text.
Default icon for theButton. Appears only ificon is not assigned.
StyleBox used when theButton is disabled.
StyleBox used when theButton is disabled (for right-to-left layouts).
StyleBox used when theButton is focused. ThefocusStyleBox is displayedover the baseStyleBox, so a partially transparentStyleBox should be used to ensure the baseStyleBox remains visible. AStyleBox that represents an outline or an underline works well for this purpose. To disable the focus visual effect, assign aStyleBoxEmpty resource. Note that disabling the focus visual effect will harm keyboard/controller navigation usability, so this is not recommended for accessibility reasons.
StyleBox used when theButton is being hovered.
StyleBox used when theButton is being hovered (for right-to-left layouts).
StyleBox used when theButton is being pressed and hovered at the same time.
StyleBoxhover_pressed_mirrored🔗
StyleBox used when theButton is being pressed and hovered at the same time (for right-to-left layouts).
DefaultStyleBox for theButton.
DefaultStyleBox for theButton (for right-to-left layouts).
StyleBox used when theButton is being pressed.
StyleBox used when theButton is being pressed (for right-to-left layouts).