From acce1dda2e07f0675e100fbf39d75ce00510dcff Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Sat, 23 Mar 2024 02:12:28 +0200 Subject: [PATCH] Move even more code away from `Project.change_project()` Making Pixelorama more modular by relying more on signals --- src/Classes/Project.gd | 53 ------------------- src/UI/Canvas/Rulers/Guide.gd | 13 +++++ src/UI/PerspectiveEditor/PerspectiveEditor.gd | 20 ++++--- src/UI/ReferenceImages/ReferenceImage.gd | 16 +++++- src/UI/TopMenuContainer/TopMenuContainer.gd | 23 +++++++- 5 files changed, 61 insertions(+), 64 deletions(-) diff --git a/src/Classes/Project.gd b/src/Classes/Project.gd index cfbf992de..850e422ff 100644 --- a/src/Classes/Project.gd +++ b/src/Classes/Project.gd @@ -191,72 +191,19 @@ func selection_map_changed() -> void: func change_project() -> void: Global.animation_timeline.project_changed() animation_tags = animation_tags - # Change the guides - for guide in Global.canvas.get_children(): - if guide is Guide: - if guide in guides: - guide.visible = Global.show_guides - if guide is SymmetryGuide: - if guide.type == Guide.Types.HORIZONTAL: - guide.visible = Global.show_x_symmetry_axis and Global.show_guides - else: - guide.visible = Global.show_y_symmetry_axis and Global.show_guides - else: - guide.visible = false # Change the project brushes Brushes.clear_project_brush() for brush in brushes: Brushes.add_project_brush(brush) - Global.transparent_checker.update_rect() - Global.perspective_editor.update_points() Global.cursor_position_label.text = "[%s×%s]" % [size.x, size.y] - Global.main_window.title = "%s - Pixelorama %s" % [name, Global.current_version] if has_changed: Global.main_window.title = Global.main_window.title + "(*)" - if export_directory_path != "": Global.open_sprites_dialog.current_path = export_directory_path Global.save_sprites_dialog.current_path = export_directory_path - Global.top_menu_container.file_menu.set_item_text( - Global.FileMenu.SAVE, tr("Save") + " %s" % file_name - ) - else: - Global.top_menu_container.file_menu.set_item_text(Global.FileMenu.SAVE, tr("Save")) - - if not was_exported: - Global.top_menu_container.file_menu.set_item_text(Global.FileMenu.EXPORT, tr("Export")) - else: - if export_overwrite: - Global.top_menu_container.file_menu.set_item_text( - Global.FileMenu.EXPORT, - tr("Overwrite") + " %s" % (file_name + Export.file_format_string(file_format)) - ) - else: - Global.top_menu_container.file_menu.set_item_text( - Global.FileMenu.EXPORT, - tr("Export") + " %s" % (file_name + Export.file_format_string(file_format)) - ) - - for j in Tiles.MODE.values(): - Global.top_menu_container.tile_mode_submenu.set_item_checked(j, j == tiles.mode) - selection_map_changed() - var edit_menu_popup: PopupMenu = Global.top_menu_container.edit_menu - edit_menu_popup.set_item_disabled(Global.EditMenu.NEW_BRUSH, !has_selection) - - # We loop through all the reference image nodes and the ones that are not apart - # of the current project we remove from the tree - # They will still be in memory though - for ri: ReferenceImage in Global.canvas.reference_image_container.get_children(): - if !reference_images.has(ri): - Global.canvas.reference_image_container.remove_child(ri) - # Now we loop through this projects reference images and add them back to the tree - var canvas_references := Global.canvas.reference_image_container.get_children() - for ri: ReferenceImage in reference_images: - if !canvas_references.has(ri) and !ri.is_inside_tree(): - Global.canvas.reference_image_container.add_child(ri) func serialize() -> Dictionary: diff --git a/src/UI/Canvas/Rulers/Guide.gd b/src/UI/Canvas/Rulers/Guide.gd index 14e05b0e6..e862512e7 100644 --- a/src/UI/Canvas/Rulers/Guide.gd +++ b/src/UI/Canvas/Rulers/Guide.gd @@ -13,6 +13,7 @@ var project := Global.current_project func _ready() -> void: + Global.project_switched.connect(_project_switched) width = 2.0 / Global.camera.zoom.x default_color = Global.guide_color project.guides.append(self) @@ -208,6 +209,18 @@ func set_color(color: Color) -> void: default_color = color +func _project_switched() -> void: + if self in Global.current_project.guides: + visible = Global.show_guides + if self is SymmetryGuide: + if type == Types.HORIZONTAL: + visible = Global.show_x_symmetry_axis and Global.show_guides + else: + visible = Global.show_y_symmetry_axis and Global.show_guides + else: + visible = false + + func _outside_canvas() -> bool: if type == Types.HORIZONTAL: return points[0].y < 0 || points[0].y > project.size.y diff --git a/src/UI/PerspectiveEditor/PerspectiveEditor.gd b/src/UI/PerspectiveEditor/PerspectiveEditor.gd index 72898d36a..ab9c6641c 100644 --- a/src/UI/PerspectiveEditor/PerspectiveEditor.gd +++ b/src/UI/PerspectiveEditor/PerspectiveEditor.gd @@ -8,6 +8,10 @@ var tracker_disabled := false @onready var vanishing_point_container = $"%VanishingPointContainer" +func _ready() -> void: + Global.project_switched.connect(_update_points) + + func _on_AddPoint_pressed() -> void: do_pool.clear() # Reset (clears Redo history of vanishing points) var project := Global.current_project @@ -18,12 +22,12 @@ func _on_AddPoint_pressed() -> void: project.undo_redo.commit_action() -func _on_TrackerLines_toggled(button_pressed: bool): +func _on_TrackerLines_toggled(button_pressed: bool) -> void: for point in vanishing_point_container.get_children(): tracker_disabled = !button_pressed -func add_vanishing_point(is_redo := false): +func add_vanishing_point(is_redo := false) -> void: var vanishing_point := vanishing_point_res.instantiate() vanishing_point_container.add_child(vanishing_point) if is_redo and !do_pool.is_empty(): @@ -34,14 +38,14 @@ func add_vanishing_point(is_redo := false): vanishing_point.initiate() -func undo_add_vanishing_point(): +func undo_add_vanishing_point() -> void: var point = vanishing_point_container.get_child(vanishing_point_container.get_child_count() - 1) point.queue_free() do_pool.append(point.serialize()) point.update_data_to_project(true) -func delete_point(idx: int): +func delete_point(idx: int) -> void: var project := Global.current_project project.undos += 1 project.undo_redo.create_action("Delete Vanishing Point") @@ -50,20 +54,20 @@ func delete_point(idx: int): project.undo_redo.commit_action() -func do_delete_point(idx: int): +func do_delete_point(idx: int) -> void: var point = vanishing_point_container.get_child(idx) delete_pool.append(point.serialize()) point.queue_free() point.update_data_to_project(true) -func undo_delete_point(idx: int): +func undo_delete_point(idx: int) -> void: var point = delete_pool.pop_back() Global.current_project.vanishing_points.insert(idx, point) - update_points() + _update_points() -func update_points(): +func _update_points() -> void: # Delete old vanishing points for c in vanishing_point_container.get_children(): c.queue_free() diff --git a/src/UI/ReferenceImages/ReferenceImage.gd b/src/UI/ReferenceImages/ReferenceImage.gd index 3600acedf..be32fa56b 100644 --- a/src/UI/ReferenceImages/ReferenceImage.gd +++ b/src/UI/ReferenceImages/ReferenceImage.gd @@ -5,9 +5,7 @@ extends Sprite2D signal properties_changed var project := Global.current_project - var shader := preload("res://src/Shaders/ReferenceImageShader.gdshader") - var image_path := "" var filter := false: set(value): @@ -33,8 +31,11 @@ var color_clamping := 0.0: if material: get_material().set_shader_parameter("clamping", value) +@onready var parent := get_parent() + func _ready() -> void: + Global.project_switched.connect(_project_switched) project.reference_images.append(self) # Make this show behind parent because we want to use _draw() to draw over it show_behind_parent = true @@ -131,3 +132,14 @@ func create_from_image(image: Image) -> void: var itex := ImageTexture.create_from_image(image) texture = itex position_reset() + + +func _project_switched() -> void: + # Remove from the tree if it doesn't belong to the current project. + # It will still be in memory though. + if Global.current_project.reference_images.has(self): + if not is_inside_tree(): + parent.add_child(self) + else: + if is_inside_tree(): + parent.remove_child(self) diff --git a/src/UI/TopMenuContainer/TopMenuContainer.gd b/src/UI/TopMenuContainer/TopMenuContainer.gd index 9fc85113a..66665175d 100644 --- a/src/UI/TopMenuContainer/TopMenuContainer.gd +++ b/src/UI/TopMenuContainer/TopMenuContainer.gd @@ -77,7 +77,7 @@ class Dialog: func _ready() -> void: - Global.project_switched.connect(_update_current_frame_mark) + Global.project_switched.connect(_project_switched) Global.cel_switched.connect(_update_current_frame_mark) _setup_file_menu() _setup_edit_menu() @@ -88,6 +88,27 @@ func _ready() -> void: _setup_help_menu() +func _project_switched() -> void: + var project := Global.current_project + edit_menu.set_item_disabled(Global.EditMenu.NEW_BRUSH, not project.has_selection) + if project.export_directory_path.is_empty(): + file_menu.set_item_text(Global.FileMenu.SAVE, tr("Save")) + else: + file_menu.set_item_text(Global.FileMenu.SAVE, tr("Save") + " %s" % project.file_name) + if project.was_exported: + var f_name := " %s" % (project.file_name + Export.file_format_string(project.file_format)) + if project.export_overwrite: + file_menu.set_item_text(Global.FileMenu.EXPORT, tr("Overwrite") + f_name) + else: + file_menu.set_item_text(Global.FileMenu.EXPORT, tr("Export") + f_name) + else: + file_menu.set_item_text(Global.FileMenu.EXPORT, tr("Export")) + for j in Tiles.MODE.values(): + tile_mode_submenu.set_item_checked(j, j == project.tiles.mode) + + _update_current_frame_mark() + + func _update_current_frame_mark() -> void: var project := Global.current_project current_frame_mark.text = "%s/%s" % [str(project.current_frame + 1), project.frames.size()]