test.gd 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. extends Control
  2. # This script demonstrates how to alter StyleBoxes at runtime.
  3. # Custom theme item properties aren't considered Object properties per se.
  4. # This means that you should use `add_theme_stylebox_override("normal", ...)`
  5. # instead of `set("custom_styles/normal", ...)`.
  6. @onready var label: Label = $Panel/MarginContainer/VBoxContainer/Label
  7. @onready var button: Button = $Panel/MarginContainer/VBoxContainer/Button
  8. @onready var button2: Button = $Panel/MarginContainer/VBoxContainer/Button2
  9. @onready var reset_all_button: Button = $Panel/MarginContainer/VBoxContainer/ResetAllButton
  10. func _ready() -> void:
  11. # Focus the first button automatically for keyboard/controller-friendly navigation.
  12. button.grab_focus()
  13. func _on_button_pressed() -> void:
  14. # We have to modify the normal, hover and pressed styleboxes all at once
  15. # to get a correct appearance when the button is hovered or pressed.
  16. # We can't use a single StyleBox for all of them as these have different
  17. # background colors.
  18. var new_stylebox_normal: StyleBoxFlat = button.get_theme_stylebox("normal").duplicate()
  19. new_stylebox_normal.border_color = Color(1, 1, 0)
  20. var new_stylebox_hover: StyleBoxFlat = button.get_theme_stylebox("hover").duplicate()
  21. new_stylebox_hover.border_color = Color(1, 1, 0)
  22. var new_stylebox_pressed: StyleBoxFlat = button.get_theme_stylebox("pressed").duplicate()
  23. new_stylebox_pressed.border_color = Color(1, 1, 0)
  24. button.add_theme_stylebox_override("normal", new_stylebox_normal)
  25. button.add_theme_stylebox_override("hover", new_stylebox_hover)
  26. button.add_theme_stylebox_override("pressed", new_stylebox_pressed)
  27. label.add_theme_color_override("font_color", Color(1, 1, 0.375))
  28. func _on_button2_pressed() -> void:
  29. var new_stylebox_normal: StyleBoxFlat = button2.get_theme_stylebox("normal").duplicate()
  30. new_stylebox_normal.border_color = Color(0, 1, 0.5)
  31. var new_stylebox_hover: StyleBoxFlat = button2.get_theme_stylebox("hover").duplicate()
  32. new_stylebox_hover.border_color = Color(0, 1, 0.5)
  33. var new_stylebox_pressed: StyleBoxFlat = button2.get_theme_stylebox("pressed").duplicate()
  34. new_stylebox_pressed.border_color = Color(0, 1, 0.5)
  35. button2.add_theme_stylebox_override("normal", new_stylebox_normal)
  36. button2.add_theme_stylebox_override("hover", new_stylebox_hover)
  37. button2.add_theme_stylebox_override("pressed", new_stylebox_pressed)
  38. label.add_theme_color_override("font_color", Color(0.375, 1, 0.75))
  39. func _on_reset_all_button_pressed() -> void:
  40. button.remove_theme_stylebox_override("normal")
  41. button.remove_theme_stylebox_override("hover")
  42. button.remove_theme_stylebox_override("pressed")
  43. button2.remove_theme_stylebox_override("normal")
  44. button2.remove_theme_stylebox_override("hover")
  45. button2.remove_theme_stylebox_override("pressed")
  46. label.remove_theme_color_override("font_color")