Add default filename for Save As dialog

Trailing number incremented.

Co-Authored-By: Russell Matney <russell.matney@gmail.com>
This commit is contained in:
Haoyu Qiu 2023-02-16 09:22:56 +08:00
parent 576708bb4a
commit 4562f4b77c
2 changed files with 44 additions and 2 deletions

View File

@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Bit depth and sample rate import options.
- Paste from JSXFR.
- Add default filename for Save As dialog, with trailing number incremented.
## [1.2.0] - 2022-10-13
### Added

View File

@ -2,6 +2,7 @@ tool
extends Container
enum ExtraOption { SAVE_AS, COPY, PASTE, PASTE_JSFXR, RECENT }
enum DefaultFilename { EMPTY, GUESS_FOR_SAVE }
const SFXRConfig := preload("../SFXRConfig.gd")
const SFXRGenerator := preload("../SFXRGenerator.gd")
@ -135,11 +136,20 @@ func _popup_message(content: String) -> void:
dialog.popup_centered()
func _popup_file_dialog(mode: int, callback: String) -> void:
func _popup_file_dialog(mode: int, callback: String, default_filename: int = DefaultFilename.EMPTY) -> void:
var dialog := EditorFileDialog.new()
add_child(dialog)
dialog.access = EditorFileDialog.ACCESS_RESOURCES
dialog.mode = mode
match default_filename:
DefaultFilename.EMPTY:
pass
DefaultFilename.GUESS_FOR_SAVE:
if _path:
dialog.current_path = _generate_serial_path(_path)
dialog.add_filter("*.sfxr; %s" % translator.tr("SFXR Audio"))
dialog.connect("popup_hide", dialog, "queue_free")
dialog.connect("file_selected", self, callback)
@ -197,6 +207,37 @@ func _sync_ui() -> void:
_syncing_ui = false
func _generate_serial_path(path: String) -> String:
var directory := Directory.new()
if directory.open(path.get_base_dir()) != OK:
return path
if not directory.file_exists(path.get_file()):
return path
var basename := path.get_basename()
var extension := path.get_extension()
# Extract trailing number.
var num_string: String
for i in range(basename.length() - 1, -1, -1):
var c: String = basename[i]
if "0" <= c and c <= "9":
num_string = c + num_string
else:
break
var number := num_string.to_int() if num_string else 0
var name_string: String = basename.substr(0, basename.length() - num_string.length())
while true:
number += 1
var attemp := "%s%d.%s" % [name_string, number, extension]
if not directory.file_exists(attemp):
return attemp
return path # Unreachable
func _on_param_changed(name, value):
if _syncing_ui:
return
@ -318,7 +359,7 @@ func _on_Extra_about_to_show():
func _on_Extra_id_pressed(id: int) -> void:
match id:
ExtraOption.SAVE_AS:
_popup_file_dialog(EditorFileDialog.MODE_SAVE_FILE, "_on_SaveAsDialog_confirmed")
_popup_file_dialog(EditorFileDialog.MODE_SAVE_FILE, "_on_SaveAsDialog_confirmed", DefaultFilename.GUESS_FOR_SAVE)
ExtraOption.COPY:
if not _config_clipboard: