bevy_aoui_widgets/dsl/
inputbox.rs

1use bevy::hierarchy::BuildChildren;
2use bevy::ecs::entity::Entity;
3use bevy::render::color::Color;
4use bevy::text::Font;
5use bevy::asset::Handle;
6use bevy::window::CursorIcon;
7use bevy_aoui::{bundles::AoUIBundle, Dimension};
8use crate::dsl::prelude::{PropagateFocus, SetCursor};
9use crate::events::EventFlags;
10use crate::widget_extension;
11use crate::widgets::TextColor;
12use crate::widgets::inputbox::{InputBox, InputBoxCursorBar, InputBoxText, InputBoxCursorArea};
13
14widget_extension!(
15    pub struct InputBoxBuilder {
16        pub text: String,
17        pub font: Handle<Font>,
18        pub color: Option<Color>,    
19        pub cursor_bar: Option<Entity>,
20        pub cursor_area: Option<Entity>,
21    },
22    this, commands,
23    components: (
24        InputBox::new(&this.text),
25        TextColor(this.color.expect("color is required.")),
26        EventFlags::DoubleClick|EventFlags::Drag|EventFlags::ClickOutside,
27        this.font,
28    ),
29    spawn: (
30        commands.spawn ((
31            AoUIBundle {
32                dimension: Dimension::INHERIT,
33                ..Default::default()
34            },
35            InputBoxText,
36        )).id(),
37        this.cursor_bar.expect("cursor_bar is required.") => InputBoxCursorBar,
38        this.cursor_area.expect("cursor_area is required.") => InputBoxCursorArea,
39    )
40);
41
42/// Construct a textbox.
43#[macro_export]
44macro_rules! inputbox {
45    {$commands: tt {$($tt:tt)*}} => 
46        {$crate::meta_dsl!($commands [$crate::dsl::builders::InputBoxBuilder] {$($tt)*})};
47}
48
49widget_extension!(
50    pub struct ButtonBuilder {
51        pub cursor: Option<CursorIcon>,
52    },
53    this, commands,
54    components: (
55        EventFlags::Click|EventFlags::Hover,
56        PropagateFocus,
57        SetCursor {
58            flags: EventFlags::Hover|EventFlags::Pressed,
59            icon: CursorIcon::Hand,
60        },
61    ),
62    pattern: (
63        Some(cursor) = this.cursor => SetCursor {
64            flags: EventFlags::Hover|EventFlags::Pressed,
65            icon: cursor,
66        },
67    ),
68);
69
70/// Construct a button.
71/// 
72/// This doesn't do a whole lot by itself, these are what `button` does:
73/// 
74/// * Add a event listener for `Hover` and `Click`
75/// * If `cursor` is set, change cutsor icon when hovering or pressing.
76/// * Propagate its status `Down`, `Click`, `Hover`, `Pressed` to its direct children.
77/// 
78/// You can use the `extra: handler!(Click => fn name() {..})` pattern to handle clicks
79/// and use [`DisplayIf`](crate::widgets::DisplayIf) for simple UI interaction.
80#[macro_export]
81macro_rules! button {
82    {$commands: tt {$($tt:tt)*}} => 
83        {$crate::meta_dsl!($commands [$crate::dsl::builders::ButtonBuilder] {$($tt)*})};
84}