Button

Inherits: BaseButton < Control < CanvasItem < Node < Object

Inherited By: CheckBox, CheckButton, ColorPickerButton, MenuButton, OptionButton, ToolButton

标准主题按钮。

描述

标准的主题按钮。它可以包含文字和图标,并根据当前的 Theme显示。

创建按钮并在按下代码时配置动作的示例:

  1. func _ready():
  2. var button = Button.new()
  3. button.text = "Click me"
  4. button.connect("pressed", self, "_button_pressed")
  5. add_child(button)
  6. func _button_pressed():
  7. print("Hello world!")

按钮像所有控件节点一样,也可以在编辑器中创建,但在某些情况下可能需要从代码中创建。

参阅 BaseButton,其中包括与此节点相关的通用属性和方法。

注意: 按钮不处理触摸输入,因此不支持多点触控,因为模拟鼠标在给定时间只能按下一个按钮。将 TouchScreenButton 用于触发游戏移动或动作的按钮,因为 TouchScreenButton 支持多点触控。

教程

属性

TextAlign

align

1

bool

clip_text

false

bool

expand_icon

false

bool

flat

false

Texture

icon

String

text

“”

主题属性

Color

font_color

Color( 0.88, 0.88, 0.88, 1 )

Color

font_color_disabled

Color( 0.9, 0.9, 0.9, 0.2 )

Color

font_color_focus

Color( 0.94, 0.94, 0.94, 1 )

Color

font_color_hover

Color( 0.94, 0.94, 0.94, 1 )

Color

font_color_pressed

Color( 1, 1, 1, 1 )

int

hseparation

2

Font

font

StyleBox

disabled

StyleBox

focus

StyleBox

hover

StyleBox

normal

StyleBox

pressed

枚举

enum TextAlign:

  • ALIGN_LEFT = 0 —- 将文本向左对齐。

  • ALIGN_CENTER = 1 —- 将文本居中对齐。。

  • ALIGN_RIGHT = 2 —- 将文本向右对齐。

属性说明

Default

1

Setter

set_text_align(value)

Getter

get_text_align()

按钮文本的文本对齐策略,使用TextAlign常量之一。


Default

false

Setter

set_clip_text(value)

Getter

get_clip_text()

当此属性被启用时,过大而无法容纳按钮的文本会被剪掉,当被禁用时,按钮将始终有足够的宽度来容纳文本。


Default

false

Setter

set_expand_icon(value)

Getter

is_expand_icon()

启用后,按钮的图标将展开/收缩以适应按钮的大小,同时保持其外观。


Default

false

Setter

set_flat(value)

Getter

is_flat()

平面按钮不显示装饰。


Setter

set_button_icon(value)

Getter

get_button_icon()

按钮的图标,存在文本时,图标会被放置于文本前方。

如果想要编辑边距以及图标的间距,请使用 Button 的主题属性 hseparation 以及所使用的 StyleBoxcontent_margin_* 属性。


Default

“”

Setter

set_text(value)

Getter

get_text()

按钮的文字,将显示在按钮的区域内。

Theme Property Descriptions

Default

Color( 0.88, 0.88, 0.88, 1 )

Button的默认文本Color


  • Color font_color_disabled

Default

Color( 0.9, 0.9, 0.9, 0.2 )

禁用Button时使用的文本Color


Default

Color( 0.94, 0.94, 0.94, 1 )

Button获得焦点时使用的文本Color。只取代按钮的正常文本颜色。禁用、悬停和按下状态优先于这个颜色。


Default

Color( 0.94, 0.94, 0.94, 1 )

悬停Button时使用的文本Color


  • Color font_color_pressed

Default

Color( 1, 1, 1, 1 )

正在按下 Button 时使用的文本 Color


  • int hseparation

Default

2

Button的图标和文本之间的水平间距。


Button文本的Font


Button被禁用时,使用StyleBox


Button获得焦点时使用的StyleBox。它显示在当前的StyleBox之上,所以使用StyleBoxEmpty只是禁用焦点视觉效果。


悬停Button时使用的StyleBox


Button的默认StyleBox


按下Button时使用的StyleBox