1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use bevy::ecs::component::Component;
use bevy::ui::{
AlignItems, AlignSelf, BackgroundColor, BorderColor, FlexDirection, JustifyContent,
JustifySelf, Node, px,
};
use bevy::utils::default;
use bevy_immediate::{
Imm,
attach::{BevyImmediateAttachPlugin, ImmediateAttach},
ui::{
CapsUi,
activated::ImmUiActivated,
anchored::ImmUiAnchored,
anchored_ui_plugin::{Anchor, AnchorOption, Direction},
floating_ui_focus_plugin::FocusCloseCurrentTree,
interaction::ImmUiInteraction,
selected::ImmUiSelected,
text::ImmUiText,
},
utils::ImmLocalHashMemoryHelper,
};
use crate::styles::{compact_button_bundle, compact_node_container, row_node_container};
pub struct AnchoredUiExamplePlugin;
impl bevy::app::Plugin for AnchoredUiExamplePlugin {
fn build(&self, app: &mut bevy::app::App) {
app.add_plugins(BevyImmediateAttachPlugin::<CapsUi, AnchoredUiExampleRoot>::new());
}
}
#[derive(Component)]
pub struct AnchoredUiExampleRoot;
impl ImmediateAttach<CapsUi> for AnchoredUiExampleRoot {
type Params = ();
fn construct(ui: &mut Imm<CapsUi>, _: &mut ()) {
ui.ch()
.on_spawn_insert(|| Node {
flex_direction: FlexDirection::Column,
flex_grow: 1.,
align_self: AlignSelf::Center,
justify_self: JustifySelf::Center,
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
})
.add(|ui| {
ui.ch().on_spawn_text("Dropdown");
ui.ch().on_spawn_insert(row_node_container).add(|ui| {
for (x, y, tx, ty, dir_text) in [
(
Anchor::Start,
Anchor::End,
Anchor::Start,
Anchor::Start,
"Up",
),
(
Anchor::Start,
Anchor::Start,
Anchor::Start,
Anchor::End,
"Down",
),
] {
let mut button = ui
.ch_id((x, y, tx, ty))
.on_spawn_insert(compact_button_bundle)
.add(|ui| {
ui.ch().on_spawn_text_fn(|| format!("Menu {}", dir_text));
});
// Helper utility to store simple state that can be checked
let mut local_state =
ImmLocalHashMemoryHelper::new(&mut button, "is_activated", &false);
if button.activated() {
local_state.store(&true);
}
button = button.selected_set(local_state.is_stored(&true));
if local_state.is_stored(&true) {
button = button.add_dropdown_container(
|| {
local_state.store(&false);
},
|container| {
container
.on_spawn_insert(|| AnchorOption {
anchor: Direction { x, y },
target_anchor: Direction { x: tx, y: ty },
..default()
})
.add(|ui| {
dropdown_content(ui);
});
},
);
}
local_state.finalize(&mut button);
}
});
});
}
}
fn dropdown_content(ui: &mut Imm<'_, '_, CapsUi>) {
let mut menu_container = ui.ch().on_spawn_insert(|| {
(
Node {
border: px(1.).into(),
..compact_node_container()
},
BackgroundColor(bevy::color::palettes::css::BLACK.into()),
BorderColor::all(bevy::color::palettes::css::WHITE),
)
});
// On ImmEntity for the lifetime of entity custom hashed values can be stored
//
// We will use locally stored hash value to store current menu choice
let mut local_state = ImmLocalHashMemoryHelper::new(&mut menu_container, "opened_menu", &None);
menu_container = menu_container.add(|ui| {
for _ in 0..10 {
let mut button = ui.ch().on_spawn_insert(compact_button_bundle).add(|ui| {
ui.ch().on_spawn_text("Example");
});
if button.hovered() {
local_state.store(&Some(button.imm_id()));
}
let is_open = local_state.is_stored(&Some(button.imm_id()));
button = button.selected_set(is_open);
if is_open {
button.add_dropdown_container(
|| {
local_state.store(&None);
},
|container| {
container
.on_spawn_insert(|| AnchorOption {
anchor: Direction {
x: Anchor::Start,
y: Anchor::Start,
},
target_anchor: Direction {
x: Anchor::End,
y: Anchor::Start,
},
..default()
})
.add(|ui| {
ui.ch()
.on_spawn_insert(|| {
(
Node {
border: px(1.).into(),
..compact_node_container()
},
BackgroundColor(
bevy::color::palettes::css::BLACK.into(),
),
BorderColor::all(bevy::color::palettes::css::DARK_GRAY),
)
})
.add(|ui| {
for _ in 0..3 {
let mut button = ui
.ch()
.on_spawn_insert(compact_button_bundle)
.add(|ui| {
ui.ch().on_spawn_text("Final button");
});
if button.activated() {
// Do something
// Mark menu hierarchy to be closed
let event = FocusCloseCurrentTree {
entity: button.entity(),
};
button.commands().trigger(event);
}
}
});
});
},
);
}
}
});
local_state.finalize(&mut menu_container);
}