Button¶
Inherits: BaseButton < Control < CanvasItem < Node < Object
Inherited By: CheckBox, CheckButton, ColorPickerButton, MenuButton, OptionButton
Standard themed Button.
Description¶
Button is the standard themed button. It can contain text and an icon, and will display them according to the current Theme.
Example of creating a button and assigning an action when pressed by code:
func _ready():
var button = Button.new()
button.text = "Click me"
button.connect("pressed", self, "_button_pressed")
add_child(button)
func _button_pressed():
print("Hello world!")
public override void _Ready()
{
var button = new Button();
button.Text = "Click me";
button.Connect("pressed", this, nameof(ButtonPressed));
AddChild(button);
}
private void ButtonPressed()
{
GD.Print("Hello world!");
}
Buttons (like all Control nodes) can also be created in the editor, but some situations may require creating them from code.
See also BaseButton which contains common properties and methods associated with this node.
Note: Buttons do not interpret touch input and therefore don't support multitouch, since mouse emulation can only press one button at a given time. Use TouchScreenButton for buttons that trigger gameplay movement or actions, as TouchScreenButton supports multitouch.
Tutorials¶
Properties¶
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
Methods¶
void |
|
get_opentype_feature ( String tag ) const |
|
void |
set_opentype_feature ( String tag, int value ) |
Theme Properties¶
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
Property Descriptions¶
HorizontalAlignment alignment
Default |
|
Setter |
set_text_alignment(value) |
Getter |
get_text_alignment() |
Text alignment policy for the button's text, use one of the HorizontalAlignment constants.
bool clip_text
Default |
|
Setter |
set_clip_text(value) |
Getter |
get_clip_text() |
When this property is enabled, text that is too large to fit the button is clipped, when disabled the Button will always be wide enough to hold the text.
bool expand_icon
Default |
|
Setter |
set_expand_icon(value) |
Getter |
is_expand_icon() |
When enabled, the button's icon will expand/shrink to fit the button's size while keeping its aspect.
bool flat
Default |
|
Setter |
set_flat(value) |
Getter |
is_flat() |
Flat buttons don't display decoration.
Texture2D icon
Setter |
set_button_icon(value) |
Getter |
get_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, use h_separation theme property and content_margin_*
properties of the used StyleBoxes.
HorizontalAlignment icon_alignment
Default |
|
Setter |
set_icon_alignment(value) |
Getter |
get_icon_alignment() |
Specifies if the icon should be aligned to the left, right, or center of a button. Uses the same HorizontalAlignment constants as the text alignment. If centered, text will draw on top of the icon.
String language
Default |
|
Setter |
set_language(value) |
Getter |
get_language() |
Language code used for line-breaking and text shaping algorithms, if left empty current locale is used instead.
String text
Default |
|
Setter |
set_text(value) |
Getter |
get_text() |
The button's text that will be displayed inside the button's area.
TextDirection text_direction
Default |
|
Setter |
set_text_direction(value) |
Getter |
get_text_direction() |
Base text writing direction.
OverrunBehavior text_overrun_behavior
Default |
|
Setter |
set_text_overrun_behavior(value) |
Getter |
get_text_overrun_behavior() |
Sets the clipping behavior when the text exceeds the node's bounding rectangle. See OverrunBehavior for a description of all modes.
Method Descriptions¶
void clear_opentype_features ( )
Removes all OpenType features.
Returns OpenType feature tag
.
Sets OpenType feature tag
. More info: OpenType feature tags.
Theme Property Descriptions¶
Color font_color
Default |
|
Default text Color of the Button
.
Color font_disabled_color
Default |
|
Text Color used when the Button
is disabled.
Color font_focus_color
Default |
|
Text Color used when the Button
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
Default |
|
Text Color used when the Button
is being hovered.
Color font_hover_pressed_color
Default |
|
Text Color used when the Button
is being hovered and pressed.
Color font_outline_color
Default |
|
The tint of text outline of the Button
.
Color font_pressed_color
Default |
|
Text Color used when the Button
is being pressed.
Color icon_disabled_color
Default |
|
Icon modulate Color used when the Button
is disabled.
Color icon_focus_color
Default |
|
Icon modulate Color used when the Button
is focused. Only replaces the normal modulate color of the button. Disabled, hovered, and pressed states take precedence over this color.
Color icon_hover_color
Default |
|
Icon modulate Color used when the Button
is being hovered.
Color icon_hover_pressed_color
Default |
|
Icon modulate Color used when the Button
is being hovered and pressed.
Color icon_normal_color
Default |
|
Default icon modulate Color of the Button
.
Color icon_pressed_color
Default |
|
Icon modulate Color used when the Button
is being pressed.
int h_separation
Default |
|
The horizontal space between Button
's icon and text.
int outline_size
Default |
|
The size of the text outline.
Font font
Font of the Button
's text.
int font_size
Font size of the Button
's text.
StyleBox disabled
StyleBox used when the Button
is disabled.
StyleBox focus
StyleBox used when the Button
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 Button
is being hovered.
StyleBox normal
Default StyleBox for the Button
.
StyleBox pressed
StyleBox used when the Button
is being pressed.