Skip to content

Commit

Permalink
Merge branch 'xml_converter' into folder_names_to_settings
Browse files Browse the repository at this point in the history
  • Loading branch information
klingbolt authored Aug 1, 2024
2 parents 6c00344 + 0b55fb7 commit 32c916e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 33 deletions.
82 changes: 54 additions & 28 deletions Spatial.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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

# 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 @@ -57,6 +56,10 @@ const gizmo_scene = preload("res://Gizmo/PointEdit.tscn")
# Scripts containing code used by this scene
const CategoryData = preload("res://CategoryData.gd")
const Waypoint = preload("res://waypoint.gd")
const FileHandler = preload("res://FileHandler.gd")

# File path for the the json that contains a hash of the data files
const HASH_BY_MAP_ID_FILEPATH: String = "user://hash_by_map_id.json"

##########Node Connections###########
onready var markers_ui := $Control/Dialogs/CategoriesDialog/MarkersUI as Tree
Expand Down Expand Up @@ -342,7 +345,9 @@ func decode_context_packet(spb: StreamPeerBuffer):

if self.map_id != old_map_id:
print("New Map")
print("Saving Old Map")
if old_map_id != 0 and not read_hash(old_map_id) == make_hash(self.waypoint_data.to_bytes()):
print("Saving Old Map")
save_map_data(old_map_id)
print("Loading New Map")
load_waypoint_markers(self.map_id)

Expand Down Expand Up @@ -627,30 +632,43 @@ func gen_new_icon(texture_path: String, waypoint_icon: Waypoint.Icon, category_i
################################################################################
# Section of functions for saving data to file
################################################################################
func set_unsaved_changes(value):
if self.unsaved_changes != value:
unsaved_changes = value
Settings.unsaved_changes = self.unsaved_changes
Settings.save()
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():
func save_map_data(map_id: int):
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)

func make_hash(data: PoolByteArray) -> String:
var ctx = HashingContext.new()
ctx.start(HashingContext.HASH_MD5)
ctx.update(data)
var res: PoolByteArray = ctx.finish()
return res.hex_encode()


# Save all hashes
func save_hashes():
var file = File.new()
var data = {}
var dir = Directory.new()
dir.open(self.unsaved_markers_dir)
dir.list_dir_begin()
var file_name = dir.get_next()
while file_name != "":
if dir.file_exists(file_name):
file.open(file_name, File.READ)
data[file_name.get_basename()] = make_hash(file.get_buffer(file.get_len()))
file_name = dir.get_next()
file.open(HASH_BY_MAP_ID_FILEPATH, File.WRITE)
file.store_string(JSON.print(data))

func read_hash(map_id: int) -> String:
var file = File.new()
if not file.file_exists(HASH_BY_MAP_ID_FILEPATH):
return ""
file.open(HASH_BY_MAP_ID_FILEPATH, File.READ)
return JSON.parse(file.get_as_text()).result.get(String(map_id), "")

################################################################################
# Adjustment and gizmo functions
################################################################################
Expand Down Expand Up @@ -768,7 +786,6 @@ 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)
Settings.set_unsaved_changes(true)

func remove_icon(waypoint_icon: Waypoint.Icon, icon: Sprite3D):
if icon.waypoint != waypoint_icon:
Expand Down Expand Up @@ -854,20 +871,15 @@ func new_trail_point_after(waypoint_trail: Waypoint.Trail, trail3d: Spatial, tra

func refresh_trail3d_points(trail3d: Spatial):
trail3d.refresh_mesh()
Settings.set_unsaved_changes(true)

func refresh_trail2d_points(trail2d: Line2D):
trail2d.refresh_points()
Settings.set_unsaved_changes(true)



################################################################################
# Signal Functions
################################################################################
func _on_SaveTrail_pressed():
save_current_map_data()
Settings.set_unsaved_changes(false)

func _on_main_menu_toggle_pressed():
$Control/Dialogs/MainMenu.show()
set_maximal_mouse_block()
Expand Down Expand Up @@ -1022,8 +1034,8 @@ func _on_ReverseTrailDirection_pressed():


func _on_ExitButton_pressed():
if self.unsaved_changes:
save_current_map_data()
if not read_hash(self.map_id) == make_hash(self.waypoint_data.to_bytes()):
save_map_data(self.map_id)
exit_burrito()


Expand All @@ -1050,3 +1062,17 @@ func _on_MarkersUI_item_edited():

func _on_ImportPath_pressed():
$Control/Dialogs/ImportPackDialog.show()


func _on_ImportPackDialog_dir_selected(dir):
var user_data_dir = str(OS.get_user_data_dir())
var args: PoolStringArray = [
"--input-taco-path", dir,
# TODO: This line is not working as intended and needs to be investigated
# "--input-waypoint-path", user_data_dir.plus_file("protobin"),
"--output-waypoint-path", user_data_dir.plus_file("protobin"),
"--output-split-waypoint-path", ProjectSettings.globalize_path(self.unsaved_markers_dir)
]
FileHandler.call_xml_converter(args)
save_hashes()
load_waypoint_markers(self.map_id)
7 changes: 2 additions & 5 deletions Spatial.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=19 format=2]
[gd_scene load_steps=18 format=2]

