diff --git a/ServerUI.gd b/ServerUI.gd index ff3fc3a..d44f0e0 100644 --- a/ServerUI.gd +++ b/ServerUI.gd @@ -40,6 +40,7 @@ var is_server_running: bool = false setget set_server_running var working_files: Array = [] # Array var current_file: FileDef = null +var current_tree_selection: TreeItem = null func _ready() -> void: @@ -64,6 +65,7 @@ func set_server_dir(dir: String) -> void: if directory.open(dir) == OK: directory.list_dir_begin() var file_name: String = directory.get_next() + var idx: int = 0 while file_name != "": if !directory.current_is_dir() && (file_name.get_extension() in ACCEPTED_FILE_FORMATS): var fd = FileDef(file_name, false) @@ -71,29 +73,42 @@ func set_server_dir(dir: String) -> void: var in_filestxt := _is_file_in_filestxt(file_name) - var item: TreeItem = file_tree.create_item(root) - item.set_cell_mode(0, TreeItem.CELL_MODE_CHECK) - item.set_metadata(0, fd) - item.set_editable(0, true) - item.set_text(0, file_name) - - item.set_checked(0, in_filestxt) fd.include_in_filestxt = in_filestxt + idx += 1 + file_name = directory.get_next() directory.list_dir_end() + _reconstruct_tree_from_working_files() + + +func _reconstruct_tree_from_working_files() -> void: + file_tree.clear() + var root: TreeItem = file_tree.create_item() + root.set_text(0, "Server files") + + for i in working_files.size(): + var fd = working_files[i] as FileDef + var item: TreeItem = file_tree.create_item(root) + item.set_cell_mode(0, TreeItem.CELL_MODE_CHECK) + item.set_metadata(0, {"file_def": fd, "id": i}) + item.set_editable(0, true) + item.set_text(0, fd.file_path) + + item.set_checked(0, fd.include_in_filestxt) + func _on_FileTree_item_edited() -> void: var item: TreeItem = file_tree.get_edited() - var fd: FileDef = item.get_metadata(0) + var fd: FileDef = item.get_metadata(0)["file_def"] fd.include_in_filestxt = item.is_checked(0) func _on_FileTree_item_selected() -> void: var item: TreeItem = file_tree.get_selected() - current_file = item.get_metadata(0) as FileDef + current_file = item.get_metadata(0)["file_def"] as FileDef if !current_file.dirty: var file_metadata := _get_file_metadata(current_file.file_path) @@ -107,7 +122,7 @@ func _on_FileTree_item_selected() -> void: func _on_FileTree_item_rmb_selected(position: Vector2) -> void: -# var item: TreeItem = file_tree.get_selected() + current_tree_selection = file_tree.get_selected() file_tree_context_menu.rect_position = position + file_tree.rect_global_position file_tree_context_menu.popup() @@ -165,6 +180,20 @@ func _get_file_metadata(path: String) -> Dictionary: return res +func _on_FileTreeContextMenu_id_pressed(id: int) -> void: + var idx = current_tree_selection.get_metadata(0)["id"] as int + var fd = working_files[idx] as FileDef + match id: + ContextMenuOptions.MOVE_DOWN: + working_files.remove(idx) + working_files.insert(idx + 1, fd) + _reconstruct_tree_from_working_files() + ContextMenuOptions.MOVE_UP: + working_files.remove(idx) + working_files.insert(idx - 1, fd) + _reconstruct_tree_from_working_files() + + func _on_DocTitleLineEdit_text_changed(new_text: String) -> void: if current_file: current_file.title = new_text diff --git a/ServerUI.tscn b/ServerUI.tscn index 0b62a7d..31d8177 100644 --- a/ServerUI.tscn +++ b/ServerUI.tscn @@ -253,3 +253,4 @@ items = [ "Move Up", null, 0, false, false, 0, 0, null, "", false, "Move Down", [connection signal="text_changed" from="VBoxContainer/HSplitContainer/VBoxContainer/HBoxContainer/VBoxContainer/HBoxContainer2/DocDateLineEdit" to="." method="_on_DocDateLineEdit_text_changed"] [connection signal="text_changed" from="VBoxContainer/HSplitContainer/VBoxContainer/HBoxContainer/VBoxContainer/HSplitContainer/ContentEditContainer/DocInputTextEdit" to="." method="_on_DocInputTextEdit_text_changed"] [connection signal="dir_selected" from="ServerFolderDialog" to="." method="set_server_dir"] +[connection signal="id_pressed" from="FileTreeContextMenu" to="." method="_on_FileTreeContextMenu_id_pressed"]