58 lines
2 KiB
GDScript
58 lines
2 KiB
GDScript
extends TextureButton
|
|
@onready var label: Label = $CenterContainer/Label # Adjust path to your Label node
|
|
|
|
var unlock_id = "" # Store the unlock ID
|
|
|
|
func _ready():
|
|
label.visible = false # Hide label initially
|
|
adjust_label_font_size()
|
|
# Connect the pressed signal
|
|
pressed.connect(_on_button_pressed)
|
|
|
|
func setup(unlock_data):
|
|
Log.pr("Setting up button for unlock:", unlock_data.unlock_name)
|
|
unlock_id = unlock_data.unlock_id # Store the ID
|
|
if label:
|
|
label.visible = false
|
|
label.text = unlock_data.unlock_name + " " + str(unlock_data.get_next_rank())
|
|
label.text = label.text + " - " + Global.currency_symbol + str(unlock_data.get_next_cost())
|
|
label.text = label.text + "\n" + unlock_data.get_next_modifiers_string()
|
|
#self.disabled = unlock_data.is_unlocked
|
|
adjust_label_font_size()
|
|
else:
|
|
Log.pr("Warning: Label node not found in button.")
|
|
|
|
func _on_button_pressed():
|
|
Log.pr("Button pressed, unlocking item:", unlock_id)
|
|
Unlocks.unlock_item(unlock_id)
|
|
|
|
func adjust_label_font_size():
|
|
if not label:
|
|
return
|
|
var available_width = size.x - 10
|
|
var available_height = size.y - 10
|
|
# Start with a reasonable font size
|
|
var font_size = 32
|
|
var min_font_size = 8
|
|
# Get or create a font
|
|
var font = label.get_theme_font("font")
|
|
# Binary search for the optimal font size
|
|
while font_size > min_font_size:
|
|
label.add_theme_font_size_override("font_size", font_size)
|
|
# Force update and get the actual text size
|
|
await get_tree().process_frame
|
|
var text_size = font.get_string_size(label.text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size)
|
|
# Check if it fits
|
|
if text_size.x <= available_width and text_size.y <= available_height:
|
|
break
|
|
# Reduce font size and try again
|
|
font_size -= 1
|
|
label.add_theme_font_size_override("font_size", font_size)
|
|
label.visible = true # Show label after resizing is complete
|
|
|
|
# Call this function whenever you change the label text
|
|
func set_label_text(new_text: String):
|
|
if label:
|
|
label.visible = false # Hide while resizing
|
|
label.text = new_text
|
|
adjust_label_font_size()
|