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

[XML Converter] Reading waypoint #157

Merged
merged 8 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
197 changes: 77 additions & 120 deletions Spatial.gd
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func _ready():
# Postion at top left corner
OS.set_window_position(Vector2(0,0))
set_minimal_mouse_block()
init_category_tree()
marker_file_dir.open("user://protobins/")
server.listen(4242)

func set_minimal_mouse_block():
Expand Down Expand Up @@ -257,7 +259,6 @@ func decode_context_packet(spb: StreamPeerBuffer):
if self.map_id != old_map_id:
print("Loading New Map")
load_waypoint_markers(self.map_id)
gen_map_markers()

# TODO move this to reset_minimap_masks
for child in $Paths.get_children():
Expand All @@ -284,27 +285,34 @@ func reset_minimap_masks():
minimap_path.material.set_shader_param("minimap_corner", compass_corner1)
minimap_path.material.set_shader_param("minimap_corner2", compass_corner2)

var markerdata = Waypoint.Waypoint.new()
var marker_file_dir = "user://protobins/"
var Waypoint_data = Waypoint.Waypoint.new()
AsherGlick marked this conversation as resolved.
Show resolved Hide resolved
var marker_file_dir = Directory.new()
AsherGlick marked this conversation as resolved.
Show resolved Hide resolved
var marker_file_path = ""
var marker_packs_array = Array()
var root: TreeItem

##########Node Connections###########
onready var marker_packs = $Control/Dialogs/MarkerPacks/MarkerPacks
onready var icons = $Icons
onready var paths = $Paths
onready var minimap = $Control/MiniMap
AsherGlick marked this conversation as resolved.
Show resolved Hide resolved


func load_waypoint_markers(map_id):
self.marker_file_path = self.marker_file_dir + String(map_id) + ".data"
print("Loading protobuf file from path ", self.marker_file_path)
self.markerdata.clear_category()
self.markerdata.clear_icon()
self.markerdata.clear_trail()
self.marker_file_path = self.marker_file_dir.get_current_dir() + String(map_id) + ".data"
self.Waypoint_data.clear_category()
clear_map_markers()
root.free()
AsherGlick marked this conversation as resolved.
Show resolved Hide resolved
init_category_tree()
var file = File.new()
print("Loading protobuf file from path ", self.marker_file_path)
file.open(self.marker_file_path, file.READ)
var data = file.get_buffer(file.get_len())
self.markerdata.from_bytes(data)
self.Waypoint_data.from_bytes(data)
if !Waypoint.PB_ERR.NO_ERRORS:
print("OK")
else:
print(Waypoint.PB_ERR)

parse_Waypoint()

var route_scene = load("res://Route.tscn")
var icon_scene = load("res://Icon.tscn")
Expand Down Expand Up @@ -382,11 +390,7 @@ func _unhandled_input(event):
################################################################################
#
################################################################################
onready var icons = $Icons
onready var paths = $Paths
onready var minimap = $Control/MiniMap

func gen_map_markers():
func clear_map_markers():
# Clear all the rendered assets to make way for the new ones
for path in paths.get_children():
path.queue_free()
Expand All @@ -397,69 +401,20 @@ func gen_map_markers():
for icon in icons.get_children():
icon.queue_free()

self.marker_packs.clear()
build_category_tree()

# Load the data from the markers
for path in self.markerdata.get_trail():
var path_points := PoolVector3Array()
var trail_data = path.get_trail_data()
if trail_data.get_points_x().size() != trail_data.get_points_y().size() or trail_data.get_points_x().size() != trail_data.get_points_z().size():
print("Warning: Trail ", path.get_category.get_name(), " does not have equal number of X, Y, and Z coordinates.")
for index in range(0, trail_data.get_points_z().size()):
path_points.append(Vector3(trail_data.get_points_x()[index], trail_data.get_points_y()[index], trail_data.get_points_z()[index]))
var texture_path = path.get_texture_path()
if texture_path == null:
print("Warning: No texture found in " , path.get_category().name())
continue
var full_texture_path = self.marker_file_dir + texture_path.get_path()
#TODO This tree look up is unnescary if we make path a child of Category in protobuf
var split_name = path.get_category().get_name().split(".")
var category_item = search_category_tree(split_name, self.marker_packs.get_root())
gen_new_path(path_points, full_texture_path, path, category_item)
for icon in self.markerdata.get_icon():
var position = icon.get_position()
if position == null:
print("Warning: No position found for icon ", icon.get_category().name())
continue
var position_vector = Vector3(position.get_x(), position.get_y(), position.get_z())
var texture_path = icon.get_texture_path()
if texture_path == null:
print("Warning: No texture found in " , icon.get_category().name())
continue
var full_texture_path = self.marker_file_dir + texture_path.get_path()
#TODO This tree look up is unnescary if we make path a child of Category in protobuf
var split_name = icon.get_category().get_name().split(".")
var category_item = search_category_tree(split_name, self.marker_packs.get_root())
gen_new_icon(position_vector, full_texture_path, icon, category_item)


func search_category_tree(split_name: PoolStringArray, category_item: TreeItem, index: int = 0):
if index == split_name.size():
return category_item
var child_item = category_item.get_children()
while child_item != null:
var joined_name = ""
for i in range(index):
joined_name += split_name[i]
if child_item.get_metadata(0) == joined_name:
return search_category_tree(split_name, child_item, index + 1)
child_item = child_item.get_next()
print("No category found for ", split_name)
return null

func build_category_tree():
var root = self.marker_packs.create_item()
func init_category_tree():
self.root = self.marker_packs.create_item()
root.set_text(0, "Markers available on current map")
root.set_selectable(0, false)
root.set_text(1, "Visible")

