1
0
Fork 0
forked from wry/wry

all: split reusable components into workspace crates

This commit is contained in:
kossLAN 2026-05-29 09:14:53 -04:00
parent 2a079ed800
commit 657e7ce2f7
No known key found for this signature in database
225 changed files with 7422 additions and 17602 deletions

137
units/src/fixed.rs Normal file
View file

@ -0,0 +1,137 @@
use std::{
cmp::Ordering,
fmt::{Debug, Display, Formatter},
ops::{Add, AddAssign, Div, Mul, Sub, SubAssign},
};
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
#[repr(transparent)]
pub struct Fixed(pub i32);
impl Fixed {
pub const EPSILON: Self = Fixed(1);
pub fn is_integer(self) -> bool {
self.0 & 255 == 0
}
pub fn from_f64(f: f64) -> Self {
Self((f * 256.0) as i32)
}
pub fn from_f32(f: f32) -> Self {
Self::from_f64(f as f64)
}
pub fn to_f64(self) -> f64 {
self.0 as f64 / 256.0
}
pub fn to_f32(self) -> f32 {
self.0 as f32 / 256.0
}
pub fn from_1616(i: i32) -> Self {
Self(i >> 8)
}
pub fn to_int(self) -> i32 {
self.0 >> 8
}
pub fn from_int(i: i32) -> Self {
Self(i << 8)
}
pub fn round_down(self) -> i32 {
self.0 >> 8
}
pub fn apply_fract(self, i: i32) -> Self {
Self((i << 8) | (self.0 & 255))
}
}
impl PartialEq<i32> for Fixed {
fn eq(&self, other: &i32) -> bool {
self.0 == *other << 8
}
}
impl PartialOrd<i32> for Fixed {
fn partial_cmp(&self, other: &i32) -> Option<Ordering> {
self.0.partial_cmp(&(*other << 8))
}
}
impl Debug for Fixed {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self.to_f64(), f)
}
}
impl Display for Fixed {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.to_f64(), f)
}
}
impl Sub for Fixed {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self(self.0 - rhs.0)
}
}
impl Add for Fixed {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl Sub<i32> for Fixed {
type Output = Self;
fn sub(self, rhs: i32) -> Self::Output {
Self(self.0 - (rhs << 8))
}
}
impl Add<i32> for Fixed {
type Output = Self;
fn add(self, rhs: i32) -> Self::Output {
Self(self.0 + (rhs << 8))
}
}
impl Mul<i32> for Fixed {
type Output = Self;
fn mul(self, rhs: i32) -> Self::Output {
Self(self.0 * rhs)
}
}
impl Div<i32> for Fixed {
type Output = Self;
fn div(self, rhs: i32) -> Self::Output {
Self(self.0 / rhs)
}
}
impl AddAssign for Fixed {
fn add_assign(&mut self, rhs: Self) {
self.0 += rhs.0;
}
}
impl SubAssign for Fixed {
fn sub_assign(&mut self, rhs: Self) {
self.0 -= rhs.0;
}
}

4
units/src/lib.rs Normal file
View file

@ -0,0 +1,4 @@
pub mod fixed;
pub mod scale;
pub use {fixed::Fixed, scale::Scale};

72
units/src/scale.rs Normal file
View file

@ -0,0 +1,72 @@
use std::fmt::{Debug, Display, Formatter};
pub const SCALE_BASE: u32 = 120;
const BASE64: i64 = SCALE_BASE as i64;
pub const SCALE_BASEF: f64 = SCALE_BASE as f64;
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct Scale(u32);
impl Default for Scale {
fn default() -> Self {
Scale::from_int(1)
}
}
impl Scale {
pub const fn from_int(f: u32) -> Self {
Self(f.saturating_mul(SCALE_BASE))
}
pub fn from_f64(f: f64) -> Self {
Self((f * SCALE_BASEF).round() as u32)
}
pub fn from_f64_as_float(f: f64) -> Self {
Self(((f * (SCALE_BASEF / 15.0)).round() as u32).saturating_mul(15))
}
pub fn to_f64(self) -> f64 {
self.0 as f64 / SCALE_BASEF
}
pub fn round_up(self) -> u32 {
self.0.saturating_add(SCALE_BASE - 1) / SCALE_BASE
}
pub const fn from_wl(wl: u32) -> Self {
Self(wl)
}
pub fn to_wl(self) -> u32 {
self.0
}
#[inline(always)]
pub fn pixel_size<const N: usize>(self, v: [i32; N]) -> [i32; N] {
if self == Scale::default() {
return v;
}
let scale = self.0 as i64;
v.map(|v| ((v as i64 * scale + v.signum() as i64 * BASE64 / 2) / BASE64) as i32)
}
}
impl PartialEq<u32> for Scale {
fn eq(&self, other: &u32) -> bool {
self.0 == other * SCALE_BASE
}
}
impl Debug for Scale {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self.to_f64(), f)
}
}
impl Display for Scale {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.to_f64(), f)
}
}