From 9081c157d60e56bcad2ca53ed8af15d28a4a419b Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Mon, 20 Jul 2020 19:52:31 +0100 Subject: [PATCH] flip/mirror/rotate functions for tiles --- src/image.rs | 6 +++--- src/tile.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/image.rs b/src/image.rs index 47f3adf..3090a69 100644 --- a/src/image.rs +++ b/src/image.rs @@ -5,7 +5,7 @@ pub struct Image { impl Image { /// flip image vertically - fn flip(&mut self) { + pub fn flip(&mut self) { let mut pixels = Vec::with_capacity(64); for row in self.pixels.chunks(8).rev() { @@ -18,7 +18,7 @@ impl Image { } /// mirror image horizontally - fn mirror(&mut self) { + pub fn mirror(&mut self) { let mut pixels = Vec::with_capacity(64); for row in self.pixels.chunks(8) { @@ -31,7 +31,7 @@ impl Image { } /// rotate image 90° clockwise - fn rotate(&mut self) { + pub fn rotate(&mut self) { let mut pixels = Vec::with_capacity(64); // start from bottom-left corner, work upward in that column, diff --git a/src/tile.rs b/src/tile.rs index 7ad36c4..0f49ab1 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -40,6 +40,32 @@ impl Tile { "".to_string() } } + + // todo refactor + + pub fn flip(&mut self) { + self.animation_frames = self.animation_frames.iter().map(|frame: &Image| { + let mut image = frame.clone(); + image.flip(); + image + }).collect() + } + + pub fn mirror(&mut self) { + self.animation_frames = self.animation_frames.iter().map(|frame: &Image| { + let mut image = frame.clone(); + image.mirror(); + image + }).collect() + } + + pub fn rotate(&mut self) { + self.animation_frames = self.animation_frames.iter().map(|frame: &Image| { + let mut image = frame.clone(); + image.rotate(); + image + }).collect() + } } impl From for Tile {