Skip to content

Commit

Permalink
Code clean up (#755)
Browse files Browse the repository at this point in the history
  • Loading branch information
MewPurPur authored May 21, 2024
1 parent abced96 commit acb26a6
Show file tree
Hide file tree
Showing 19 changed files with 536 additions and 221 deletions.
4 changes: 2 additions & 2 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,11 @@ quit={
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"command_or_control_autoremap":true,"alt_pressed":false,"shift_pressed":false,"pressed":false,"keycode":81,"physical_keycode":0,"key_label":0,"unicode":113,"echo":false,"script":null)
]
}
load_reference_image={
load_reference={
"deadzone": 0.5,
"events": []
}
view_reference_image={
view_show_reference={
"deadzone": 0.5,
"events": []
}
Expand Down
21 changes: 11 additions & 10 deletions src/FileUtils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ non_native_callable: Callable) -> void:
var svg_export_dialog := GoodFileDialog.instantiate()
svg_export_dialog.setup(Utils.get_last_dir(),
Utils.get_file_name(GlobalSettings.save_data.current_file_path),
GoodFileDialogType.FileMode.SAVE, extension)
GoodFileDialogType.FileMode.SAVE, PackedStringArray([extension]))
HandlerGUI.add_overlay(svg_export_dialog)
svg_export_dialog.file_selected.connect(non_native_callable)

Expand Down Expand Up @@ -102,34 +102,35 @@ static func open_import_dialog() -> void:
else:
var svg_import_dialog := GoodFileDialog.instantiate()
svg_import_dialog.setup(Utils.get_last_dir(), "",
GoodFileDialogType.FileMode.SELECT, "svg")
GoodFileDialogType.FileMode.SELECT, PackedStringArray(["svg"]))
HandlerGUI.add_overlay(svg_import_dialog)
svg_import_dialog.file_selected.connect(apply_svg_from_path)

static func open_reference_import_dialog() -> void:
static func open_reference_load_dialog() -> void:
if FileUtils._is_native_preferred():
DisplayServer.file_dialog_show(TranslationServer.translate("Import a .png file"),
DisplayServer.file_dialog_show(TranslationServer.translate("Load an image file"),
Utils.get_last_dir(), "", false, DisplayServer.FILE_DIALOG_MODE_OPEN_FILE,
["*.png"], native_file_import)
# TODO : Add Web Support
["*.svg", "*.png", "*.jpeg", "*.jpg", "*.webp"], native_file_import)
# TODO: Add Web Support
#elif OS.has_feature("web"):
#HandlerGUI.web_load_svg()
else:
var png_import_dialog := GoodFileDialog.instantiate()
png_import_dialog.setup(Utils.get_last_dir(), "",
GoodFileDialogType.FileMode.SELECT, "png")
GoodFileDialogType.FileMode.SELECT,
PackedStringArray(["svg", "png", "jpeg", "jpg", "webp"]))
HandlerGUI.add_overlay(png_import_dialog)
png_import_dialog.file_selected.connect(reference_image_import)
png_import_dialog.file_selected.connect(load_reference_image)

static func native_file_import(has_selected: bool, files: PackedStringArray,
_filter_idx: int) -> void:
if has_selected:
if files[0].ends_with(".svg"):
apply_svg_from_path(files[0])
else:
reference_image_import(files[0])
load_reference_image(files[0])

static func reference_image_import(path : String) -> void:
static func load_reference_image(path: String) -> void:
GlobalSettings.modify_save_data("reference_path", path)
Indications.imported_reference.emit()

