Skip to content

Commit 89b4214

Browse files
authored
Added basic sorting for asset browser (#256)
* Added basic sorting for asset browser * Remove Ord implementation for Entry and move ordering to seperate functions * Remove temporary fix I accidentally made permenant
1 parent ef28bd9 commit 89b4214

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

bevy_editor_panes/bevy_asset_browser/src/io/task.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{AssetBrowserLocation, DirectoryContent, Entry};
1+
use crate::{AssetBrowserLocation, DirectoryContent, DirectoryContentOrder, Entry};
22
use bevy::{
33
asset::io::AssetSourceBuilders,
44
prelude::*,
@@ -20,9 +20,12 @@ pub(crate) fn fetch_task_is_running(
2020
pub(crate) fn poll_task(
2121
mut commands: Commands,
2222
mut task_query: Query<(Entity, &mut FetchDirectoryContentTask)>,
23+
content_order: Res<DirectoryContentOrder>,
2324
) {
2425
let (task_entity, mut task) = task_query.single_mut().unwrap();
25-
if let Some(content) = block_on(poll_once(&mut task.0)) {
26+
if let Some(mut content) = block_on(poll_once(&mut task.0)) {
27+
content_order.sort(&mut content);
28+
2629
commands.entity(task_entity).despawn();
2730
commands.insert_resource(content);
2831
}

bevy_editor_panes/bevy_asset_browser/src/lib.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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

66
use 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)]
75118
pub enum Entry {

0 commit comments

Comments
 (0)