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
106
107
//! An anchor that sticks an object to a specific side or corner of the screen

use sdl2::rect::{Point, Rect};
use sdl2::render::Canvas;
use sdl2::video::Window;

use drawable::{DrawSettings, Drawable, KnownSize, Position, State};

/// The direction to anchor the object to
#[allow(missing_docs)]
pub enum AnchorDirection {
    North,
    East,
    South,
    West,

    NorthEast,
    SouthEast,
    SouthWest,
    NorthWest,
}

/// An anchor instance
pub struct Anchor<T: Drawable + KnownSize> {
    /// The inner object to be anchored
    pub inner: T,
    /// The direction to anchor to
    pub direction: AnchorDirection,
}

impl<T: Drawable + KnownSize> Anchor<T> {
    /// Create a new Anchor instance
    pub fn new(direction: AnchorDirection, inner: T) -> Anchor<T> {
        Anchor { direction, inner }
    }
}

impl<T: Drawable + KnownSize> Drawable for Anchor<T> {
    fn content(&self) -> Vec<&dyn Drawable> {
        vec![&self.inner]
    }

    fn content_mut(&mut self) -> Vec<&mut dyn Drawable> {
        vec![&mut self.inner]
    }

    fn load(&mut self) {
        self.inner.load();
    }

    fn update(&mut self, dt: f64) {
        self.inner.update(dt);
    }

    fn step(&mut self) {
        self.inner.step()
    }

    fn state(&self) -> State {
        self.inner.state()
    }

    fn draw(&self, canvas: &mut Canvas<Window>, pos: &Position, settings: DrawSettings) {
        let rect = match pos {
            Position::Rect(r) => r,
            _ => {
                self.inner.draw(canvas, pos, settings);
                return;
            }
        };

        let (iwidth, iheight) = (self.width() as i32, self.height() as i32);
        let (rwidth, rheight) = (rect.width() as i32, rect.height() as i32);

        let corner = match self.direction {
            AnchorDirection::North => Point::new(rect.x + (rwidth - iwidth) / 2, rect.y),
            AnchorDirection::East => {
                Point::new(rect.x + rwidth - iwidth, rect.y + (rheight - iheight) / 2)
            }
            AnchorDirection::South => {
                Point::new(rect.x + (rwidth - iwidth) / 2, rect.y + rheight - iheight)
            }
            AnchorDirection::West => Point::new(rect.x, rect.y + (rheight - iheight) / 2),
            AnchorDirection::NorthWest => Point::new(rect.x, rect.y),
            AnchorDirection::NorthEast => Point::new(rect.x + rwidth - iwidth, rect.y),
            AnchorDirection::SouthWest => Point::new(rect.x, rect.y + rheight - iheight),
            AnchorDirection::SouthEast => {
                Point::new(rect.x + rwidth - iwidth, rect.y + rheight - iheight)
            }
        };
        self.inner.draw(
            canvas,
            &Position::Rect(Rect::new(corner.x, corner.y, iwidth as u32, iheight as u32)),
            settings,
        );
    }
}

impl<T: Drawable + KnownSize> KnownSize for Anchor<T> {
    fn width(&self) -> usize {
        self.inner.width()
    }

    fn height(&self) -> usize {
        self.inner.height()
    }
}