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_tree_monologue)
.add_observer(remove_tree_monologue)
.add_observer(hide_monologue_preview)
.run();
}
@ -162,6 +163,9 @@ fn init_ui(mut commands: Commands) {
.observe(hover_dialog_box_out);
}
#[derive(Component)]
struct MonologuesContainer;
#[derive(Component)]
struct MonologuesList;
@ -178,20 +182,21 @@ fn init_debug_ui(mut commands: Commands) {
height: Val::Percent(90.0),
align_self: AlignSelf::Center,
justify_self: JustifySelf::Start,
width: Val::Percent(60.0),
..default()
},
MonologuesContainer,
GlobalZIndex(i32::MAX - 1),
DebuggingState::On,
))
.with_children(|parent| {
parent.spawn((
Node {
min_width: Val::Px(5.0),
min_height: Val::Px(5.0),
max_height: Val::Percent(90.0),
max_width: Val::Percent(40.0),
flex_direction: FlexDirection::Column,
padding: UiRect::all(Val::Px(10.0)),
margin: UiRect::all(Val::Px(10.0)),
..default()
},
BackgroundColor(PINK.into()),
@ -201,9 +206,9 @@ fn init_debug_ui(mut commands: Commands) {
Node {
min_height: Val::Px(5.0),
max_height: Val::Percent(90.0),
max_width: Val::Percent(60.0),
flex_direction: FlexDirection::Column,
padding: UiRect::all(Val::Px(10.0)),
margin: UiRect::all(Val::Px(10.0)),
..default()
},
BackgroundColor(ORANGE.into()),
@ -377,7 +382,11 @@ fn dialog_engine(
if let Some(options) = monologue.get(*idx) {
// Spawn the dialog options in the dialog box
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;
} else {
@ -617,6 +626,7 @@ fn spawn_debug_buttons(
.spawn((
Button,
Text::new(handle.path().unwrap().to_string()),
TextLayout::new(JustifyText::Left, LineBreak::WordBoundary),
TreeMonologue(handle.clone()),
MonologuesList,
BackgroundColor(PINK.into()),
@ -665,13 +675,14 @@ fn preview_monologue(
let mut i = 0;
commands.entity(*container).with_children(|parent| {
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() {
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
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")
}
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 value: Vec<Vec<String>> = raw_string
// First split on the '---' separators between batches
.split_terminator("---")
.map(|batch| {
batch
// Then split batches into newline-separated groups of text
.split_terminator("\n\n")
.filter_map(|line| {
// Filter out comments, empty lines, and extraneous newlines
(!line.starts_with("#") && !line.is_empty() && line != "\n")
// Trim the resulting dialog option
.then_some(line.trim().into())
})
.collect()
})
.filter(|sub: &Vec<String>| !sub.is_empty())
.collect();
let mut value: Vec<Vec<String>> = vec![vec![]];
for line in raw_string.lines() {
if line.starts_with("---") {
value.push(Vec::new());
} else if line.starts_with("#") || line.is_empty() {
// Skip comments and blank lines
} else {
value.last_mut().unwrap().push(line.into());
}
}
// Clear empty batches
value.retain(|batch| !batch.is_empty());
debug!("Monologue: {:#?}", value);

Loading…
Cancel
Save