Skip to content

Commit

Permalink
Add basic file drag and drop support
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowMitia authored and cart committed Dec 31, 2020
1 parent bbae58a commit 8cf3f29
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ path = "examples/3d/z_sort_debug.rs"
name = "custom_loop"
path = "examples/app/custom_loop.rs"

[[example]]
name = "drag_and_drop"
path = "examples/app/drag_and_drop.rs"

[[example]]
name = "empty_defaults"
path = "examples/app/empty_defaults.rs"
Expand Down
12 changes: 12 additions & 0 deletions crates/bevy_window/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use super::{WindowDescriptor, WindowId};
use bevy_math::Vec2;

Expand Down Expand Up @@ -77,3 +79,13 @@ pub struct WindowBackendScaleFactorChanged {
pub id: WindowId,
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 },
}
5 changes: 3 additions & 2 deletions crates/bevy_window/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub use windows::*;

pub mod prelude {
pub use crate::{
CursorEntered, CursorLeft, CursorMoved, ReceivedCharacter, Window, WindowDescriptor,
Windows,
CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, ReceivedCharacter, Window,
WindowDescriptor, Windows,
};
}

Expand Down Expand Up @@ -46,6 +46,7 @@ impl Plugin for WindowPlugin {
.add_event::<WindowFocused>()
.add_event::<WindowScaleFactorChanged>()
.add_event::<WindowBackendScaleFactorChanged>()
.add_event::<FileDragAndDrop>()
.init_resource::<Windows>();

if self.add_primary_window {
Expand Down
24 changes: 23 additions & 1 deletion crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod converters;
mod winit_config;
mod winit_windows;

use bevy_input::{
keyboard::KeyboardInput,
mouse::{MouseButtonInput, MouseMotion, MouseScrollUnit, MouseWheel},
Expand All @@ -14,7 +15,7 @@ use bevy_ecs::{IntoSystem, Resources, World};
use bevy_math::Vec2;
use bevy_utils::tracing::{error, trace, warn};
use bevy_window::{
CreateWindow, CursorEntered, CursorLeft, CursorMoved, ReceivedCharacter,
CreateWindow, CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, ReceivedCharacter,
WindowBackendScaleFactorChanged, WindowCloseRequested, WindowCreated, WindowFocused,
WindowResized, WindowScaleFactorChanged, Windows,
};
Expand Down Expand Up @@ -397,6 +398,27 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) {
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 });
}
_ => {}
}
}
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ 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).
`drag_and_drop` | [`app/empty_defaults.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` | [`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
Expand Down
17 changes: 17 additions & 0 deletions examples/app/drag_and_drop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use bevy::prelude::*;

fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_system(dropped_file_system.system())
.run();
}

fn dropped_file_system(
mut reader: Local<EventReader<FileDragAndDrop>>,
events: Res<Events<FileDragAndDrop>>,
) {
for event in reader.iter(&events) {
println!("{:?}", event);
}
}

0 comments on commit 8cf3f29

Please sign in to comment.