diff --git a/src/bin/trees/main.rs b/src/bin/trees/main.rs index 8ffad02..e5689de 100644 --- a/src/bin/trees/main.rs +++ b/src/bin/trees/main.rs @@ -1,5 +1,6 @@ #![allow(clippy::type_complexity)] +use bevy::color::palettes::css::{DARK_GREY, DARK_ORANGE, GREY, ORANGE}; use games::*; fn main() { @@ -15,9 +16,11 @@ fn main() { ( dialog_engine.run_if(input_just_pressed(KeyCode::KeyN)), mouse_wheel_scroll.run_if(on_event::), - auto_scroll.run_if(any_component_added::) + auto_scroll.run_if(any_component_added::), ), ) + .add_observer(add_dialog_option) + .add_observer(add_dialog_line) .run(); } @@ -132,6 +135,37 @@ fn position_camera(mut query: Query<&mut Transform, (With, With, + mut scroll_position: Query<&mut ScrollPosition>, +) { + events.read().for_each(|MouseWheel { unit, y, .. }| { + let offset_y = match unit { + MouseScrollUnit::Line => 10.0 * y, + MouseScrollUnit::Pixel => 1.0 * y, + }; + scroll_position + .iter_mut() + .for_each(|mut pos| pos.offset_y += offset_y); + }); +} + +/// Automatically scrolls dialog when a new batch of options are added +fn auto_scroll( + added: Query>, + mut scroll_positions: Query<&mut ScrollPosition>, +) { + debug_assert!( + !added.is_empty(), + "Should only scroll when dialog options are added" + ); + + scroll_positions.iter_mut().for_each(|mut sp| { + sp.offset_y = f32::MAX; + }); +} + /// A possible line of dialog the user can choose #[derive(Component)] struct DialogOption; @@ -159,9 +193,7 @@ fn dialog_engine( commands.entity(*dialog_box).with_children(|parent| { if let Some(options) = dialog.get(*idx) { options.iter().for_each(|option| { - parent - .spawn((Text::new(*option), Button, DialogOption)) - .observe(choose_dialog); + parent.spawn((Text::new(*option), DialogOption)); }); } }); @@ -170,42 +202,68 @@ fn dialog_engine( } /// When a dialog option is chosen (clicked on) we do the following: -/// 1. Remove the Button and DialogOption components from that entity -/// 2. Insert the DialogLine component to that entity -/// 3. Remove all other DialogOptions from the DialogBox -fn choose_dialog(trigger: Trigger>, mut commands: Commands) { +fn choose_dialog_option( + trigger: Trigger>, + mut commands: Commands, + texts: Query<&Text>, + options: Query>, + dialog_box: Single>, +) { info!("Choosing dialog {:?}", trigger.target()); - commands - .entity(trigger.target()) - .remove::