Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions examples/2d/2d_shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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(
Expand Down
63 changes: 33 additions & 30 deletions examples/helpers/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>` events.
/// If there is a need to identify the checkbox that originated the value change,
Expand All @@ -16,38 +29,28 @@ use bevy::{
pub fn feathers_option_checkbox<T>(
option_name: &str,
checkbox_identifier: Option<T>,
status: IsChecked,
) -> Box<dyn Scene>
where
T: Template<Output: Component> + 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 })}
Comment thread
tevans-3 marked this conversation as resolved.
]
})
}
Loading