[gd_scene load_steps=4 format=3 uid="uid://he12yvtcmf87"]

[sub_resource type="GDScript" id="GDScript_kxd0b"]
script/source = "@tool
extends Control

var plugin: EditorPlugin

func _enter_tree() -> void:
	if owner:
		plugin = owner.plugin

func _notification(what: int) -> void:
	if not plugin:
		return
	
	if what == NOTIFICATION_THEME_CHANGED:
		%ValidationPanel.add_theme_stylebox_override(&\"panel\", get_theme_stylebox(&\"panel\", &\"Tree\"))

func edit_settings_pressed():
	plugin.get_editor_interface().edit_resource(MetSys.settings)

func force_reload() -> void:
	MetSys.map_data = MetSys.MapData.new()
	MetSys.map_data.load_data()
	
	for group in MetSys.map_data.cell_groups.values():
		var i: int
		while i < group.size():
			if MetSys.map_data.get_cell_at(group[i]):
				i += 1
			else:
				group.remove_at(i)

func edit_database_theme() -> void:
	plugin.get_editor_interface().edit_resource(owner.theme)

func reset_theme() -> void:
	%AllowReset.button_pressed = false
	
	var th: Theme = owner.theme
	th.set_color(&\"group_color\", &\"MetSys\", Color(Color.MEDIUM_PURPLE, 0.8))
	th.set_color(&\"highlighted_room\", &\"MetSys\", Color(Color.GREEN, 0.25))
	th.set_color(&\"assigned_scene\", &\"MetSys\", Color(Color.MEDIUM_SEA_GREEN, 0.8))
	th.set_color(&\"border_highlight\", &\"MetSys\", Color(Color.GREEN, 0.5))
	th.set_color(&\"cursor_color\", &\"MetSys\", Color.GREEN)
	th.set_color(&\"cursor_color_erase\", &\"MetSys\", Color.RED)
	th.set_color(&\"marked_collectible_room\", &\"MetSys\", Color(Color.BLUE_VIOLET, 0.75))
	th.set_color(&\"foreign_marked_collectible_room\", &\"MetSys\", Color(Color.RED, 0.25))
	th.set_color(&\"current_scene_room\", &\"MetSys\", Color(Color.RED, 0.5))
	th.set_color(&\"room_not_assigned\", &\"MetSys\", Color.RED)
	th.set_color(&\"room_assigned\", &\"MetSys\", Color.WHITE)
	th.set_color(&\"active_custom_element\", &\"MetSys\", Color(0.1, 0.1, 1, 0.8))
	th.set_color(&\"inactive_custom_element\", &\"MetSys\", Color(0, 0, 1, 0.4))
	th.set_color(&\"custom_element_marker\", &\"MetSys\", Color.YELLOW)
	ResourceSaver.save(th)

func toggle_allow_reset(button_pressed: bool) -> void:
	%ResetButton.disabled = not button_pressed

func refresh_custom_elements() -> void:
	var custom_script := MetSys.settings.custom_element_script
	MetSys.settings.custom_element_script = null
	MetSys.settings.custom_element_script = custom_script
	MetSys.settings.custom_elements_changed.emit()

func export_json() -> void:
	$FileDialog.popup_centered_ratio(0.6)

func json_file_selected(path: String) -> void:
	var map_data: Dictionary
	
	var cells: Dictionary
	map_data[\"cells\"] = cells
	
	for cell in MetSys.map_data.cells:
		var cell_to_save: Dictionary
		cells[\"%s,%s,%s\" % [cell.x, cell.y, cell.z]] = cell_to_save
		
		var cell_data := MetSys.map_data.get_cell_at(cell)
		cell_to_save[\"borders\"] = cell_data.borders
		if cell_data.symbol > -1:
			cell_to_save[\"symbol\"] = cell_data.symbol
		if cell_data.color.a > 0:
			cell_to_save[\"color\"] = cell_data.color.to_html(false)
		if cell_data.border_colors.any(func(color: Color) -> bool: return color.a > 0):
			cell_to_save[\"border_colors\"] = cell_data.border_colors.map(func(color: Color) -> String:
				return \"000000\" if color.a == 0 else color.to_html(false))
		if not cell_data.assigned_scene.is_empty():
			cell_to_save[\"assigned_scene\"] = cell_data.assigned_scene
	
	var groups: Dictionary
	
	for group in MetSys.map_data.cell_groups:
		var group_cells: Array[Vector3i]
		group_cells.assign(MetSys.map_data.cell_groups[group])
		
		if group_cells.is_empty():
			continue
		
		var group_to_save: Array
		groups[group] = group_to_save
		
		for cell in group_cells:
			group_to_save.append(\"%s,%s,%s\" % [cell.x, cell.y, cell.z])
	
	if not groups.is_empty():
		map_data[\"groups\"] = groups
	
	var elements: Dictionary
	
	for element in MetSys.map_data.custom_elements:
		var element_to_save: Dictionary
		elements[\"%s,%s,%s\" % [element.x, element.y, element.z]] = element_to_save
		
		var element_data: Dictionary = MetSys.map_data.custom_elements[element]
		element_to_save[\"name\"] = element_data[\"name\"]
		element_to_save[\"size\"] = \"%s,%s\" % [element_data[\"size\"].x, element_data[\"size\"].y]
		if not element_data[\"data\"].is_empty():
			element_to_save[\"data\"] = element_data[\"data\"]
	
	if not elements.is_empty():
		map_data[\"custom_elements\"] = elements
	
	var file := FileAccess.open(path, FileAccess.WRITE)
	file.store_string(JSON.stringify(map_data, \"\\t\"))
"

[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_6lvlf"]
content_margin_left = 4.0
content_margin_top = 4.0
content_margin_right = 4.0
content_margin_bottom = 5.0
bg_color = Color(0.1, 0.1, 0.1, 0.6)
corner_radius_top_left = 3
corner_radius_top_right = 3
corner_radius_bottom_right = 3
corner_radius_bottom_left = 3
corner_detail = 5

[sub_resource type="GDScript" id="GDScript_x20sf"]
resource_name = "ValidationPlanel"
script/source = "@tool
extends PanelContainer

enum { SUCCESS, WARNING, ERROR, INFO }

var plugin: EditorPlugin

var warning_color: Color
var error_color: Color
var success_color: Color

var has_error: bool

func _enter_tree() -> void:
	if owner:
		plugin = owner.plugin

func _ready() -> void:
	if plugin:
		hide()

func _notification(what: int) -> void:
	if what == NOTIFICATION_THEME_CHANGED:
		warning_color = get_theme_color(&\"warning_color\", &\"Editor\")
		error_color = get_theme_color(&\"error_color\", &\"Editor\")
		success_color = get_theme_color(&\"success_color\", &\"Editor\")

func validate_map_data() -> void:
	dismiss()
	has_error = false
	
	var map_theme: MapTheme = MetSys.settings.theme
	
	var unused_symbols: Array[int]
	unused_symbols.assign(range(map_theme.symbols.size()))
	unused_symbols.erase(map_theme.uncollected_item_symbol)
	unused_symbols.erase(map_theme.collected_item_symbol)
	
	for coords in MetSys.map_data.cells:
		var cell_data: MetroidvaniaSystem.MapData.CellData = MetSys.map_data.cells[coords]
		
		if not MetSys.map_data.get_assigned_scene_at(coords):
			add_label(\"No assigned map at: %s\" % coords, WARNING)
		
		var symbol := cell_data.get_symbol()
		if symbol >= map_theme.symbols.size():
			add_label(\"Invalid symbol (%d) at: %s\" % [symbol, coords], ERROR)
		else:
			unused_symbols.erase(symbol)
		
		for i in 4:
			var border := cell_data.get_border(i)
			if map_theme.rectangle and border >= map_theme.vertical_borders.size() + 2:
				add_label(\"Invalid border (%d) at: %s\" % [border, coords], ERROR)
			elif not map_theme.rectangle and border >= map_theme.borders.size() + 2:
				add_label(\"Invalid border (%d) at: %s\" % [border, coords], ERROR)
			elif cell_data.get_border(i) != 0:
				var next: Vector3i = coords + Vector3i(MetroidvaniaSystem.MapData.FWD[i].x, MetroidvaniaSystem.MapData.FWD[i].y, 0)
				if not MetSys.map_data.get_cell_at(next):
					add_label(\"Passage to nowhere at: %s\" % coords, WARNING)
	
	for symbol in unused_symbols:
		add_label(\"Potentially unused symbol: %d\" % symbol, WARNING)
	
	if not has_error:
		add_label(\"Map data is valid.\", SUCCESS)

func validate_map_theme() -> void:
	dismiss()
	has_error = false
	
	var map_theme: MapTheme = MetSys.settings.theme
	
	if map_theme.center_texture:
		add_label(\"Cell Shape: %s\" % (\"Rectangle\" if map_theme.rectangle else \"Square\"), INFO)
		add_label(\"Base Cell Size: %s\" % map_theme.center_texture.get_size(), INFO)
	else:
		add_label(\"Theme is missing center texture. Map can't be drawn.\", ERROR)
		return
	
	if map_theme.is_unicorner():
		add_label(\"Uses unified corners.\", INFO)
	
	if map_theme.empty_space_texture and map_theme.empty_space_texture.get_size() != map_theme.center_texture.get_size():
		add_label(\"Size mismatch between empty texture (%s) and center texture.\" % map_theme.empty_space_texture.get_size(), ERROR)
	
	if map_theme.rectangle:
		if map_theme.vertical_borders.size() != map_theme.horizontal_borders.size():
			add_label(\"Number of horizontal and vertical borders do not match.\", ERROR)
		
		check_vertical_border_texture(map_theme.vertical_wall, \"Vertical wall texture\")
		check_vertical_border_texture(map_theme.vertical_passage, \"Vertical passage texture\")
		
		for i in map_theme.vertical_borders.size():
			var texture: Texture2D = map_theme.vertical_borders[i]
			check_vertical_border_texture(texture, \"Vertical border texture at index %d\" % i)
		
		check_horizontal_border_texture(map_theme.horizontal_wall, \"Horizontal wall texture\")
		check_horizontal_border_texture(map_theme.horizontal_passage, \"Horizontal passage texture\")
		
		for i in map_theme.horizontal_borders.size():
			var texture: Texture2D = map_theme.horizontal_borders[i]
			check_horizontal_border_texture(texture, \"Horizontal border texture at index %d\" % i)
	else:
		check_vertical_border_texture(map_theme.wall, \"Wall texture\")
		check_vertical_border_texture(map_theme.passage, \"Passage texture\")
		
		for i in map_theme.borders.size():
			var texture: Texture2D = map_theme.borders[i]
			check_vertical_border_texture(texture, \"Border texture at index %d\" % i)
	
	if map_theme.uncollected_item_symbol >= map_theme.symbols.size():
		add_label(\"Uncollected item symbol index is greater than number of available symbols.\", ERROR)
	
	if map_theme.collected_item_symbol >= map_theme.symbols.size():
		add_label(\"Collected item symbol index is greater than number of available symbols.\", ERROR)
	
	for i in map_theme.symbols.size():
		check_symbol_texture(map_theme.symbols[i], \"Symbol %d texture\" % i)
	if map_theme.use_shared_borders:
		check_corner_texture(map_theme.u_corner, \"U corner texture\")
		check_corner_texture(map_theme.l_corner, \"L corner texture\")
		check_corner_texture(map_theme.t_corner, \"T corner texture\")
		check_corner_texture(map_theme.cross_corner, \"Cross corner texture\")
	else:
		check_corner_texture(map_theme.inner_corner, \"Inner corner texture\")
		check_corner_texture(map_theme.outer_corner, \"Outer corner texture\")
	
	if map_theme.player_location_scene:
		var test := map_theme.player_location_scene.instantiate()
		test.queue_free()
		if not test is Node2D:
			add_label(\"Player location scene is not a Node2D.\", ERROR)
	else:
		add_label(\"Missing player location scene. Player location can't be displayed using built-in methods.\", WARNING)
	
	if not has_error:
		add_label(\"Theme is valid.\", SUCCESS)

func check_vertical_border_texture(texture: Texture2D, texture_name: String):
	if texture:
		var map_theme: MapTheme = MetSys.settings.theme
		if texture.get_height() != map_theme.center_texture.get_height():
			add_label(\"%s has invalid height (%d). It should be vertical, oriented towards the right side and match the height of the center texture.\" % [texture_name, texture.get_height()], ERROR)
		elif texture.get_width() > map_theme.center_texture.get_width() / 2:
			add_label(\"%s is wider than half of the center texture. It may cause overlaps.\" % texture_name, WARNING)
	else:
		add_label(\"%s is empty.\" % texture_name, ERROR)

func check_horizontal_border_texture(texture: Texture2D, texture_name: String):
	if texture:
		var map_theme: MapTheme = MetSys.settings.theme
		if texture.get_height() != map_theme.center_texture.get_width():
			add_label(\"%s has invalid height (%d). It should be vertical, oriented towards the right side and height should match the width of the center texture.\" % [texture_name, texture.get_height()], ERROR)
		elif texture.get_width() > map_theme.center_texture.get_height() / 2:
			add_label(\"%s is wider than half of the height of the center texture. It may cause overlaps.\" % texture_name, WARNING)
	else:
		add_label(\"%s is empty.\" % texture_name, ERROR)

func check_symbol_texture(texture: Texture2D, texture_name: String):
	if texture:
		var map_theme: MapTheme = MetSys.settings.theme
		if texture.get_width() > map_theme.center_texture.get_width() or texture.get_height() > map_theme.center_texture.get_height():
			add_label(\"%s is bigger than center texture. It will stick out of cells.\" % texture_name, WARNING)
	else:
		add_label(\"%s is empty.\" % texture_name, ERROR)

func check_corner_texture(texture: Texture2D, texture_name: String):
	if texture:
		var map_theme: MapTheme = MetSys.settings.theme
		if texture.get_width() > map_theme.center_texture.get_width() / 2 or texture.get_height() > map_theme.center_texture.get_height() / 2:
			add_label(\"%s is bigger than half of the center texture. It may cause overlaps.\" % texture_name, WARNING)
	else:
		add_label(\"%s is empty.\" % texture_name, ERROR)

func add_label(text: String, type: int):
	show()
	
	var label := Label.new()
	label.autowrap_mode = TextServer.AUTOWRAP_WORD
	label.text = \"• \" + text
	
	match type:
		SUCCESS:
			label.modulate = success_color
		WARNING:
			label.modulate = warning_color
		ERROR:
			label.modulate = error_color
			has_error = true
	
	%Output.add_child(label)

func dismiss() -> void:
	if not visible:
		return
	
	for node in %Output.get_children():
		node.queue_free()
	hide()
"

[node name="Manage" type="MarginContainer"]
anchors_preset = 13
anchor_left = 0.5
anchor_right = 0.5
anchor_bottom = 1.0
offset_left = -200.0
offset_right = 200.0
grow_horizontal = 2
grow_vertical = 2
size_flags_vertical = 3
theme_override_constants/margin_top = 32
script = SubResource("GDScript_kxd0b")

[node name="VBoxContainer" type="VBoxContainer" parent="."]
custom_minimum_size = Vector2(400, 0)
layout_mode = 2
size_flags_horizontal = 4

[node name="VBoxContainer" type="VBoxContainer" parent="VBoxContainer"]
layout_mode = 2
size_flags_horizontal = 4

[node name="Button" type="Button" parent="VBoxContainer/VBoxContainer"]
layout_mode = 2
text = "一般设置"

[node name="Button6" type="Button" parent="VBoxContainer/VBoxContainer"]
layout_mode = 2
text = "刷新默认元素"

[node name="Button2" type="Button" parent="VBoxContainer/VBoxContainer"]
layout_mode = 2
text = "重新加载并清理地图数据"

[node name="HSeparator3" type="HSeparator" parent="VBoxContainer/VBoxContainer"]
layout_mode = 2

[node name="Button4" type="Button" parent="VBoxContainer/VBoxContainer"]
layout_mode = 2
text = "校验地图数据"

[node name="Button5" type="Button" parent="VBoxContainer/VBoxContainer"]
layout_mode = 2
text = "校验地图主题"

[node name="Button7" type="Button" parent="VBoxContainer/VBoxContainer"]
layout_mode = 2
text = "导出地图数据为JSON"

[node name="HSeparator" type="HSeparator" parent="VBoxContainer/VBoxContainer"]
layout_mode = 2

[node name="Button3" type="Button" parent="VBoxContainer/VBoxContainer"]
layout_mode = 2
text = "编辑数据库主题"

[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer/VBoxContainer"]
layout_mode = 2

[node name="AllowReset" type="CheckBox" parent="VBoxContainer/VBoxContainer/HBoxContainer"]
unique_name_in_owner = true
layout_mode = 2

[node name="ResetButton" type="Button" parent="VBoxContainer/VBoxContainer/HBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
size_flags_horizontal = 3
disabled = true
text = "重置数据库主题"

[node name="HSeparator2" type="HSeparator" parent="VBoxContainer/VBoxContainer"]
layout_mode = 2

[node name="ValidationPanel" type="PanelContainer" parent="VBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
size_flags_vertical = 3
theme_override_styles/panel = SubResource("StyleBoxFlat_6lvlf")
script = SubResource("GDScript_x20sf")

[node name="VBoxContainer" type="VBoxContainer" parent="VBoxContainer/ValidationPanel"]
layout_mode = 2

[node name="Label" type="Label" parent="VBoxContainer/ValidationPanel/VBoxContainer"]
layout_mode = 2
text = "校验结果"
horizontal_alignment = 1

[node name="ScrollContainer" type="ScrollContainer" parent="VBoxContainer/ValidationPanel/VBoxContainer"]
layout_mode = 2
size_flags_vertical = 3

[node name="Output" type="VBoxContainer" parent="VBoxContainer/ValidationPanel/VBoxContainer/ScrollContainer"]
unique_name_in_owner = true
layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 3

[node name="Button" type="Button" parent="VBoxContainer/ValidationPanel/VBoxContainer"]
layout_mode = 2
size_flags_horizontal = 4
text = "关闭"

[node name="FileDialog" type="FileDialog" parent="."]
size = Vector2i(483, 162)
access = 2
filters = PackedStringArray("*.json")

[connection signal="pressed" from="VBoxContainer/VBoxContainer/Button" to="." method="edit_settings_pressed"]
[connection signal="pressed" from="VBoxContainer/VBoxContainer/Button6" to="." method="refresh_custom_elements"]
[connection signal="pressed" from="VBoxContainer/VBoxContainer/Button2" to="." method="force_reload"]
[connection signal="pressed" from="VBoxContainer/VBoxContainer/Button4" to="VBoxContainer/ValidationPanel" method="validate_map_data"]
[connection signal="pressed" from="VBoxContainer/VBoxContainer/Button5" to="VBoxContainer/ValidationPanel" method="validate_map_theme"]
[connection signal="pressed" from="VBoxContainer/VBoxContainer/Button7" to="." method="export_json"]
[connection signal="pressed" from="VBoxContainer/VBoxContainer/Button3" to="." method="edit_database_theme"]
[connection signal="toggled" from="VBoxContainer/VBoxContainer/HBoxContainer/AllowReset" to="." method="toggle_allow_reset"]
[connection signal="pressed" from="VBoxContainer/VBoxContainer/HBoxContainer/ResetButton" to="." method="reset_theme"]
[connection signal="pressed" from="VBoxContainer/ValidationPanel/VBoxContainer/Button" to="VBoxContainer/ValidationPanel" method="dismiss"]
[connection signal="file_selected" from="FileDialog" to="." method="json_file_selected"]
