Randoloot/scripts/ui/captain_skill_button.gd
2026-10-15 21:46:44 +02:00

75 lines
2.8 KiB
GDScript

extends Button
const ICON_SHADER := preload("res://resources/ui/skill_icon.gdshader")
var _icon_material: ShaderMaterial
var _normal: StyleBoxFlat
var _disabled: StyleBoxFlat
var _active: StyleBoxFlat
var _was_active := false
var _artwork: Texture2D
func configure(artwork: Texture2D, accent: Color) -> void:
if _normal != null and _artwork == artwork:
return
_artwork = artwork
_was_active = false
text = ""
var old_picture := get_node_or_null("RoundedArtwork")
if old_picture != null:
remove_child(old_picture)
old_picture.queue_free()
custom_minimum_size = Vector2(36, 36)
focus_mode = Control.FOCUS_NONE
mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
_normal = _frame(Color("405c70"), Color("0a1420"))
_disabled = _frame(Color("2d4253"), Color("09111c"))
_active = _frame(Color("a9efb4"), Color("142b28"))
add_theme_stylebox_override("normal", _normal)
add_theme_stylebox_override("disabled", _disabled)
add_theme_stylebox_override("hover", _frame(accent.lightened(0.3), Color("162638")))
add_theme_stylebox_override("pressed", _frame(Color("e5f6ff"), Color("1b3445")))
add_theme_stylebox_override("focus", StyleBoxEmpty.new())
if artwork == null:
text = "·"
add_theme_color_override("font_disabled_color", Color("4a6376"))
return
var picture := TextureRect.new()
picture.name = "RoundedArtwork"
picture.texture = artwork
picture.texture_filter = CanvasItem.TEXTURE_FILTER_LINEAR_WITH_MIPMAPS
picture.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
picture.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_COVERED
picture.mouse_filter = Control.MOUSE_FILTER_IGNORE
_icon_material = ShaderMaterial.new()
_icon_material.shader = ICON_SHADER
picture.material = _icon_material
add_child(picture)
move_child(picture, 0)
picture.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
picture.offset_left = 2
picture.offset_top = 2
picture.offset_right = -2
picture.offset_bottom = -2
picture.resized.connect(func() -> void: _icon_material.set_shader_parameter("icon_size", picture.size))
func set_visual_state(cooldown_fraction: float, active: bool, blocked: bool) -> void:
if _icon_material != null:
_icon_material.set_shader_parameter("cooldown", clampf(cooldown_fraction, 0.0, 1.0))
_icon_material.set_shader_parameter("dimming", 0.0 if active else (0.6 if blocked else 0.0))
if active != _was_active:
_was_active = active
add_theme_stylebox_override("normal", _active if active else _normal)
add_theme_stylebox_override("disabled", _active if active else _disabled)
func _frame(border: Color, background: Color) -> StyleBoxFlat:
var style := StyleBoxFlat.new()
style.bg_color = background
style.border_color = border
style.set_border_width_all(1)
style.set_corner_radius_all(6)
style.set_content_margin_all(0)
style.shadow_color = Color(0.0, 0.0, 0.0, 0.35)
style.shadow_size = 2
style.shadow_offset = Vector2(0, 1)
return style