Skip to content

Commit 237a865

Browse files
committed
Improve the screen capture demo
1 parent e3fd4c6 commit 237a865

File tree

3 files changed

+15
-18
lines changed

3 files changed

+15
-18
lines changed

viewport/screen_capture/project.godot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ _global_script_class_icons={
1616
[application]
1717

1818
config/name="Screen Capture"
19-
run/main_scene="res://capture_screen.tscn"
19+
run/main_scene="res://screen_capture.tscn"
2020
config/icon="res://icon.png"
2121

2222
[debug]
Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
1+
extends Node
12

2-
extends Control
3+
onready var captured_image = $CapturedImage
34

4-
5-
func _ready():
6-
get_node("Button").connect("pressed", self, "_on_button_pressed");
7-
8-
9-
func _on_button_pressed():
5+
func _on_CaptureButton_pressed():
106
get_viewport().set_clear_mode(Viewport.CLEAR_MODE_ONLY_NEXT_FRAME)
11-
# Let two frames pass to make sure the screen was captured
7+
# Let two frames pass to make sure the screen was captured.
128
yield(get_tree(), "idle_frame")
139
yield(get_tree(), "idle_frame")
1410

15-
# Retrieve the captured image
11+
# Retrieve the captured image.
1612
var img = get_viewport().get_texture().get_data()
1713

18-
# Flip it on the y-axis (because it's flipped)
14+
# Flip it on the y-axis (because it's flipped).
1915
img.flip_y()
2016

21-
# Create a texture for it
17+
# Create a texture for it.
2218
var tex = ImageTexture.new()
2319
tex.create_from_image(img)
2420

25-
# Set it to the capture node
26-
get_node("capture").set_texture(tex)
21+
# Set the texture to the captured image node.
22+
captured_image.set_texture(tex)

viewport/screen_capture/capture_screen.tscn renamed to viewport/screen_capture/screen_capture.tscn

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
[ext_resource path="res://screen_capture.gd" type="Script" id=1]
44
[ext_resource path="res://mountains.png" type="Texture" id=2]
55

6-
[node name="Control" type="Control"]
6+
[node name="ScreenCapture" type="Control"]
77
margin_right = 40.0
88
margin_bottom = 40.0
99
script = ExtResource( 1 )
1010

11-
[node name="BG" type="TextureRect" parent="."]
11+
[node name="Background" type="TextureRect" parent="."]
1212
anchor_right = 1.0
1313
anchor_bottom = 1.0
1414
margin_right = 986.0
@@ -18,7 +18,7 @@ grow_vertical = 0
1818
texture = ExtResource( 2 )
1919
expand = true
2020

21-
[node name="capture" type="TextureRect" parent="."]
21+
[node name="CapturedImage" type="TextureRect" parent="."]
2222
anchor_right = 1.0
2323
anchor_bottom = 1.0
2424
margin_right = 2013.0
@@ -29,9 +29,10 @@ rect_scale = Vector2( 0.5, 0.5 )
2929
expand = true
3030
stretch_mode = 4
3131

32-
[node name="Button" type="Button" parent="."]
32+
[node name="CaptureButton" type="Button" parent="."]
3333
margin_left = 48.0
3434
margin_top = 53.0
3535
margin_right = 188.0
3636
margin_bottom = 113.0
3737
text = "Capture screen"
38+
[connection signal="pressed" from="CaptureButton" to="." method="_on_CaptureButton_pressed"]

0 commit comments

Comments
 (0)