62 lines
1.4 KiB
Rust
62 lines
1.4 KiB
Rust
use std::cmp;
|
|
|
|
// Keep synced with orbital's SyncRect
|
|
// Technically orbital uses i32 rather than u32, but values larger than i32::MAX
|
|
// would be a bug anyway.
|
|
#[derive(Debug, Copy, Clone)]
|
|
#[repr(C, packed)]
|
|
pub struct Damage {
|
|
pub x: u32,
|
|
pub y: u32,
|
|
pub width: u32,
|
|
pub height: u32,
|
|
}
|
|
|
|
impl Damage {
|
|
pub const NONE: Self = Damage {
|
|
x: 0,
|
|
y: 0,
|
|
width: 0,
|
|
height: 0,
|
|
};
|
|
|
|
pub fn merge(self, other: Self) -> Self {
|
|
if self.width == 0 || self.height == 0 {
|
|
return other;
|
|
}
|
|
|
|
if other.width == 0 || other.height == 0 {
|
|
return self;
|
|
}
|
|
|
|
let x = cmp::min(self.x, other.x);
|
|
let y = cmp::min(self.y, other.y);
|
|
let x2 = cmp::max(self.x + self.width, other.x + other.width);
|
|
let y2 = cmp::max(self.y + self.height, other.y + other.height);
|
|
|
|
Damage {
|
|
x,
|
|
y,
|
|
width: x2 - x,
|
|
height: y2 - y,
|
|
}
|
|
}
|
|
|
|
#[must_use]
|
|
pub fn clip(mut self, width: u32, height: u32) -> Self {
|
|
// Clip damage
|
|
let x2 = self.x + self.width;
|
|
self.x = cmp::min(self.x, width);
|
|
if x2 > width {
|
|
self.width = width - self.x;
|
|
}
|
|
|
|
let y2 = self.y + self.height;
|
|
self.y = cmp::min(self.y, height);
|
|
if y2 > height {
|
|
self.height = height - self.y;
|
|
}
|
|
self
|
|
}
|
|
}
|