#[derive(Debug, Clone, Copy)]
pub struct Transform {
pub position: (f32, f32),
pub rotation: f32, pub scale: (f32, f32),
pub flip_h: bool, pub flip_v: bool, }
impl Transform {
pub fn new(x: f32, y: f32) -> Self {
Self {
position: (x, y),
rotation: 0.0,
scale: (1.0, 1.0),
flip_h: false,
flip_v: false,
}
}
pub fn with_scale(mut self, scale_x: f32, scale_y: f32) -> Self {
self.scale = (scale_x, scale_y);
self
}
pub fn with_rotation(mut self, rotation: f32) -> Self {
self.rotation = rotation;
self
}
pub fn with_flip_h(mut self, flip: bool) -> Self {
self.flip_h = flip;
self
}
pub fn with_flip_v(mut self, flip: bool) -> Self {
self.flip_v = flip;
self
}
pub fn translate(&mut self, dx: f32, dy: f32) {
self.position.0 += dx;
self.position.1 += dy;
}
pub fn rotate(&mut self, angle: f32) {
self.rotation += angle;
}
pub fn set_rotation(&mut self, angle: f32) {
self.rotation = angle;
}
pub fn set_rotation_degrees(&mut self, degrees: f32) {
self.rotation = degrees.to_radians();
}
pub fn get_rotation_degrees(&self) -> f32 {
self.rotation.to_degrees()
}
pub fn rotate_point(&self, x: f32, y: f32) -> (f32, f32) {
let cos = self.rotation.cos();
let sin = self.rotation.sin();
(x * cos - y * sin, x * sin + y * cos)
}
}
impl Default for Transform {
fn default() -> Self {
Self {
position: (0.0, 0.0),
rotation: 0.0,
scale: (1.0, 1.0),
flip_h: false,
flip_v: false,
}
}
}