Expand Down
4 changes: 2 additions & 2 deletions src/GlobalSettings.gd
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ var default_input_events := {} # Dictionary{String: Array[InputEvent]}
const keybinds_dict = {
"file": {
"import": true,
"load_reference_image": true,
"export": true,
"save": true,
"optimize": true,
Expand All @@ -94,7 +93,8 @@ const keybinds_dict = {
"view_show_grid": true,
"view_show_handles": true,
"view_rasterized_svg": true,
"view_reference_image": true,
"load_reference": true,
"view_show_reference": true,
"view_overlay_reference": true,
},
"tool": {
Expand Down
4 changes: 2 additions & 2 deletions src/TranslationUtils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ static func get_shortcut_description(action_name: String) -> String:
match action_name:
"export": return TranslationServer.translate("Export")
"import": return TranslationServer.translate("Import")
"load_reference_image": return TranslationServer.translate("Load Reference Image")
"save": return TranslationServer.translate("Save SVG")
"optimize": return TranslationServer.translate("Optimize")
"copy_svg_text": return TranslationServer.translate("Copy all text")
Expand All @@ -24,7 +23,8 @@ static func get_shortcut_description(action_name: String) -> String:
"view_show_grid": return TranslationServer.translate("Show grid")
"view_show_handles": return TranslationServer.translate("Show handles")
"view_rasterized_svg": return TranslationServer.translate("Show rasterized SVG")
"view_reference_image": return TranslationServer.translate("Show reference image")
"load_reference": return TranslationServer.translate("Load reference image")
"view_show_reference": return TranslationServer.translate("Show reference image")
"view_overlay_reference": return TranslationServer.translate("Overlay reference image")
"debug": return TranslationServer.translate("View debug information")
"move_relative": return "%s (%s)" %\
Expand Down
2 changes: 1 addition & 1 deletion src/ui_parts/confirm_dialog.gd
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ func setup(title: String, message: String, action_text: String, action: Callable
action_button.text = action_text
action_button.pressed.connect(action)
action_button.grab_focus()
label.custom_minimum_size.x = 280.0
label.custom_minimum_size.x = 300.0
28 changes: 19 additions & 9 deletions src/ui_parts/display.gd
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const NumberEditType = preload("res://src/ui_elements/number_edit.gd")
const BetterToggleButtonType = preload("res://src/ui_elements/BetterToggleButton.gd")

const NumberField = preload("res://src/ui_elements/number_field.tscn")
const ConfirmDialog := preload("res://src/ui_parts/confirm_dialog.tscn")

@onready var viewport: SubViewport = %Viewport
@onready var controls: Control = %Viewport/Checkerboard/Controls
Expand Down Expand Up @@ -61,7 +62,7 @@ func _unhandled_input(input_event: InputEvent) -> void:
debug_container.show()
update_debug()
input_debug_label.text = ""
elif input_event.is_action_pressed("load_reference_image"):
elif input_event.is_action_pressed("load_reference"):
load_reference_image()
elif input_event.is_action_pressed("open_settings"):
_on_settings_pressed()
Expand All @@ -71,7 +72,7 @@ func _unhandled_input(input_event: InputEvent) -> void:
toggle_handles_visuals()
elif input_event.is_action_pressed("view_rasterized_svg"):
toggle_rasterization()
elif input_event.is_action_pressed("view_reference_image"):
elif input_event.is_action_pressed("view_show_reference"):
toggle_reference_image()
elif input_event.is_action_pressed("view_overlay_reference"):
toggle_reference_overlay()
Expand All @@ -85,6 +86,8 @@ func _unhandled_input(input_event: InputEvent) -> void:
open_about()
elif input_event.is_action_pressed("about_donate"):
open_sponsor()
elif input_event.is_action_pressed("check_updates"):
open_update_checker()


func update_translations() -> void:
Expand Down Expand Up @@ -125,11 +128,11 @@ func _on_settings_pressed() -> void:

func _on_reference_pressed() -> void:
var btn_arr: Array[Button] = [
ContextPopup.create_button(TranslationServer.translate("Import Reference Image"),
ContextPopup.create_button(TranslationServer.translate("Load reference image"),
load_reference_image, false, load("res://visual/icons/Reference.svg")),
ContextPopup.create_checkbox(TranslationServer.translate("Show Reference Image"),
toggle_reference_image, reference_texture.visible),
ContextPopup.create_checkbox(TranslationServer.translate("Overlay Reference Image"),
ContextPopup.create_checkbox(TranslationServer.translate("Show reference"),
toggle_reference_image, reference_texture.visible, ""),
ContextPopup.create_checkbox(TranslationServer.translate("Overlay reference"),
toggle_reference_overlay, reference_overlay)
]

Expand Down Expand Up @@ -191,6 +194,14 @@ func open_sponsor() -> void:
HandlerGUI.add_overlay(donate_menu_instance)

func open_update_checker() -> void:
var confirmation_dialog := ConfirmDialog.instantiate()
HandlerGUI.add_overlay(confirmation_dialog)
confirmation_dialog.setup(TranslationServer.translate("Check for updates?"),
TranslationServer.translate("This requires GodSVG to connect to the internet."),
TranslationServer.translate("OK"), _list_updates)

func _list_updates() -> void:
HandlerGUI.remove_all_overlays()
var update_menu_instance := update_menu.instantiate()
HandlerGUI.add_overlay(update_menu_instance)

Expand All @@ -214,14 +225,13 @@ func toggle_reference_image() -> void:

func toggle_reference_overlay() -> void:
reference_overlay = not reference_overlay

if reference_overlay:
viewport.move_child(reference_texture, 2)
viewport.move_child(reference_texture, viewport.get_child_count() - 1)
else:
viewport.move_child(reference_texture, 0)

func load_reference_image() -> void:
FileUtils.open_reference_import_dialog()
FileUtils.open_reference_load_dialog()
await Indications.imported_reference
var ref_path = GlobalSettings.save_data.get("reference_path")
var img = Image.load_from_file(ref_path)
Expand Down
65 changes: 35 additions & 30 deletions src/ui_parts/good_file_dialog.gd
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const system_dirs_to_show = [OS.SYSTEM_DIR_DESKTOP, OS.SYSTEM_DIR_DOCUMENTS,
enum FileMode {SELECT, SAVE}
var mode: FileMode

var current_dir: String
var current_dir := ""
var current_file := ""
var default_file := ""
var extension := ""
var extensions := PackedStringArray()

var item_height := 16
var search_text := ""
Expand Down Expand Up @@ -75,13 +75,13 @@ func call_right_click_action(actions: Actions) -> void:


func setup(new_dir: String, new_file: String, new_mode: FileMode,
new_extension: String) -> void:
new_extensions: PackedStringArray) -> void:
current_dir = new_dir
current_file = new_file
if new_mode == FileMode.SAVE:
default_file = new_file
mode = new_mode
extension = new_extension
extensions = new_extensions


func _ready() -> void:
Expand Down Expand Up @@ -110,13 +110,19 @@ func _ready() -> void:
TranslationServer.translate("Toggle the visibility of hidden files")
search_button.tooltip_text = TranslationServer.translate("Search files")

title_label.text = TranslationServer.translate("Select an SVG") if\
mode == FileMode.SELECT else TranslationServer.translate("Save SVG")
if mode != FileMode.SELECT:
title_label.text = TranslationServer.translate("Save SVG")
extension_label.text = "." + extensions[0]
else:
if extensions.size() == 1 and extensions[0] == "svg":
title_label.text = TranslationServer.translate("Select an SVG")
else:
title_label.text = TranslationServer.translate("Select an image")

close_button.text = TranslationServer.translate("Close")
special_button.text = TranslationServer.translate("Select") if\
mode == FileMode.SELECT else TranslationServer.translate("Save")
path_label.text = TranslationServer.translate("Path") + ":"
extension_label.text = "." + extension

# Should always be safe.
refresh_dir()
Expand Down Expand Up @@ -194,7 +200,7 @@ func set_dir(dir: String) -> void:
enter_dir.bind(dir_path), unfocus_file, open_dir_context.bind(dir_path)))

for file in files:
if file.get_extension() != extension or\
if not file.get_extension() in extensions or\
(not search_text.is_empty() and not search_text.is_subsequence_ofn(file)):
continue

Expand All @@ -214,9 +220,9 @@ func set_file(file: String) -> void:
else:
special_button.disabled = false
special_button.mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
if not file.is_empty():
if file.get_extension() != extension:
file += "." + extension
if not file.is_empty() and not file.get_extension() in extensions and\
extensions.size() == 1:
file += "." + extensions[0]
file_list.ensure_current_is_visible()
current_file = file
file_field.text = current_file
Expand All @@ -230,25 +236,24 @@ func _setup_file_images() -> void:
if !is_instance_valid(file_list.get_item_icon(item_idx)) and\
file_rect.end.y > visible_start and file_rect.position.y < visible_end:
var file := file_list.get_item_text(item_idx)
match file.get_extension():
"png":
var img := Image.load_from_file(current_dir.path_join(file))
if !is_instance_valid(img) or img.is_empty():
file_list.set_item_icon(item_idx, broken_file_icon)
else:
file_list.set_item_icon(item_idx, ImageTexture.create_from_image(img))
"svg":
# Setup a clean SVG graphic by using the scaling parameter.
var svg_text := FileAccess.open(current_dir.path_join(file),
FileAccess.READ).get_as_text()
var img := Image.new()
img.load_svg_from_string(svg_text)
if !is_instance_valid(img) or img.is_empty():
file_list.set_item_icon(item_idx, broken_file_icon)
else:
img.load_svg_from_string(svg_text,
item_height / maxf(img.get_width(), img.get_height()))
file_list.set_item_icon(item_idx, ImageTexture.create_from_image(img))
if file.get_extension() == "svg":
# Setup a clean SVG graphic by using the scaling parameter.
var svg_text := FileAccess.open(current_dir.path_join(file),
FileAccess.READ).get_as_text()
var img := Image.new()
img.load_svg_from_string(svg_text)
if !is_instance_valid(img) or img.is_empty():
file_list.set_item_icon(item_idx, broken_file_icon)
else:
img.load_svg_from_string(svg_text,
item_height / maxf(img.get_width(), img.get_height()))
file_list.set_item_icon(item_idx, ImageTexture.create_from_image(img))
else:
var img := Image.load_from_file(current_dir.path_join(file))
if !is_instance_valid(img) or img.is_empty():
file_list.set_item_icon(item_idx, broken_file_icon)
else:
file_list.set_item_icon(item_idx, ImageTexture.create_from_image(img))


func select_file() -> void:
Expand Down
15 changes: 11 additions & 4 deletions src/ui_parts/settings_menu.gd
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,19 @@ func disable_format_checkboxes() -> void:
%ColorVBox/UseNamedColors.set_checkbox_enabled(is_autoformatting_colors)


var shortcut_tab_names := {
"file": TranslationServer.translate("File"),
"edit": TranslationServer.translate("Edit"),
"view": TranslationServer.translate("View"),
"tool": TranslationServer.translate("Tool"),
"help": TranslationServer.translate("Help"),
}

func setup_shortcuts_tab() -> void:
for setup_arr in [["file", "File"], ["edit", "Edit"], ["view", "View"],
["tool", "Tool"], ["help", "Help"]]:
for tab_idx in shortcut_tab_names:
var btn := Button.new()
btn.pressed.connect(show_keybinds.bind(setup_arr[0]))
btn.text = TranslationServer.translate(setup_arr[1])
btn.pressed.connect(show_keybinds.bind(tab_idx))
btn.text = shortcut_tab_names[tab_idx]
btn.mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
shortcut_categories.add_child(btn)
# Add them all to a button group.
Expand Down
Loading

0 comments on commit acb26a6

Please sign in to comment.