diff --git a/examples/2d/2d_shapes.rs b/examples/2d/2d_shapes.rs index 4fc2498889e0c..e2313539c1620 100644 --- a/examples/2d/2d_shapes.rs +++ b/examples/2d/2d_shapes.rs @@ -19,8 +19,8 @@ use bevy::{ feathers::{controls::FeathersCheckbox, theme::UiTheme, FeathersPlugins}, ui_widgets::{checkbox_self_update, ValueChange}, }; -use checkbox::feathers_option_checkbox; -use scene::{bottom_left_scene, top_left_scene}; +use checkbox::{feathers_option_checkbox, IsChecked}; +use scene::top_left_scene; #[path = "../helpers/checkbox.rs"] mod checkbox; @@ -29,6 +29,7 @@ mod checkbox; mod theme; #[path = "../helpers/scene.rs"] +#[expect(dead_code, reason = "not all scenes used")] mod scene; /// Various settings for the demo. @@ -166,24 +167,15 @@ fn setup( spawn_buttons(&mut commands); } -/// Spawns the checkboxes in the bottom left corner of the screen. +/// Spawns the checkboxes in the top left corner of the screen. fn spawn_buttons(commands: &mut Commands) { - if !cfg!(target_arch = "wasm32") { - commands.spawn_scene(bsn! { - @bottom_left_scene() - Children [ - @feathers_option_checkbox("ROTATE", Some(CheckboxInput::Rotation)), - @feathers_option_checkbox("WIREFRAME", Some(CheckboxInput::Wireframe)), - ] - }); - } else { - commands.spawn_scene(bsn! { - @top_left_scene() // so the user can immediately see the control in browser w/o scrolling - Children [ - @feathers_option_checkbox("ROTATE", Some(CheckboxInput::Rotation)), - ] - }); - } + commands.spawn_scene(bsn! { + top_left_scene() + Children [ + feathers_option_checkbox("ROTATE", Some(CheckboxInput::Rotation), IsChecked(false)), + {(!cfg!(target_arch = "wasm32")).then(|| { bsn! { feathers_option_checkbox("WIREFRAME", Some(CheckboxInput::Wireframe), IsChecked(false)) }})} + ] + }); } fn handle_value_change_checkbox( diff --git a/examples/helpers/checkbox.rs b/examples/helpers/checkbox.rs index e3c507993aa2d..65e052c102808 100644 --- a/examples/helpers/checkbox.rs +++ b/examples/helpers/checkbox.rs @@ -4,10 +4,23 @@ use bevy::{ feathers::{controls::FeathersCheckbox, display::caption}, picking::hover::Hovered, prelude::*, + ui::Checked, ui_widgets::checkbox_self_update, }; -/// Creates a single feathers checkbox that allows configuration of a setting. +/// A newtype bool wrapper to indicate a widget's checked status. +pub struct IsChecked(pub bool); + +impl IsChecked { + fn checked(&self) -> bool { + match self { + IsChecked(true) => true, + IsChecked(false) => false, + } + } +} + +/// Creates a single feathers checkbox that allows configuration of a setting. /// /// Examples that use this to create a checkbox should handle its `ValueChange` events. /// If there is a need to identify the checkbox that originated the value change, @@ -16,38 +29,28 @@ use bevy::{ pub fn feathers_option_checkbox( option_name: &str, checkbox_identifier: Option, + status: IsChecked, ) -> Box where T: Template + Clone + Default + Send + Sync + Unpin + 'static, { - if let Some(identifier) = checkbox_identifier { - Box::new(bsn! { - Node { - align_items: AlignItems::Center, - column_gap: px(5), - } - Children [ - @FeathersCheckbox { - @caption: bsn! { @caption(option_name) } - } - Hovered - identifier - on(checkbox_self_update) - ] - }) - } else { - Box::new(bsn! { - Node { - align_items: AlignItems::Center, - column_gap: px(5), + Box::new(bsn! { + Node { + align_items: AlignItems::Center, + column_gap: px(5), + } + Children [ + @FeathersCheckbox { + @caption: bsn! { caption(option_name) } } - Children [ - @FeathersCheckbox { - @caption: bsn! { @caption(option_name) } - } - Hovered - on(checkbox_self_update) - ] - }) - } + Hovered::default() + { + checkbox_identifier.map(|checkbox_identifier| { + bsn! { template_value(checkbox_identifier) } + }) + } + on(checkbox_self_update) + {status.checked().then(|| bsn! { Checked })} + ] + }) }