Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save the current data to the map files #323

Merged
merged 5 commits into from
Aug 1, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 43 additions & 30 deletions Spatial.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var map_is_open: bool
var compass_is_top_right: bool

var edit_panel_open: bool = false
var unsaved_changes: bool = false setget set_unsaved_changes
AsherGlick marked this conversation as resolved.
Show resolved Hide resolved

# This is the path to the texture that will be used for the next created 3d-path
# object or icon object in the UI
Expand Down Expand Up @@ -340,6 +341,8 @@ func decode_context_packet(spb: StreamPeerBuffer):
if self.map_id != old_map_id:
print("New Map")
print("Saving Old Map")
if self.unsaved_changes:
save_current_map_data()
print("Loading New Map")
load_waypoint_markers(self.map_id)

Expand Down Expand Up @@ -389,11 +392,14 @@ func reset_3D_minimap_masks(category: Spatial):


var waypoint_data = Waypoint.Waypoint.new()
var marker_file_dir = "user://protobins/"
#We save the marker data in this directory when the files are have been split
#by Map ID. All changes made by the editor are automatically saved in these
#files prior to export.
AsherGlick marked this conversation as resolved.
Show resolved Hide resolved
var unsaved_markers_dir = "user://protobins/"
var marker_file_path = ""

func load_waypoint_markers(map_id):
self.marker_file_path = self.marker_file_dir + String(map_id) + ".bin"
func load_waypoint_markers(map_id_to_load: int):
self.marker_file_path = self.unsaved_markers_dir + String(map_id_to_load) + ".bin"
self.waypoint_data = Waypoint.Waypoint.new()
clear_map_markers()
init_category_tree()
Expand Down Expand Up @@ -537,7 +543,7 @@ func _waypoint_categories_to_godot_nodes(item: TreeItem, waypoint_category: Wayp
if texture_id == null:
print("Warning: No texture found in " , category_name)
continue
var full_texture_path = self.marker_file_dir + self.waypoint_data.get_textures()[texture_id].get_filepath()
var full_texture_path = self.unsaved_markers_dir + self.waypoint_data.get_textures()[texture_id].get_filepath()
gen_new_trail(full_texture_path, trail, category_item)


Expand All @@ -546,7 +552,7 @@ func _waypoint_categories_to_godot_nodes(item: TreeItem, waypoint_category: Wayp
if texture_id == null:
print("Warning: No texture found in " , category_name)
continue
var full_texture_path = self.marker_file_dir + self.waypoint_data.get_textures()[texture_id].get_filepath()
var full_texture_path = self.unsaved_markers_dir + self.waypoint_data.get_textures()[texture_id].get_filepath()
gen_new_icon(full_texture_path, icon, category_item)

for category_child in waypoint_category.get_children():
Expand Down Expand Up @@ -621,31 +627,30 @@ func gen_new_icon(texture_path: String, waypoint_icon: Waypoint.Icon, category_i
var category_data = category_item.get_metadata(0)
category_data.category3d.add_icon(new_icon)

# This function take all of the currently rendered objects and converts it into
# the data format that is saved/loaded from.
func data_from_renderview():
var icons_data = []
var paths_data = []

for icon in $Icons.get_children():
icons_data.append({
"position": [icon.translation.x, icon.translation.y, -icon.translation.z],
"texture": icon.texture_path
})

for path in $Paths.get_children():
#print(path)
var points = []
for point in range(path.get_point_count()):
var point_position:Vector3 = path.get_point_position(point)
points.append([point_position.x, point_position.y, -point_position.z])
paths_data.append({
"points": points,
"texture": path.texture_path
})

var data_out = {"icons": icons_data, "paths": paths_data}
return data_out
################################################################################
# Section of functions for saving data to file
################################################################################
func set_unsaved_changes(value):
if self.unsaved_changes != value:
unsaved_changes = value
update_burrito_icon()

func update_burrito_icon():
if self.unsaved_changes:
#TODO: Determine if this is the best color and alpha value to use
$Control/GlobalMenuButton/TextureRect.modulate = ColorN("red", 1)
$Control/GlobalMenuButton/main_menu_toggle.hint_tooltip = "Unsaved Data"
else:
$Control/GlobalMenuButton/TextureRect.modulate = ColorN("white", 0.44)
$Control/GlobalMenuButton/main_menu_toggle.hint_tooltip = ""

func save_current_map_data():
var packed_bytes = self.waypoint_data.to_bytes()
var file = File.new()
file.open(self.marker_file_path, file.WRITE)
file.store_buffer(packed_bytes)
set_unsaved_changes(false)


################################################################################
# Adjustment and gizmo functions
Expand Down Expand Up @@ -764,6 +769,7 @@ func set_icon_position(new_position: Vector3, waypoint_icon: Waypoint.Icon, icon
position.set_y(new_position.y)
position.set_z(new_position.z)
icon.set_position(new_position)
set_unsaved_changes(true)

func remove_icon(waypoint_icon: Waypoint.Icon, icon: Sprite3D):
if icon.waypoint != waypoint_icon:
Expand Down Expand Up @@ -849,14 +855,19 @@ func new_trail_point_after(waypoint_trail: Waypoint.Trail, trail3d: Spatial, tra

func refresh_trail3d_points(trail3d: Spatial):
trail3d.refresh_mesh()
set_unsaved_changes(true)
AsherGlick marked this conversation as resolved.
Show resolved Hide resolved

func refresh_trail2d_points(trail2d: Line2D):
trail2d.refresh_points()
set_unsaved_changes(true)
AsherGlick marked this conversation as resolved.
Show resolved Hide resolved


################################################################################
# Signal Functions
################################################################################
func _on_SaveTrail_pressed():
save_current_map_data()

AsherGlick marked this conversation as resolved.
Show resolved Hide resolved
func _on_main_menu_toggle_pressed():
$Control/Dialogs/MainMenu.show()
set_maximal_mouse_block()
Expand Down Expand Up @@ -1011,6 +1022,8 @@ func _on_ReverseTrailDirection_pressed():


func _on_ExitButton_pressed():
if self.unsaved_changes:
save_current_map_data()
AsherGlick marked this conversation as resolved.
Show resolved Hide resolved
exit_burrito()


Expand Down
Loading