拖放文件
可参考的官方例子:
drag_and_drop
.
Bevy 支持大多数桌面操作系统上常见的拖放手势,但只支持文件,不支持任意的数据/对象。
如果你把一个文件(例如,从文件管理器应用程序)拖到 Bevy 的应用程序中,Bevy 将产生一个 FileDragAndDrop
事件,事件中包含被拖入的文件的路径。
通常,在一个图形应用程序中,你可能想根据它被放置的位置来做不同的事情。为此,你可以获取鼠标指针的位置,或者使用 Bevy UI Interaction
。
例如,这里是如何检测一个文件是否被投放到一个特殊的 UI 部件或元素(我们用一个自定义的标记组件来识别):
#[derive(Component)]
struct MyDropTarget;
fn file_drop(
mut dnd_evr: EventReader<FileDragAndDrop>,
query_ui_droptarget: Query<&Interaction, With<MyDropTarget>>,
) {
for ev in dnd_evr.iter() {
println!("{:?}", ev);
if let FileDragAndDrop::DroppedFile { id, path_buf } = ev {
println!("Dropped file with path: {:?}", path_buf);
if id.is_primary() {
// it was dropped over the main window
}
for interaction in query_ui_droptarget.iter() {
if *interaction == Interaction::Hovered {
// it was dropped over our UI element
// (our UI element is being hovered over)
}
}
}
}
}