Add basic file drag and drop support (#1096)

Add basic file drag and drop support
This commit is contained in:
Dimitri Belopopsky 2021-01-01 22:31:22 +01:00 committed by GitHub
parent 228c3df751
commit a01f22e0c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 60 additions and 3 deletions

View file

@ -142,6 +142,10 @@ path = "examples/3d/z_sort_debug.rs"
name = "custom_loop" name = "custom_loop"
path = "examples/app/custom_loop.rs" path = "examples/app/custom_loop.rs"
[[example]]
name = "drag_and_drop"
path = "examples/app/drag_and_drop.rs"
[[example]] [[example]]
name = "empty_defaults" name = "empty_defaults"
path = "examples/app/empty_defaults.rs" path = "examples/app/empty_defaults.rs"

View file

@ -1,3 +1,5 @@
use std::path::PathBuf;
use super::{WindowDescriptor, WindowId}; use super::{WindowDescriptor, WindowId};
use bevy_math::Vec2; use bevy_math::Vec2;
@ -77,3 +79,13 @@ pub struct WindowBackendScaleFactorChanged {
pub id: WindowId, pub id: WindowId,
pub scale_factor: f64, pub scale_factor: f64,
} }
/// Events related to files being dragged and dropped on a window.
#[derive(Debug, Clone)]
pub enum FileDragAndDrop {
DroppedFile { id: WindowId, path_buf: PathBuf },
HoveredFile { id: WindowId, path_buf: PathBuf },
HoveredFileCancelled { id: WindowId },
}

View file

@ -11,8 +11,8 @@ pub use windows::*;
pub mod prelude { pub mod prelude {
pub use crate::{ pub use crate::{
CursorEntered, CursorLeft, CursorMoved, ReceivedCharacter, Window, WindowDescriptor, CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, ReceivedCharacter, Window,
Windows, WindowDescriptor, Windows,
}; };
} }
@ -46,6 +46,7 @@ impl Plugin for WindowPlugin {
.add_event::<WindowFocused>() .add_event::<WindowFocused>()
.add_event::<WindowScaleFactorChanged>() .add_event::<WindowScaleFactorChanged>()
.add_event::<WindowBackendScaleFactorChanged>() .add_event::<WindowBackendScaleFactorChanged>()
.add_event::<FileDragAndDrop>()
.init_resource::<Windows>(); .init_resource::<Windows>();
if self.add_primary_window { if self.add_primary_window {

View file

@ -1,6 +1,7 @@
mod converters; mod converters;
mod winit_config; mod winit_config;
mod winit_windows; mod winit_windows;
use bevy_input::{ use bevy_input::{
keyboard::KeyboardInput, keyboard::KeyboardInput,
mouse::{MouseButtonInput, MouseMotion, MouseScrollUnit, MouseWheel}, mouse::{MouseButtonInput, MouseMotion, MouseScrollUnit, MouseWheel},
@ -14,7 +15,7 @@ use bevy_ecs::{IntoSystem, Resources, World};
use bevy_math::Vec2; use bevy_math::Vec2;
use bevy_utils::tracing::{error, trace, warn}; use bevy_utils::tracing::{error, trace, warn};
use bevy_window::{ use bevy_window::{
CreateWindow, CursorEntered, CursorLeft, CursorMoved, ReceivedCharacter, CreateWindow, CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, ReceivedCharacter,
WindowBackendScaleFactorChanged, WindowCloseRequested, WindowCreated, WindowFocused, WindowBackendScaleFactorChanged, WindowCloseRequested, WindowCreated, WindowFocused,
WindowResized, WindowScaleFactorChanged, Windows, WindowResized, WindowScaleFactorChanged, Windows,
}; };
@ -397,6 +398,27 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) {
focused, focused,
}); });
} }
WindowEvent::DroppedFile(path_buf) => {
let mut events =
app.resources.get_mut::<Events<FileDragAndDrop>>().unwrap();
events.send(FileDragAndDrop::DroppedFile {
id: window_id,
path_buf,
});
}
WindowEvent::HoveredFile(path_buf) => {
let mut events =
app.resources.get_mut::<Events<FileDragAndDrop>>().unwrap();
events.send(FileDragAndDrop::HoveredFile {
id: window_id,
path_buf,
});
}
WindowEvent::HoveredFileCancelled => {
let mut events =
app.resources.get_mut::<Events<FileDragAndDrop>>().unwrap();
events.send(FileDragAndDrop::HoveredFileCancelled { id: window_id });
}
_ => {} _ => {}
} }
} }

View file

@ -81,6 +81,7 @@ Example | File | Description
Example | File | Description Example | File | Description
--- | --- | --- --- | --- | ---
`custom_loop` | [`app/custom_loop.rs`](./app/custom_loop.rs) | Demonstrates how to create a custom runner (to update an app manually). `custom_loop` | [`app/custom_loop.rs`](./app/custom_loop.rs) | Demonstrates how to create a custom runner (to update an app manually).
`drag_and_drop` | [`app/drag_and_drop.rs`](./app/drag_and_drop.rs) | An example that shows how to handle drag and drop in an app.
`empty_defaults` | [`app/empty_defaults.rs`](./app/empty_defaults.rs) | An empty application with default plugins `empty_defaults` | [`app/empty_defaults.rs`](./app/empty_defaults.rs) | An empty application with default plugins
`empty` | [`app/empty.rs`](./app/empty.rs) | An empty application (does nothing) `empty` | [`app/empty.rs`](./app/empty.rs) | An empty application (does nothing)
`headless` | [`app/headless.rs`](./app/headless.rs) | An application that runs without default plugins `headless` | [`app/headless.rs`](./app/headless.rs) | An application that runs without default plugins

View file

@ -0,0 +1,17 @@
use bevy::prelude::*;
fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_system(file_drag_and_drop_system.system())
.run();
}
fn file_drag_and_drop_system(
mut reader: Local<EventReader<FileDragAndDrop>>,
events: Res<Events<FileDragAndDrop>>,
) {
for event in reader.iter(&events) {
println!("{:?}", event);
}
}