Skip to content

Commit

Permalink
Add FileDragAndDropPlugin and FileDragAndDrop resource
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowMitia committed Dec 25, 2020
1 parent 7c29505 commit 52566de
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 27 deletions.
3 changes: 3 additions & 0 deletions crates/bevy_internal/src/default_plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ impl PluginGroup for DefaultPlugins {
#[cfg(feature = "bevy_winit")]
group.add(bevy_winit::WinitPlugin::default());

#[cfg(feature = "bevy_winit")]
group.add(bevy_winit::FileDragAndDropPlugin::default());

#[cfg(feature = "bevy_wgpu")]
group.add(bevy_wgpu::WgpuPlugin::default());
}
Expand Down
71 changes: 71 additions & 0 deletions crates/bevy_winit/src/drag_and_drop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use std::path::PathBuf;

use bevy_app::prelude::*;

use bevy_app::EventReader;
use bevy_ecs::{IntoSystem, Local, Res, ResMut};
use bevy_window::{DroppedFile, HoveredFile, HoveredFileCancelled, WindowId};

type FilePath = (PathBuf, WindowId);
#[derive(Debug, Default)]
pub struct FileDragAndDrop {
hovered_files: Vec<FilePath>,
dropped_files: Vec<FilePath>,
}

impl FileDragAndDrop {
pub fn drain(&mut self) -> std::vec::Drain<'_, FilePath> {
self.dropped_files.drain(0..)
}

pub fn is_hovered(&self) -> bool {
!self.hovered_files.is_empty()
}
}

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

// NOTE: in winit, each Dropped, Hovered, and Canceled are raised once for EACH file
fn drag_and_drop_system(
mut state: Local<DroppedFileSystemState>,
mut drag_and_drop: ResMut<FileDragAndDrop>,
dropped_file_events: Res<Events<DroppedFile>>,
hovered_file_events: Res<Events<HoveredFile>>,
mut hovered_file_canceled_events: ResMut<Events<HoveredFileCancelled>>,
) {
for dropped_file in state.dropped_filed_event_reader.iter(&dropped_file_events) {
let DroppedFile { id, path_buf } = dropped_file;
drag_and_drop.dropped_files.push((path_buf.clone(), *id))
}

for hovered_file in state.hovered_file_event_reader.iter(&hovered_file_events) {
let HoveredFile { id, path_buf } = hovered_file;
drag_and_drop.hovered_files.push((path_buf.clone(), *id));
}

// Since we only deal with one mouse, we can assume that a cancelled event
// means all hovered files are gone.
if hovered_file_canceled_events.drain().next().is_some() {
hovered_file_canceled_events.clear();
drag_and_drop.hovered_files.clear();
}
}

#[derive(Default)]
pub struct FileDragAndDropPlugin {}

impl Plugin for FileDragAndDropPlugin {
fn build(&self, app: &mut AppBuilder) {
app.init_resource::<FileDragAndDrop>()
.add_system_to_stage(bevy_app::stage::EVENT, drag_and_drop_system.system());
}

fn name(&self) -> &str {
std::any::type_name::<Self>()
}
}
5 changes: 5 additions & 0 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
mod converters;
mod winit_config;
mod winit_windows;
mod drag_and_drop;

pub use drag_and_drop::FileDragAndDrop;
pub use drag_and_drop::FileDragAndDropPlugin;

use bevy_input::{
keyboard::KeyboardInput,
mouse::{MouseButtonInput, MouseMotion, MouseScrollUnit, MouseWheel},
Expand Down
31 changes: 4 additions & 27 deletions examples/app/drag_and_drop.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
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>,
}
use bevy::{prelude::*, winit::FileDragAndDrop};

fn main() {
App::build()
Expand All @@ -14,24 +7,8 @@ fn main() {
.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);
fn dropped_file_system(mut drag_and_drop: ResMut<FileDragAndDrop>) {
for file in drag_and_drop.drain() {
println!("Dropped path {:?}", file);
}
}

0 comments on commit 52566de

Please sign in to comment.