texture_packer/texture/
mod.rs1pub use self::{memory_rgba8_texture::MemoryRGBA8Texture, sub_texture::SubTexture};
3use std::ops::{Deref, DerefMut};
4
5pub mod image_texture;
6pub mod memory_rgba8_texture;
7pub mod sub_texture;
8
9pub trait Texture {
11 type Pixel: Pixel;
13
14 fn width(&self) -> u32;
16 fn height(&self) -> u32;
18 fn get(&self, x: u32, y: u32) -> Option<Self::Pixel>;
21 fn set(&mut self, x: u32, y: u32, val: Self::Pixel);
23
24 fn get_rotated(&self, x: u32, y: u32) -> Option<Self::Pixel> {
26 let w = self.height();
27 self.get(y, w - x - 1)
28 }
29
30 fn is_column_transparent(&self, col: u32) -> bool {
32 for y in 0..self.height() {
33 if let Some(p) = self.get(col, y) {
34 if !p.is_transparent() {
35 return false;
36 }
37 }
38 }
39 true
40 }
41
42 fn is_row_transparent(&self, row: u32) -> bool {
44 for x in 0..self.width() {
45 if let Some(p) = self.get(x, row) {
46 if !p.is_transparent() {
47 return false;
48 }
49 }
50 }
51 true
52 }
53}
54
55pub trait Pixel: Sized {
57 fn is_transparent(&self) -> bool;
59 fn transparency() -> Option<Self>;
61 fn outline() -> Self;
63}
64
65impl<P: Pixel> Texture for Box<dyn Texture<Pixel = P> + 'static> {
66 type Pixel = P;
67
68 fn width(&self) -> u32 {
69 self.deref().width()
70 }
71
72 fn height(&self) -> u32 {
73 self.deref().height()
74 }
75
76 fn get(&self, x: u32, y: u32) -> Option<P> {
77 self.deref().get(x, y)
78 }
79
80 fn set(&mut self, x: u32, y: u32, val: P) {
81 self.deref_mut().set(x, y, val);
82 }
83}