Skip to content

Commit

Permalink
added files dialog sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
RonenNess committed Apr 25, 2024
1 parent 9511d47 commit 93a87aa
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 10 deletions.
4 changes: 2 additions & 2 deletions GeonBit.UI.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>GeonBit.UI</id>
<version>4.3.0.3</version>
<version>4.3.0.4</version>
<authors>Ronen Ness</authors>
<owners>RonenNess</owners>
<title>MonoGame GeonBit.UI</title>
Expand All @@ -12,7 +12,7 @@ It provide all the basic elements required to make a game / editor UI, and comes
<projectUrl>https://github.com/RonenNess/GeonBit.UI</projectUrl>
<iconUrl>https://raw.githubusercontent.com/RonenNess/GeonBit.UI/master/assets/img/nuget_icon.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<releaseNotes>Updated to .net 7, added text rotation, and fixed some text validators.</releaseNotes>
<releaseNotes>Added sorting to files dialog.</releaseNotes>
<description>UI system for MonoGame projects.</description>
<tags>monogame ui gui hud user-interface gamedev</tags>
<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion GeonBit.UI/Source/UserInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public enum BuiltinThemes
public class UserInterface : System.IDisposable
{
/// <summary>Current GeonBit.UI version identifier.</summary>
public const string VERSION = "4.3.0.3";
public const string VERSION = "4.3.0.4";

/// <summary>
/// The currently active user interface instance.
Expand Down
104 changes: 97 additions & 7 deletions GeonBit.UI/Source/Utils/MessageBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace GeonBit.UI.Utils
{
Expand Down Expand Up @@ -260,6 +261,81 @@ public static MessageBoxHandle ShowMsgBox(string header, string text, string clo
}, size: size ?? DefaultMsgBoxSize, extraEntities: extraEntities, onDone: onDone);
}

/// <summary>
/// How to sort files when presenting them in files dialog.
/// </summary>
public enum FilesSortingMode
{
/// <summary>
/// Files will be presented in whatever order the OS returns them.
/// </summary>
Unsorted,

/// <summary>
/// Sort files by name, ascending (0 to 9, A to Z).
/// </summary>
ByNameAscending,

/// <summary>
/// Sort files by name, descending (9 to 0, Z to A).
/// </summary>
ByNameDescending,

/// <summary>
/// Sort files by creation time, ascending.
/// </summary>
ByCreationDateAscending,

/// <summary>
/// Sort files by creation time, descending.
/// </summary>
ByCreationDateDescending,

/// <summary>
/// Sort files by modify time, ascending.
/// </summary>
ByModifyDateAscending,

/// <summary>
/// Sort files by modify time, descending.
/// </summary>
ByModifyDateDescending,
}

/// <summary>
/// Sort list of files / folder names.
/// </summary>
private static void _SortFilesList(ref List<string> filenames, string path, FilesSortingMode sorting)
{
switch (sorting)
{
case FilesSortingMode.ByNameAscending:
filenames.Sort();
break;

case FilesSortingMode.ByNameDescending:
filenames.Sort();
filenames.Reverse();
break;

case FilesSortingMode.ByCreationDateAscending:
filenames = filenames.OrderBy(x => new FileInfo(Path.Combine(path, x)).CreationTime).ToList();
break;

case FilesSortingMode.ByCreationDateDescending:
filenames = filenames.OrderByDescending(x => new FileInfo(Path.Combine(path, x)).CreationTime).ToList();
break;

case FilesSortingMode.ByModifyDateAscending:
filenames = filenames.OrderBy(x => new FileInfo(Path.Combine(path, x)).LastWriteTime).ToList();
break;

case FilesSortingMode.ByModifyDateDescending:
filenames = filenames.OrderByDescending(x => new FileInfo(Path.Combine(path, x)).LastWriteTime).ToList();
break;
}
}

