Fix parsing problem in .mono files

main
Elijah Voigt 4 months ago
parent e385a354ff
commit beffe54ebc

@ -53,6 +53,7 @@ fn main() {
.add_observer(add_dialog_line) .add_observer(add_dialog_line)
.add_observer(add_tree_monologue) .add_observer(add_tree_monologue)
.add_observer(remove_tree_monologue) .add_observer(remove_tree_monologue)
.add_observer(hide_monologue_preview)
.run(); .run();
} }
@ -162,6 +163,9 @@ fn init_ui(mut commands: Commands) {
.observe(hover_dialog_box_out); .observe(hover_dialog_box_out);
} }
#[derive(Component)]
struct MonologuesContainer;
#[derive(Component)] #[derive(Component)]
struct MonologuesList; struct MonologuesList;
@ -178,20 +182,21 @@ fn init_debug_ui(mut commands: Commands) {
height: Val::Percent(90.0), height: Val::Percent(90.0),
align_self: AlignSelf::Center, align_self: AlignSelf::Center,
justify_self: JustifySelf::Start, justify_self: JustifySelf::Start,
width: Val::Percent(60.0),
..default() ..default()
}, },
MonologuesContainer,
GlobalZIndex(i32::MAX - 1), GlobalZIndex(i32::MAX - 1),
DebuggingState::On, DebuggingState::On,
)) ))
.with_children(|parent| { .with_children(|parent| {
parent.spawn(( parent.spawn((
Node { Node {
min_width: Val::Px(5.0),
min_height: Val::Px(5.0), min_height: Val::Px(5.0),
max_height: Val::Percent(90.0), max_height: Val::Percent(90.0),
max_width: Val::Percent(40.0),
flex_direction: FlexDirection::Column, flex_direction: FlexDirection::Column,
padding: UiRect::all(Val::Px(10.0)), padding: UiRect::all(Val::Px(10.0)),
margin: UiRect::all(Val::Px(10.0)),
..default() ..default()
}, },
BackgroundColor(PINK.into()), BackgroundColor(PINK.into()),
@ -201,9 +206,9 @@ fn init_debug_ui(mut commands: Commands) {
Node { Node {
min_height: Val::Px(5.0), min_height: Val::Px(5.0),
max_height: Val::Percent(90.0), max_height: Val::Percent(90.0),
max_width: Val::Percent(60.0),
flex_direction: FlexDirection::Column, flex_direction: FlexDirection::Column,
padding: UiRect::all(Val::Px(10.0)), padding: UiRect::all(Val::Px(10.0)),
margin: UiRect::all(Val::Px(10.0)),
..default() ..default()
}, },
BackgroundColor(ORANGE.into()), BackgroundColor(ORANGE.into()),
@ -377,7 +382,11 @@ fn dialog_engine(
if let Some(options) = monologue.get(*idx) { if let Some(options) = monologue.get(*idx) {
// Spawn the dialog options in the dialog box // Spawn the dialog options in the dialog box
options.iter().for_each(|option| { options.iter().for_each(|option| {
parent.spawn((Text::new(option.clone()), DialogOption)); parent.spawn((
Text::new(option.clone()),
DialogOption,
TextLayout::new(JustifyText::Left, LineBreak::NoWrap),
));
}); });
*idx += 1; *idx += 1;
} else { } else {
@ -617,6 +626,7 @@ fn spawn_debug_buttons(
.spawn(( .spawn((
Button, Button,
Text::new(handle.path().unwrap().to_string()), Text::new(handle.path().unwrap().to_string()),
TextLayout::new(JustifyText::Left, LineBreak::WordBoundary),
TreeMonologue(handle.clone()), TreeMonologue(handle.clone()),
MonologuesList, MonologuesList,
BackgroundColor(PINK.into()), BackgroundColor(PINK.into()),
@ -665,13 +675,14 @@ fn preview_monologue(
let mut i = 0; let mut i = 0;
commands.entity(*container).with_children(|parent| { commands.entity(*container).with_children(|parent| {
while let Some(options) = monologue.get(i) { while let Some(options) = monologue.get(i) {
parent.spawn((Text::new(format!("{i}. ---")), MonologuePreview)); parent.spawn((Text::new("---"), MonologuePreview));
for (n, item) in options.iter().enumerate() { for (n, item) in options.iter().enumerate() {
parent.spawn((Text::new(format!("{i}.{n}. {item}")), MonologuePreview)); parent.spawn((Text::new(format!("{i}.{n}: {item}")), MonologuePreview));
} }
// TODO: Just implement iter_batches or something // TODO: Just implement iter_batches or something
i += 1; i += 1;
} }
parent.spawn((Text::new("---"), MonologuePreview));
}); });
} }
} }
@ -681,3 +692,21 @@ fn spawn_monologue_tree(trigger: Trigger<Pointer<Click>>, mut commands: Commands
// todo!("Spawn monologue tree when button is clicked") // todo!("Spawn monologue tree when button is clicked")
} }
fn hide_monologue_preview(
trigger: Trigger<Pointer<Over>>,
preview: Single<Entity, (With<MonologuePreview>, Without<Button>, Without<Text>)>,
query: Query<
Entity,
Or<(
With<MonologuePreview>,
With<MonologuesList>,
With<MonologuesContainer>,
With<Window>,
)>,
>,
mut commands: Commands,
) {
if !query.contains(trigger.target()) {
commands.entity(*preview).despawn_related::<Children>();
}
}

@ -43,23 +43,20 @@ impl AssetLoader for MonologueLoader {
let raw_string = String::from_utf8(bytes)?; let raw_string = String::from_utf8(bytes)?;
let value: Vec<Vec<String>> = raw_string let mut value: Vec<Vec<String>> = vec![vec![]];
// First split on the '---' separators between batches
.split_terminator("---") for line in raw_string.lines() {
.map(|batch| { if line.starts_with("---") {
batch value.push(Vec::new());
// Then split batches into newline-separated groups of text } else if line.starts_with("#") || line.is_empty() {
.split_terminator("\n\n") // Skip comments and blank lines
.filter_map(|line| { } else {
// Filter out comments, empty lines, and extraneous newlines value.last_mut().unwrap().push(line.into());
(!line.starts_with("#") && !line.is_empty() && line != "\n") }
// Trim the resulting dialog option }
.then_some(line.trim().into())
}) // Clear empty batches
.collect() value.retain(|batch| !batch.is_empty());
})
.filter(|sub: &Vec<String>| !sub.is_empty())
.collect();
debug!("Monologue: {:#?}", value); debug!("Monologue: {:#?}", value);

Loading…
Cancel
Save