1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-02-20 12:33:14 +00:00

Move even more code away from Global

The idea is to make the project less dependant on Global. Global should strictly be used for reusable code in multiple places, not as a second Main. I just hope I'm not breaking anything during this process.
This commit is contained in:
Manolis Papadeas 2021-11-22 17:37:06 +02:00
parent 19c062fa07
commit 36680795ee
4 changed files with 66 additions and 64 deletions

View file

@ -20,7 +20,6 @@ var projects := [] # Array of Projects
var current_project : Project
var current_project_index := 0 setget project_changed
var recent_projects := []
var panel_layout = PanelLayout.AUTO
# Indices are as in the Direction enum
@ -194,21 +193,16 @@ onready var current_version : String = ProjectSettings.get_setting("application/
func _ready() -> void:
randomize()
if OS.get_name() == "OSX":
use_osx_shortcuts()
if OS.has_feature("standalone"):
root_directory = OS.get_executable_path().get_base_dir()
# root_directory must be set earlier than this is because XDGDataDirs depends on it
directory_module = XDGDataPaths.new()
# Load settings from the config file
config_cache.load("user://cache.ini")
recent_projects = config_cache.get_value("data", "recent_projects", [])
panel_layout = config_cache.get_value("window", "panel_layout", PanelLayout.AUTO)
# root_directory must be set earlier than this is because XDGDataDirs depends on it
directory_module = XDGDataPaths.new()
Input.set_custom_mouse_cursor(cursor_image, Input.CURSOR_CROSS, Vector2(15, 15))
projects.append(Project.new())
projects[0].layers.append(Layer.new())
current_project = projects[0]
@ -510,54 +504,3 @@ Hold %s to displace the shape's origin""") % [InputMap.get_action_list("left_ell
func is_cjk(locale : String) -> bool:
return "zh" in locale or "ko" in locale or "ja" in locale
func _exit_tree() -> void:
config_cache.set_value("window", "panel_layout", panel_layout)
config_cache.set_value("window", "screen", OS.current_screen)
config_cache.set_value("window", "maximized", OS.window_maximized || OS.window_fullscreen)
config_cache.set_value("window", "position", OS.window_position)
config_cache.set_value("window", "size", OS.window_size)
config_cache.save("user://cache.ini")
var i := 0
for project in projects:
project.undo_redo.free()
OpenSave.remove_backup(i)
i += 1
func save_project_to_recent_list(path : String) -> void:
if path.get_file().substr(0, 7) == "backup-" or path == "":
return
if recent_projects.has(path):
return
if recent_projects.size() >= 5:
recent_projects.pop_front()
recent_projects.push_back(path)
config_cache.set_value("data", "recent_projects", recent_projects)
recent_projects_submenu.clear()
update_recent_projects_submenu()
func update_recent_projects_submenu() -> void:
for project in recent_projects:
recent_projects_submenu.add_item(project.get_file())
func use_osx_shortcuts() -> void:
var inputmap := InputMap
for action in inputmap.get_actions():
var event : InputEvent = inputmap.get_action_list(action)[0]
if event.is_action("show_pixel_grid"):
event.shift = true
if event.control:
event.control = false
event.command = true

View file

@ -124,7 +124,7 @@ func open_pxo_file(path : String, untitled_backup : bool = false, replace_empty
Global.top_menu_container.file_menu.set_item_text(4, tr("Save") + " %s" % path.get_file())
Global.top_menu_container.file_menu.set_item_text(6, tr("Export"))
Global.save_project_to_recent_list(path)
save_project_to_recent_list(path)
# For pxo files older than v0.8
@ -343,7 +343,7 @@ func save_pxo_file(path : String, autosave : bool, use_zstd_compression := true,
project.was_exported = false
Global.top_menu_container.file_menu.set_item_text(4, tr("Save") + " %s" % path.get_file())
Global.save_project_to_recent_list(path)
save_project_to_recent_list(path)
func open_image_as_new_tab(path : String, image : Image) -> void:
@ -615,3 +615,21 @@ func reload_backup_file(project_paths : Array, backup_paths : Array) -> void:
Global.current_project.has_changed = true
Global.notification_label("Backup reloaded")
func save_project_to_recent_list(path : String) -> void:
var top_menu_container : Panel = Global.top_menu_container
if path.get_file().substr(0, 7) == "backup-" or path == "":
return
if top_menu_container.recent_projects.has(path):
return
if top_menu_container.recent_projects.size() >= 5:
top_menu_container.recent_projects.pop_front()
top_menu_container.recent_projects.push_back(path)
Global.config_cache.set_value("data", "recent_projects", top_menu_container.recent_projects)
Global.recent_projects_submenu.clear()
top_menu_container.update_recent_projects_submenu()

View file

@ -21,6 +21,7 @@ onready var scroll_container := $MenuAndUI/UI/RightPanel/MarginContainer/Preview
func _ready() -> void:
randomize()
add_child(alternate_transparent_background)
move_child(alternate_transparent_background,0)
alternate_transparent_background.visible = false
@ -34,6 +35,10 @@ func _ready() -> void:
handle_resize()
get_tree().get_root().connect("size_changed", self, "handle_resize")
if OS.get_name() == "OSX":
use_osx_shortcuts()
Input.set_custom_mouse_cursor(Global.cursor_image, Input.CURSOR_CROSS, Vector2(15, 15))
Global.window_title = tr("untitled") + " - Pixelorama " + Global.current_version
Global.current_project.layers[0].name = tr("Layer") + " 0"
@ -422,3 +427,32 @@ func _on_BackupConfirmation_delete(project_paths : Array, backup_paths : Array)
func _on_BackupConfirmation_popup_hide() -> void:
OpenSave.autosave_timer.start()
func use_osx_shortcuts() -> void:
var inputmap := InputMap
for action in inputmap.get_actions():
var event : InputEvent = inputmap.get_action_list(action)[0]
if event.is_action("show_pixel_grid"):
event.shift = true
if event.control:
event.control = false
event.command = true
func _exit_tree() -> void:
Global.config_cache.set_value("window", "panel_layout", Global.panel_layout)
Global.config_cache.set_value("window", "screen", OS.current_screen)
Global.config_cache.set_value("window", "maximized", OS.window_maximized || OS.window_fullscreen)
Global.config_cache.set_value("window", "position", OS.window_position)
Global.config_cache.set_value("window", "size", OS.window_size)
Global.config_cache.save("user://cache.ini")
var i := 0
for project in Global.projects:
project.undo_redo.free()
OpenSave.remove_backup(i)
i += 1

View file

@ -21,6 +21,7 @@ onready var panel_layout_submenu : PopupMenu = PopupMenu.new()
var file_menu : PopupMenu
var view_menu : PopupMenu
var zen_mode := false
var recent_projects := []
func _ready() -> void:
@ -62,13 +63,19 @@ func setup_file_menu() -> void:
func setup_recent_projects_submenu(item : String) -> void:
recent_projects = Global.config_cache.get_value("data", "recent_projects", [])
Global.recent_projects_submenu.connect("id_pressed", self, "on_recent_projects_submenu_id_pressed")
Global.update_recent_projects_submenu()
update_recent_projects_submenu()
file_menu.add_child(Global.recent_projects_submenu)
file_menu.add_submenu_item(item, Global.recent_projects_submenu.get_name())
func update_recent_projects_submenu() -> void:
for project in recent_projects:
Global.recent_projects_submenu.add_item(project.get_file())
func setup_edit_menu() -> void:
var edit_menu_items := { # order as in EditMenuId enum
"Undo" : InputMap.get_action_list("undo")[0].get_scancode_with_modifiers(),
@ -296,7 +303,7 @@ func export_file() -> void:
func on_recent_projects_submenu_id_pressed(id : int) -> void:
Global.control.load_recent_project_file(Global.recent_projects[id])
Global.control.load_recent_project_file(recent_projects[id])
func edit_menu_id_pressed(id : int) -> void: