Skip to content

Commit

Permalink
made some changes for auto conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
MalekiRe committed Dec 4, 2022
1 parent 2333431 commit f8f23ed
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "MIT"
links = "StereoKitC"
name = "stereokit-sys"
repository = "https://github.com/MalekiRe/stereokit-sys"
version = "2.0.1"
version = "2.2.0"

include = [
"Cargo.toml",
Expand All @@ -27,9 +27,21 @@ readme = "README.md"
default = []
linux-egl = []

[dependencies]
mint = "0.5.9"

[dependencies.prisma]
version = "0.1.1"
optional = true

[dependencies.palette]
version = "0.6.1"
default-features = false
optional = true
features = ["std"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[build-dependencies]
bindgen = "0.60.1"
cmake = "0.1.48"


182 changes: 182 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,186 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

use mint::{ColumnMatrix4, RowMatrix4};
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));


impl From<mint::Vector3<f32>> for vec3 {
fn from(val: mint::Vector3<f32>) -> Self {
Self {
x: val.x,
y: val.y,
z: val.z
}
}
}

impl From<mint::Vector2<f32>> for vec2 {
fn from(val: mint::Vector2<f32>) -> Self {
Self {
x: val.x,
y: val.y
}
}
}

impl From<mint::Vector4<f32>> for vec4 {
fn from(val: mint::Vector4<f32>) -> Self {
Self {
x: val.x,
y: val.y,
z: val.z,
w: val.w
}
}
}

impl From<mint::ColumnMatrix4<f32>> for matrix {
fn from(m: mint::ColumnMatrix4<f32>) -> Self {
matrix {
row: [
m.x.into(),
m.y.into(),
m.z.into(),
m.w.into(),
],
}
}
}

impl Into<mint::ColumnMatrix4<f32>> for matrix {
fn into(self) -> mint::ColumnMatrix4<f32> {
unsafe {
//This should not work, but it does work, do not ask me why, god only knows.
match self {
matrix { m: ma} => { ColumnMatrix4::from(ma) }
matrix { row: r} => { ColumnMatrix4::from(RowMatrix4::from([
r[0].x, r[0].y, r[0].z, r[0].w, r[1].x, r[1].y, r[1].z, r[1].w, r[2].x, r[2].y,
r[2].z, r[2].w, r[3].x, r[3].y, r[3].z, r[3].w,
])) }
}
}
}
}

impl From<mint::Quaternion<f32>> for quat {
fn from(val: mint::Quaternion<f32>) -> Self {
quat {
x: val.v.x,
y: val.v.y,
z: val.v.z,
w: val.s
}
}
}


impl color128 {
pub const fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
Self {
r,
g,
b,
a,
}
}
pub const fn new_rgb(r: f32, g: f32, b: f32) -> Self {
Self::new(r, g, b, 1.0)
}
}

impl color32 {
pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
Self {
r,
g,
b,
a
}
}
pub const fn new_rgb(r: u8, g: u8, b: u8) -> Self {
Self {
r,
g,
b,
a: 255
}
}
}

impl From<color128> for color32 {
fn from(a: color128) -> Self {
Self::new((a.r*255.0) as u8, (a.g*255.0) as u8, (a.b*255.0) as u8, (a.a*255.0) as u8)
}
}

impl From<color32> for color128 {
fn from(a: color32) -> Self {
Self::new(a.r as f32 / 255.0, a.g as f32 / 255.0, a.b as f32 / 255.0, a.a as f32 / 255.0)
}
}

#[cfg(feature = "prisma")]
pub mod prisma_specific {
use prisma::Rgba;
use crate::{color128, color32};

impl From<Rgba<f32>> for color128 {
fn from(color: Rgba<f32>) -> Self {
Self {
r: color.red(),
g: color.green(),
b: color.blue(),
a: color.alpha()
}
}
}
impl From<Rgba<u8>> for color32 {
fn from(color: Rgba<u8>) -> Self {
Self {
r: color.red(),
g: color.green(),
b: color.blue(),
a: color.alpha(),
}
}
}
}

#[cfg(feature="palette")]
pub mod pallet_specific {
use crate::{color128, color32};

impl From<palette::Rgba<f32>> for color128 {
fn from(val: palette::Rgba<f32>) -> Self {
Self {
r: val.red(),
g: val.green(),
b: val.blue(),
a: val.alpha()
}
}
}

impl From<palette::Rgba<u8>> for color32 {
fn from(val: palette::Rgba<u8>) -> Self {
Self {
r: val.red(),
g: val.green(),
b: val.blue(),
a: val.alpha()
}
}
}

impl<T: palette::RgbStandard> From<palette::Alpha<palette::Rgb<T>, f32>> for color128 {
fn from(color: palette::Alpha<palette::Rgb<T>, f32>) -> Self {
Self {
r: color.red,
g: color.green,
b: color.blue,
a: color.alpha
}
}
}
}

0 comments on commit f8f23ed

Please sign in to comment.