use galileo_types::cartesian::{Point2, Vector2};
use maybe_sync::{MaybeSend, MaybeSync};
use crate::map::Map;
mod event_processor;
mod map;
pub use event_processor::EventProcessor;
pub use map::{MapController, MapControllerConfiguration};
pub trait UserEventHandler {
fn handle(&self, event: &UserEvent, map: &mut Map) -> EventPropagation;
}
impl<T: for<'a> Fn(&'a UserEvent, &'a mut Map) -> EventPropagation> UserEventHandler for T
where
T: MaybeSync + MaybeSend,
{
fn handle(&self, event: &UserEvent, map: &mut Map) -> EventPropagation {
self(event, map)
}
}
pub enum RawUserEvent {
ButtonPressed(MouseButton),
ButtonReleased(MouseButton),
PointerMoved(Point2),
Scroll(f64),
TouchStart(TouchEvent),
TouchMove(TouchEvent),
TouchEnd(TouchEvent),
}
#[derive(Debug, Clone)]
pub enum UserEvent {
ButtonPressed(MouseButton, MouseEvent),
ButtonReleased(MouseButton, MouseEvent),
Click(MouseButton, MouseEvent),
DoubleClick(MouseButton, MouseEvent),
PointerMoved(MouseEvent),
DragStarted(MouseButton, MouseEvent),
Drag(MouseButton, Vector2<f64>, MouseEvent),
DragEnded(MouseButton, MouseEvent),
Scroll(f64, MouseEvent),
Zoom(f64, Point2),
}
pub enum EventPropagation {
Propagate,
Stop,
Consume,
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum MouseButton {
Left,
Middle,
Right,
Other,
}
#[derive(Debug, Clone)]
pub struct MouseEvent {
pub screen_pointer_position: Point2,
pub buttons: MouseButtonsState,
}
pub type TouchId = u64;
#[derive(Debug, Clone)]
pub struct TouchEvent {
pub touch_id: TouchId,
pub position: Point2,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum MouseButtonState {
Pressed,
Released,
}
#[derive(Debug, Copy, Clone)]
pub struct MouseButtonsState {
pub left: MouseButtonState,
pub middle: MouseButtonState,
pub right: MouseButtonState,
}
impl MouseButtonsState {
pub(crate) fn set_pressed(&mut self, button: MouseButton) {
self.set_state(button, MouseButtonState::Pressed);
}
pub(crate) fn set_released(&mut self, button: MouseButton) {
self.set_state(button, MouseButtonState::Released);
}
fn set_state(&mut self, button: MouseButton, state: MouseButtonState) {
match button {
MouseButton::Left => self.left = state,
MouseButton::Middle => self.middle = state,
MouseButton::Right => self.right = state,
MouseButton::Other => {}
}
}
fn single_pressed(&self) -> Option<MouseButton> {
let mut button = None;
if self.left == MouseButtonState::Pressed && button.replace(MouseButton::Left).is_some() {
return None;
}
if self.middle == MouseButtonState::Pressed && button.replace(MouseButton::Middle).is_some()
{
return None;
}
if self.right == MouseButtonState::Pressed && button.replace(MouseButton::Right).is_some() {
return None;
}
button
}
}
impl Default for MouseButtonsState {
fn default() -> Self {
Self {
left: MouseButtonState::Released,
middle: MouseButtonState::Released,
right: MouseButtonState::Released,
}
}
}