Camera roll previews queued free on file deletion

This commit is contained in:
Davide Di Staso committed 2025-10-11 12:40:39 +02:00
1 parent d25aae8428
commit 78c925d372
5 files changed
+163 -17

No files matched your search

@@ -1,4 +1,4 @@
[gd_scene load_steps=11 format=3 uid="uid://b2ktx6dty0dq4"]
[gd_scene load_steps=12 format=3 uid="uid://b2ktx6dty0dq4"]
[ext_resource type="Script" uid="uid://culd18vnvt4ej" path="res://addons/screenshots/debugger/bottom_panel/bottom_panel.gd" id="1_7ct7k"]
[ext_resource type="Script" uid="uid://bq5amyoktrfo4" path="res://addons/screenshots/debugger/bottom_panel/settings.gd" id="2_ivg3g"]
@@ -6,6 +6,7 @@
[ext_resource type="Script" uid="uid://c034i367kogf6" path="res://addons/screenshots/debugger/bottom_panel/locales.gd" id="6_m7syr"]
[ext_resource type="Script" uid="uid://jfpjg1put47h" path="res://addons/screenshots/debugger/bottom_panel/save_folder.gd" id="7_q2ypf"]
[ext_resource type="Script" uid="uid://cvvxb0tom0wc" path="res://addons/screenshots/debugger/bottom_panel/camera_roll.gd" id="8_6oft3"]
[ext_resource type="Script" uid="uid://x5sotkduu1be" path="res://addons/screenshots/debugger/bottom_panel/directory_watcher.gd" id="8_2337e"]
[ext_resource type="Script" uid="uid://c5poruw4iu2ve" path="res://addons/screenshots/debugger/bottom_panel/camera_roll_bottom_buttons.gd" id="8_epjix"]
[ext_resource type="Script" uid="uid://cx82rfmwl8gk0" path="res://addons/screenshots/screenshots_local_data.gd" id="9_ke0xa"]
@@ -336,6 +337,10 @@ unique_name_in_owner = true
layout_mode = 2
theme_override_constants/v_separation = 20
[node name="DirectoryWatcher" type="Node" parent="HSplitContainer/VBoxContainer2/ScrollContainer2/CameraRoll"]
unique_name_in_owner = true
script = ExtResource("8_2337e")
[node name="CameraRollBottomButtons" type="HBoxContainer" parent="HSplitContainer/VBoxContainer2"]
layout_mode = 2
alignment = 2
@@ -6,23 +6,29 @@ extends VBoxContainer
const SCREENSHOT_PREVIEW = preload("res://addons/screenshots/debugger/bottom_panel/screenshot_preview/screenshot_preview.tscn")
var roll_directory : String
func _ready() -> void:
%RollPanelContainer.add_theme_stylebox_override("panel", get_theme_stylebox("panel", "ItemListSecondary"))
%CameraRollIcon.texture = get_theme_icon("CameraTexture", "EditorIcons")
for filepath in local_data.settings.load_value("misc", "CameraRollPreviews", []):
var img = Image.new()
img = img.load_from_file(filepath)
roll_directory = local_data.settings.load_value("misc", "PathLineEdit", "user://screenshots")
%DirectoryWatcher.add_scan_directory(roll_directory)
local_data.settings.value_changed.connect(_on_local_data_changed)
%DirectoryWatcher.files_deleted.connect(_on_files_deleted)
if img == null:
continue
func _on_files_deleted(files: PackedStringArray) -> void:
for file in files:
for preview in previews_container.get_children():
if ProjectSettings.globalize_path(preview.path) == ProjectSettings.localize_path(file):
preview.queue_free()
var s = SCREENSHOT_PREVIEW.instantiate()
s.texture = ImageTexture.create_from_image(img)
s.path = filepath
s.locale = ""
previews_container.add_child(s)
func _on_local_data_changed(section: String, key: String, new_value) -> void:
if not key == "PathLineEdit":
return
%DirectoryWatcher.remove_scan_directory(roll_directory)
roll_directory = new_value
%DirectoryWatcher.add_scan_directory(roll_directory)
local_data.settings.save("misc", "CameraRollPreviews", get_all_previews_paths())
func add_preview(file_path: String, timestamp: String, locale: String) -> void:
var s = SCREENSHOT_PREVIEW.instantiate()
@@ -0,0 +1,119 @@
@tool
extends Node
## Scans the provided directory (or directories) and informs about file changes. The scan is done periodically with configurable speed.
class WatchedDirectory:
var first_scan := true
var new: PackedStringArray
var modified: PackedStringArray
var current: Dictionary#[String, int]
var previous: Dictionary#[String, int]
var _directory := DirAccess.open(".")
var _directory_list: Dictionary#[String, WatchedDirectory]
var _directory_cache: Array[String]
var _to_delete: Array
var _current_directory_index: int
var _current_directory_name: String
var _remaining_steps: int
var _current_delay: float
## Delay between directory scans (in seconds).
var scan_delay: float = 1
## Files scanned per frame.
var scan_step := 50
## Emitted when files are created in the scanned directories.
signal files_created(files: PackedStringArray)
## Emitted when files are modified in the scanned directories (based on modified time).
signal files_modified(files: PackedStringArray)
## Emitted when files are deleted in the scanned directories.
signal files_deleted(files: PackedStringArray)
func _ready() -> void:
_current_delay = scan_delay
_remaining_steps = scan_step
_directory.include_hidden = true
## Adds a directory that will be scanned. You can add more than 1. Only supports absolute paths and res://, user://.
func add_scan_directory(directory: String):
directory = ProjectSettings.globalize_path(directory)
_directory_list[directory] = WatchedDirectory.new()
_directory_cache.assign(_directory_list.keys())
## Removes a scanned directory. Does nothing if the directory wasn't added.
func remove_scan_directory(directory: String):
directory = ProjectSettings.globalize_path(directory)
_to_delete.append(directory)
func _process(delta: float) -> void:
if _directory_list.is_empty():
push_error("No directory to watch. Please kill me ;_;")
return
if _current_delay > 0:
_current_delay -= delta
return
while _remaining_steps > 0:
if _current_directory_name.is_empty():
_current_directory_name = _directory_cache[_current_directory_index]
_directory.change_dir(_current_directory_name)
_directory.list_dir_begin()
var directory: WatchedDirectory = _directory_list[_current_directory_name]
var file := _directory.get_next()
if file.is_empty():
_current_directory_index += 1
_current_directory_name = ""
if directory.first_scan:
directory.new.clear()
directory.modified.clear()
directory.first_scan = false
else:
if not directory.new.is_empty():
files_created.emit(directory.new)
directory.new.clear()
if not directory.modified.is_empty():
files_modified.emit(directory.modified)
directory.modified.clear()
var deleted: PackedStringArray
for path in directory.previous:
if not path in directory.current:
deleted.append(_directory.get_current_dir().path_join(path))
if not deleted.is_empty():
files_deleted.emit(deleted)
directory.previous = directory.current
directory.current = {}
if _current_directory_index == _directory_list.size():
if not _to_delete.is_empty():
for dir in _to_delete:
_directory_list.erase(dir)
_current_directory_index = 0
break
else:
if _directory.current_is_dir():
continue
var full_file := _directory.get_current_dir().path_join(file)
directory.current[file] = FileAccess.get_modified_time(full_file)
if directory.previous.get(file, -1) == -1:
directory.new.append(full_file)
elif directory.current[file] > directory.previous[file]:
directory.modified.append(full_file)
_remaining_steps -= 1
_remaining_steps = scan_step
_current_delay = scan_delay
@@ -0,0 +1 @@
uid://x5sotkduu1be
+20 -5
View File
@@ -1,12 +1,17 @@
@tool
extends Node
var settings: LocalFile
func _ready():
settings = LocalFile.new("res://addons/screenshots/settings.cfg")
settings.value_changed.connect(_on_settings_changed)
func _on_settings_changed(section: String, key: String, new_value):
print("Setting changed: [%s] %s = %s" % [section, key, new_value])
class LocalFile:
signal value_changed(section: String, key: String, new_value)
var config: ConfigFile
var file_path: String
@@ -23,10 +28,20 @@ class LocalFile:
pass
func save(section: String, key: String, value):
config.set_value(section, key, value)
var err = config.save(file_path)
if err != OK:
push_error("Failed to save config file " + file_path + ": " + str(err))
var old_value = null
var has_existing_key = config.has_section_key(section, key)
if has_existing_key:
old_value = config.get_value(section, key)
# Only save and emit if the value is different
if not has_existing_key or old_value != value:
config.set_value(section, key, value)
var err = config.save(file_path)
if err != OK:
push_error("Failed to save config file " + file_path + ": " + str(err))
else:
value_changed.emit(section, key, value)
func load_value(section: String, key: String, default_value = null):
# Check if the key exists first to avoid C++ errors