In Godot 4 I want to make a Node that displays a local filesystem for a given directory.…
- User
In Godot 4 I want to make a Node that displays a local filesystem for a given directory. The code should be written in c#. The user should be able to select any file or directory and have it emit signals depending on the type selected along with the path. The file system display should allow for an optional icon for directories or files. The file icon should be able to support multiple types of files.
The code should be split among multiple files with each part being decoupled and doing the minimal amount needed for that components concept. The code should be well documented, but only document important things that aren't obvious from a quick glance at the code. Methods should be kept small and single purpose.
- Assistant
Below is an implementation of your request in C#. The code is split into multiple files, each handling a specific responsibility. The implementation uses Godot 4's features and adheres to the principles of decoupling and single responsibility.
File:
FileSystemBrowser.csThis is the main node that manages the file system display and user interaction.
using Godot; using System; public partial class FileSystemBrowser : VBoxContainer { [Signal] public delegate void FileSelected(string filePath); [Signal] public delegate void DirectorySelected(string directoryPath); [Export] public string StartDirectory = "res://"; [Export] public Texture2D DirectoryIcon; [Export] public Dictionary<string, Texture2D> FileIcons = new(); private FileSystemList _fileSystemList; public override void _Ready() { _fileSystemList = new FileSystemList(); AddChild(_fileSystemList); _fileSystemList.Connect("ItemSelected", new Callable(this, nameof(OnItemSelected))); _fileSystemList.Populate(StartDirectory, DirectoryIcon, FileIcons); } private void OnItemSelected(string path, bool isDirectory) { if (isDirectory) { EmitSignal(SignalName.DirectorySelected, path); } else { EmitSignal(SignalName.FileSelected, path); } } }
File:
FileSystemList.csThis handles the display of the file system and populates the list with files and directories.
using Godot; using System; using System.IO; public partial class FileSystemList : ItemList { [Signal] public delegate void ItemSelected(string path, bool isDirectory); public void Populate(string directory, Texture2D directoryIcon, Dictionary<string, Texture2D> fileIcons) { Clear(); if (!Directory.Exists(directory)) { GD.PrintErr($"Directory does not exist: {directory}"); return; } // Add directories foreach (var dir in Directory.GetDirectories(directory)) { var dirName = Path.GetFileName(dir); AddItem(dirName, directoryIcon); SetItemMetadata(GetItemCount() - 1, new FileSystemItem(dir, true)); } // Add files foreach (var file in Directory.GetFiles(directory)) { var fileName = Path.GetFileName(file); var fileExtension = Path.GetExtension(file).ToLower(); Texture2D fileIcon = fileIcons.ContainsKey(fileExtension) ? fileIcons[fileExtension] : null; AddItem(fileName, fileIcon); SetItemMetadata(GetItemCount() - 1, new FileSystemItem(file, false)); } } public override void _GuiInput(InputEvent @event) { if (@event is InputEventMouseButton mouseEvent && mouseEvent.Pressed && mouseEvent.ButtonIndex == MouseButton.Left) { var selectedIndex = GetSelectedItems().Count > 0 ? GetSelectedItems()[0] : -1; if (selectedIndex >= 0) { var item = (FileSystemItem)GetItemMetadata(selectedIndex); EmitSignal(SignalName.ItemSelected, item.Path, item.IsDirectory); } } } }
File:
FileSystemItem.csThis is a simple data class to store information about a file system item.
public class FileSystemItem { public string Path { get; } public bool IsDirectory { get; } public FileSystemItem(string path, bool isDirectory) { Path = path; IsDirectory = isDirectory; } }
Explanation of the Code
-
FileSystemBrowser:- Acts as the main entry point for the file system browser.
- Emits signals when a file or directory is selected.
- Allows configuration of the starting directory, directory icon, and file icons.
-
FileSystemList:- Handles the display of files and directories using an
ItemList. - Populates the list with items from the specified directory.
- Emits a signal when an item is selected, passing the path and whether it's a directory.
- Handles the display of files and directories using an
-
FileSystemItem:- A simple class to encapsulate the path and type (file or directory) of an item.
-
Icons:
- Directory and file icons are optional.
- File icons can be specified per file extension using a dictionary.
-
Signals:
FileSelectedandDirectorySelectedsignals are emitted byFileSystemBrowserto notify when a file or directory is selected.
Usage
- Add the
FileSystemBrowsernode to your scene. - Configure the
StartDirectory,DirectoryIcon, andFileIconsproperties in the Godot editor or via code. - Connect to the
FileSelectedandDirectorySelectedsignals to handle user interactions.
Example Scene Setup
public partial class Main : Node { public override void _Ready() { var fileSystemBrowser = new FileSystemBrowser { StartDirectory = "res://", DirectoryIcon = GD.Load<Texture2D>("res://icons/folder.png"), FileIcons = new Dictionary<string, Texture2D> { { ".txt", GD.Load<Texture2D>("res://icons/text_file.png") }, { ".png", GD.Load<Texture2D>("res://icons/image_file.png") } } }; AddChild(fileSystemBrowser); fileSystemBrowser.Connect("FileSelected", new Callable(this, nameof(OnFileSelected))); fileSystemBrowser.Connect("DirectorySelected", new Callable(this, nameof(OnDirectorySelected))); } private void OnFileSelected(string filePath) { GD.Print($"File selected: {filePath}"); } private void OnDirectorySelected(string directoryPath) { GD.Print($"Directory selected: {directoryPath}"); } }This implementation is modular, easy to extend, and adheres to good coding practices.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.