Galerie de contrôles
Les contrôles de base sur un seul écran, chacun rendant compte au même label via son signal.
L'éditeur ci-dessus fait tourner ce projet. Changez une ligne et il se recharge.
Classes Godot utilisées
ButtonCheckBoxOptionButtonHSliderSpinBoxLineEdit
Le code
scripts/main.gd
extends Node
var status: Label
func _ready() -> void:
var column: VBoxContainer = _column()
var title: Label = Label.new()
title.text = 'Control gallery'
title.add_theme_font_size_override('font_size', 24)
column.add_child(title)
var button: Button = Button.new()
button.text = 'Button'
button.pressed.connect(func(): _say('button pressed'))
column.add_child(button)
var toggle: CheckBox = CheckBox.new()
toggle.text = 'CheckBox'
toggle.toggled.connect(func(on: bool): _say('checkbox %s' % ('on' if on else 'off')))
column.add_child(toggle)
var switch: CheckButton = CheckButton.new()
switch.text = 'CheckButton'
switch.toggled.connect(func(on: bool): _say('switch %s' % ('on' if on else 'off')))
column.add_child(switch)
var options: OptionButton = OptionButton.new()
for entry in ['OptionButton', 'second item', 'third item']:
options.add_item(entry)
options.item_selected.connect(func(index: int): _say('option %d' % index))
column.add_child(options)
var slider: HSlider = HSlider.new()
slider.min_value = 0
slider.max_value = 100
slider.value = 40
slider.custom_minimum_size = Vector2(260, 0)
slider.value_changed.connect(func(value: float): _say('slider %d' % int(value)))
column.add_child(slider)
var spin: SpinBox = SpinBox.new()
spin.max_value = 50
spin.value = 12
spin.value_changed.connect(func(value: float): _say('spinbox %d' % int(value)))
column.add_child(spin)
var field: LineEdit = LineEdit.new()
field.placeholder_text = 'LineEdit'
field.text_changed.connect(func(text: String): _say('typed %s' % text))
column.add_child(field)
status = Label.new()
status.add_theme_color_override('font_color', Color('#8eef97'))
column.add_child(status)
_say('every control reports here through its signal')
func _say(message: String) -> void:
status.text = message
func _column() -> VBoxContainer:
var background: ColorRect = ColorRect.new()
background.color = Color('#12141a')
add_child(background)
background.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
var center: CenterContainer = CenterContainer.new()
add_child(center)
center.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
# without a fixed width the VBoxContainer stretches every control across the screen
var column: VBoxContainer = VBoxContainer.new()
column.custom_minimum_size = Vector2(320, 0)
column.add_theme_constant_override('separation', 14)
center.add_child(column)
return column