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.
Checking the stable version of the documentation...
Button
繼承: BaseButton < Control < CanvasItem < Node < Object
被繼承: CheckBox, CheckButton, ColorPickerButton, MenuButton, OptionButton
按鈕,支援主題,能夠包含文字和圖示。
說明
Button is the standard themed button. It can contain text and an icon, and it will display them according to the current Theme.
Example: Create a button and connect a method that will be called when the button is pressed:
func _ready():
var button = Button.new()
button.text = "Click me"
button.pressed.connect(_button_pressed)
add_child(button)
func _button_pressed():
print("Hello world!")
public override void _Ready()
{
var button = new Button();
button.Text = "Click me";
button.Pressed += ButtonPressed;
AddChild(button);
}
private void ButtonPressed()
{
GD.Print("Hello world!");
}
See also BaseButton which contains common properties and methods associated with this node.
Note: Buttons support multitouch via touch input, allowing multiple buttons to be pressed at the same time. Otherwise, mouse input is used, limiting interaction to one button press at a time.
教學
屬性
|
||
|
||
BitField[LineBreakFlag] |
|
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
主題屬性
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
屬性說明
HorizontalAlignment alignment = 1 🔗
void set_text_alignment(value: HorizontalAlignment)
HorizontalAlignment get_text_alignment()
Text alignment policy for the button's text.
AutowrapMode autowrap_mode = 0 🔗
void set_autowrap_mode(value: AutowrapMode)
AutowrapMode get_autowrap_mode()
If set to something other than TextServer.AUTOWRAP_OFF, the text gets wrapped inside the node's bounding rectangle.
BitField[LineBreakFlag] autowrap_trim_flags = 128 🔗
void set_autowrap_trim_flags(value: BitField[LineBreakFlag])
BitField[LineBreakFlag] get_autowrap_trim_flags()
Autowrap space trimming flags. See TextServer.BREAK_TRIM_START_EDGE_SPACES and TextServer.BREAK_TRIM_END_EDGE_SPACES for more info.
If true, text that is too large to fit the button is clipped horizontally. If false, 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.
啟用後,將在保持按鈕圖示長寬比的前提下對該圖示進行擴充/收縮,從而適應按鈕的大小。另見 icon_max_width。
平面按鈕不顯示裝飾。
按鈕的圖示,如果文字存在,則圖示將被放置在文字之前。
要編輯圖示的邊距和間距,請使用 h_separation 主題屬性,和所用 StyleBox 的 content_margin_* 屬性。
HorizontalAlignment icon_alignment = 0 🔗
void set_icon_alignment(value: HorizontalAlignment)
HorizontalAlignment get_icon_alignment()
指定圖示在按鈕上水平對齊的方式應該為左對齊、右對齊還是居中對齊。請使用與文字對齊相同的 HorizontalAlignment 常數。如果水平居中並且垂直居中,則文字將被繪製在圖示之上。
Language code used for line-breaking and text shaping algorithms. If left empty, the current locale is used instead.
該按鈕的文字,將顯示在按鈕的區域內。
TextDirection text_direction = 0 🔗
void set_text_direction(value: TextDirection)
TextDirection get_text_direction()
基礎文字書寫方向。
OverrunBehavior text_overrun_behavior = 0 🔗
void set_text_overrun_behavior(value: OverrunBehavior)
OverrunBehavior get_text_overrun_behavior()
Sets the clipping behavior when the text exceeds the node's bounding rectangle.
VerticalAlignment vertical_icon_alignment = 1 🔗
void set_vertical_icon_alignment(value: VerticalAlignment)
VerticalAlignment get_vertical_icon_alignment()
指定圖示在按鈕上垂直對齊的方式應該為頂端對齊、底部對齊還是居中對齊。請使用與文字對齊相同的 VerticalAlignment 常數。如果水平居中並且垂直居中,則文本將被繪製在圖示之上。
主題屬性說明
Color font_color = Color(0.875, 0.875, 0.875, 1) 🔗
該 Button 的預設文字 Color。
Color font_disabled_color = Color(0.875, 0.875, 0.875, 0.5) 🔗
該 Button 處於禁用狀態時,使用的文字 Color。
Color font_focus_color = Color(0.95, 0.95, 0.95, 1) 🔗
該 Button 處於聚焦狀態時,使用的文字 Color。只替換該按鈕的正常文字顏色。禁用、懸停、按下狀態優先於這個顏色。
Color font_hover_color = Color(0.95, 0.95, 0.95, 1) 🔗
該 Button 處於懸停狀態時,使用的文字 Color。
Color font_hover_pressed_color = Color(1, 1, 1, 1) 🔗
該 Button 處於懸停並按下狀態時,使用的文字 Color。
Color font_outline_color = Color(0, 0, 0, 1) 🔗
該 Button 的文字輪廓的色調。
Color font_pressed_color = Color(1, 1, 1, 1) 🔗
該 Button 處於按下狀態時,使用的文字 Color 。
Color icon_disabled_color = Color(1, 1, 1, 0.4) 🔗
該 Button 處於禁用狀態時,使用的圖示調變 Color。
Color icon_focus_color = Color(1, 1, 1, 1) 🔗
該 Button 處於聚焦狀態時,使用的圖示調變 Color。僅替換該按鈕的正常調變顏色。禁用、懸停和按下狀態優先於這個顏色。
Color icon_hover_color = Color(1, 1, 1, 1) 🔗
該 Button 處於懸停狀態時,使用的圖示調變Color。
Color icon_hover_pressed_color = Color(1, 1, 1, 1) 🔗
該 Button 處於懸停並按下按下狀態時,使用的圖示調變 Color。
Color icon_normal_color = Color(1, 1, 1, 1) 🔗
該 Button 的預設圖示調變 Color。
Color icon_pressed_color = Color(1, 1, 1, 1) 🔗
該 Button 處於按下狀態時,使用的圖示調變 Color。
int align_to_largest_stylebox = 0 🔗
This constant acts as a boolean. If true, 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.
Button 的圖示和文字之間的水平間距。使用時會將負值當作 0。
The maximum allowed width of the Button's icon. This limit is applied on top of the default size of the icon, or its expanded size if expand_icon is true. 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.
文字輪廓的大小。
注意:如果使用啟用了 FontFile.multichannel_signed_distance_field 的字形,其 FontFile.msdf_pixel_range 必須至少設定為 outline_size 的兩倍,輪廓算繪才能看起來正確。否則,輪廓可能會比預期的更早被切斷。
該 Button 文字的 Font。
該 Button 文字的字形大小。
Default icon for the Button. Appears only if icon is not assigned.
該 Button 處於禁用狀態時使用的 StyleBox。
該 OptionButton 處於禁用狀態時使用的 StyleBox(用於從右至左佈局)。
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.
該 Button 處於懸停狀態時使用的 StyleBox。
該 OptionButton 處於懸停狀態時使用的 StyleBox(用於從右至左佈局)。
該 Button 處於按下狀態時使用的 StyleBox。
StyleBox hover_pressed_mirrored 🔗
該 OptionButton 處於按下狀態時使用的 StyleBox(用於從右至左佈局)。
該 Button 的預設 StyleBox。
該 OptionButton 的預設 StyleBox(用於從右至左佈局)。
該 Button 處於按下狀態時使用的 StyleBox。
該 OptionButton 處於按下狀態時使用的 StyleBox(用於從右至左佈局)。