Skip to main content

animation_graph/helpers/
number_input_f32.rs

1/// Helpers to create a basic f32 number input using `FeathersNumberInput`
2/// Using these helpers requires the `bevy_feathers` feature to be enabled.
3use bevy::{
4    feathers::{
5        controls::{FeathersNumberInput, HardLimit, NumberInputPrecision, NumberInputValue},
6        display::label,
7    },
8    prelude::*,
9};
10
11/// Creates an f32 number input.
12///
13/// `number_input_identifier` should be a component that will distinguish this
14/// number input from any others if needed.
15///
16/// Examples that use this to create a number input should handle its `ValueChange<f32>` events.
17/// If there is a need to identify the number input that originated the value change,
18/// query which `number_input_identifier` with the `FeathersNumberInput` is
19/// the value change's source entity.
20pub fn number_input_f32<T>(
21    name: &'static str,
22    number_input_identifier: Option<T>,
23    value: f32,
24    precision: NumberInputPrecision,
25    limits: core::ops::RangeInclusive<f32>,
26) -> Box<dyn Scene>
27where
28    T: Template<Output: Component> + Send + Sync + Unpin + 'static,
29{
30    if let Some(identifier) = number_input_identifier {
31        Box::new(bsn! {
32            Node {
33                align_items: AlignItems::Center,
34                column_gap: px(5),
35            }
36            Children [
37                Node {
38                    align_items: AlignItems::Center,
39                    width: px(150),
40                }
41                Children [
42                    @label(name)
43                ]
44                --
45                identifier
46                @FeathersNumberInput
47                NumberInputValue::F32(value)
48                precision
49                HardLimit::f32(limits)
50                Node {
51                    align_items: AlignItems::Center,
52                    flex_grow: 1.0,
53                    min_width: px(50),
54                }
55            ]
56        })
57    } else {
58        Box::new(bsn! {
59            Node {
60                align_items: AlignItems::Center,
61                column_gap: px(5),
62            }
63            Children [
64                Node {
65                    align_items: AlignItems::Center,
66                    width: px(150),
67                }
68                Children [
69                    @label(name)
70                ]
71                --
72                @FeathersNumberInput
73                NumberInputValue::F32(value)
74                precision
75                HardLimit::f32(limits)
76                Node {
77                    align_items: AlignItems::Center,
78                    flex_grow: 1.0,
79                    min_width: px(50),
80                }
81            ]
82        })
83    }
84}