Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ pub fn simulate_mouse_with_touch(option: bool) {
get_context().simulate_mouse_with_touch = option;
}

/// This is set to false by default, meaning mouse events will NOT raise touch events in addition to raising mouse events.
/// If set to true, mouse events WILL raise touch events in addition to raising mouse events.
pub fn is_simulating_touch_with_mouse() -> bool {
get_context().simulate_touch_with_mouse
}

/// This is set to false by default, meaning mouse events will NOT raise touch events in addition to raising mouse events.
/// If set to true, mouse events WILL raise touch events in addition to raising mouse events.
pub fn simulate_touch_with_mouse(option: bool) {
get_context().simulate_touch_with_mouse = option;
}

/// Return touches with positions in pixels.
pub fn touches() -> Vec<Touch> {
get_context().touches.values().cloned().collect()
Expand Down
72 changes: 71 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
screen_height: f32,

simulate_mouse_with_touch: bool,
simulate_touch_with_mouse: bool,

keys_down: HashSet<KeyCode>,
keys_pressed: HashSet<KeyCode>,
Expand Down Expand Up @@ -215,7 +216,7 @@
last_frame_time: f64,
frame_time: f64,

#[cfg(one_screenshot)]

Check warning on line 219 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 219 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 219 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 219 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 219 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

unexpected `cfg` condition name: `one_screenshot`
counter: usize,

camera_stack: Vec<camera::CameraState>,
Expand Down Expand Up @@ -321,7 +322,8 @@
screen_height,

simulate_mouse_with_touch: true,

simulate_touch_with_mouse: false,

keys_down: HashSet::new(),
keys_pressed: HashSet::new(),
keys_released: HashSet::new(),
Expand Down Expand Up @@ -363,7 +365,7 @@
last_frame_time: miniquad::date::now(),
frame_time: 1. / 60.,

#[cfg(one_screenshot)]

Check warning on line 368 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 368 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 368 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 368 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 368 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

unexpected `cfg` condition name: `one_screenshot`
counter: 0,
unwind: false,
recovery_future: None,
Expand Down Expand Up @@ -420,7 +422,7 @@

get_quad_context().commit_frame();

#[cfg(one_screenshot)]

Check warning on line 425 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 425 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 425 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 425 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 425 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

unexpected `cfg` condition name: `one_screenshot`
{
get_context().counter += 1;
if get_context().counter == 3 {
Expand Down Expand Up @@ -497,17 +499,17 @@
fn get_context() -> &'static mut Context {
thread_assert::same_thread();

unsafe { CONTEXT.as_mut().unwrap_or_else(|| panic!()) }

Check warning on line 502 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

creating a mutable reference to mutable static is discouraged

Check warning on line 502 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

creating a mutable reference to mutable static is discouraged

Check warning on line 502 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

creating a mutable reference to mutable static is discouraged

Check warning on line 502 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

creating a mutable reference to mutable static is discouraged

Check warning on line 502 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

creating a mutable reference to mutable static is discouraged
}

fn get_quad_context() -> &'static mut dyn miniquad::RenderingBackend {
thread_assert::same_thread();

unsafe {
assert!(CONTEXT.is_some());

Check warning on line 509 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

creating a shared reference to mutable static is discouraged

Check warning on line 509 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

creating a shared reference to mutable static is discouraged

Check warning on line 509 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

creating a shared reference to mutable static is discouraged

Check warning on line 509 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

creating a shared reference to mutable static is discouraged

Check warning on line 509 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

creating a shared reference to mutable static is discouraged
}

unsafe { &mut *CONTEXT.as_mut().unwrap().quad_context }

Check warning on line 512 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

creating a mutable reference to mutable static is discouraged

Check warning on line 512 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

creating a mutable reference to mutable static is discouraged

Check warning on line 512 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

creating a mutable reference to mutable static is discouraged

Check warning on line 512 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

creating a mutable reference to mutable static is discouraged

Check warning on line 512 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

creating a mutable reference to mutable static is discouraged
}

struct Stage {
Expand Down Expand Up @@ -554,6 +556,29 @@
.for_each(|arr| arr.push(MiniquadInputEvent::MouseMotion { x, y }));
}

// Generate touch events when simulate_touch_with_mouse is enabled
// Only generate move events if the left mouse button is down
if context.simulate_touch_with_mouse && context.mouse_down.contains(&MouseButton::Left) {
let id = 0; // Use a consistent ID for mouse-simulated touch
context.touches.insert(
id,
input::Touch {
id,
phase: input::TouchPhase::Moved,
position: Vec2::new(x, y),
},
);

context.input_events.iter_mut().for_each(|arr| {
arr.push(MiniquadInputEvent::Touch {
phase: TouchPhase::Moved,
id,
x,
y,
})
});
}

if context.update_on.mouse_motion {
miniquad::window::schedule_update();
}
Expand Down Expand Up @@ -590,6 +615,28 @@
context.mouse_position = Vec2::new(x, y);
}

// Generate touch events when simulate_touch_with_mouse is enabled
if context.simulate_touch_with_mouse && btn == MouseButton::Left {
let id = 0; // Use a consistent ID for mouse-simulated touch
context.touches.insert(
id,
input::Touch {
id,
phase: input::TouchPhase::Started,
position: Vec2::new(x, y),
},
);

context.input_events.iter_mut().for_each(|arr| {
arr.push(MiniquadInputEvent::Touch {
phase: TouchPhase::Started,
id,
x,
y,
})
});
}

if context.update_on.mouse_down {
miniquad::window::schedule_update();
}
Expand All @@ -609,6 +656,29 @@
if !context.cursor_grabbed {
context.mouse_position = Vec2::new(x, y);
}

// Generate touch events when simulate_touch_with_mouse is enabled
if context.simulate_touch_with_mouse && btn == MouseButton::Left {
let id = 0; // Use a consistent ID for mouse-simulated touch
context.touches.insert(
id,
input::Touch {
id,
phase: input::TouchPhase::Ended,
position: Vec2::new(x, y),
},
);

context.input_events.iter_mut().for_each(|arr| {
arr.push(MiniquadInputEvent::Touch {
phase: TouchPhase::Ended,
id,
x,
y,
})
});
}

if context.update_on.mouse_up {
miniquad::window::schedule_update();
}
Expand Down Expand Up @@ -932,7 +1002,7 @@
main_future: Box::pin(async {
future.await;
unsafe {
if let Some(ctx) = CONTEXT.as_mut() {

Check warning on line 1005 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

creating a mutable reference to mutable static is discouraged

Check warning on line 1005 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

creating a mutable reference to mutable static is discouraged

Check warning on line 1005 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

creating a mutable reference to mutable static is discouraged

Check warning on line 1005 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

creating a mutable reference to mutable static is discouraged

Check warning on line 1005 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

creating a mutable reference to mutable static is discouraged
ctx.gl.reset();
}
}
Expand Down
Loading