use std::path::Path;
use bevy::prelude::*;
use bevy_blob_loader::path::serialize_url;
use wasm_bindgen::prelude::*;
pub struct WebFileDropPlugin;
impl Plugin for WebFileDropPlugin {
fn build(&self, app: &mut App) {
#[cfg(target_family = "wasm")]
{
use bevy_blob_loader::BlobLoaderPlugin;
app.add_plugins(BlobLoaderPlugin)
.add_systems(Startup, init_js)
.add_systems(Update, read_dropped_files);
}
}
}
#[wasm_bindgen(module = "/src/drop.js")]
extern "C" {
fn init();
fn next_dropped_file() -> Option<Vec<String>>;
}
fn init_js() {
init();
}
fn read_dropped_files(
mut writer: EventWriter<FileDragAndDrop>,
windows: Query<Entity, With<Window>>,
) {
if let Some(vec) = next_dropped_file() {
let url = &vec[0];
let ext = &vec[1];
info!("Got dropped {} file: {}", ext, url);
for window in windows.iter() {
let serialized = serialize_url(url, ext);
let path = Path::new(&serialized);
let path_buf = path.to_path_buf();
writer.send(FileDragAndDrop::DroppedFile { window, path_buf });
}
}
}