/// <summary>
/// Open a dialog to select file for saving.
/// </summary>
Expand All @@ -274,8 +350,9 @@ public static MessageBoxHandle ShowMsgBox(string header, string text, string clo
/// <param name="saveButtonTxt">String to show on the save file button.</param>
/// <param name="cancelButtonTxt">String to show on the cancel button.</param>
/// <param name="overrideWarning">If not null, will show this warning in a Yes/No prompt if the user tries to select an existing file.</param>
/// <param name="sorting">How to sort files.</param>
/// <returns>Message box handle.</returns>
public static MessageBoxHandle OpenSaveFileDialog(string path, Func<FileDialogResponse, bool> onSelected, Action onCancel = null!, FileDialogOptions options = FileDialogOptions.Default, Func<string, bool> filterFiles = null!, Func<string, bool> filterFolders = null!, string title = "Save File As..", string message = null!, string saveButtonTxt = "Save File", string cancelButtonTxt = "Cancel", string overrideWarning = "File '<filename>' already exists!\nAre you sure you want to override it?")
public static MessageBoxHandle OpenSaveFileDialog(string path, Func<FileDialogResponse, bool> onSelected, Action onCancel = null!, FileDialogOptions options = FileDialogOptions.Default, Func<string, bool> filterFiles = null!, Func<string, bool> filterFolders = null!, string title = "Save File As..", string message = null!, string saveButtonTxt = "Save File", string cancelButtonTxt = "Cancel", string overrideWarning = "File '<filename>' already exists!\nAre you sure you want to override it?", FilesSortingMode sorting = FilesSortingMode.Unsorted)
{
// current path
var currPath = string.IsNullOrEmpty(path) ? Path.GetFullPath(Directory.GetCurrentDirectory()) : Path.GetFullPath(path);
Expand Down Expand Up @@ -346,25 +423,37 @@ void UpdateFilesList()
}

// add folders
List<string> folders = new List<string>();
foreach (var dir in Directory.GetDirectories(currPath))
{
if (filterFolders == null || filterFolders(dir))
{
filesList.AddItem(Path.GetFileName(dir));
filesList.SetIcon("textures/folder_icon", filesList.Count - 1);
folders.Add(Path.GetFileName(dir));
}
}
_SortFilesList(ref folders, currPath, sorting);
foreach (var dir in folders)
{
filesList.AddItem(dir);
filesList.SetIcon("textures/folder_icon", filesList.Count - 1);
}
}

// add files
List<string> files = new List<string>();
foreach (var file in Directory.GetFiles(currPath))
{
if (filterFiles == null || filterFiles(file))
{
filesList.AddItem(Path.GetFileName(file));
filesList.SetIcon("textures/file_icon", filesList.Count - 1);
files.Add(Path.GetFileName(file));
}
}
_SortFilesList(ref files, currPath, sorting);
foreach (var file in files)
{
filesList.AddItem(file);
filesList.SetIcon("textures/file_icon", filesList.Count - 1);
}
}

// click on files list - check if enter or exit folder
Expand Down Expand Up @@ -520,11 +609,12 @@ void UpdateFilesList()
/// <param name="message">Optional message to show above files.</param>
/// <param name="loadButtonTxt">String to show on the load file button.</param>
/// <param name="cancelButtonTxt">String to show on the cancel button.</param>
/// <param name="sorting">How to sort files.</param>
/// <returns>Message box handle.</returns>
public static MessageBoxHandle OpenLoadFileDialog(string path, Func<FileDialogResponse, bool> onSelected, Action onCancel = null!, FileDialogOptions options = FileDialogOptions.Default, Func<string, bool> filterFiles = null!, Func<string, bool> filterFolders = null!, string title = "Open File..", string message = null!, string loadButtonTxt = "Open File", string cancelButtonTxt = "Cancel")
public static MessageBoxHandle OpenLoadFileDialog(string path, Func<FileDialogResponse, bool> onSelected, Action onCancel = null!, FileDialogOptions options = FileDialogOptions.Default, Func<string, bool> filterFiles = null!, Func<string, bool> filterFolders = null!, string title = "Open File..", string message = null!, string loadButtonTxt = "Open File", string cancelButtonTxt = "Cancel", FilesSortingMode sorting = FilesSortingMode.Unsorted)
{
options |= FileDialogOptions.MustSelectExistingFile;
return OpenSaveFileDialog(path, onSelected, onCancel, options, filterFiles, filterFolders, title, message, loadButtonTxt, cancelButtonTxt, null);
return OpenSaveFileDialog(path, onSelected, onCancel, options, filterFiles, filterFolders, title, message, loadButtonTxt, cancelButtonTxt, null, sorting);
}
}

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2111,6 +2111,10 @@ If you want to use the new files dialog, you must include the new textures that
- Added option to emulate mouse click via the mouse input provider.
- Fixed repo link in demo project.

### 4.3.0.4

- Added optional sorting to file dialogs.

## Credits

GeonBit.UI was written by Ronen Ness, but uses some free textures made by awesome people who share their work for free.
Expand Down

0 comments on commit 93a87aa

Please sign in to comment.