Button¶
Inherits: BaseButton < Control < CanvasItem < Node < Object
Inherited By: CheckBox, CheckButton, ColorPickerButton, MenuButton, OptionButton, ToolButton
标准主题按钮。
描述¶
标准的主题按钮。它可以包含文字和图标,并根据当前的 Theme显示。
创建按钮并在按下代码时配置动作的示例:
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!")
按钮像所有控件节点一样,也可以在编辑器中创建,但在某些情况下可能需要从代码中创建。
参阅 BaseButton,其中包括与此节点相关的通用属性和方法。
注意: 按钮不处理触摸输入,因此不支持多点触控,因为模拟鼠标在给定时间只能按下一个按钮。将 TouchScreenButton 用于触发游戏移动或动作的按钮,因为 TouchScreenButton 支持多点触控。
教程¶
属性¶
|
||
|
||
|
||
|
||
|
主题属性¶
|
||
|
||
|
||
|
||
|
||
|
||
枚举¶
enum TextAlign:
ALIGN_LEFT = 0 --- 将文本向左对齐。
ALIGN_CENTER = 1 --- 将文本居中对齐。。
ALIGN_RIGHT = 2 --- 将文本向右对齐。
属性说明¶
TextAlign align
Default |
|
Setter |
set_text_align(value) |
Getter |
get_text_align() |
按钮文本的文本对齐策略,使用TextAlign常量之一。
bool clip_text
Default |
|
Setter |
set_clip_text(value) |
Getter |
get_clip_text() |
当此属性被启用时,过大而无法容纳按钮的文本会被剪掉,当被禁用时,按钮将始终有足够的宽度来容纳文本。
bool expand_icon
Default |
|
Setter |
set_expand_icon(value) |
Getter |
is_expand_icon() |
启用后,按钮的图标将展开/收缩以适应按钮的大小,同时保持其外观。
bool flat
Default |
|
Setter |
set_flat(value) |
Getter |
is_flat() |
平面按钮不显示装饰。
Texture icon
Setter |
set_button_icon(value) |
Getter |
get_button_icon() |
按钮的图标,存在文本时,图标会被放置于文本前方。
如果想要编辑边距以及图标的间距,请使用 Button
的主题属性 hseparation
以及所使用的 StyleBox 的 content_margin_*
属性。
String text
Default |
|
Setter |
set_text(value) |
Getter |
get_text() |
按钮的文字,将显示在按钮的区域内。
Theme Property Descriptions¶
Color font_color
Default |
|
Button
的默认文本Color。
Color font_color_disabled
Default |
|
禁用Button
时使用的文本Color。
Color font_color_focus
Default |
|
当Button
获得焦点时使用的文本Color。只取代按钮的正常文本颜色。禁用、悬停和按下状态优先于这个颜色。
Color font_color_hover
Default |
|
悬停Button
时使用的文本Color。
Color font_color_pressed
Default |
|
正在按下 Button
时使用的文本 Color 。
int hseparation
Default |
|
Button
的图标和文本之间的水平间距。
Font font
Button
文本的Font。
StyleBox disabled
当Button
被禁用时,使用StyleBox。
StyleBox focus
当Button
获得焦点时使用的StyleBox。它显示在当前的StyleBox之上,所以使用StyleBoxEmpty只是禁用焦点视觉效果。
StyleBox hover
悬停Button
时使用的StyleBox。
StyleBox normal
Button
的默认StyleBox。
StyleBox pressed
按下Button
时使用的StyleBox。