egui_glium: add bool to simulate touch screens to test egui

This commit is contained in:
Emil Ernerfeldt 2021-08-28 10:34:05 +02:00
parent 1a177f7ecd
commit fe2b3e26aa
2 changed files with 60 additions and 7 deletions

View file

@ -172,6 +172,8 @@ pub enum Event {
/// IME composition ended with this final result. /// IME composition ended with this final result.
CompositionEnd(String), CompositionEnd(String),
/// On touch screens, report this *in addition to*
/// [`Self::PointerMoved`], [`Self::PointerButton`], [`Self::PointerGone`]
Touch { Touch {
/// Hashed device identifier (if available; may be zero). /// Hashed device identifier (if available; may be zero).
/// Can be used to separate touches from different devices. /// Can be used to separate touches from different devices.

View file

@ -46,6 +46,7 @@ pub use copypasta::ClipboardContext; // TODO: remove
pub struct GliumInputState { pub struct GliumInputState {
pub pointer_pos_in_points: Option<Pos2>, pub pointer_pos_in_points: Option<Pos2>,
pub any_pointer_button_down: bool,
pub raw: egui::RawInput, pub raw: egui::RawInput,
} }
@ -53,6 +54,7 @@ impl GliumInputState {
pub fn from_pixels_per_point(pixels_per_point: f32) -> Self { pub fn from_pixels_per_point(pixels_per_point: f32) -> Self {
Self { Self {
pointer_pos_in_points: Default::default(), pointer_pos_in_points: Default::default(),
any_pointer_button_down: false,
raw: egui::RawInput { raw: egui::RawInput {
pixels_per_point: Some(pixels_per_point), pixels_per_point: Some(pixels_per_point),
..Default::default() ..Default::default()
@ -96,20 +98,51 @@ pub fn input_to_egui(
clipboard: Option<&mut ClipboardContext>, clipboard: Option<&mut ClipboardContext>,
input_state: &mut GliumInputState, input_state: &mut GliumInputState,
) { ) {
// Useful for debugging egui touch support on non-touch devices.
let simulate_touches = false;
use glutin::event::WindowEvent; use glutin::event::WindowEvent;
match event { match event {
WindowEvent::ScaleFactorChanged { scale_factor, .. } => { WindowEvent::ScaleFactorChanged { scale_factor, .. } => {
input_state.raw.pixels_per_point = Some(*scale_factor as f32); input_state.raw.pixels_per_point = Some(*scale_factor as f32);
} }
WindowEvent::MouseInput { state, button, .. } => { WindowEvent::MouseInput { state, button, .. } => {
if let Some(pos_in_points) = input_state.pointer_pos_in_points { if let Some(pos) = input_state.pointer_pos_in_points {
if let Some(button) = translate_mouse_button(*button) { if let Some(button) = translate_mouse_button(*button) {
let pressed = *state == glutin::event::ElementState::Pressed;
input_state.raw.events.push(egui::Event::PointerButton { input_state.raw.events.push(egui::Event::PointerButton {
pos: pos_in_points, pos,
button, button,
pressed: *state == glutin::event::ElementState::Pressed, pressed,
modifiers: input_state.raw.modifiers, modifiers: input_state.raw.modifiers,
}); });
if simulate_touches {
if pressed {
input_state.any_pointer_button_down = true;
input_state.raw.events.push(egui::Event::Touch {
device_id: egui::TouchDeviceId(0),
id: egui::TouchId(0),
phase: egui::TouchPhase::Start,
pos,
force: 0.0
});
} else {
input_state.any_pointer_button_down = false;
input_state.raw.events.push(egui::Event::PointerGone);
input_state.raw.events.push(egui::Event::Touch {
device_id: egui::TouchDeviceId(0),
id: egui::TouchId(0),
phase: egui::TouchPhase::End,
pos,
force: 0.0
});
};
}
} }
} }
} }
@ -122,10 +155,28 @@ pub fn input_to_egui(
pos_in_pixels.y as f32 / pixels_per_point, pos_in_pixels.y as f32 / pixels_per_point,
); );
input_state.pointer_pos_in_points = Some(pos_in_points); input_state.pointer_pos_in_points = Some(pos_in_points);
input_state
.raw if simulate_touches {
.events if input_state.any_pointer_button_down {
.push(egui::Event::PointerMoved(pos_in_points)); input_state
.raw
.events
.push(egui::Event::PointerMoved(pos_in_points));
input_state.raw.events.push(egui::Event::Touch {
device_id: egui::TouchDeviceId(0),
id: egui::TouchId(0),
phase: egui::TouchPhase::Move,
pos: pos_in_points,
force: 0.0
});
}
} else {
input_state
.raw
.events
.push(egui::Event::PointerMoved(pos_in_points));
}
} }
WindowEvent::CursorLeft { .. } => { WindowEvent::CursorLeft { .. } => {
input_state.pointer_pos_in_points = None; input_state.pointer_pos_in_points = None;