[ext_resource path="res://Spatial.gd" type="Script" id=1]
[ext_resource path="res://shaders/range_indicators.shader" type="Shader" id=2]
Expand All @@ -12,7 +12,6 @@
[ext_resource path="res://icon_new_point.png" type="Texture" id=11]
[ext_resource path="res://SettingsDialog.gd" type="Script" id=12]
[ext_resource path="res://Category3D.gd" type="Script" id=13]
[ext_resource path="res://ImportPackDialog.gd" type="Script" id=14]
[ext_resource path="res://Category2D.gd" type="Script" id=15]

[sub_resource type="Shader" id=1]
Expand Down Expand Up @@ -193,7 +192,6 @@ mode = 2
access = 2
current_dir = ""
current_path = ""
script = ExtResource( 14 )
__meta__ = {
"_edit_use_anchors_": false
}
Expand Down Expand Up @@ -886,7 +884,7 @@ material/0 = SubResource( 4 )
[connection signal="pressed" from="Control/GlobalMenuButton/EditorQuckPanel/HBoxContainer/NewTrailPoint" to="." method="_on_NewTrailPoint_pressed"]
[connection signal="pressed" from="Control/GlobalMenuButton/EditorQuckPanel/HBoxContainer/NewIcon" to="." method="_on_NewIcon_pressed"]
[connection signal="pressed" from="Control/GlobalMenuButton/EditorQuckPanel/HBoxContainer/AdjustPoints" to="." method="_on_AdjustNodesButton_pressed"]
[connection signal="dir_selected" from="Control/Dialogs/ImportPackDialog" to="Control/Dialogs/ImportPackDialog" method="_on_FileDialog_dir_selected"]
[connection signal="dir_selected" from="Control/Dialogs/ImportPackDialog" to="." method="_on_ImportPackDialog_dir_selected"]
[connection signal="hide" from="Control/Dialogs/ImportPackDialog" to="." method="_on_Dialog_hide"]
[connection signal="hide" from="Control/Dialogs/MainMenu" to="." method="_on_Dialog_hide"]
[connection signal="pressed" from="Control/Dialogs/MainMenu/ScrollContainer/VBoxContainer/LoadTrail" to="." method="_on_LoadTrail_pressed"]
Expand All @@ -913,7 +911,6 @@ material/0 = SubResource( 4 )
[connection signal="value_changed" from="Control/Dialogs/RangesDialog/GridContainer/SpinBox7" to="Control/Dialogs/RangesDialog" method="on_change"]
[connection signal="file_selected" from="Control/Dialogs/TexturePathOpen" to="." method="_on_TexturePathOpen_file_selected"]
[connection signal="hide" from="Control/Dialogs/TexturePathOpen" to="." method="_on_Dialog_hide"]
[connection signal="file_selected" from="Control/Dialogs/SaveDialog" to="." method="_on_SaveDialog_file_selected"]
[connection signal="hide" from="Control/Dialogs/SaveDialog" to="." method="_on_Dialog_hide"]
[connection signal="hide" from="Control/Dialogs/NodeEditorDialog" to="." method="_on_NodeEditorDialog_hide"]
[connection signal="pressed" from="Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/DeleteNode" to="." method="_on_DeleteNode_pressed"]
Expand Down

0 comments on commit 32c916e

Please sign in to comment.