1
0
Fork 0
zenolith/src/layout/Size.zig
2024-02-19 22:40:17 +01:00

49 lines
1.3 KiB
Zig

//! A size, usually used for widget sizes.
const Position = @import("Position.zig");
width: u31,
height: u31,
const Size = @This();
pub const zero = Size{ .width = 0, .height = 0 };
/// Returns a Size where both components are identical.
pub inline fn two(size: u31) Size {
return .{ .width = size, .height = size };
}
/// Returns the area of this size.
pub inline fn area(self: Size) u31 {
return self.width * self.height;
}
/// Performs a component-wise addition on two Sizes.
pub inline fn add(self: Size, other: Size) Size {
return .{
.width = self.width + other.width,
.height = self.height + other.height,
};
}
/// Performs a component-wise addition on two Sizes.
pub inline fn sub(self: Size, other: Size) Size {
return .{
.width = self.width - other.width,
.height = self.height - other.height,
};
}
/// Converts this Size to a Position.
pub inline fn position(self: Size) Position {
return .{ .x = self.width, .y = self.height };
}
/// Scales this size by a fraction given as a numerator and denominator.
pub inline fn scaleFrac(self: Size, numerator: u31, denominator: u31) Size {
return .{
.width = self.width * numerator / denominator,
.height = self.height * numerator / denominator,
};
}