micro_bevy_web_utils 0.4.0

Utilities for patching various missing parts from Bevy web/touch support
Documentation
use bevy_ecs::entity::Entity;
use bevy_ecs::event::EventWriter;
use bevy_ecs::query::With;
use bevy_ecs::system::Query;
use bevy_input::touch::{TouchInput, TouchPhase};
use bevy_math::Vec2;
use bevy_window::{PrimaryWindow, Window};
use std::fmt::{Display, Formatter};

#[derive(serde::Deserialize)]
struct InnerTouchInput {
	id: u64,
	phase: TouchPhase,
	position: Vec2,
}

#[derive(Clone, Debug)]
pub struct TouchEventError {
	message: String,
}

impl TouchEventError {
	pub fn new(message: impl ToString) -> Self {
		TouchEventError {
			message: message.to_string(),
		}
	}
}

impl Display for TouchEventError {
	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
		write!(f, "Touch Error: {}", self.message)
	}
}

impl std::error::Error for TouchEventError {}

fn inner_touch_events() -> Result<impl IntoIterator<Item = InnerTouchInput>, TouchEventError> {
	let raw_touch_events = super::bindings::take_touch_events();
	let touch_events: Vec<InnerTouchInput> =
		serde_json::from_str(&raw_touch_events).map_err(|e| TouchEventError::new(e))?;

	Ok(touch_events.into_iter())
}

pub fn emit_touch_events(
	windows: Query<Entity, With<PrimaryWindow>>,
	mut events: EventWriter<TouchInput>,
) {
	let window = match windows.iter().next() {
		Some(window) => window,
		None => return,
	};

	if let Ok(inner_events) = inner_touch_events() {
		for input in inner_events {
			events.send(TouchInput {
				window,
				phase: input.phase,
				position: input.position,
				id: input.id,
				force: None,
			});
		}
	}
}