2020-02-26 12:34:34 +01:00
|
|
|
use guillotiere::{AtlasAllocator, Size};
|
|
|
|
|
|
|
|
|
|
pub struct Allocator {
|
|
|
|
|
raw: AtlasAllocator,
|
2020-02-26 18:49:46 +01:00
|
|
|
size: u32,
|
2020-02-26 12:34:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Allocator {
|
2020-02-26 18:49:46 +01:00
|
|
|
const PADDING: u32 = 1;
|
|
|
|
|
|
2020-02-26 12:34:34 +01:00
|
|
|
pub fn new(size: u32) -> Allocator {
|
|
|
|
|
let raw = AtlasAllocator::new(Size::new(size as i32, size as i32));
|
|
|
|
|
|
2020-02-26 18:49:46 +01:00
|
|
|
Allocator { raw, size }
|
2020-02-26 12:34:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn allocate(&mut self, width: u32, height: u32) -> Option<Region> {
|
2020-02-26 18:49:46 +01:00
|
|
|
let padding = (
|
|
|
|
|
if width + Self::PADDING * 2 < self.size {
|
|
|
|
|
Self::PADDING
|
|
|
|
|
} else {
|
|
|
|
|
0
|
|
|
|
|
},
|
|
|
|
|
if height + Self::PADDING * 2 < self.size {
|
|
|
|
|
Self::PADDING
|
|
|
|
|
} else {
|
|
|
|
|
0
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let allocation = self.raw.allocate(Size::new(
|
|
|
|
|
(width + padding.0 * 2) as i32,
|
|
|
|
|
(height + padding.1 * 2) as i32,
|
|
|
|
|
))?;
|
2020-02-26 12:34:34 +01:00
|
|
|
|
2020-02-26 18:49:46 +01:00
|
|
|
Some(Region {
|
|
|
|
|
allocation,
|
|
|
|
|
padding,
|
|
|
|
|
})
|
2020-02-26 12:34:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn deallocate(&mut self, region: Region) {
|
2020-02-26 18:49:46 +01:00
|
|
|
self.raw.deallocate(region.allocation.id);
|
2020-02-26 12:34:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-26 18:49:46 +01:00
|
|
|
pub struct Region {
|
|
|
|
|
allocation: guillotiere::Allocation,
|
|
|
|
|
padding: (u32, u32),
|
|
|
|
|
}
|
2020-02-26 12:34:34 +01:00
|
|
|
|
|
|
|
|
impl Region {
|
|
|
|
|
pub fn position(&self) -> (u32, u32) {
|
2020-02-26 18:49:46 +01:00
|
|
|
let rectangle = &self.allocation.rectangle;
|
2020-02-26 12:34:34 +01:00
|
|
|
|
2020-02-26 18:49:46 +01:00
|
|
|
(
|
|
|
|
|
rectangle.min.x as u32 + self.padding.0,
|
|
|
|
|
rectangle.min.y as u32 + self.padding.1,
|
|
|
|
|
)
|
2020-02-26 12:34:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn size(&self) -> (u32, u32) {
|
2020-02-26 18:49:46 +01:00
|
|
|
let size = self.allocation.rectangle.size();
|
2020-02-26 12:34:34 +01:00
|
|
|
|
2020-02-26 18:49:46 +01:00
|
|
|
(
|
|
|
|
|
size.width as u32 - self.padding.0 * 2,
|
|
|
|
|
size.height as u32 - self.padding.1 * 2,
|
|
|
|
|
)
|
2020-02-26 12:34:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl std::fmt::Debug for Allocator {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
write!(f, "Allocator")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl std::fmt::Debug for Region {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2020-02-26 18:49:46 +01:00
|
|
|
f.debug_struct("Region")
|
|
|
|
|
.field("id", &self.allocation.id)
|
|
|
|
|
.field("rectangle", &self.allocation.rectangle)
|
|
|
|
|
.field("padding", &self.padding)
|
|
|
|
|
.finish()
|
2020-02-26 12:34:34 +01:00
|
|
|
}
|
|
|
|
|
}
|