Skip to content

Commit

Permalink
Implement drag and drop file support
Browse files Browse the repository at this point in the history
  • Loading branch information
zyllian authored and not-fl3 committed Jan 27, 2025
1 parent 939cae8 commit edd00dc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
use std::collections::HashSet;

use crate::get_context;
use crate::prelude::screen_height;
use crate::prelude::screen_width;
use crate::Vec2;
use crate::{get_context, DroppedFile};
pub use miniquad::{KeyCode, MouseButton};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -212,6 +212,11 @@ pub fn is_quit_requested() -> bool {
get_context().quit_requested
}

/// Gets the files which have been dropped on the window.
pub fn get_dropped_files() -> Vec<DroppedFile> {
get_context().dropped_files()
}

/// Functions for advanced input processing.
///
/// Functions in this module should be used by external tools that uses miniquad system, like different UI libraries. User shouldn't use this function.
Expand Down
27 changes: 27 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ struct Context {
textures: crate::texture::TexturesContext,

update_on: conf::UpdateTrigger,

dropped_files: Vec<DroppedFile>,
}

#[derive(Clone)]
Expand Down Expand Up @@ -367,6 +369,8 @@ impl Context {
default_filter_mode,
textures: crate::texture::TexturesContext::new(),
update_on,

dropped_files: Vec::new(),
}
}

Expand All @@ -385,6 +389,11 @@ impl Context {
}
}

/// Returns the files which have been dropped onto the window.
pub fn dropped_files(&mut self) -> Vec<DroppedFile> {
std::mem::take(&mut self.dropped_files)
}

fn begin_frame(&mut self) {
telemetry::begin_gpu_query("GPU");

Expand Down Expand Up @@ -441,6 +450,8 @@ impl Context {
touch.phase = input::TouchPhase::Stationary;
}
}

self.dropped_files.clear();
}

pub(crate) fn pixel_perfect_projection_matrix(&self) -> glam::Mat4 {
Expand Down Expand Up @@ -700,6 +711,16 @@ impl EventHandler for Stage {
}
}

fn files_dropped_event(&mut self) {
let context = get_context();
for i in 0..miniquad::window::dropped_file_count() {
context.dropped_files.push(DroppedFile {
path: miniquad::window::dropped_file_path(i),
bytes: miniquad::window::dropped_file_bytes(i),
});
}
}

fn draw(&mut self) {
{
let _z = telemetry::ZoneGuard::new("Event::draw");
Expand Down Expand Up @@ -886,3 +907,9 @@ impl Window {
});
}
}

/// Information about a dropped file.
pub struct DroppedFile {
pub path: Option<std::path::PathBuf>,
pub bytes: Option<Vec<u8>>,
}
2 changes: 1 addition & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ pub use crate::experimental::*;

pub use crate::logging::*;

pub use crate::color_u8;
pub use crate::{color_u8, DroppedFile};

pub use image::ImageFormat;

0 comments on commit edd00dc

Please sign in to comment.