Implemented text preview, sans font

main
Elijah Voigt 2 years ago
parent fa7b52e8ce
commit d887f49c7b

@ -0,0 +1 @@
This is a placeholder monologue

@ -0,0 +1 @@
This is a placeholder monologue

@ -61,7 +61,7 @@ fn main() {
.init_asset_loader::<MonologueLoader>() .init_asset_loader::<MonologueLoader>()
.add_event::<CustomAssetEvent<Scene>>() .add_event::<CustomAssetEvent<Scene>>()
.add_event::<CustomAssetEvent<AnimationClip>>() .add_event::<CustomAssetEvent<AnimationClip>>()
.add_systems(Startup, (initialize_ui, welcome_message)) .add_systems(Startup, (initialize_ui, init_texts_ui, welcome_message))
.add_systems( .add_systems(
Update, Update,
( (
@ -80,6 +80,7 @@ fn main() {
manage_camera, manage_camera,
play_animation, play_animation,
play_audio, play_audio,
show_preview_text,
), ),
) )
.run(); .run();
@ -184,6 +185,11 @@ fn initialize_ui(mut commands: Commands) {
parent, parent,
ui::Select::Single, ui::Select::Single,
)); ));
content_containers.push(spawn_tab_container::<MonologueWidget>(
"Monologue",
parent,
ui::Select::Single,
));
content_containers.push(spawn_tab_container::<AudioWidget>( content_containers.push(spawn_tab_container::<AudioWidget>(
"Audio", "Audio",
parent, parent,
@ -265,7 +271,7 @@ fn initialize_ui(mut commands: Commands) {
justify_content: JustifyContent::SpaceBetween, justify_content: JustifyContent::SpaceBetween,
..default() ..default()
}, },
background_color: Color::ALICE_BLUE.into(), background_color: Color::WHITE.into(),
border_color: Color::BLACK.into(), border_color: Color::BLACK.into(),
..default() ..default()
}, },
@ -365,74 +371,65 @@ mod audio {
current: Query<(Entity, &ui::TargetAsset<AudioSource>)>, current: Query<(Entity, &ui::TargetAsset<AudioSource>)>,
server: Res<AssetServer>, server: Res<AssetServer>,
) { ) {
events events.iter().for_each(|event| match event {
.iter() AssetEvent::Created { handle } => {
.filter(|&event| match event { info!("Asset created! {:?}", event);
AssetEvent::Created { handle } let id = create_asset_button(
| AssetEvent::Removed { handle } &widget,
| AssetEvent::Modified { handle } => { &mut commands,
has_extensions(&server, handle.clone(), &["ogg"]) ui::TargetAsset {
} handle: handle.clone(),
}) },
.for_each(|event| match event { get_asset_name(&server, handle.clone()),
AssetEvent::Created { handle } => { None,
info!("Asset created! {:?}", event); );
let id = create_asset_button( commands.entity(id).insert(AudioSourceBundle {
&widget, source: handle.clone(),
&mut commands, settings: PlaybackSettings {
ui::TargetAsset { mode: PlaybackMode::Loop,
handle: handle.clone(), paused: true,
}, ..default()
get_asset_name(&server, handle.clone()), },
None, });
); }
commands.entity(id).insert(AudioSourceBundle { AssetEvent::Removed { handle } => {
source: handle.clone(), info!("Asset removed! {:?}", event);
settings: PlaybackSettings { destroy_asset_button(
mode: PlaybackMode::Loop, &current,
paused: true, &mut commands,
..default() &ui::TargetAsset {
}, handle: handle.clone(),
}); },
} );
AssetEvent::Removed { handle } => { }
info!("Asset removed! {:?}", event); AssetEvent::Modified { handle } => {
destroy_asset_button( info!("Asset modified! {:?}", event);
&current, destroy_asset_button(
&mut commands, &current,
&ui::TargetAsset { &mut commands,
handle: handle.clone(), &ui::TargetAsset {
}, handle: handle.clone(),
); },
} );
AssetEvent::Modified { handle } => { let id = create_asset_button(
info!("Asset modified! {:?}", event); &widget,
destroy_asset_button( &mut commands,
&current, ui::TargetAsset {
&mut commands, handle: handle.clone(),
&ui::TargetAsset { },
handle: handle.clone(), get_asset_name(&server, handle.clone()),
}, None,
); );
let id = create_asset_button( commands.entity(id).insert(AudioSourceBundle {
&widget, source: handle.clone(),
&mut commands, settings: PlaybackSettings {
ui::TargetAsset { mode: PlaybackMode::Loop,
handle: handle.clone(), paused: true,
}, ..default()
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( pub fn play_audio(
@ -579,58 +576,49 @@ mod gltf {
current: Query<(Entity, &ui::TargetAsset<Gltf>)>, current: Query<(Entity, &ui::TargetAsset<Gltf>)>,
server: Res<AssetServer>, server: Res<AssetServer>,
) { ) {
events events.iter().for_each(|event| match event {
.iter() AssetEvent::Created { handle } => {
.filter(|&event| match event { info!("Asset created! {:?}", event);
AssetEvent::Created { handle } create_asset_button(
| AssetEvent::Removed { handle } &widget,
| AssetEvent::Modified { handle } => { &mut commands,
has_extensions(&server, handle.clone(), &["gltf", "glb"]) ui::TargetAsset {
} handle: handle.clone(),
}) },
.for_each(|event| match event { get_asset_name(&server, handle.clone()),
AssetEvent::Created { handle } => { None,
info!("Asset created! {:?}", event); );
create_asset_button( }
&widget, AssetEvent::Removed { handle } => {
&mut commands, info!("Asset removed! {:?}", event);
ui::TargetAsset { destroy_asset_button(
handle: handle.clone(), &current,
}, &mut commands,
get_asset_name(&server, handle.clone()), &ui::TargetAsset {
None, handle: handle.clone(),
); },
} );
AssetEvent::Removed { handle } => { }
info!("Asset removed! {:?}", event); AssetEvent::Modified { handle } => {
destroy_asset_button( info!("Asset modified! {:?}", event);
&current, destroy_asset_button(
&mut commands, &current,
&ui::TargetAsset { &mut commands,
handle: handle.clone(), &ui::TargetAsset {
}, handle: handle.clone(),
); },
} );
AssetEvent::Modified { handle } => { create_asset_button(
info!("Asset modified! {:?}", event); &widget,
destroy_asset_button( &mut commands,
&current, ui::TargetAsset {
&mut commands, handle: handle.clone(),
&ui::TargetAsset { },
handle: handle.clone(), get_asset_name(&server, handle.clone()),
}, None,
); );
create_asset_button( }
&widget, });
&mut commands,
ui::TargetAsset {
handle: handle.clone(),
},
get_asset_name(&server, handle.clone()),
None,
);
}
});
} }
pub fn manage_active_gltf( pub fn manage_active_gltf(
@ -902,65 +890,59 @@ mod fonts {
current: Query<(Entity, &ui::TargetAsset<Font>)>, current: Query<(Entity, &ui::TargetAsset<Font>)>,
server: Res<AssetServer>, server: Res<AssetServer>,
) { ) {
events events.iter().for_each(|event| match event {
.iter() AssetEvent::Created { handle } => {
.filter(|&event| match event { info!("Asset created! {:?}", event);
AssetEvent::Created { handle } create_asset_button(
| AssetEvent::Removed { handle } &widget,
| AssetEvent::Modified { handle } => { &mut commands,
has_extensions(&server, handle.clone(), &["ttf", "otf"]) ui::TargetAsset {
} handle: handle.clone(),
}) },
.for_each(|event| match event { get_asset_name(&server, handle.clone()),
AssetEvent::Created { handle } => { Some(handle.clone()),
info!("Asset created! {:?}", event); );
create_asset_button( }
&widget, AssetEvent::Removed { handle } => {
&mut commands, info!("Asset removed! {:?}", event);
ui::TargetAsset { destroy_asset_button(
handle: handle.clone(), &current,
}, &mut commands,
get_asset_name(&server, handle.clone()), &ui::TargetAsset {
Some(handle.clone()), handle: handle.clone(),
); },
} );
AssetEvent::Removed { handle } => { }
info!("Asset removed! {:?}", event); AssetEvent::Modified { handle } => {
destroy_asset_button( info!("Asset modified! {:?}", event);
&current, destroy_asset_button(
&mut commands, &current,
&ui::TargetAsset { &mut commands,
handle: handle.clone(), &ui::TargetAsset {
}, handle: handle.clone(),
); },
} );
AssetEvent::Modified { handle } => { create_asset_button(
info!("Asset modified! {:?}", event); &widget,
destroy_asset_button( &mut commands,
&current, ui::TargetAsset {
&mut commands, handle: handle.clone(),
&ui::TargetAsset { },
handle: handle.clone(), get_asset_name(&server, handle.clone()),
}, Some(handle.clone()),
); );
create_asset_button( }
&widget, });
&mut commands,
ui::TargetAsset {
handle: handle.clone(),
},
get_asset_name(&server, handle.clone()),
Some(handle.clone()),
);
}
});
} }
} }
use monologues::*; use monologues::*;
mod monologues { mod monologues {
use super::*; use super::*;
use bevy::reflect::{TypePath, TypeUuid}; use bevy::{
reflect::{TypePath, TypeUuid},
ui::FocusPolicy,
};
use serde::Deserialize; use serde::Deserialize;
#[derive(Debug, Component, Default)] #[derive(Debug, Component, Default)]
@ -972,6 +954,12 @@ mod monologues {
text: String, text: String,
} }
#[derive(Debug, Component)]
pub struct MonologueModal;
#[derive(Debug, Component)]
pub struct MonologueContainer;
#[derive(Default)] #[derive(Default)]
pub struct MonologueLoader; pub struct MonologueLoader;
@ -982,29 +970,161 @@ mod monologues {
load_context: &'a mut LoadContext, load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, Result<(), bevy::asset::Error>> { ) -> BoxedFuture<'a, Result<(), bevy::asset::Error>> {
Box::pin(async move { Box::pin(async move {
load_context.set_default_asset(LoadedAsset::new( let asset = Monologue {
String::from_utf8(bytes.to_vec()).expect("Convert bytes to String"), text: String::from_utf8(bytes.to_vec())?,
)); };
load_context.set_default_asset(LoadedAsset::new(asset));
Ok(()) Ok(())
}) })
} }
fn extensions(&self) -> &[&str] { fn extensions(&self) -> &[&str] {
&[".monologue.txt"] &["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 // TODO: Load .txt files for monologues
pub fn texts_ui( pub fn texts_ui(
mut events: EventReader<AssetEvent<Monologue>>, mut events: EventReader<AssetEvent<Monologue>>,
mut _commands: Commands, mut commands: Commands,
_widget: Query<Entity, With<MonologueWidget>>, widget: Query<Entity, With<MonologueWidget>>,
_current: Query<(Entity, &ui::TargetAsset<Monologue>)>, current: Query<(Entity, &ui::TargetAsset<Monologue>)>,
_server: Res<AssetServer>, server: Res<AssetServer>,
) { ) {
events.iter().for_each(|_event| { events.iter().for_each(|event| match event {
info!("Loading monologue"); 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(
&current,
&mut commands,
&ui::TargetAsset {
handle: handle.clone(),
},
);
}
AssetEvent::Modified { handle } => {
info!("Monologue modified! {:?}", event);
destroy_asset_button(
&current,
&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(
show: 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,
) {
removed
.iter()
.filter_map(|entity| monologue_handles.get(entity).ok())
.for_each(|_| {
commands.entity(container.single()).despawn_descendants();
});
show.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((
NodeBundle {
style: Style {
padding: UiRect::all(Val::Px(1.0)),
margin: UiRect::all(Val::Px(1.0)),
border: UiRect::all(Val::Px(1.0)),
flex_direction: FlexDirection::Row,
align_items: AlignItems::Center,
justify_content: JustifyContent::SpaceBetween,
..default()
},
background_color: Color::VIOLET.into(),
border_color: Color::BLACK.into(),
..default()
},
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(),
..default()
},
));
});
});
})
} }
} }

Loading…
Cancel
Save