forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_menu.rs
More file actions
197 lines (177 loc) · 6.13 KB
/
context_menu.rs
File metadata and controls
197 lines (177 loc) · 6.13 KB
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
//! This example illustrates how to create a context menu that changes the clear color
use bevy::{
color::palettes::basic,
ecs::{relationship::RelatedSpawner, spawn::SpawnWith},
prelude::*,
};
use std::fmt::Debug;
/// event opening a new context menu at position `pos`
#[derive(Event)]
struct OpenContextMenu {
pos: Vec2,
}
/// event will be sent to close currently open context menus
#[derive(Event)]
struct CloseContextMenus;
/// marker component identifying root of a context menu
#[derive(Component)]
struct ContextMenu;
/// context menu item data storing what background color `Srgba` it activates
#[derive(Component)]
struct ContextMenuItem(Srgba);
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_observer(on_trigger_menu)
.add_observer(on_trigger_close_menus)
.add_observer(text_color_on_hover::<Out>(basic::WHITE.into()))
.add_observer(text_color_on_hover::<Over>(basic::RED.into()))
.run();
}
/// helper function to reduce code duplication when generating almost identical observers for the hover text color change effect
fn text_color_on_hover<T: Debug + Clone + Reflect>(
color: Color,
) -> impl FnMut(On<Pointer<T>>, Query<&mut TextColor>, Query<&Children>) {
move |mut event: On<Pointer<T>>,
mut text_color: Query<&mut TextColor>,
children: Query<&Children>| {
let Ok(children) = children.get(event.original_event_target()) else {
return;
};
event.propagate(false);
// find the text among children and change its color
for child in children.iter() {
if let Ok(mut col) = text_color.get_mut(child) {
col.0 = color;
}
}
}
}
fn setup(mut commands: Commands) {
commands.spawn(Camera2d);
commands.spawn(background_and_button()).observe(
// any click bubbling up here should lead to closing any open menu
|_: On<Pointer<Press>>, mut commands: Commands| {
commands.trigger(CloseContextMenus);
},
);
}
fn on_trigger_close_menus(
_event: On<CloseContextMenus>,
mut commands: Commands,
menus: Query<Entity, With<ContextMenu>>,
) {
for e in menus.iter() {
commands.entity(e).despawn();
}
}
fn on_trigger_menu(event: On<OpenContextMenu>, mut commands: Commands) {
commands.trigger(CloseContextMenus);
let pos = event.pos;
debug!("open context menu at: {pos}");
commands
.spawn((
Name::new("context menu"),
ContextMenu,
Node {
position_type: PositionType::Absolute,
left: px(pos.x),
top: px(pos.y),
flex_direction: FlexDirection::Column,
border_radius: BorderRadius::all(px(4)),
..default()
},
BorderColor::all(Color::BLACK),
BackgroundColor(Color::linear_rgb(0.1, 0.1, 0.1)),
children![
context_item("fuchsia", basic::FUCHSIA),
context_item("gray", basic::GRAY),
context_item("maroon", basic::MAROON),
context_item("purple", basic::PURPLE),
context_item("teal", basic::TEAL),
],
))
.observe(
|event: On<Pointer<Press>>,
menu_items: Query<&ContextMenuItem>,
mut clear_col: ResMut<ClearColor>,
mut commands: Commands| {
let target = event.original_event_target();
if let Ok(item) = menu_items.get(target) {
clear_col.0 = item.0.into();
commands.trigger(CloseContextMenus);
}
},
);
}
fn context_item(text: &str, col: Srgba) -> impl Bundle {
(
Name::new(format!("item-{text}")),
ContextMenuItem(col),
Button,
Node {
padding: UiRect::all(px(5)),
..default()
},
children![(
Pickable::IGNORE,
Text::new(text),
TextFont {
font_size: 24.0,
..default()
},
TextColor(Color::WHITE),
)],
)
}
fn background_and_button() -> impl Bundle {
(
Name::new("background"),
Node {
width: percent(100),
height: percent(100),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
..default()
},
ZIndex(-10),
Children::spawn(SpawnWith(|parent: &mut RelatedSpawner<ChildOf>| {
parent
.spawn((
Name::new("button"),
Button,
Node {
width: px(250),
height: px(65),
border: UiRect::all(px(5)),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
border_radius: BorderRadius::MAX,
..default()
},
BorderColor::all(Color::BLACK),
BackgroundColor(Color::BLACK),
children![(
Pickable::IGNORE,
Text::new("Context Menu"),
TextFont {
font_size: 28.0,
..default()
},
TextColor(Color::WHITE),
TextShadow::default(),
)],
))
.observe(|mut event: On<Pointer<Press>>, mut commands: Commands| {
// by default this event would bubble up further leading to the `CloseContextMenus`
// event being triggered and undoing the opening of one here right away.
event.propagate(false);
debug!("click: {}", event.pointer_location.position);
commands.trigger(OpenContextMenu {
pos: event.pointer_location.position,
});
});
})),
)
}