1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
use std::mem;
use std::ops::Mul;
use ui_sys::{self, uiDrawMatrix};

/// A transformation which can be applied to the contents of a DrawContext.
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Transform {
    ui_matrix: uiDrawMatrix,
}

impl Transform {
    /// Create a Transform from an existing raw uiDrawMatrix.
    pub fn from_ui_matrix(ui_matrix: &uiDrawMatrix) -> Transform {
        Transform {
            ui_matrix: *ui_matrix,
        }
    }

    /// Create a new Transform that does nothing.
    pub fn identity() -> Transform {
        unsafe {
            let mut matrix = mem::uninitialized();
            ui_sys::uiDrawMatrixSetIdentity(&mut matrix);
            Transform::from_ui_matrix(&matrix)
        }
    }

    /// Modify this Transform to translate by the given amounts.
    pub fn translate(&mut self, x: f64, y: f64) {
        unsafe { ui_sys::uiDrawMatrixTranslate(&mut self.ui_matrix, x, y) }
    }

    /// Modify this Transform to scale by the given amounts from the given center.
    pub fn scale(&mut self, x_center: f64, y_center: f64, x: f64, y: f64) {
        unsafe { ui_sys::uiDrawMatrixScale(&mut self.ui_matrix, x_center, y_center, x, y) }
    }

    /// Modify this Transform to rotate around the given center by the given angle.
    pub fn rotate(&mut self, x: f64, y: f64, angle: f64) {
        unsafe { ui_sys::uiDrawMatrixRotate(&mut self.ui_matrix, x, y, angle) }
    }

    /// Modify this Transform to skew from the given point by the given amount.
    pub fn skew(&mut self, x: f64, y: f64, xamount: f64, yamount: f64) {
        unsafe { ui_sys::uiDrawMatrixSkew(&mut self.ui_matrix, x, y, xamount, yamount) }
    }

    /// Compose this Transform with another, creating a Transform which represents both operations.
    pub fn compose(&mut self, src: &Transform) {
        unsafe { ui_sys::uiDrawMatrixMultiply(&mut self.ui_matrix, src.ptr()) }
    }

    /// Returns true if inverting this Transform is possible.
    pub fn invertible(&self) -> bool {
        unsafe {
            ui_sys::uiDrawMatrixInvertible(
                &self.ui_matrix as *const uiDrawMatrix as *mut uiDrawMatrix,
            ) != 0
        }
    }

    /// Attempts to invert the Transform, returning true if it succeeded and false if it failed.
    pub fn invert(&mut self) -> bool {
        unsafe { ui_sys::uiDrawMatrixInvert(&mut self.ui_matrix) != 0 }
    }

    pub fn transform_point(&self, mut point: (f64, f64)) -> (f64, f64) {
        unsafe {
            ui_sys::uiDrawMatrixTransformPoint(
                &self.ui_matrix as *const uiDrawMatrix as *mut uiDrawMatrix,
                &mut point.0,
                &mut point.1,
            );
            point
        }
    }

    pub fn transform_size(&self, mut size: (f64, f64)) -> (f64, f64) {
        unsafe {
            ui_sys::uiDrawMatrixTransformSize(
                &self.ui_matrix as *const uiDrawMatrix as *mut uiDrawMatrix,
                &mut size.0,
                &mut size.1,
            );
            size
        }
    }

    pub fn ptr(&self) -> *mut uiDrawMatrix {
        &self.ui_matrix as *const uiDrawMatrix as *mut uiDrawMatrix
    }
}

impl Mul<Transform> for Transform {
    type Output = Transform;

    fn mul(mut self, other: Transform) -> Transform {
        self.compose(&other);
        self
    }
}