use crate::input::core::event_processor::{EventProcessor, ImeEvent, PlatformEvent};
use crate::input::keyboard::events::KeyCode;
use crate::input::pointer::state::{InputState, ModifierKeys, MouseButton};
pub struct EventSynthesizer {
processor: EventProcessor,
input: InputState,
time: f64,
}
impl Default for EventSynthesizer {
fn default() -> Self {
Self::new()
}
}
impl EventSynthesizer {
pub fn new() -> Self {
Self {
processor: EventProcessor::new(),
input: InputState::new(),
time: 0.0,
}
}
fn apply(&mut self, event: PlatformEvent) -> &mut Self {
self.input.time = self.time;
self.processor.process(&event, &mut self.input, self.time);
self
}
pub fn pointer_move(&mut self, x: f64, y: f64) -> &mut Self {
self.apply(PlatformEvent::PointerMoved { x, y })
}
pub fn pointer_down(&mut self, x: f64, y: f64, button: MouseButton) -> &mut Self {
self.apply(PlatformEvent::PointerDown { x, y, button })
}
pub fn pointer_up(&mut self, x: f64, y: f64, button: MouseButton) -> &mut Self {
self.apply(PlatformEvent::PointerUp { x, y, button })
}
pub fn click(&mut self, x: f64, y: f64) -> &mut Self {
self.click_button(x, y, MouseButton::Left)
}
pub fn click_button(&mut self, x: f64, y: f64, button: MouseButton) -> &mut Self {
self.pointer_down(x, y, button);
self.pointer_up(x, y, button)
}
pub fn drag(&mut self, from: (f64, f64), to: (f64, f64), steps: u32, button: MouseButton) -> &mut Self {
self.pointer_down(from.0, from.1, button);
let steps = steps.max(1);
for step in 1..=steps {
let t = f64::from(step) / f64::from(steps);
let x = from.0 + (to.0 - from.0) * t;
let y = from.1 + (to.1 - from.1) * t;
self.pointer_move(x, y);
}
self.pointer_up(to.0, to.1, button)
}
pub fn wheel(&mut self, dx: f64, dy: f64) -> &mut Self {
self.apply(PlatformEvent::Scroll { dx, dy })
}
pub fn key_down(&mut self, key: KeyCode, modifiers: ModifierKeys) -> &mut Self {
self.apply(PlatformEvent::KeyDown { key, modifiers })
}
pub fn key_up(&mut self, key: KeyCode, modifiers: ModifierKeys) -> &mut Self {
self.apply(PlatformEvent::KeyUp { key, modifiers })
}
pub fn text(&mut self, text: &str) -> &mut Self {
self.apply(PlatformEvent::TextInput { text: text.to_owned() })
}
pub fn ime_preedit(&mut self, text: &str, sel: Option<(usize, usize)>) -> &mut Self {
self.apply(PlatformEvent::Ime(ImeEvent::Preedit(text.to_owned(), sel)))
}
pub fn ime_commit(&mut self, text: &str) -> &mut Self {
self.apply(PlatformEvent::Ime(ImeEvent::Commit(text.to_owned())))
}
pub fn focus_window(&mut self, focused: bool) -> &mut Self {
self.apply(PlatformEvent::WindowFocused(focused))
}
pub fn tick(&mut self, dt: f64) -> &mut Self {
self.time += dt;
self.input.time = self.time;
self
}
pub fn input_state(&self) -> InputState {
self.input.clone()
}
pub fn end_frame(&mut self) {
self.input.end_frame();
self.input.scroll_delta = (0.0, 0.0);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn click_sets_clicked_on_the_accumulated_input_state() {
let mut synth = EventSynthesizer::new();
synth.click(10.0, 10.0);
assert_eq!(synth.input_state().pointer.clicked, Some(MouseButton::Left));
}
#[test]
fn click_button_uses_the_requested_button() {
let mut synth = EventSynthesizer::new();
synth.click_button(10.0, 10.0, MouseButton::Right);
assert_eq!(synth.input_state().pointer.clicked, Some(MouseButton::Right));
}
#[test]
fn drag_lands_on_target_after_the_right_number_of_intermediate_moves() {
let mut synth = EventSynthesizer::new();
synth.drag((0.0, 0.0), (100.0, 0.0), 4, MouseButton::Left);
let state = synth.input_state();
assert_eq!(state.pointer.pos, Some((100.0, 0.0)));
assert_eq!(state.pointer.prev_pos, Some((75.0, 0.0)));
}
#[test]
fn drag_with_one_step_moves_directly_from_start_to_target() {
let mut synth = EventSynthesizer::new();
synth.drag((0.0, 0.0), (100.0, 0.0), 1, MouseButton::Left);
let state = synth.input_state();
assert_eq!(state.pointer.pos, Some((100.0, 0.0)));
assert_eq!(state.pointer.prev_pos, Some((0.0, 0.0)));
}
#[test]
fn end_frame_clears_clicked_and_scroll_delta() {
let mut synth = EventSynthesizer::new();
synth.click(10.0, 10.0);
synth.wheel(0.0, 5.0);
synth.end_frame();
let state = synth.input_state();
assert_eq!(state.pointer.clicked, None);
assert_eq!(state.scroll_delta, (0.0, 0.0));
}
#[test]
fn tick_advances_the_clock_without_emitting_an_event() {
let mut synth = EventSynthesizer::new();
synth.tick(0.5);
assert_eq!(synth.time, 0.5);
assert_eq!(synth.input_state().pointer.pos, None);
}
}