Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multitouch support in EguiMq and demo.rs #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
23 changes: 23 additions & 0 deletions examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,29 @@ impl mq::EventHandler for Stage {
fn key_up_event(&mut self, _ctx: &mut mq::Context, keycode: mq::KeyCode, keymods: mq::KeyMods) {
self.egui_mq.key_up_event(keycode, keymods);
}

fn touch_event(
&mut self,
ctx: &mut mq::Context,
phase: mq::TouchPhase,
id: u64,
x: f32,
y: f32,
) {
if phase == mq::TouchPhase::Started {
self.mouse_button_down_event(ctx, mq::MouseButton::Left, x, y);
}

if phase == mq::TouchPhase::Ended {
self.mouse_button_up_event(ctx, mq::MouseButton::Left, x, y);
}

if phase == mq::TouchPhase::Moved {
self.mouse_motion_event(ctx, x, y);
}
Comment on lines +147 to +157
Copy link
Sponsor Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be moved into EguiMq::touch_event to lessen the amount of code the user has to write

Copy link
Sponsor Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could also be a match phase


self.egui_mq.touch_event(phase, id, x, y);
}
}

fn main() {
Expand Down
26 changes: 26 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,23 @@ impl EguiMq {
}
}

/// Call from your [`miniquad::EventHandler`].
pub fn touch_event(&mut self, phase: mq::TouchPhase, id: u64, x: f32, y: f32) {
let pos = egui::pos2(
x / self.egui_ctx.pixels_per_point(),
y / self.egui_ctx.pixels_per_point(),
);
let phase = to_egui_phase(phase);

self.egui_input.events.push(egui::Event::Touch {
device_id: egui::TouchDeviceId(0),
id: egui::TouchId(id),
phase,
pos,
force: 0.,
})
}

#[cfg(not(target_os = "macos"))]
fn set_clipboard(&mut self, mq_ctx: &mut mq::Context, text: String) {
mq_ctx.clipboard_set(&text);
Expand Down Expand Up @@ -414,6 +431,15 @@ fn to_egui_button(mb: mq::MouseButton) -> egui::PointerButton {
}
}

fn to_egui_phase(phase: mq::TouchPhase) -> egui::TouchPhase {
match phase {
mq::TouchPhase::Started => egui::TouchPhase::Start,
mq::TouchPhase::Moved => egui::TouchPhase::Move,
mq::TouchPhase::Ended => egui::TouchPhase::End,
mq::TouchPhase::Cancelled => egui::TouchPhase::Cancel,
}
}

fn to_mq_cursor_icon(cursor_icon: egui::CursorIcon) -> Option<mq::CursorIcon> {
match cursor_icon {
// Handled outside this function
Expand Down