Skip to content

Commit

Permalink
Updates screenshot tool for godot 4 (not working yet)
Browse files Browse the repository at this point in the history
Discussed in issue #316
  • Loading branch information
Mathias Baumgartinger committed Apr 18, 2023
1 parent 52198b5 commit 50b1867
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 100 deletions.
23 changes: 19 additions & 4 deletions Global/Screencapture.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ extends Node
# Will be automatically set from the world once the positionmanager is loaded
var pos_manager

signal screenshot_finished


func _input(event):
if event.is_action_pressed("screenshot"): screenshot()
Expand All @@ -29,21 +31,34 @@ func screenshot(
var previous_viewport_size = pos_manager.get_viewport().size
var previous_plant_extent_factor = Vegetation.plant_extent_factor

# Tweak anti-aliasing to be optimal for screenshotting and
# disable taa because it messes up the image
var msaa_before = pos_manager.get_viewport().msaa_3d
var taa_before = pos_manager.get_viewport().is_using_taa()
pos_manager.get_viewport().set_use_taa(false)
pos_manager.get_viewport().set_msaa_3d(Viewport.MSAA_MAX)

pos_manager.get_viewport().size = previous_viewport_size * upscale_viewport
Vegetation.plant_extent_factor = plant_extent

#await get_tree().create_timer(1).timeout

RenderingServer.force_sync()
RenderingServer.force_draw()

# get data of the viewport and flip (because ... it is flipped
var image = pos_manager.get_viewport().get_texture().get_data()
image.flip_y()

# get data of the viewport
var image = pos_manager.get_viewport().get_texture().get_image()

# save to a file
image.save_png(image_name)

# Reset to prior configuration
pos_manager.get_viewport().size = previous_viewport_size
Vegetation.plant_extent_factor = previous_plant_extent_factor
pos_manager.get_viewport().set_use_taa(taa_before)
pos_manager.get_viewport().set_msaa_3d(msaa_before)

RenderingServer.force_sync()
RenderingServer.force_draw()

screenshot_finished.emit()
19 changes: 8 additions & 11 deletions UI/Screenshot/ScreenshotMenu.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ var time_manager: TimeManager
#FIXME: use a calendar for timeseries

func _ready():
$Inputs/ScreenshotButton.connect("pressed",Callable(self,"_on_screenshot"))
$Inputs/CheckBoxTimeSeries.connect("toggled",Callable(self,"_toggle_time_series"))
# $Inputs/TimeSeriesContainer/FromButton.connect(
# "pressed", $Inputs/TimeSeriesContainer/FromButton/popupFrom, "popup")
# $Inputs/TimeSeriesContainer/ToButton.connect(
# "pressed", $Inputs/TimeSeriesContainer/ToButton/popupTo, "popup")
$Inputs/ScreenshotButton.pressed.connect(_on_screenshot)
$Inputs/CheckBoxTimeSeries.toggled.connect(_toggle_time_series)


func _toggle_time_series(toggled: bool):
Expand All @@ -21,31 +17,32 @@ func _toggle_time_series(toggled: bool):


func _on_screenshot():
if not $Inputs/CheckBoxTimeSeries.pressed:
if not $Inputs/CheckBoxTimeSeries.is_pressed():
Screencapture.screenshot(
$Inputs/ScreenShotName.text,
$Inputs/UpscaleViewport.value,
$Inputs/PlantExtent.value
)
else:
var prev_datetime = time_manager.date_time
var prev_datetime = time_manager.datetime
var current_datetime = $Inputs/TimeSeriesContainer/From.value
var to = $Inputs/TimeSeriesContainer/To.value
var interval_idx = 0
var interval = $Inputs/TimeSeriesContainer/Interval/Hours.value
interval += $Inputs/TimeSeriesContainer/Interval/Minutes.value / 60

while to > current_datetime:
time_manager.set_time(current_datetime)
await get_tree().idle_frame
time_manager.set_time(current_datetime, 0)
await get_tree().process_frame
Screencapture.screenshot(
$Inputs/ScreenShotName.text,
$Inputs/UpscaleViewport.value,
$Inputs/PlantExtent.value,
"-%d" % interval_idx
)
await Screencapture.screenshot_finished

current_datetime += interval
interval_idx += 1

time_manager.set_datetime_by_class(prev_datetime)
time_manager.set_datetime_by_dict(prev_datetime)
110 changes: 25 additions & 85 deletions UI/Screenshot/ScreenshotMenu.tscn
Original file line number Diff line number Diff line change
@@ -1,162 +1,102 @@
[gd_scene load_steps=2 format=2]
[gd_scene load_steps=2 format=3 uid="uid://88rg1t88n3e7"]

[ext_resource path="res://UI/Screenshot/ScreenshotMenu.gd" type="Script" id=1]
[ext_resource type="Script" path="res://UI/Screenshot/ScreenshotMenu.gd" id="1"]

[node name="Control" type="HBoxContainer"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_right = -1269.0
offset_bottom = -745.0
script = ExtResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}
script = ExtResource("1")

[node name="Labels" type="VBoxContainer" parent="."]
offset_right = 206.0
offset_bottom = 335.0
layout_mode = 2

[node name="LabelName" type="Label" parent="Labels"]
offset_right = 206.0
offset_bottom = 25.0
layout_mode = 2
text = "Screenshot name: "
valign = 1

[node name="LabelUpscale" type="Label" parent="Labels"]
offset_top = 33.0
offset_right = 206.0
offset_bottom = 58.0
layout_mode = 2
text = "Upscale viewport: "
valign = 1

[node name="LabelExtent" type="Label" parent="Labels"]
offset_top = 66.0
offset_right = 206.0
offset_bottom = 91.0
layout_mode = 2
text = "Plant extent: "
valign = 1

[node name="TimeSeries" type="Label" parent="Labels"]
offset_top = 99.0
offset_right = 206.0
offset_bottom = 124.0
layout_mode = 2
text = "Enable time-series:"
valign = 1

[node name="TimeSeriesLabels" type="VBoxContainer" parent="Labels"]
visible = false
offset_top = 215.0
offset_right = 206.0
offset_bottom = 366.0
layout_mode = 2

[node name="FromLabel" type="Label" parent="Labels/TimeSeriesLabels"]
offset_right = 206.0
offset_bottom = 45.0
layout_mode = 2
text = "From:"
valign = 1

[node name="ToLabel" type="Label" parent="Labels/TimeSeriesLabels"]
offset_top = 53.0
offset_right = 206.0
offset_bottom = 98.0
layout_mode = 2
text = "To:"
valign = 1

[node name="IntervalLabel" type="Label" parent="Labels/TimeSeriesLabels"]
offset_top = 106.0
offset_right = 206.0
offset_bottom = 151.0
layout_mode = 2
text = "Interval:"
valign = 1

[node name="Inputs" type="VBoxContainer" parent="."]
offset_left = 214.0
offset_right = 474.0
offset_bottom = 335.0
layout_mode = 2

[node name="ScreenShotName" type="LineEdit" parent="Inputs"]
offset_right = 260.0
offset_bottom = 45.0
minimum_size = Vector2( 260, 0 )
layout_mode = 2
placeholder_text = "user://image.png"

[node name="UpscaleViewport" type="SpinBox" parent="Inputs"]
offset_top = 53.0
offset_right = 260.0
offset_bottom = 98.0
layout_mode = 2
max_value = 4.0
step = 0.1
value = 1.0
align = 1
__meta__ = {
"_editor_description_": ""
}

[node name="PlantExtent" type="SpinBox" parent="Inputs"]
offset_top = 106.0
offset_right = 260.0
offset_bottom = 151.0
layout_mode = 2
max_value = 15.0
step = 0.5
value = 6.0
align = 1
__meta__ = {
"_editor_description_": ""
}

[node name="CheckBoxTimeSeries" type="CheckBox" parent="Inputs"]
offset_top = 159.0
offset_right = 48.0
offset_bottom = 207.0
layout_mode = 2
size_flags_horizontal = 0

[node name="TimeSeriesContainer" type="VBoxContainer" parent="Inputs"]
visible = false
offset_top = 215.0
offset_right = 400.0
offset_bottom = 366.0
layout_mode = 2

[node name="From" type="SpinBox" parent="Inputs/TimeSeriesContainer"]
offset_right = 400.0
offset_bottom = 45.0
layout_mode = 2
max_value = 24.0
align = 1
suffix = "h"

[node name="To" type="SpinBox" parent="Inputs/TimeSeriesContainer"]
offset_top = 53.0
offset_right = 400.0
offset_bottom = 98.0
layout_mode = 2
max_value = 24.0
value = 12.0
align = 1
suffix = "h"

[node name="Interval" type="HBoxContainer" parent="Inputs/TimeSeriesContainer"]
offset_top = 106.0
offset_right = 400.0
offset_bottom = 151.0
layout_mode = 2

[node name="Hours" type="SpinBox" parent="Inputs/TimeSeriesContainer/Interval"]
offset_right = 196.0
offset_bottom = 45.0
layout_mode = 2
max_value = 24.0
value = 1.0
align = 1
suffix = "h"
__meta__ = {
"_editor_description_": ""
}

[node name="Minutes" type="SpinBox" parent="Inputs/TimeSeriesContainer/Interval"]
offset_left = 204.0
offset_right = 400.0
offset_bottom = 45.0
layout_mode = 2
max_value = 59.0
suffix = "m"

[node name="ScreenshotButton" type="Button" parent="Inputs"]
offset_top = 215.0
offset_right = 260.0
offset_bottom = 252.0
layout_mode = 2
text = "Screenshot"
4 changes: 4 additions & 0 deletions World/TimeManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ signal datetime_changed(new_datetime)
signal daytime_changed(is_day)


func set_datetime_by_dict(date):
datetime = date


func set_date(year, month, day):
var new_datetime = {
"year": year,
Expand Down

0 comments on commit 50b1867

Please sign in to comment.