Files
aetherbound-guild/runtime/ui/shared/shared_button.gd
T

64 lines
2.5 KiB
GDScript

class_name ABGSharedButton
extends Button
const Tokens = preload("res://foundation/ui_tokens.gd")
## Generic semantic button. It emits the built-in `pressed` intent and owns no
## game state, save behavior or page lookup.
var semantic_variant := "secondary"
var selected_state := false
var busy_state := false
var _refreshing := false
func _ready() -> void:
focus_mode = Control.FOCUS_ALL
custom_minimum_size = Tokens.command_size(semantic_variant, custom_minimum_size)
set_meta("ui_factory_control", true)
set_meta("ui_semantic_variant", semantic_variant)
_refresh_style()
func configure(next_text: String, next_variant := "secondary"):
text = next_text
semantic_variant = Tokens.normalize_variant(next_variant) if Tokens.is_supported_variant(Tokens.normalize_variant(next_variant)) else "secondary"
custom_minimum_size = Tokens.command_size(semantic_variant, custom_minimum_size)
set_meta("ui_semantic_variant", semantic_variant)
_refresh_style()
return self
func set_view_state(enabled := true, selected := false, busy := false) -> void:
disabled = not enabled
selected_state = selected
busy_state = busy
set_meta("ui_selected", selected_state)
set_meta("ui_busy", busy_state)
_refresh_style()
func _refresh_style() -> void:
if _refreshing:
return
if not is_inside_tree() and get_parent() == null:
return
_refreshing = true
var colors := Tokens.command_colors(semantic_variant, disabled, false, selected_state)
var normal_fill: Color = colors.fill
var hover_fill: Color = colors.hover
var pressed_fill: Color = colors.pressed
var border: Color = colors.border
var text_color: Color = colors.text
add_theme_color_override("font_color", text_color)
add_theme_color_override("font_hover_color", text_color)
add_theme_color_override("font_pressed_color", text_color)
add_theme_color_override("font_focus_color", text_color)
add_theme_font_size_override("font_size", Tokens.font_size("label"))
add_theme_stylebox_override("normal", Tokens.flat_command_style(semantic_variant, "normal", selected_state, disabled))
add_theme_stylebox_override("hover", Tokens.flat_command_style(semantic_variant, "hover", selected_state, disabled))
add_theme_stylebox_override("pressed", Tokens.flat_command_style(semantic_variant, "pressed", selected_state, disabled))
add_theme_stylebox_override("disabled", Tokens.flat_command_style(semantic_variant, "normal", selected_state, true))
add_theme_stylebox_override("focus", Tokens.flat_command_style(semantic_variant, "focus", selected_state, disabled))
_refreshing = false