Skip to content

Commit

Permalink
Add basic drag and drop support
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowMitia committed Dec 20, 2020
1 parent 3b2c6ce commit 4f02ad8
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ path = "examples/app/return_after_run.rs"
name = "thread_pool_resources"
path = "examples/app/thread_pool_resources.rs"

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

[[example]]
name = "hot_asset_reloading"
path = "examples/asset/hot_asset_reloading.rs"
Expand Down
19 changes: 19 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 @@ -64,3 +66,20 @@ pub struct WindowFocused {
pub id: WindowId,
pub focused: bool,
}

#[derive(Debug, Clone)]
pub struct DroppedFile {
pub id: WindowId,
pub path_buf: PathBuf,
}

#[derive(Debug, Clone)]
pub struct HoveredFile {
pub id: WindowId,
pub path_buf: PathBuf,
}

#[derive(Debug, Clone)]
pub struct HoveredFileCancelled {
pub id: WindowId,
}
7 changes: 5 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, DroppedFile, HoveredFile, HoveredFileCancelled,
ReceivedCharacter, Window, WindowDescriptor, Windows,
};
}

Expand Down Expand Up @@ -44,6 +44,9 @@ impl Plugin for WindowPlugin {
.add_event::<CursorLeft>()
.add_event::<ReceivedCharacter>()
.add_event::<WindowFocused>()
.add_event::<DroppedFile>()
.add_event::<HoveredFile>()
.add_event::<HoveredFileCancelled>()
.init_resource::<Windows>();

if self.add_primary_window {
Expand Down
35 changes: 33 additions & 2 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ use bevy_ecs::{IntoSystem, Resources, World};
use bevy_math::Vec2;
use bevy_utils::tracing::{error, trace};
use bevy_window::{
CreateWindow, CursorEntered, CursorLeft, CursorMoved, ReceivedCharacter, WindowCloseRequested,
WindowCreated, WindowFocused, WindowResized, Windows,
CreateWindow, CursorEntered, CursorLeft, CursorMoved, DroppedFile, HoveredFile,
HoveredFileCancelled, ReceivedCharacter, WindowCloseRequested, WindowCreated, WindowFocused,
WindowResized, Windows,
};
use winit::{
event::{self, DeviceEvent, Event, WindowEvent},
Expand Down Expand Up @@ -350,6 +351,35 @@ pub fn winit_runner(mut app: App) {
),
}
}
WindowEvent::DroppedFile(path_buf) => {
let mut cursor_left_events =
app.resources.get_mut::<Events<DroppedFile>>().unwrap();
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
cursor_left_events.send(DroppedFile {
id: window_id,
path_buf,
});
}
WindowEvent::HoveredFile(path_buf) => {
let mut cursor_left_events =
app.resources.get_mut::<Events<HoveredFile>>().unwrap();
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
cursor_left_events.send(HoveredFile {
id: window_id,
path_buf,
});
}
WindowEvent::HoveredFileCancelled => {
let mut cursor_left_events = app
.resources
.get_mut::<Events<HoveredFileCancelled>>()
.unwrap();
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
cursor_left_events.send(HoveredFileCancelled { id: window_id });
}
_ => {}
},
event::Event::DeviceEvent {
Expand All @@ -370,6 +400,7 @@ pub fn winit_runner(mut app: App) {
);
app.update();
}

_ => (),
}
};
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).
`draganddrop` | [`app/empty_defaults.rs`](./app/draganddrop.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
37 changes: 37 additions & 0 deletions examples/app/draganddrop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use bevy::prelude::*;

#[derive(Default)]
struct DroppedFileSystemState {
dropped_filed_event_reader: EventReader<DroppedFile>,
hovered_file_event_reader: EventReader<HoveredFile>,
hovered_file_canceled_event_reader: EventReader<HoveredFileCancelled>,
}

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

fn dropped_file_system(
mut state: Local<DroppedFileSystemState>,
dropped_file_events: Res<Events<DroppedFile>>,
hovered_file_events: Res<Events<HoveredFile>>,
hovered_file_canceled_events: Res<Events<HoveredFileCancelled>>,
) {
for event in state.dropped_filed_event_reader.iter(&dropped_file_events) {
println!("Dropped file{:?}", event);
}

for event in state.hovered_file_event_reader.iter(&hovered_file_events) {
println!("Hovered file{:?}", event);
}

for event in state
.hovered_file_canceled_event_reader
.iter(&hovered_file_canceled_events)
{
println!("Dropped file{:?}", event);
}
}

0 comments on commit 4f02ad8

Please sign in to comment.