for category in self.markerdata.get_category():
self.marker_packs_array.append(category.get_name())
add_category(root, category, category.get_name(), false)


func add_category(item: TreeItem, category, full_category_name: String, collapsed: bool):
func parse_Waypoint():
AsherGlick marked this conversation as resolved.
Show resolved Hide resolved
for category in self.Waypoint_data.get_category():
parse_category(root, category, category.get_name(), false)


func parse_category(item: TreeItem, category, full_category_name: String, collapsed: bool):
AsherGlick marked this conversation as resolved.
Show resolved Hide resolved
var category_item = self.marker_packs.create_item(item)
if category.get_name() == "":
# If this is called, there is an error in the Waypoint data
Expand All @@ -474,8 +429,36 @@ func add_category(item: TreeItem, category, full_category_name: String, collapse
category_item.set_tooltip(1, "Show/Hide")
category_item.set_editable(1, true)
category_item.set_collapsed(collapsed)

for path in category.get_trail():
var path_points := PoolVector3Array()
var trail_data = path.get_trail_data()
if trail_data.get_points_x().size() != trail_data.get_points_y().size() or trail_data.get_points_x().size() != trail_data.get_points_z().size():
print("Warning: Trail ", full_category_name, " does not have equal number of X, Y, and Z coordinates.")
for index in range(0, trail_data.get_points_z().size()):
path_points.append(Vector3(trail_data.get_points_x()[index], trail_data.get_points_y()[index], trail_data.get_points_z()[index]))
var texture_path = path.get_texture_path()
if texture_path == null:
print("Warning: No texture found in " , full_category_name)
continue
var full_texture_path = self.marker_file_dir.get_current_dir() + texture_path.get_path()
gen_new_path(path_points, full_texture_path, path, category_item)

for icon in category.get_icon():
var position = icon.get_position()
if position == null:
print("Warning: No position found for icon ", full_category_name)
continue
var position_vector = Vector3(position.get_x(), position.get_y(), position.get_z())
var texture_path = icon.get_texture_path()
if texture_path == null:
print("Warning: No texture found in " , full_category_name)
continue
var full_texture_path = self.marker_file_dir.get_current_dir() + texture_path.get_path()
gen_new_icon(position_vector, full_texture_path, icon, category_item)

for category_child in category.get_children():
add_category(category_item, category_child, full_category_name + "." + category_child.get_name(), true)
parse_category(category_item, category_child, full_category_name + "." + category_child.get_name(), true)


func apply_category_visibility_to_nodes(category_item: TreeItem):
Expand Down Expand Up @@ -524,7 +507,6 @@ func is_category_visible(category_item: TreeItem) -> bool:


func gen_new_path(points: Array, texture_path: String, waypoint_trail, category_item: TreeItem):

# Create the texture to use from an image file
# TODO: We want to be able to cache this data so that if a texture is used
# by multiple objects we only need to keep ony copy of it in memory. #22.
Expand Down Expand Up @@ -571,9 +553,6 @@ func gen_new_path(points: Array, texture_path: String, waypoint_trail, category_
minimap.add_child(new_2d_path)


################################################################################
#
################################################################################
func gen_new_icon(position: Vector3, texture_path: String, waypoint_icon, category_item: TreeItem):
position.z = -position.z
var new_icon = icon_scene.instance()
Expand All @@ -586,39 +565,10 @@ func gen_new_icon(position: Vector3, texture_path: String, waypoint_icon, catego
new_icon.visible = false
icons.add_child(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
AsherGlick marked this conversation as resolved.
Show resolved Hide resolved

func _on_main_menu_toggle_pressed():
$Control/Dialogs/MainMenu.show()
set_maximal_mouse_block()

func _on_FileDialog_file_selected(path):
pass

################################################################################
# Adjustment and gizmo functions
################################################################################
# The adjust nodes button creates handles at all the node points to allow for
# editing of them via in-game interface. (Nodes can only be edited if the input
Expand All @@ -632,6 +582,10 @@ func _on_AdjustNodesButton_pressed():


func gen_adjustment_nodes():
if self.currently_active_category == null:
print("No category selected")
return

for index in range(self.paths.get_child_count()):
var route = self.paths.get_child(index)
var path2d = self.minimap.get_child(index)
Expand Down Expand Up @@ -692,6 +646,15 @@ func clear_adjustment_nodes():
$Gizmos.remove_child(child)
child.queue_free()

################################################################################
# Signal Functions
################################################################################
func _on_main_menu_toggle_pressed():
$Control/Dialogs/MainMenu.show()
set_maximal_mouse_block()

func _on_FileDialog_file_selected(path):
pass

func _on_Dialog_hide():
for dialog in $Control/Dialogs.get_children():
Expand Down Expand Up @@ -771,21 +734,17 @@ func _on_NewPathPoint_pressed():


################################################################################
# open the save dialog window. When a path is selected
# _on_SaveDialog_file_selected() will be called with the user specified path.
#
################################################################################
func _on_SavePath_pressed():
$Control/Dialogs/SaveDialog.show()
#TODO: Save to Waypoint
pass
AsherGlick marked this conversation as resolved.
Show resolved Hide resolved

################################################################################
# Save the current markers to a file, this includes all markers in memory not
# just the markers on the current map.
# TODO: This function will be used when exporting packs
################################################################################
func _on_SaveDialog_file_selected(path):
self.markerdata[str(self.map_id)] = data_from_renderview()
var save_game = File.new()
save_game.open(path, File.WRITE)
save_game.store_string(JSON.print(self.markerdata))
pass
AsherGlick marked this conversation as resolved.
Show resolved Hide resolved


func _on_NodeEditorDialog_hide():
Expand Down Expand Up @@ -877,5 +836,3 @@ 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)


Loading