From 62fc405cc57e0f5d53818781152eb1fd59bc3ee9 Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 20 Jan 2022 21:12:05 +0100 Subject: [PATCH] Undo redo support for the uv editor. --- .../addons/mesh_data_resource_editor/MDIEd.gd | 87 ++++++++++--------- .../mesh_data_resource_editor/plugin.gd | 2 +- .../uv_editor/RectEditor.gd | 6 +- .../uv_editor/RectView.gd | 51 +++++++++-- 4 files changed, 98 insertions(+), 48 deletions(-) diff --git a/game/addons/mesh_data_resource_editor/MDIEd.gd b/game/addons/mesh_data_resource_editor/MDIEd.gd index be201dd2..cbd7c7e0 100644 --- a/game/addons/mesh_data_resource_editor/MDIEd.gd +++ b/game/addons/mesh_data_resource_editor/MDIEd.gd @@ -1,7 +1,7 @@ tool extends Control -var plugin : EditorPlugin +var _plugin : EditorPlugin export var uv_preview_path : NodePath export var uv_editor_path : NodePath @@ -13,6 +13,15 @@ func _enter_tree(): uv_preview = get_node(uv_preview_path) uv_editor = get_node(uv_editor_path) + if _plugin && uv_editor: + uv_editor.set_plugin(_plugin) + +func set_plugin(plugin : EditorPlugin) -> void: + _plugin = plugin + + if uv_editor: + uv_editor.set_plugin(plugin) + func set_mesh_data_resource(a : MeshDataResource) -> void: if uv_preview: uv_preview.set_mesh_data_resource(a) @@ -67,18 +76,18 @@ func set_edit_mode_scale() -> void: func on_edit_mode_translate_toggled(on : bool) -> void: if on: - if plugin: - plugin.set_translate(on) + if _plugin: + _plugin.set_translate(on) func on_edit_mode_rotate_toggled(on : bool) -> void: if on: - if plugin: - plugin.set_rotate(on) + if _plugin: + _plugin.set_rotate(on) func on_edit_mode_scale_toggled(on : bool) -> void: if on: - if plugin: - plugin.set_scale(on) + if _plugin: + _plugin.set_scale(on) #axis locks func get_axis_x() -> bool: @@ -100,32 +109,32 @@ func set_axis_z(on : bool) -> void: $VBoxContainer/Actions/Actions/VBoxContainer2/HBoxContainer2/AxisZ.pressed = on func on_axis_x_toggled(on : bool) -> void: - if plugin: - plugin.set_axis_x(on) + if _plugin: + _plugin.set_axis_x(on) func on_axis_y_toggled(on : bool) -> void: - if plugin: - plugin.set_axis_y(on) + if _plugin: + _plugin.set_axis_y(on) func on_axis_z_toggled(on : bool) -> void: - if plugin: - plugin.set_axis_z(on) + if _plugin: + _plugin.set_axis_z(on) #selection modes func on_selection_mode_vertex_toggled(on : bool) -> void: if on: - if plugin: - plugin.set_selection_mode_vertex() + if _plugin: + _plugin.set_selection_mode_vertex() func on_selection_mode_edge_toggled(on : bool) -> void: if on: - if plugin: - plugin.set_selection_mode_edge() + if _plugin: + _plugin.set_selection_mode_edge() func on_selection_mode_face_toggled(on : bool) -> void: if on: - if plugin: - plugin.set_selection_mode_face() + if _plugin: + _plugin.set_selection_mode_face() func set_selection_mode_vertex() -> void: $VBoxContainer/Actions/Actions/VBoxContainer2/HBoxContainer3/Vertex.pressed = true @@ -141,64 +150,64 @@ func _on_Extrude_pressed(): pass # Replace with function body. func _on_AddBox_pressed(): - plugin.add_box() + _plugin.add_box() func _on_UnwrapButton_pressed(): - plugin.uv_unwrap() + _plugin.uv_unwrap() func _on_add_triangle_pressed(): - plugin.add_triangle() + _plugin.add_triangle() func _on_add_quad_pressed(): - plugin.add_quad() + _plugin.add_quad() func _on_split_pressed(): - plugin.split() + _plugin.split() func _on_connect_to_first_selected_pressed(): - plugin.onnect_to_first_selected() + _plugin.onnect_to_first_selected() func _on_connect_to_avg_pressed(): - plugin.connect_to_avg() + _plugin.connect_to_avg() func _on_connect_to_last_selected_pressed(): - plugin.connect_to_last_selected() + _plugin.connect_to_last_selected() func _on_disconnect_pressed(): - plugin.disconnect_action() + _plugin.disconnect_action() func _on_add_triangle_at_pressed(): - plugin.add_triangle_at() + _plugin.add_triangle_at() func _on_add_auad_at_pressed(): - plugin.add_quad_at() + _plugin.add_quad_at() func _oncreate_face_pressed(): - plugin.create_face() + _plugin.create_face() func _on_delete_pressed(): - plugin.delete_selected() + _plugin.delete_selected() func _on_GenNormals_pressed(): - plugin.generate_normals() + _plugin.generate_normals() func _on_RemDoubles_pressed(): - plugin.remove_doubles() + _plugin.remove_doubles() func _on_MergeOptimize_pressed(): - plugin.merge_optimize() + _plugin.merge_optimize() func _on_GenTangents_pressed(): - plugin.generate_tangents() + _plugin.generate_tangents() func _on_mark_seam_pressed(): - plugin.mark_seam() + _plugin.mark_seam() func _on_unmark_seam_pressed(): - plugin.unmark_seam() + _plugin.unmark_seam() func _on_apply_seams_pressed(): - plugin.apply_seam() + _plugin.apply_seam() func _on_uv_edit_pressed(): $Popups/UVEditorPopup.popup_centered() diff --git a/game/addons/mesh_data_resource_editor/plugin.gd b/game/addons/mesh_data_resource_editor/plugin.gd index 98f4fecb..d90acb85 100644 --- a/game/addons/mesh_data_resource_editor/plugin.gd +++ b/game/addons/mesh_data_resource_editor/plugin.gd @@ -18,7 +18,7 @@ func _enter_tree(): gizmo_plugin = MdiGizmoPlugin.new() mdi_ed_gui = MDIEdGui.instance() - mdi_ed_gui.plugin = self + mdi_ed_gui.set_plugin(self) active_gizmos = [] gizmo_plugin.plugin = self diff --git a/game/addons/mesh_data_resource_editor/uv_editor/RectEditor.gd b/game/addons/mesh_data_resource_editor/uv_editor/RectEditor.gd index 8226092f..b48f31f0 100644 --- a/game/addons/mesh_data_resource_editor/uv_editor/RectEditor.gd +++ b/game/addons/mesh_data_resource_editor/uv_editor/RectEditor.gd @@ -1,9 +1,8 @@ tool extends PanelContainer -func _init(): -# Control/EditorZoomWidget - pass +func set_plugin(plugin : EditorPlugin) -> void: + $ScrollContainer/MarginContainer/RectView.set_plugin(plugin) func set_mesh_data_resource(a : MeshDataResource) -> void: $ScrollContainer/MarginContainer/RectView.set_mesh_data_resource(a) @@ -16,3 +15,4 @@ func ok_pressed() -> void: func cancel_pressed() -> void: $ScrollContainer/MarginContainer/RectView.cancel_pressed() + diff --git a/game/addons/mesh_data_resource_editor/uv_editor/RectView.gd b/game/addons/mesh_data_resource_editor/uv_editor/RectView.gd index 91d90eb4..322af4cf 100644 --- a/game/addons/mesh_data_resource_editor/uv_editor/RectView.gd +++ b/game/addons/mesh_data_resource_editor/uv_editor/RectView.gd @@ -17,6 +17,9 @@ var edited_resource_current_size : Vector2 = Vector2() var _stored_uvs : PoolVector2Array = PoolVector2Array() +var _plugin : EditorPlugin = null +var _undo_redo : UndoRedo = null + func _enter_tree(): var zoom_widget : Node = get_node_or_null(zoom_widget_path) @@ -29,6 +32,11 @@ func _enter_tree(): if !is_connected("visibility_changed", self, "on_visibility_changed"): connect("visibility_changed", self, "on_visibility_changed") +func set_plugin(plugin : EditorPlugin) -> void: + _plugin = plugin + + _undo_redo = _plugin.get_undo_redo() + func on_visibility_changed() -> void: if is_visible_in_tree(): store_uvs() @@ -110,6 +118,20 @@ func set_mesh_data_instance(a : MeshDataInstance) -> void: func on_edited_resource_changed() -> void: call_deferred("refresh") +func get_uvs(mdr : MeshDataResource) -> PoolVector2Array: + if !_mdr: + return PoolVector2Array() + + var arrays : Array = _mdr.get_array() + + if arrays.size() != ArrayMesh.ARRAY_MAX: + return PoolVector2Array() + + if arrays[ArrayMesh.ARRAY_TEX_UV] == null: + return PoolVector2Array() + + return arrays[ArrayMesh.ARRAY_TEX_UV] + func store_uvs() -> void: _stored_uvs.resize(0) @@ -127,10 +149,27 @@ func store_uvs() -> void: # Make sure it gets copied _stored_uvs.append_array(arrays[ArrayMesh.ARRAY_TEX_UV]) +func apply_uvs(mdr : MeshDataResource, stored_uvs : PoolVector2Array) -> void: + if !_mdr: + return + + var arrays : Array = _mdr.get_array() + + if arrays.size() != ArrayMesh.ARRAY_MAX: + return + + if arrays[ArrayMesh.ARRAY_TEX_UV] == null: + return + + arrays[ArrayMesh.ARRAY_TEX_UV] = stored_uvs + + _mdr.array = arrays func ok_pressed() -> void: - #todo undo redo - pass + _undo_redo.create_action("UV Editor Accept") + _undo_redo.add_do_method(self, "apply_uvs", _mdr, get_uvs(_mdr)) + _undo_redo.add_undo_method(self, "apply_uvs", _mdr, _stored_uvs) + _undo_redo.commit_action() func cancel_pressed() -> void: if !_mdr: @@ -144,8 +183,10 @@ func cancel_pressed() -> void: # Make sure it gets copied var uvs : PoolVector2Array = PoolVector2Array() uvs.append_array(_stored_uvs) - arrays[ArrayMesh.ARRAY_TEX_UV] = uvs - - _mdr.array = arrays + + _undo_redo.create_action("UV Editor Cancel") + _undo_redo.add_do_method(self, "apply_uvs", _mdr, uvs) + _undo_redo.add_undo_method(self, "apply_uvs", _mdr, get_uvs(_mdr)) + _undo_redo.commit_action() _stored_uvs.resize(0)