You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1325 lines
45 KiB
Rust
1325 lines
45 KiB
Rust
// Monologe Trees Editor
|
|
//
|
|
// Editor for creating Monologue Trees levels
|
|
//
|
|
// BUGS:
|
|
// * Camera order ambiguity
|
|
// * Multi-GLTF UX is bad.
|
|
// * Consider GLTF hierarchy (GLTF1 > Scene1a/Scene1b, GlTF2 > Scene2a/Scene2b, etc)
|
|
// * Easy despawn when de-selecting gltf
|
|
//
|
|
// TODO:
|
|
// * (easy) Play all animations
|
|
// * (easy) Clear button to wipe spawned scene
|
|
// * (brutal) export level
|
|
// * (hard) import level
|
|
//
|
|
// Asset types:
|
|
// * Audios (done)
|
|
// * Loop individual (done)
|
|
// * Gltfs (doing)
|
|
// * Scenes
|
|
// * Animations
|
|
// * Play/Pause all
|
|
// * Fonts (done)
|
|
// * Monologues (done)
|
|
|
|
use bevy::{
|
|
asset::{Asset, Assets},
|
|
asset::{AssetLoader, LoadContext, LoadedAsset},
|
|
audio::PlaybackMode,
|
|
gltf::Gltf,
|
|
prelude::*,
|
|
utils::BoxedFuture,
|
|
};
|
|
use monologue_trees::{debug::*, ui};
|
|
|
|
const WELCOME_MESSAGES: &'static [&'static str] = &[
|
|
"Welcome to the Monologue Trees editor!",
|
|
concat!(
|
|
"Import assets by dragging and dropping files into the editor\n",
|
|
"\n",
|
|
"Supported file types (for now):\n",
|
|
"* 3D: .gltf, .glb\n",
|
|
"* Audio: .ogg\n",
|
|
"* Font: .ttf, .otf\n",
|
|
"* Monologues: .monologue.txt",
|
|
),
|
|
];
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins((
|
|
DefaultPlugins.set(WindowPlugin {
|
|
primary_window: Some(Window {
|
|
title: "Monologue Trees Editor".into(),
|
|
resolution: (640., 480.).into(),
|
|
..default()
|
|
}),
|
|
..default()
|
|
}),
|
|
DebugInfoPlugin,
|
|
ui::GameUiPlugin,
|
|
))
|
|
.init_resource::<AssetRegistry>()
|
|
.add_asset::<Monologue>()
|
|
.init_asset_loader::<MonologueLoader>()
|
|
.add_event::<CustomAssetEvent<Scene>>()
|
|
.add_event::<CustomAssetEvent<AnimationClip>>()
|
|
.add_systems(Startup, (initialize_ui, init_texts_ui, welcome_message))
|
|
.add_systems(
|
|
Update,
|
|
(
|
|
manage_gltf_animation_ui,
|
|
init_animations_ui,
|
|
animations_ui,
|
|
play_all_animations,
|
|
play_animation,
|
|
),
|
|
)
|
|
.add_systems(
|
|
Update,
|
|
(manage_gltf_scene_ui, scenes_ui, control_active_scenes),
|
|
)
|
|
.add_systems(
|
|
Update,
|
|
(
|
|
cameras_ui,
|
|
manage_active_camera,
|
|
control_active_camera,
|
|
fallback_camera,
|
|
),
|
|
)
|
|
.add_systems(Update, (audio_ui, play_audio))
|
|
.add_systems(
|
|
Update,
|
|
(
|
|
import_files,
|
|
gltf_ui,
|
|
fonts_ui,
|
|
texts_ui,
|
|
manage_active_gltf,
|
|
show_preview_text,
|
|
sync_monologue_font,
|
|
),
|
|
)
|
|
.run();
|
|
}
|
|
|
|
#[derive(Resource, Default)]
|
|
pub struct AssetRegistry(Vec<HandleUntyped>);
|
|
|
|
#[derive(Event)]
|
|
pub enum CustomAssetEvent<T: Asset> {
|
|
Add { handle: Handle<T>, name: String },
|
|
Remove { handle: Handle<T> },
|
|
Clear,
|
|
}
|
|
|
|
#[derive(Debug, Component)]
|
|
pub struct TabRoot;
|
|
|
|
#[derive(Debug, Component)]
|
|
pub struct LevelRoot;
|
|
|
|
#[derive(Debug, Component)]
|
|
pub struct EditorCamera;
|
|
|
|
fn initialize_ui(mut commands: Commands) {
|
|
// Empty entity for populating the level being edited
|
|
commands.spawn((SpatialBundle { ..default() }, LevelRoot));
|
|
|
|
commands.spawn((
|
|
Camera2dBundle { ..default() },
|
|
UiCameraConfig { show_ui: true },
|
|
Name::new("Editor Camera"),
|
|
EditorCamera,
|
|
ui::Active,
|
|
));
|
|
|
|
let base_style = Style {
|
|
border: UiRect::all(Val::Px(1.0)),
|
|
padding: UiRect::all(Val::Px(1.0)),
|
|
overflow: Overflow::clip(),
|
|
flex_direction: FlexDirection::Column,
|
|
..default()
|
|
};
|
|
|
|
commands
|
|
.spawn(NodeBundle {
|
|
style: Style {
|
|
top: Val::Px(0.0),
|
|
left: Val::Px(0.0),
|
|
position_type: PositionType::Absolute,
|
|
border: UiRect::all(Val::Px(1.0)),
|
|
margin: UiRect::all(Val::Px(1.0)),
|
|
padding: UiRect::all(Val::Px(1.0)),
|
|
flex_direction: FlexDirection::Column,
|
|
overflow: Overflow::clip(),
|
|
..default()
|
|
},
|
|
background_color: Color::WHITE.into(),
|
|
border_color: Color::BLACK.into(),
|
|
..default()
|
|
})
|
|
.with_children(|parent| {
|
|
let container = parent
|
|
.spawn((NodeBundle {
|
|
style: Style {
|
|
border: UiRect::all(Val::Px(1.0)),
|
|
margin: UiRect::all(Val::Px(1.0)),
|
|
padding: UiRect::all(Val::Px(1.0)),
|
|
flex_direction: FlexDirection::Row,
|
|
overflow: Overflow::clip(),
|
|
..default()
|
|
},
|
|
background_color: Color::WHITE.into(),
|
|
border_color: Color::BLACK.into(),
|
|
..default()
|
|
},))
|
|
.with_children(|parent| {
|
|
// HACK: This is super janky but I think we need it like this for UI layout rules
|
|
let mut content_containers: Vec<(String, Entity)> = Vec::new();
|
|
|
|
// Containers with asset content
|
|
parent
|
|
.spawn((
|
|
NodeBundle {
|
|
style: Style {
|
|
border: UiRect::all(Val::Px(1.0)),
|
|
margin: UiRect::all(Val::Px(1.0)),
|
|
padding: UiRect::all(Val::Px(1.0)),
|
|
flex_direction: FlexDirection::Column,
|
|
overflow: Overflow::clip(),
|
|
justify_content: JustifyContent::FlexStart,
|
|
..default()
|
|
},
|
|
background_color: Color::WHITE.into(),
|
|
border_color: Color::BLACK.into(),
|
|
..default()
|
|
},
|
|
ui::Sorting(2),
|
|
))
|
|
.with_children(|parent| {
|
|
content_containers.push(spawn_tab_container::<FontWidget>(
|
|
"Font",
|
|
parent,
|
|
ui::Select::Single,
|
|
));
|
|
content_containers.push(spawn_tab_container::<MonologueWidget>(
|
|
"Monologue",
|
|
parent,
|
|
ui::Select::Single,
|
|
));
|
|
content_containers.push(spawn_tab_container::<AudioWidget>(
|
|
"Audio",
|
|
parent,
|
|
ui::Select::Multi,
|
|
));
|
|
content_containers.push(spawn_tab_container::<GltfWidget>(
|
|
"Gltf",
|
|
parent,
|
|
ui::Select::Single,
|
|
));
|
|
content_containers.push(spawn_tab_container::<SceneWidget>(
|
|
"Scene",
|
|
parent,
|
|
ui::Select::Multi,
|
|
));
|
|
content_containers.push(spawn_tab_container::<AnimationWidget>(
|
|
"Animation",
|
|
parent,
|
|
ui::Select::Multi,
|
|
));
|
|
content_containers.push(spawn_tab_container::<CameraWidget>(
|
|
"Camera",
|
|
parent,
|
|
ui::Select::Single,
|
|
));
|
|
});
|
|
|
|
// Container for tabs that open/close containers
|
|
parent
|
|
.spawn((
|
|
NodeBundle {
|
|
style: Style {
|
|
border: UiRect::all(Val::Px(1.0)),
|
|
margin: UiRect::all(Val::Px(1.0)),
|
|
padding: UiRect::all(Val::Px(1.0)),
|
|
flex_direction: FlexDirection::Column,
|
|
overflow: Overflow::clip(),
|
|
..default()
|
|
},
|
|
background_color: Color::WHITE.into(),
|
|
border_color: Color::BLACK.into(),
|
|
..default()
|
|
},
|
|
ui::Sorting(1),
|
|
ui::Select::Single,
|
|
))
|
|
.with_children(|parent| {
|
|
let b = ButtonBundle {
|
|
style: Style {
|
|
..base_style.clone()
|
|
},
|
|
background_color: Color::WHITE.into(),
|
|
border_color: Color::BLACK.into(),
|
|
..default()
|
|
};
|
|
content_containers.iter().for_each(|(name, target)| {
|
|
parent.spawn((
|
|
b.clone(),
|
|
ui::Title {
|
|
text: name.clone(),
|
|
..default()
|
|
},
|
|
ui::Collapse { target: *target },
|
|
));
|
|
});
|
|
});
|
|
})
|
|
.id();
|
|
parent.spawn((
|
|
ui::TitleBarBase::new(Color::WHITE).bundle(),
|
|
ui::Title {
|
|
text: "Assets".into(),
|
|
..default()
|
|
},
|
|
ui::Minimize { target: container },
|
|
ui::Sorting(0),
|
|
));
|
|
});
|
|
}
|
|
|
|
fn welcome_message(mut writer: EventWriter<ui::Alert>) {
|
|
WELCOME_MESSAGES
|
|
.iter()
|
|
.for_each(|&msg| writer.send(ui::Alert::Info(msg.into())))
|
|
}
|
|
|
|
fn spawn_tab_container<T: Default + Component>(
|
|
title: &'static str,
|
|
parent: &mut ChildBuilder,
|
|
select: ui::Select,
|
|
) -> (String, Entity) {
|
|
(
|
|
title.into(),
|
|
// Content node
|
|
parent
|
|
.spawn((
|
|
NodeBundle {
|
|
style: Style {
|
|
display: Display::None,
|
|
border: UiRect::all(Val::Px(1.0)),
|
|
margin: UiRect::all(Val::Px(1.0)),
|
|
padding: UiRect::all(Val::Px(1.0)),
|
|
flex_direction: FlexDirection::Column,
|
|
..default()
|
|
},
|
|
background_color: Color::WHITE.into(),
|
|
border_color: Color::BLACK.into(),
|
|
..default()
|
|
},
|
|
T::default(),
|
|
ui::Scroll,
|
|
Interaction::default(),
|
|
select,
|
|
))
|
|
.id(),
|
|
)
|
|
}
|
|
|
|
fn import_files(
|
|
mut events: EventReader<FileDragAndDrop>,
|
|
server: Res<AssetServer>,
|
|
mut registry: ResMut<AssetRegistry>,
|
|
) {
|
|
events.iter().for_each(|event| match event {
|
|
FileDragAndDrop::DroppedFile { path_buf, .. } => {
|
|
registry.0.push(
|
|
server.load_untyped(
|
|
path_buf
|
|
.clone()
|
|
.into_os_string()
|
|
.into_string()
|
|
.expect("Path converts to string"),
|
|
),
|
|
);
|
|
}
|
|
_ => (),
|
|
})
|
|
}
|
|
|
|
use audio::*;
|
|
mod audio {
|
|
|
|
use super::*;
|
|
|
|
#[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);
|
|
let id = create_asset_button(
|
|
&widget,
|
|
&mut commands,
|
|
ui::TargetAsset {
|
|
handle: handle.clone(),
|
|
},
|
|
get_asset_name(&server, handle.clone()),
|
|
None,
|
|
);
|
|
commands.entity(id).insert(AudioSourceBundle {
|
|
source: handle.clone(),
|
|
settings: PlaybackSettings {
|
|
mode: PlaybackMode::Loop,
|
|
paused: true,
|
|
..default()
|
|
},
|
|
});
|
|
}
|
|
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(),
|
|
},
|
|
);
|
|
let id = create_asset_button(
|
|
&widget,
|
|
&mut commands,
|
|
ui::TargetAsset {
|
|
handle: handle.clone(),
|
|
},
|
|
get_asset_name(&server, handle.clone()),
|
|
None,
|
|
);
|
|
commands.entity(id).insert(AudioSourceBundle {
|
|
source: handle.clone(),
|
|
settings: PlaybackSettings {
|
|
mode: PlaybackMode::Loop,
|
|
paused: true,
|
|
..default()
|
|
},
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
pub fn play_audio(
|
|
events: Query<(Entity, &Interaction, &AudioSink), (With<Button>, Changed<Interaction>)>,
|
|
mut commands: Commands,
|
|
) {
|
|
events
|
|
.iter()
|
|
.filter(|(_, &interaction, _)| interaction == Interaction::Pressed)
|
|
.for_each(|(entity, _, sink)| {
|
|
sink.toggle();
|
|
if sink.is_paused() {
|
|
commands.entity(entity).remove::<ui::Active>();
|
|
} else {
|
|
commands.entity(entity).insert(ui::Active);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
use assets::*;
|
|
mod assets {
|
|
|
|
use super::*;
|
|
|
|
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)| {
|
|
if this.handle == target.handle {
|
|
Some(entity)
|
|
} else {
|
|
None
|
|
}
|
|
}) {
|
|
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)| {
|
|
if this.entity == target.entity {
|
|
Some(entity)
|
|
} else {
|
|
None
|
|
}
|
|
}) {
|
|
commands.entity(entity).despawn_recursive();
|
|
}
|
|
}
|
|
|
|
pub fn has_extensions<T: Asset>(
|
|
server: &AssetServer,
|
|
handle: Handle<T>,
|
|
extensions: &[&'static str],
|
|
) -> bool {
|
|
if let Some(asset_path) = server.get_handle_path(handle.clone()) {
|
|
if let Some(extension) = asset_path.path().extension() {
|
|
extensions.iter().any(|&check| check == extension)
|
|
} else {
|
|
false
|
|
}
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
}
|
|
|
|
use gltf::*;
|
|
mod gltf {
|
|
use super::*;
|
|
|
|
#[derive(Debug, Component, Default)]
|
|
pub struct GltfWidget;
|
|
|
|
// TODO: Mark selected gltf as active ~single exclusive~
|
|
pub fn gltf_ui(
|
|
mut events: EventReader<AssetEvent<Gltf>>,
|
|
mut commands: Commands,
|
|
widget: Query<Entity, With<GltfWidget>>,
|
|
current: Query<(Entity, &ui::TargetAsset<Gltf>)>,
|
|
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,
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
pub fn manage_active_gltf(
|
|
events: Query<
|
|
(Entity, &Interaction, Option<&ui::Active>),
|
|
(With<Button>, Changed<Interaction>),
|
|
>,
|
|
mut commands: Commands,
|
|
) {
|
|
events
|
|
.iter()
|
|
.filter(|(_, &interaction, _)| interaction == Interaction::Pressed)
|
|
.for_each(|(entity, _, active_ish)| match active_ish {
|
|
Some(_) => {
|
|
commands.entity(entity).remove::<ui::Active>();
|
|
}
|
|
None => {
|
|
commands.entity(entity).insert(ui::Active);
|
|
}
|
|
});
|
|
}
|
|
|
|
pub fn manage_gltf_animation_ui(
|
|
added: Query<Entity, (With<Button>, Added<ui::Active>)>,
|
|
mut removed: RemovedComponents<ui::Active>,
|
|
targets_gltf: Query<&ui::TargetAsset<Gltf>>,
|
|
gltfs: Res<Assets<Gltf>>,
|
|
mut animation_clip_events: EventWriter<CustomAssetEvent<AnimationClip>>,
|
|
) {
|
|
removed
|
|
.iter()
|
|
.filter_map(|entity| {
|
|
if let Ok(ui::TargetAsset { handle }) = targets_gltf.get(entity) {
|
|
gltfs.get(handle)
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
.for_each(|gltf| {
|
|
gltf.named_animations
|
|
.iter()
|
|
.for_each(|(animation_name, animation_handle)| {
|
|
info!("Named animation: {:?}", animation_name);
|
|
animation_clip_events.send(CustomAssetEvent::Remove {
|
|
handle: animation_handle.clone(),
|
|
});
|
|
});
|
|
});
|
|
added
|
|
.iter()
|
|
.filter_map(|entity| {
|
|
if let Ok(ui::TargetAsset { handle }) = targets_gltf.get(entity) {
|
|
gltfs.get(handle)
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
.for_each(|gltf| {
|
|
// Populate animations tab
|
|
gltf.named_animations
|
|
.iter()
|
|
.for_each(|(animation_name, animation_handle)| {
|
|
animation_clip_events.send(CustomAssetEvent::Add {
|
|
name: animation_name.clone(),
|
|
handle: animation_handle.clone(),
|
|
})
|
|
});
|
|
});
|
|
}
|
|
|
|
pub fn manage_gltf_scene_ui(
|
|
added: Query<Entity, (With<Button>, Added<ui::Active>)>,
|
|
mut removed: RemovedComponents<ui::Active>,
|
|
targets_gltf: Query<&ui::TargetAsset<Gltf>>,
|
|
gltfs: Res<Assets<Gltf>>,
|
|
mut scene_events: EventWriter<CustomAssetEvent<Scene>>,
|
|
) {
|
|
removed
|
|
.iter()
|
|
.filter_map(|entity| {
|
|
if let Ok(ui::TargetAsset { handle }) = targets_gltf.get(entity) {
|
|
gltfs.get(handle)
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
.for_each(|gltf| {
|
|
gltf.named_scenes
|
|
.iter()
|
|
.for_each(|(scene_name, scene_handle)| {
|
|
info!("Named scene: {:?}", scene_name);
|
|
scene_events.send(CustomAssetEvent::Remove {
|
|
handle: scene_handle.clone(),
|
|
});
|
|
});
|
|
});
|
|
added
|
|
.iter()
|
|
.filter_map(|entity| {
|
|
if let Ok(ui::TargetAsset { handle }) = targets_gltf.get(entity) {
|
|
gltfs.get(handle)
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
.for_each(|gltf| {
|
|
// Populate scenes tab
|
|
gltf.named_scenes
|
|
.iter()
|
|
.for_each(|(scene_name, scene_handle)| {
|
|
info!("Named scene: {:?}", scene_name);
|
|
scene_events.send(CustomAssetEvent::Add {
|
|
name: scene_name.clone(),
|
|
handle: scene_handle.clone(),
|
|
})
|
|
})
|
|
});
|
|
}
|
|
}
|
|
|
|
// TODO: Mark loaded animation as active
|
|
use scenes::*;
|
|
mod scenes {
|
|
use super::*;
|
|
|
|
#[derive(Debug, Component, Default)]
|
|
pub struct SceneWidget;
|
|
|
|
pub fn scenes_ui(
|
|
mut events: EventReader<CustomAssetEvent<Scene>>,
|
|
mut commands: Commands,
|
|
widget: Query<Entity, With<SceneWidget>>,
|
|
current: Query<(Entity, &ui::TargetAsset<Scene>)>,
|
|
) {
|
|
events.iter().for_each(|event| {
|
|
let empty = current.iter().len() == 0;
|
|
|
|
match event {
|
|
CustomAssetEvent::Add { name, handle } => {
|
|
info!("Asset loading! {:?}({:?})", name, handle);
|
|
// Spawn new tree
|
|
let e = create_asset_button(
|
|
&widget,
|
|
&mut commands,
|
|
ui::TargetAsset {
|
|
handle: handle.clone(),
|
|
},
|
|
name.clone(),
|
|
None,
|
|
);
|
|
// If this is the first scene being added, set it as active
|
|
if empty {
|
|
commands.entity(e).insert(ui::Active);
|
|
}
|
|
}
|
|
CustomAssetEvent::Remove { handle } => {
|
|
destroy_asset_button(
|
|
¤t,
|
|
&mut commands,
|
|
&ui::TargetAsset {
|
|
handle: handle.clone(),
|
|
},
|
|
);
|
|
}
|
|
CustomAssetEvent::Clear => {
|
|
commands.entity(widget.single()).despawn_descendants();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
pub fn control_active_scenes(
|
|
added: Query<Entity, (With<Button>, Added<ui::Active>)>,
|
|
mut removed: RemovedComponents<ui::Active>,
|
|
scene_refs: Query<&ui::TargetAsset<Scene>>,
|
|
scenes: Query<(Entity, &Handle<Scene>)>,
|
|
level_root: Query<Entity, With<LevelRoot>>,
|
|
mut commands: Commands,
|
|
) {
|
|
// A scene button was marked inactive
|
|
removed.iter().for_each(|entity| {
|
|
// Get the handle associated with that button
|
|
if let Ok(ui::TargetAsset { handle }) = scene_refs.get(entity) {
|
|
if let Some(entity) = scenes.iter().find_map(|(entity, this_handle)| {
|
|
if this_handle == handle {
|
|
Some(entity)
|
|
} else {
|
|
None
|
|
}
|
|
}) {
|
|
commands.entity(entity).despawn_recursive();
|
|
}
|
|
}
|
|
});
|
|
added.iter().for_each(|entity| {
|
|
if let Ok(ui::TargetAsset { handle }) = scene_refs.get(entity) {
|
|
info!("Spawning Scene {:?}", handle);
|
|
commands
|
|
.entity(level_root.single())
|
|
.with_children(|parent| {
|
|
parent.spawn(SceneBundle {
|
|
scene: handle.clone(),
|
|
..default()
|
|
});
|
|
});
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
// TODO: Play all animations
|
|
// TODO: Mark playing animation as active
|
|
use animations::*;
|
|
mod animations {
|
|
use super::*;
|
|
|
|
#[derive(Debug, Component, Default)]
|
|
pub struct AnimationWidget;
|
|
|
|
#[derive(Debug, Component)]
|
|
pub struct AnimationPlayAll;
|
|
|
|
pub fn init_animations_ui(
|
|
events: Query<Entity, Added<AnimationWidget>>,
|
|
mut commands: Commands,
|
|
) {
|
|
events.iter().for_each(|entity| {
|
|
commands.entity(entity).with_children(|parent| {
|
|
parent.spawn((
|
|
AnimationPlayAll,
|
|
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: "Play All".into(),
|
|
..default()
|
|
},
|
|
));
|
|
});
|
|
})
|
|
}
|
|
|
|
pub fn animations_ui(
|
|
mut events: EventReader<CustomAssetEvent<AnimationClip>>,
|
|
mut commands: Commands,
|
|
widget: Query<Entity, With<AnimationWidget>>,
|
|
current: Query<(Entity, &ui::TargetAsset<AnimationClip>)>,
|
|
) {
|
|
events.iter().for_each(|event| {
|
|
match event {
|
|
CustomAssetEvent::Add { name, handle } => {
|
|
info!("Asset loading! {:?}({:?})", name, handle);
|
|
// Spawn new tree
|
|
create_asset_button(
|
|
&widget,
|
|
&mut commands,
|
|
ui::TargetAsset {
|
|
handle: handle.clone(),
|
|
},
|
|
name.clone(),
|
|
None,
|
|
);
|
|
}
|
|
CustomAssetEvent::Remove { handle } => {
|
|
destroy_asset_button(
|
|
¤t,
|
|
&mut commands,
|
|
&ui::TargetAsset {
|
|
handle: handle.clone(),
|
|
},
|
|
);
|
|
}
|
|
CustomAssetEvent::Clear => {
|
|
commands.entity(widget.single()).despawn_descendants();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
pub fn play_all_animations(
|
|
start: Query<Entity, (With<Button>, Added<ui::Active>)>,
|
|
mut stop: RemovedComponents<ui::Active>,
|
|
play_all_btn: Query<Entity, With<AnimationPlayAll>>,
|
|
clip_btns: Query<Entity, With<ui::TargetAsset<AnimationClip>>>,
|
|
mut commands: Commands,
|
|
) {
|
|
stop.iter()
|
|
.filter(|&entity| play_all_btn.contains(entity))
|
|
.for_each(|_| {
|
|
clip_btns.iter().for_each(|entity| {
|
|
commands.entity(entity).remove::<ui::Active>();
|
|
})
|
|
});
|
|
start
|
|
.iter()
|
|
.filter(|&entity| play_all_btn.contains(entity))
|
|
.for_each(|_| {
|
|
clip_btns.iter().for_each(|entity| {
|
|
commands.entity(entity).insert(ui::Active);
|
|
})
|
|
});
|
|
}
|
|
|
|
pub fn play_animation(
|
|
start: Query<Entity, (With<Button>, Added<ui::Active>)>,
|
|
mut stop: RemovedComponents<ui::Active>,
|
|
clip_refs: Query<&ui::TargetAsset<AnimationClip>>,
|
|
mut targets: Query<(&mut AnimationPlayer, &Name), With<Transform>>,
|
|
clips: Res<Assets<AnimationClip>>,
|
|
) {
|
|
stop.iter().for_each(|entity| {
|
|
if let Ok(ui::TargetAsset { handle }) = clip_refs.get(entity) {
|
|
let clip = clips.get(&handle).expect("Load animation clip");
|
|
targets
|
|
.iter_mut()
|
|
.filter(|(_, name)| clip.compatible_with(name))
|
|
.for_each(|(mut player, _)| {
|
|
player.pause();
|
|
})
|
|
}
|
|
});
|
|
start.iter().for_each(|entity| {
|
|
if let Ok(ui::TargetAsset { handle }) = clip_refs.get(entity) {
|
|
let clip = clips.get(&handle).expect("Load animation clip");
|
|
targets
|
|
.iter_mut()
|
|
.filter(|(_, name)| clip.compatible_with(name))
|
|
.for_each(|(mut player, _)| {
|
|
if player.is_paused() {
|
|
player.resume();
|
|
} else {
|
|
player.play(handle.clone()).repeat();
|
|
}
|
|
})
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
use fonts::*;
|
|
mod fonts {
|
|
use super::*;
|
|
|
|
#[derive(Debug, Component, Default)]
|
|
pub struct FontWidget;
|
|
|
|
pub fn fonts_ui(
|
|
mut events: EventReader<AssetEvent<Font>>,
|
|
mut commands: Commands,
|
|
widget: Query<Entity, With<FontWidget>>,
|
|
current: Query<(Entity, &ui::TargetAsset<Font>)>,
|
|
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()),
|
|
Some(handle.clone()),
|
|
);
|
|
}
|
|
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()),
|
|
Some(handle.clone()),
|
|
);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
use monologues::*;
|
|
mod monologues {
|
|
use super::*;
|
|
use bevy::{
|
|
reflect::{TypePath, TypeUuid},
|
|
ui::FocusPolicy,
|
|
};
|
|
use serde::Deserialize;
|
|
|
|
#[derive(Debug, Component, Default)]
|
|
pub struct MonologueWidget;
|
|
|
|
#[derive(Debug, Deserialize, TypeUuid, TypePath, PartialEq)]
|
|
#[uuid = "216a570b-d142-4026-baed-d7feb0250458"]
|
|
pub struct Monologue {
|
|
text: String,
|
|
}
|
|
|
|
#[derive(Debug, Component)]
|
|
pub struct MonologueModal;
|
|
|
|
#[derive(Debug, Component)]
|
|
pub struct MonologueContainer;
|
|
|
|
#[derive(Default)]
|
|
pub struct MonologueLoader;
|
|
|
|
impl AssetLoader for MonologueLoader {
|
|
fn load<'a>(
|
|
&'a self,
|
|
bytes: &'a [u8],
|
|
load_context: &'a mut LoadContext,
|
|
) -> BoxedFuture<'a, Result<(), bevy::asset::Error>> {
|
|
Box::pin(async move {
|
|
let asset = Monologue {
|
|
text: String::from_utf8(bytes.to_vec())?,
|
|
};
|
|
load_context.set_default_asset(LoadedAsset::new(asset));
|
|
Ok(())
|
|
})
|
|
}
|
|
|
|
fn extensions(&self) -> &[&str] {
|
|
&["monologue.txt"]
|
|
}
|
|
}
|
|
|
|
pub fn init_texts_ui(mut commands: Commands) {
|
|
commands.spawn((
|
|
NodeBundle {
|
|
style: Style {
|
|
width: Val::Percent(100.0),
|
|
align_items: AlignItems::Center,
|
|
justify_content: JustifyContent::Center,
|
|
..default()
|
|
},
|
|
focus_policy: FocusPolicy::Pass,
|
|
..default()
|
|
},
|
|
MonologueContainer,
|
|
));
|
|
}
|
|
|
|
// TODO: Load .txt files for monologues
|
|
pub fn texts_ui(
|
|
mut events: EventReader<AssetEvent<Monologue>>,
|
|
mut commands: Commands,
|
|
widget: Query<Entity, With<MonologueWidget>>,
|
|
current: Query<(Entity, &ui::TargetAsset<Monologue>)>,
|
|
server: Res<AssetServer>,
|
|
) {
|
|
events.iter().for_each(|event| match event {
|
|
AssetEvent::Created { handle } => {
|
|
info!("Monologue created! {:?}", event);
|
|
create_asset_button(
|
|
&widget,
|
|
&mut commands,
|
|
ui::TargetAsset {
|
|
handle: handle.clone(),
|
|
},
|
|
get_asset_name(&server, handle.clone()),
|
|
None,
|
|
);
|
|
}
|
|
AssetEvent::Removed { handle } => {
|
|
info!("Monologue removed! {:?}", event);
|
|
destroy_asset_button(
|
|
¤t,
|
|
&mut commands,
|
|
&ui::TargetAsset {
|
|
handle: handle.clone(),
|
|
},
|
|
);
|
|
}
|
|
AssetEvent::Modified { handle } => {
|
|
info!("Monologue 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,
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
// TODO(BUG): Better handle hide/close monologue
|
|
pub fn show_preview_text(
|
|
added: Query<Entity, (With<Button>, Added<ui::Active>)>,
|
|
mut removed: RemovedComponents<ui::Active>,
|
|
monologue_handles: Query<&ui::TargetAsset<Monologue>>,
|
|
monologues: Res<Assets<Monologue>>,
|
|
container: Query<Entity, With<MonologueContainer>>,
|
|
mut commands: Commands,
|
|
) {
|
|
added
|
|
.iter()
|
|
.filter_map(|entity| monologue_handles.get(entity).ok())
|
|
.for_each(|ui::TargetAsset { handle }| {
|
|
let monologue = monologues.get(handle).expect("Preview loaded monologue");
|
|
commands
|
|
.entity(container.single())
|
|
.despawn_descendants()
|
|
.with_children(|parent| {
|
|
parent
|
|
.spawn(NodeBundle {
|
|
style: Style {
|
|
max_width: Val::Percent(50.0),
|
|
padding: UiRect::all(Val::Px(1.0)),
|
|
margin: UiRect::all(Val::Px(1.0)),
|
|
border: UiRect::all(Val::Px(1.0)),
|
|
flex_direction: FlexDirection::Column,
|
|
..default()
|
|
},
|
|
background_color: Color::WHITE.into(),
|
|
border_color: Color::BLACK.into(),
|
|
..default()
|
|
})
|
|
.with_children(|parent| {
|
|
parent.spawn((
|
|
ui::TitleBarBase::new(Color::VIOLET).bundle(),
|
|
ui::Title {
|
|
text: "Monologue".into(),
|
|
..default()
|
|
},
|
|
ui::Close {
|
|
target: parent.parent_entity(),
|
|
},
|
|
ui::Sorting(0),
|
|
));
|
|
parent.spawn((
|
|
TextBundle::from_section(
|
|
monologue.text.clone(),
|
|
TextStyle {
|
|
color: Color::BLACK.into(),
|
|
font_size: 16.0,
|
|
..default()
|
|
},
|
|
),
|
|
handle.clone(),
|
|
));
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
// TODO: Sync Handle<Monologue> and TextStyle components to automagically generate and sync text
|
|
pub fn sync_monologue_font(
|
|
events: Query<&ui::TargetAsset<Font>, Added<ui::Active>>,
|
|
mut texts: Query<&mut Text, With<Handle<Monologue>>>,
|
|
) {
|
|
events.iter().for_each(|ui::TargetAsset { handle }| {
|
|
texts.iter_mut().for_each(|mut text| {
|
|
text.sections.iter_mut().for_each(|section| {
|
|
section.style.font = handle.clone();
|
|
});
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
use cameras::*;
|
|
mod cameras {
|
|
use super::*;
|
|
|
|
#[derive(Debug, Component, Default)]
|
|
pub struct CameraWidget;
|
|
|
|
// TODO: Despawn camera button when camera removed
|
|
pub fn cameras_ui(
|
|
mut added: Query<(Entity, &mut Camera, &Name), (Added<Camera>, Without<EditorCamera>)>,
|
|
mut removed: RemovedComponents<Camera>,
|
|
widget: Query<Entity, With<CameraWidget>>,
|
|
current: Query<(Entity, &ui::TargetEntity)>,
|
|
mut commands: Commands,
|
|
) {
|
|
removed.iter().for_each(|entity| {
|
|
info!("Destroy button for {:?}", entity);
|
|
destroy_entity_button(¤t, &mut commands, &ui::TargetEntity { entity });
|
|
});
|
|
added.iter_mut().for_each(|(entity, mut camera, name)| {
|
|
let empty = current.iter().len() == 0;
|
|
|
|
info!("Camera added {:?} {:?}", entity, name);
|
|
let e = create_entity_button(
|
|
&widget,
|
|
&mut commands,
|
|
ui::TargetEntity { entity },
|
|
name.as_str().into(),
|
|
);
|
|
if empty {
|
|
commands.entity(e).insert(ui::Active);
|
|
} else {
|
|
camera.is_active = false;
|
|
}
|
|
});
|
|
}
|
|
|
|
/// Set the camera active component based on button clicks
|
|
pub fn manage_active_camera(
|
|
events: Query<(&Interaction, &ui::TargetEntity), Changed<Interaction>>,
|
|
cameras: Query<Entity, With<Camera>>,
|
|
mut commands: Commands,
|
|
) {
|
|
events
|
|
.iter()
|
|
.filter(|(&interaction, _)| interaction == Interaction::Pressed)
|
|
.for_each(|(_, ui::TargetEntity { entity })| {
|
|
cameras.iter().for_each(|this_entity| {
|
|
if this_entity == *entity {
|
|
info!("Marking {:?} as active camera", entity);
|
|
commands.entity(this_entity).insert(ui::Active);
|
|
} else {
|
|
info!("Marking {:?} as inactive camera", entity);
|
|
commands.entity(this_entity).remove::<ui::Active>();
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
/// Set the active camera based on the Active marker component
|
|
pub fn control_active_camera(
|
|
added: Query<Entity, (Added<ui::Active>, With<Camera>)>,
|
|
mut removed: RemovedComponents<ui::Active>,
|
|
mut cameras: Query<&mut Camera>,
|
|
) {
|
|
removed.iter().for_each(|entity| {
|
|
info!("Setting {:?} to inactive camera", entity);
|
|
if let Ok(mut camera) = cameras.get_mut(entity) {
|
|
camera.is_active = false;
|
|
}
|
|
});
|
|
added.iter().for_each(|entity| {
|
|
info!("Setting {:?} to active camera", entity);
|
|
if let Ok(mut camera) = cameras.get_mut(entity) {
|
|
camera.is_active = true;
|
|
}
|
|
});
|
|
}
|
|
|
|
// In the event that an active camera is despawned, fall back to the editor camera
|
|
pub fn fallback_camera(
|
|
modified: Query<Entity, (Changed<Camera>, Without<EditorCamera>)>,
|
|
mut removed: RemovedComponents<Camera>,
|
|
other_cameras: Query<&Camera, Without<EditorCamera>>,
|
|
mut editor_camera: Query<&mut Camera, With<EditorCamera>>,
|
|
) {
|
|
// Any time a camera is modified
|
|
modified.iter().chain(removed.iter()).for_each(|_| {
|
|
// If no other cameras are active
|
|
if !other_cameras.iter().any(|camera| camera.is_active) {
|
|
// Make the editor camera active
|
|
editor_camera.single_mut().is_active = true;
|
|
}
|
|
})
|
|
}
|
|
}
|