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
102
103
104
105
extern crate piston;

use std::fmt::{Formatter, Display, Error};

#[derive(Debug, Clone)]
pub struct Mouse {
    overed: bool,
    coordinate_cell: (u32, u32),
    dimension: (u32, u32),
}

impl Mouse {
    pub fn new (
        sizes: (u32, u32),
    ) -> Self {
        let mut event: Self = Default::default();

        event.set_dimension(sizes);
        event
    }

    pub fn set_dimension (
        &mut self,
        dimension: (u32, u32),
    ) {
        self.dimension = dimension;
    }

    pub fn get_dimension (
        &self,
    ) -> (u32, u32) {
        self.dimension
    }

    pub fn set_coordinate (
        &mut self,
        coordinate: (u32, u32),
    ) {
        self.coordinate_cell = coordinate;
    }

    pub fn get_coordinate (
        &self,
    ) -> (u32, u32) {
        self.coordinate_cell
    }

    fn set_over (
        &mut self,
        mouse: bool,
    ) {
        self.overed = mouse;
    }

    pub fn get_over (
        &mut self,
    ) -> bool {
        self.overed
    }

    pub fn check_inside_window (
        &mut self,
        coordinate: (u32, u32),
        length: u32,
    ) -> Option<(u32, u32)> {
        let mouse:bool = 0u32 < coordinate.0
                      && coordinate.0 <  self.dimension.0
                      && 0u32 < coordinate.1
                      && coordinate.1 < self.dimension.1;

        if mouse {
            self.set_over(true);
            let coordinate_cell_new = (
                coordinate.0 / {self.dimension.0 / length},
                coordinate.1 / {self.dimension.1 / length}
            );
            if coordinate_cell_new.0 != self.coordinate_cell.0
            || coordinate_cell_new.1 != self.coordinate_cell.1 {
                return Some(coordinate_cell_new);
            }
        }
        else {
            self.set_over(false);
        }
        None
    }
}

impl Default for Mouse {
    fn default() -> Self {
        Mouse {
          overed: false,
          coordinate_cell: (0u32, 0u32),
          dimension: (0u32, 0u32),
        }
    }
}

impl Display for Mouse {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        let _ = write!(f, stringify!(self.coordinate_cell.width));
        let _ = write!(f, stringify!(self.coordinate_cell.height));
        Ok(())
    }
}