Button
继承: BaseButton < Control < CanvasItem < Node < Object
派生: CheckBox, CheckButton, ColorPickerButton, MenuButton, OptionButton
按钮,支持主题,能够包含文本和图标。
描述
Button 是标准的主题按钮,可以包含文字和图标,显示样式会根据当前的 Theme 改变。
示例:创建按钮并连接在按下时调用的方法:
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!");
}
另见 BaseButton,其中包含与此节点相关联的通用属性和方法。
注意:按钮不处理触摸输入,因此不支持多点触控,因为模拟鼠标在给定时间只能按下一个按钮。请用 TouchScreenButton 制作触发游戏移动或动作的按钮。
教程
属性
|
||
|
||
BitField[LineBreakFlag] |
|
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
主题属性
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
属性说明
HorizontalAlignment alignment = 1 🔗
void set_text_alignment(value: HorizontalAlignment)
HorizontalAlignment get_text_alignment()
按钮文本的文本对齐策略。
AutowrapMode autowrap_mode = 0 🔗
void set_autowrap_mode(value: AutowrapMode)
AutowrapMode get_autowrap_mode()
如果设置为 TextServer.AUTOWRAP_OFF 以外的值,则文本将在节点的边界矩形内换行。
BitField[LineBreakFlag] autowrap_trim_flags = 128 🔗
void set_autowrap_trim_flags(value: BitField[LineBreakFlag])
BitField[LineBreakFlag] get_autowrap_trim_flags()
自动换行空格修剪标志。详见 TextServer.BREAK_TRIM_START_EDGE_SPACES 和 TextServer.BREAK_TRIM_END_EDGE_SPACES。
如果为 true,则因过长而无法适配按钮的文本会被水平截断。如果为 false,则按钮的宽度始终足够容纳文本。文本不会垂直截断,按钮的高度不受该属性影响。
启用后,将在保持按钮图标长宽比的前提下对该图标进行扩展/收缩,从而适应按钮的大小。另见 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()
设置文本超出节点边界框时的裁剪行为。
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 🔗
该常量是作为布尔值使用的。如果为 true,则按钮的最小尺寸和文本/图标对齐始终基于最大样式盒边距,否则则基于当前按钮状态样式盒边距。
Button 的图标和文本之间的水平间距。使用时会将负值当作 0。
Button 图标的最大允许宽度。该限制应用于图标的默认大小,如果 expand_icon 为 true,则应用于其扩展大小。高度根据图标的缩放进行调整。如果按钮有其他图标(例如 CheckBox),它们也将会受到限制。
行与行之间的额外纵向留白(单位为像素),留白会被添加到行的降部。该值可以为负数。
文字轮廓的大小。
注意:如果使用启用了 FontFile.multichannel_signed_distance_field 的字体,其 FontFile.msdf_pixel_range 必须至少设置为 outline_size 的两倍,轮廓渲染才能看起来正确。否则,轮廓可能会比预期的更早被切断。
该 Button 文本的 Font。
该 Button 文本的字体大小。
该 Button 的默认图标。仅在未指定 icon 时显示。
该 Button 处于禁用状态时使用的 StyleBox。
该 Button 处于禁用状态时使用的 StyleBox(用于从右至左布局)。
该 Button 处于聚焦状态时使用的 StyleBox。focus StyleBox 显示在基础 StyleBox 之上,所以应该使用部分透明的 StyleBox,确保基础 StyleBox 仍然可见。代表轮廓或下划线的 StyleBox 可以很好地实现这个目的。要禁用聚焦的视觉效果,请指定 StyleBoxEmpty 资源。请注意,禁用聚焦的视觉效果会影响使用键盘/手柄进行导航的可用性,所以出于可访问性的原因,不建议这样做。
该 Button 处于悬停状态时使用的 StyleBox。
该 Button 处于悬停状态时使用的 StyleBox(用于从右至左布局)。
该 Button 同时处于按下和悬停状态时使用的 StyleBox。
StyleBox hover_pressed_mirrored 🔗
该 Button 同时处于按下和悬停状态时使用的 StyleBox(用于从右至左布局)。
该 Button 的默认 StyleBox。
该 Button 的默认 StyleBox(用于从右至左布局)。
该 Button 处于按下状态时使用的 StyleBox。
该 Button 处于按下状态时使用的 StyleBox(用于从右至左布局)。