screen_capture.gd 703 B

123456789101112131415161718192021222324
  1. extends Node
  2. @onready var captured_image: TextureRect = $CapturedImage
  3. @onready var capture_button: Button = $CaptureButton
  4. func _ready() -> void:
  5. # Focus button for keyboard/gamepad-friendly navigation.
  6. capture_button.grab_focus()
  7. func _on_capture_button_pressed() -> void:
  8. # Retrieve the captured image.
  9. var img := get_viewport().get_texture().get_image()
  10. # Create a texture for it.
  11. var tex := ImageTexture.create_from_image(img)
  12. # Set the texture to the captured image node.
  13. captured_image.set_texture(tex)
  14. # Colorize the button with a random color, so you can see which button belongs to which capture.
  15. capture_button.modulate = Color.from_hsv(randf(), randf_range(0.2, 0.8), 1.0)