Skip to main content

2d_shapes/helpers/
checkbox.rs

1/// Helpers to create an option menu using Feathers Checkboxes.
2/// Using these helpers requires the `bevy_feathers` feature to be enabled.
3use bevy::{
4    feathers::{controls::FeathersCheckbox, display::caption},
5    picking::hover::Hovered,
6    prelude::*,
7    ui_widgets::checkbox_self_update,
8};
9
10/// Creates a single feathers checkbox that allows configuration of a setting.
11///
12/// Examples that use this to create a checkbox should handle its `ValueChange<bool>` events.
13/// If there is a need to identify the checkbox that originated the value change,
14/// query which `checkbox_identifier` with the `FeathersCheckbox` is the value change's
15/// source entity.
16pub fn feathers_option_checkbox<T>(
17    option_name: &str,
18    checkbox_identifier: Option<T>,
19) -> Box<dyn Scene>
20where
21    T: Template<Output: Component> + Clone + Default + Send + Sync + Unpin + 'static,
22{
23    if let Some(identifier) = checkbox_identifier {
24        Box::new(bsn! {
25            Node {
26                align_items: AlignItems::Center,
27                column_gap: px(5),
28            }
29            Children [
30                @FeathersCheckbox {
31                    @caption: bsn! { @caption(option_name) }
32                }
33                Hovered
34                identifier
35                on(checkbox_self_update)
36            ]
37        })
38    } else {
39        Box::new(bsn! {
40            Node {
41                align_items: AlignItems::Center,
42                column_gap: px(5),
43            }
44            Children [
45                @FeathersCheckbox {
46                    @caption: bsn! { @caption(option_name) }
47                }
48                Hovered
49                on(checkbox_self_update)
50            ]
51        })
52    }
53}