From 24acc83a5fd15f7451ee0931d16e0f7960daa452 Mon Sep 17 00:00:00 2001 From: klingbolt Date: Thu, 24 Aug 2023 23:57:59 -0400 Subject: [PATCH] Saves Waypoint data to file. Includes autosaving at key points --- Gizmo/PointEdit.gd | 2 ++ Icon.gd | 6 ++++++ Route.gd | 6 ++++++ Spatial.gd | 54 +++++++++++++++++++++++++++++++++++++++++----- Spatial.tscn | 12 +++++++++-- 5 files changed, 73 insertions(+), 7 deletions(-) diff --git a/Gizmo/PointEdit.gd b/Gizmo/PointEdit.gd index ea6769ba..d3b7c449 100644 --- a/Gizmo/PointEdit.gd +++ b/Gizmo/PointEdit.gd @@ -48,8 +48,10 @@ func update_point(): if point_type == "path" || point_type == "area": self.object_link.set_point_position(self.object_index, self.translation) self.object_2d_link.points[self.object_index] = Vector2(self.translation.x, self.translation.z) + self.object_link.update_waypoint_trail(self.object_index) if point_type == "icon": self.object_link.translation = self.translation + self.object_link.update_waypoint_icon() print("update") self.last_translation = self.translation diff --git a/Icon.gd b/Icon.gd index 1cd9164c..2f2fa654 100644 --- a/Icon.gd +++ b/Icon.gd @@ -5,6 +5,12 @@ const Waypoint = preload("res://waypoint.gd") var texture_path var waypoint: Waypoint.Icon +func update_waypoint_icon(): + var position = self.waypoint.position() + position.set_x(translation[0]) + position.set_y(translation[1]) + position.set_z(-translation[2]) + func set_icon_image(texture_path: String): self.texture_path = texture_path diff --git a/Route.gd b/Route.gd index 07a87187..491edd28 100644 --- a/Route.gd +++ b/Route.gd @@ -8,6 +8,12 @@ var waypoint: Waypoint.Trail var point_list := PoolVector3Array() +func update_waypoint_trail(index): + var trail_data = waypoint.get_trail_data() + trail_data.get_points_x()[index] = self.point_list[index][0] + trail_data.get_points_y()[index] = self.point_list[index][1] + trail_data.get_points_z()[index] = -self.point_list[index][2] + func create_mesh(point_list: PoolVector3Array): self.point_list = point_list refresh_mesh() diff --git a/Spatial.gd b/Spatial.gd index 8335ad9f..572fec46 100644 --- a/Spatial.gd +++ b/Spatial.gd @@ -258,6 +258,8 @@ func decode_context_packet(spb: StreamPeerBuffer): # this to just be a radian to degree conversion. if self.map_id != old_map_id: + if unsaved_data_icon.visible: + auto_saving() print("Loading New Map") load_waypoint_markers(self.map_id) @@ -289,10 +291,12 @@ func reset_minimap_masks(): var Waypoint_data = Waypoint.Waypoint.new() var marker_file_dir = Directory.new() var marker_file_path = "" +var auto_save_file_path = "" var root: TreeItem ##########Node Connections########### onready var marker_packs = $Control/Dialogs/MarkerPacks/MarkerPacks +onready var unsaved_data_icon = $Control/GlobalMenuButton/BurritoIcon/UnsavedData onready var icons = $Icons onready var paths = $Paths onready var minimap = $Control/MiniMap @@ -304,9 +308,17 @@ func load_waypoint_markers(map_id): clear_map_markers() root.free() init_category_tree() + self.auto_save_file_path = self.marker_file_dir.get_current_dir() + String(map_id) + "_auto.data" var file = File.new() - print("Loading protobuf file from path ", self.marker_file_path) - file.open(self.marker_file_path, file.READ) + if self.marker_file_dir.file_exists(self.auto_save_file_path): + print("Loading autosave file from path ", self.auto_save_file_path) + file.open(self.auto_save_file_path, file.READ) + self.unsaved_data_icon.visible = true + elif self.marker_file_dir.file_exists(self.marker_file_path): + print("Loading protobuf file from path ", self.marker_file_path) + file.open(self.marker_file_path, file.READ) + else: + print("No markers on this map") var data = file.get_buffer(file.get_len()) self.Waypoint_data.from_bytes(data) if !Waypoint.PB_ERR.NO_ERRORS: @@ -566,7 +578,30 @@ func gen_new_icon(position: Vector3, texture_path: String, waypoint_icon, catego new_icon.visible = false icons.add_child(new_icon) - +################################################################################ +# Section of functions for saving changes to markers +################################################################################ +func on_change_made(): + unsaved_data_icon.visible = true + +func auto_saving(): + print("Auto save") + var packed_bytes = self.Waypoint_data.to_bytes() + if packed_bytes.size() > 0: + var file = File.new() + file.open(self.auto_save_file_path, file.WRITE) + file.store_buffer(packed_bytes) + +func manual_save(): + print("Saving") + var packed_bytes = self.Waypoint_data.to_bytes() + if packed_bytes.size() > 0: + var file = File.new() + file.open(self.marker_file_path, file.WRITE) + file.store_buffer(packed_bytes) + if self.marker_file_dir.file_exists(self.auto_save_file_path): + self.marker_file_dir.remove(self.auto_save_file_path) + unsaved_data_icon.visible = false ################################################################################ # Adjustment and gizmo functions @@ -619,6 +654,7 @@ func gen_adjustment_nodes(): var currently_selected_node = null func on_gizmo_selected(object): + on_change_made() self.currently_selected_node = object $Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/DeleteNode.disabled = false # Only enable these buttons if the object selected is a point on the path not an icon @@ -738,8 +774,7 @@ func _on_NewPathPoint_pressed(): # ################################################################################ func _on_SavePath_pressed(): - #TODO: Save to Waypoint - pass + manual_save() ################################################################################ # TODO: This function will be used when exporting packs @@ -820,6 +855,8 @@ func _on_ReversePathDirection_pressed(): func _on_ExitButton_pressed(): + if unsaved_data_icon.visible: + auto_saving() get_tree().quit() @@ -837,3 +874,10 @@ func _on_MarkerPacks_cell_selected(): func _on_MarkerPacks_item_edited(): var category_item = self.marker_packs.get_edited() apply_category_visibility_to_nodes(category_item) + + +func _on_UnsavedData_visibility_changed(): + if $Control/GlobalMenuButton/BurritoIcon/UnsavedData.visible: + $Control/GlobalMenuButton/main_menu_toggle.hint_tooltip = "Unsaved Data" + else: + $Control/GlobalMenuButton/main_menu_toggle.hint_tooltip = "" diff --git a/Spatial.tscn b/Spatial.tscn index 5db276b4..2bd155b2 100644 --- a/Spatial.tscn +++ b/Spatial.tscn @@ -78,9 +78,9 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="TextureRect" type="TextureRect" parent="Control/GlobalMenuButton"] +[node name="BurritoIcon" type="TextureRect" parent="Control/GlobalMenuButton"] modulate = Color( 1, 1, 1, 0.439216 ) -margin_left = 1.591 +margin_left = 2.0 margin_top = 3.18198 margin_right = 26.591 margin_bottom = 28.182 @@ -89,6 +89,13 @@ __meta__ = { "_edit_use_anchors_": false } +[node name="UnsavedData" type="TextureRect" parent="Control/GlobalMenuButton/BurritoIcon"] +visible = false +modulate = Color( 1, 0.0313726, 0.0313726, 1 ) +margin_right = 40.0 +margin_bottom = 40.0 +texture = ExtResource( 3 ) + [node name="main_menu_toggle" type="Button" parent="Control/GlobalMenuButton"] modulate = Color( 1, 1, 1, 0 ) margin_right = 27.0 @@ -821,6 +828,7 @@ color = Color( 0, 0, 0, 1 ) mesh = SubResource( 3 ) material/0 = SubResource( 4 ) +[connection signal="visibility_changed" from="Control/GlobalMenuButton/BurritoIcon/UnsavedData" to="." method="_on_UnsavedData_visibility_changed"] [connection signal="pressed" from="Control/GlobalMenuButton/main_menu_toggle" to="." method="_on_main_menu_toggle_pressed"] [connection signal="pressed" from="Control/GlobalMenuButton/EditorQuckPanel/HBoxContainer/CloseEditorQuickPanel" to="." method="_on_CloseEditorQuickPanel_pressed"] [connection signal="pressed" from="Control/GlobalMenuButton/EditorQuckPanel/HBoxContainer/ChangeTexture" to="." method="_on_ChangeTexture_pressed"]