#![deny(missing_docs)]
#![doc = include_str!("../README.md")]
use cranpose_ui_graphics::{Point, Rect};
#[derive(Debug, Clone)]
pub struct AndroidPlatform {
scale_factor: f64,
input_surface_offset_x_px: f64,
input_surface_offset_y_px: f64,
}
impl Default for AndroidPlatform {
fn default() -> Self {
Self {
scale_factor: 1.0,
input_surface_offset_x_px: 0.0,
input_surface_offset_y_px: 0.0,
}
}
}
impl AndroidPlatform {
pub fn new() -> Self {
Self::default()
}
pub fn set_scale_factor(&mut self, scale_factor: f64) {
self.scale_factor = scale_factor;
}
pub fn set_input_surface_offset_px(&mut self, x_px: f64, y_px: f64) {
self.input_surface_offset_x_px = x_px;
self.input_surface_offset_y_px = y_px;
}
pub fn input_surface_offset_px(&self) -> (f64, f64) {
(
self.input_surface_offset_x_px,
self.input_surface_offset_y_px,
)
}
pub fn pointer_position(&self, physical_x: f64, physical_y: f64) -> Point {
let scale = self.scale_factor;
Point {
x: ((physical_x + self.input_surface_offset_x_px) / scale) as f32,
y: ((physical_y + self.input_surface_offset_y_px) / scale) as f32,
}
}
pub fn scale_factor(&self) -> f32 {
self.scale_factor as f32
}
pub fn physical_rect(&self, rect: Rect) -> [i32; 4] {
let scale = self.scale_factor;
let left = (f64::from(rect.x) * scale - self.input_surface_offset_x_px).floor();
let top = (f64::from(rect.y) * scale - self.input_surface_offset_y_px).floor();
let right =
(f64::from(rect.x + rect.width) * scale - self.input_surface_offset_x_px).ceil();
let bottom =
(f64::from(rect.y + rect.height) * scale - self.input_surface_offset_y_px).ceil();
[
left as i32,
top as i32,
(right - left) as i32,
(bottom - top) as i32,
]
}
}
#[cfg(test)]
#[path = "tests/android_tests.rs"]
mod tests;