11//! A UI element for browsing assets in the Bevy Editor.
22/// The intent of this system is to provide a simple and frictionless way to browse assets in the Bevy Editor.
33/// The asset browser is a replica of the your asset directory on disk and get's automatically updated when the directory is modified.
4- use std:: path:: PathBuf ;
4+ use std:: { cmp :: Ordering , path:: PathBuf } ;
55
66use bevy:: {
77 asset:: {
@@ -47,6 +47,8 @@ impl Plugin for AssetBrowserPanePlugin {
4747 . insert_resource ( DefaultSourceFilePath ( default_source_absolute_file_path) )
4848 . insert_resource ( AssetBrowserLocation :: default ( ) )
4949 . insert_resource ( DirectoryContent :: default ( ) )
50+ . insert_resource ( DirectoryContentOrder :: ReverseAlphabetical )
51+ // .init_resource::<DirectoryContentOrder>()
5052 . add_systems ( Startup , io:: task:: fetch_directory_content)
5153 // .add_systems(Update, button_interaction)
5254 . add_systems (
@@ -70,6 +72,47 @@ impl Plugin for AssetBrowserPanePlugin {
7072 }
7173}
7274
75+ fn alphabetical_sort ( left : & Entry , right : & Entry ) -> Ordering {
76+ match ( left, right) {
77+ ( Entry :: Folder ( left_name) , Entry :: Folder ( right_name) )
78+ | ( Entry :: File ( left_name) , Entry :: File ( right_name) ) => left_name. cmp ( right_name) ,
79+ ( Entry :: File ( _) , Entry :: Folder ( _) ) => Ordering :: Greater ,
80+ ( Entry :: Folder ( _) , Entry :: File ( _) ) => Ordering :: Less ,
81+ // TODO: Figure out whether or not ignoring the order of asset sources is a good idea.
82+ _ => Ordering :: Equal ,
83+ }
84+ }
85+
86+ fn reverse_alphabetical_sort ( left : & Entry , right : & Entry ) -> Ordering {
87+ match ( left, right) {
88+ ( Entry :: Folder ( left_name) , Entry :: Folder ( right_name) )
89+ | ( Entry :: File ( left_name) , Entry :: File ( right_name) ) => left_name. cmp ( right_name) . reverse ( ) ,
90+ ( Entry :: File ( _) , Entry :: Folder ( _) ) => Ordering :: Greater ,
91+ ( Entry :: Folder ( _) , Entry :: File ( _) ) => Ordering :: Less ,
92+ // TODO: Figure out whether or not ignoring the order of asset sources is a good idea.
93+ _ => Ordering :: Equal ,
94+ }
95+ }
96+
97+ /// How [`DirectoryContent`] should be ordered
98+ #[ derive( Resource , Default , Debug , Clone , PartialEq , Eq ) ]
99+ pub enum DirectoryContentOrder {
100+ /// Ordered alphabetically with respect to folders
101+ #[ default]
102+ Alphabetical ,
103+ /// Ordered reverse alphabetically with respect to folders
104+ ReverseAlphabetical ,
105+ }
106+ impl DirectoryContentOrder {
107+ /// Sorts a given [`DirectoryContent`] with the current method
108+ pub fn sort ( & self , content : & mut DirectoryContent ) {
109+ match self {
110+ Self :: Alphabetical => content. 0 . sort_by ( alphabetical_sort) ,
111+ Self :: ReverseAlphabetical => content. 0 . sort_by ( reverse_alphabetical_sort) ,
112+ }
113+ }
114+ }
115+
73116/// One entry of [`DirectoryContent`]
74117#[ derive( Debug , Clone , PartialEq , Eq ) ]
75118pub enum Entry {
0 commit comments