refactor some editor functions to files
Focusing on getting button -> event refactor. As well as spawned thing -> button active refactor. Both should allow levels to work propery, setting buttons active and such. Once that's working just iron out bugs like double spawn.main
parent
2433f6386d
commit
2b9f96d4b4
@ -0,0 +1,121 @@
|
||||
use crate::ui;
|
||||
use bevy::{asset::Asset, prelude::*};
|
||||
|
||||
#[derive(Resource, Default)]
|
||||
pub struct AssetRegistry(pub Vec<HandleUntyped>);
|
||||
|
||||
#[derive(Debug, Component)]
|
||||
pub struct ReloadAssets;
|
||||
|
||||
pub fn reload_assets(
|
||||
server: Res<AssetServer>,
|
||||
mut registry: ResMut<AssetRegistry>,
|
||||
mut writer: EventWriter<ui::Alert>,
|
||||
) {
|
||||
match server.load_folder(".") {
|
||||
Ok(handles) => registry.0 = handles,
|
||||
Err(e) => writer.send(ui::Alert::Warn(format!(
|
||||
"Could not find `assets` folder!\n{:?}",
|
||||
e
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_asset_name<T: Asset>(server: &AssetServer, handle: Handle<T>) -> String {
|
||||
if let Some(asset_path) = server.get_handle_path(handle.clone()) {
|
||||
if let Some(stem) = asset_path.path().file_stem() {
|
||||
if let Some(val) = stem.to_str() {
|
||||
String::from(val)
|
||||
} else {
|
||||
String::from("???")
|
||||
}
|
||||
} else {
|
||||
String::from("???")
|
||||
}
|
||||
} else {
|
||||
String::from("???")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_asset_button<A: Asset, C: Component>(
|
||||
root: &Query<Entity, With<C>>,
|
||||
commands: &mut Commands,
|
||||
target: ui::TargetAsset<A>,
|
||||
name: String,
|
||||
font: Option<Handle<Font>>,
|
||||
) -> Entity {
|
||||
commands
|
||||
.spawn((
|
||||
target,
|
||||
ButtonBundle {
|
||||
style: Style {
|
||||
border: UiRect::all(Val::Px(1.0)),
|
||||
margin: UiRect::all(Val::Px(1.0)),
|
||||
padding: UiRect::all(Val::Px(1.0)),
|
||||
..default()
|
||||
},
|
||||
border_color: Color::BLACK.into(),
|
||||
..default()
|
||||
},
|
||||
ui::Title {
|
||||
text: name,
|
||||
font: font.clone(),
|
||||
},
|
||||
))
|
||||
.set_parent(root.single())
|
||||
.id()
|
||||
}
|
||||
|
||||
pub fn create_entity_button<C: Component>(
|
||||
root: &Query<Entity, With<C>>,
|
||||
commands: &mut Commands,
|
||||
target: ui::TargetEntity,
|
||||
name: String,
|
||||
) -> Entity {
|
||||
commands
|
||||
.spawn((
|
||||
target,
|
||||
ButtonBundle {
|
||||
style: Style {
|
||||
border: UiRect::all(Val::Px(1.0)),
|
||||
margin: UiRect::all(Val::Px(1.0)),
|
||||
padding: UiRect::all(Val::Px(1.0)),
|
||||
..default()
|
||||
},
|
||||
border_color: Color::BLACK.into(),
|
||||
..default()
|
||||
},
|
||||
ui::Title {
|
||||
text: name,
|
||||
..default()
|
||||
},
|
||||
))
|
||||
.set_parent(root.single())
|
||||
.id()
|
||||
}
|
||||
|
||||
pub fn destroy_asset_button<A: Asset>(
|
||||
current: &Query<(Entity, &ui::TargetAsset<A>)>,
|
||||
commands: &mut Commands,
|
||||
target: &ui::TargetAsset<A>,
|
||||
) {
|
||||
if let Some(entity) = current
|
||||
.iter()
|
||||
.find_map(|(entity, this)| (this.handle == target.handle).then_some(entity))
|
||||
{
|
||||
commands.entity(entity).despawn_recursive();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn destroy_entity_button(
|
||||
current: &Query<(Entity, &ui::TargetEntity)>,
|
||||
commands: &mut Commands,
|
||||
target: &ui::TargetEntity,
|
||||
) {
|
||||
if let Some(entity) = current
|
||||
.iter()
|
||||
.find_map(|(entity, this)| (this.entity == target.entity).then_some(entity))
|
||||
{
|
||||
commands.entity(entity).despawn_recursive();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,138 @@
|
||||
use crate::{editor::assets::*, ui};
|
||||
use bevy::{audio::PlaybackMode, prelude::*};
|
||||
|
||||
#[derive(Debug, Component, Reflect, Default)]
|
||||
#[reflect(Component)]
|
||||
pub struct AudioRoot;
|
||||
|
||||
#[derive(Debug, Component, Default)]
|
||||
pub struct AudioWidget;
|
||||
|
||||
pub fn audio_ui(
|
||||
mut events: EventReader<AssetEvent<AudioSource>>,
|
||||
mut commands: Commands,
|
||||
widget: Query<Entity, With<AudioWidget>>,
|
||||
current: Query<(Entity, &ui::TargetAsset<AudioSource>)>,
|
||||
server: Res<AssetServer>,
|
||||
) {
|
||||
events.iter().for_each(|event| match event {
|
||||
AssetEvent::Created { handle } => {
|
||||
info!("Asset created! {:?}", event);
|
||||
create_asset_button(
|
||||
&widget,
|
||||
&mut commands,
|
||||
ui::TargetAsset {
|
||||
handle: handle.clone(),
|
||||
},
|
||||
get_asset_name(&server, handle.clone()),
|
||||
None,
|
||||
);
|
||||
}
|
||||
AssetEvent::Removed { handle } => {
|
||||
info!("Asset removed! {:?}", event);
|
||||
destroy_asset_button(
|
||||
¤t,
|
||||
&mut commands,
|
||||
&ui::TargetAsset {
|
||||
handle: handle.clone(),
|
||||
},
|
||||
);
|
||||
}
|
||||
AssetEvent::Modified { handle } => {
|
||||
info!("Asset modified! {:?}", event);
|
||||
destroy_asset_button(
|
||||
¤t,
|
||||
&mut commands,
|
||||
&ui::TargetAsset {
|
||||
handle: handle.clone(),
|
||||
},
|
||||
);
|
||||
create_asset_button(
|
||||
&widget,
|
||||
&mut commands,
|
||||
ui::TargetAsset {
|
||||
handle: handle.clone(),
|
||||
},
|
||||
get_asset_name(&server, handle.clone()),
|
||||
None,
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[derive(Debug, Event)]
|
||||
pub enum ControlAudio {
|
||||
Loop(Handle<AudioSource>),
|
||||
Stop(Handle<AudioSource>),
|
||||
}
|
||||
|
||||
pub fn control_audio(
|
||||
mut events: EventReader<ControlAudio>,
|
||||
root: Query<Entity, With<AudioRoot>>,
|
||||
mut commands: Commands,
|
||||
sources: Query<(Entity, &Handle<AudioSource>), With<AudioSink>>,
|
||||
) {
|
||||
events.iter().for_each(|event| match event {
|
||||
ControlAudio::Loop(handle) => {
|
||||
info!("Looping audio {:?}", handle);
|
||||
let root = if let Ok(entity) = root.get_single() {
|
||||
entity
|
||||
} else {
|
||||
commands.spawn(AudioRoot).id()
|
||||
};
|
||||
commands.entity(root).with_children(|parent| {
|
||||
parent.spawn(AudioSourceBundle {
|
||||
source: handle.clone(),
|
||||
settings: PlaybackSettings {
|
||||
mode: PlaybackMode::Loop,
|
||||
paused: false,
|
||||
..default()
|
||||
},
|
||||
});
|
||||
info!("Done spawning");
|
||||
});
|
||||
}
|
||||
ControlAudio::Stop(handle) => {
|
||||
info!("Stopping audio {:?}", handle);
|
||||
sources
|
||||
.iter()
|
||||
.find_map(|(entity, source_handle)| {
|
||||
if source_handle == handle {
|
||||
Some(entity)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.iter()
|
||||
.for_each(|&entity| {
|
||||
commands.entity(entity).despawn_recursive();
|
||||
info!("Done despawning");
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn ui_control_audio(
|
||||
events: Query<
|
||||
(
|
||||
&Interaction,
|
||||
&ui::TargetAsset<AudioSource>,
|
||||
Option<&ui::Active>,
|
||||
),
|
||||
(With<Button>, Changed<Interaction>),
|
||||
>,
|
||||
mut writer: EventWriter<ControlAudio>,
|
||||
) {
|
||||
events
|
||||
.iter()
|
||||
.filter_map(
|
||||
|(interaction, ui::TargetAsset { handle }, active)| match interaction {
|
||||
Interaction::Pressed => Some((handle, active)),
|
||||
_ => None,
|
||||
},
|
||||
)
|
||||
.for_each(|(handle, active)| match active {
|
||||
Some(_) => writer.send(ControlAudio::Stop(handle.clone())),
|
||||
None => writer.send(ControlAudio::Loop(handle.clone())),
|
||||
});
|
||||
}
|
||||
@ -0,0 +1,47 @@
|
||||
pub mod audio;
|
||||
|
||||
pub mod assets;
|
||||
|
||||
use crate::ui;
|
||||
use bevy::{asset::Asset, prelude::*};
|
||||
|
||||
pub fn ui_active<T: Asset>(
|
||||
events: Query<&Handle<T>, Added<Handle<T>>>,
|
||||
buttons: Query<(Entity, &ui::TargetAsset<T>)>,
|
||||
mut commands: Commands,
|
||||
) {
|
||||
events.iter().for_each(|this_handle| {
|
||||
buttons
|
||||
.iter()
|
||||
.find_map(|(entity, ui::TargetAsset { handle })| {
|
||||
if handle == this_handle {
|
||||
Some(entity)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.and_then(|entity| {
|
||||
commands.entity(entity).insert(ui::Active);
|
||||
Some(())
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
pub fn ui_inactive<T: Asset>(
|
||||
mut events: RemovedComponents<Handle<T>>,
|
||||
targets: Query<(Entity, &ui::TargetAsset<T>), With<ui::Active>>,
|
||||
sources: Query<&Handle<T>>,
|
||||
mut commands: Commands,
|
||||
) {
|
||||
events.iter().for_each(|_| {
|
||||
targets
|
||||
.iter()
|
||||
.find_map(|(entity, ui::TargetAsset { handle })| {
|
||||
(!sources.iter().any(|this_handle| this_handle == handle)).then_some(entity)
|
||||
})
|
||||
.and_then(|entity| {
|
||||
commands.entity(entity).remove::<ui::Active>();
|
||||
Some(())
|
||||
});
|
||||
});
|
||||
}
|
||||
Loading…
Reference in New Issue