added wasm port
This commit is contained in:
16
rdraught-ui-common/Cargo.toml
Normal file
16
rdraught-ui-common/Cargo.toml
Normal file
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "rdraught-ui-common"
|
||||
authors.workspace = true
|
||||
edition.workspace = true
|
||||
homepage.workspace = true
|
||||
license.workspace = true
|
||||
repository.workspace = true
|
||||
rust-version.workspace = true
|
||||
version.workspace = true
|
||||
|
||||
[lib]
|
||||
crate-type = ["lib"]
|
||||
|
||||
[dependencies]
|
||||
rdraught.workspace = true
|
||||
rmath.workspace = true
|
261
rdraught-ui-common/src/geo2d.rs
Normal file
261
rdraught-ui-common/src/geo2d.rs
Normal file
@@ -0,0 +1,261 @@
|
||||
use rmath::NumericalMatrix;
|
||||
use rmath::SMatrix;
|
||||
use std::clone::Clone;
|
||||
use std::cmp::Eq;
|
||||
use std::cmp::PartialEq;
|
||||
use std::fmt::Display;
|
||||
use std::marker::Copy;
|
||||
use std::ops::Add;
|
||||
use std::ops::Div;
|
||||
use std::ops::Mul;
|
||||
use std::ops::Neg;
|
||||
use std::ops::Sub;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Point {
|
||||
x: f64,
|
||||
y: f64,
|
||||
}
|
||||
|
||||
impl Point {
|
||||
pub fn x(&self) -> f64 {
|
||||
self.x
|
||||
}
|
||||
|
||||
pub fn y(&self) -> f64 {
|
||||
self.y
|
||||
}
|
||||
|
||||
pub fn new(x: f64, y: f64) -> Point {
|
||||
Point { x, y }
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<Point> for Point {
|
||||
fn add(self, rhs: Point) -> Self::Output {
|
||||
Point {
|
||||
x: self.x() + rhs.x(),
|
||||
y: self.y() + rhs.y(),
|
||||
}
|
||||
}
|
||||
type Output = Point;
|
||||
}
|
||||
|
||||
impl Sub<Point> for Point {
|
||||
fn sub(self, rhs: Point) -> Self::Output {
|
||||
Point {
|
||||
x: self.x() - rhs.x(),
|
||||
y: self.y() - rhs.y(),
|
||||
}
|
||||
}
|
||||
type Output = Point;
|
||||
}
|
||||
|
||||
impl Mul<f64> for Point {
|
||||
fn mul(self, rhs: f64) -> Self::Output {
|
||||
Point::new(self.x * rhs, self.y * rhs)
|
||||
}
|
||||
type Output = Point;
|
||||
}
|
||||
|
||||
impl Div<f64> for Point {
|
||||
fn div(self, rhs: f64) -> Self::Output {
|
||||
Point::new(self.x / rhs, self.y / rhs)
|
||||
}
|
||||
type Output = Point;
|
||||
}
|
||||
|
||||
impl Neg for Point {
|
||||
fn neg(self) -> Self::Output {
|
||||
Point::new(-self.x(), -self.y())
|
||||
}
|
||||
|
||||
type Output = Point;
|
||||
}
|
||||
|
||||
impl PartialEq for Point {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.x.eq(&other.x) && self.y.eq(&other.y)
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Point {}
|
||||
|
||||
impl Clone for Point {
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
impl Copy for Point {}
|
||||
|
||||
impl Display for Point {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Point({}, {})", self.x, self.y)
|
||||
}
|
||||
}
|
||||
|
||||
impl Point {}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Rect {
|
||||
tl: Point,
|
||||
br: Point,
|
||||
}
|
||||
|
||||
impl Rect {
|
||||
pub fn new(tl: Point, br: Point) -> Rect {
|
||||
Rect { tl, br }
|
||||
}
|
||||
|
||||
pub fn from_size(tl: Point, width: f64, height: f64) -> Rect {
|
||||
Rect {
|
||||
tl,
|
||||
br: tl + Point::new(width, height),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn width(&self) -> f64 {
|
||||
self.br.x - self.tl.x
|
||||
}
|
||||
|
||||
pub fn height(&self) -> f64 {
|
||||
self.br.y - self.tl.y
|
||||
}
|
||||
|
||||
pub fn tl(&self) -> Point {
|
||||
self.tl
|
||||
}
|
||||
|
||||
pub fn br(&self) -> Point {
|
||||
self.br
|
||||
}
|
||||
|
||||
pub fn center(&self) -> Point {
|
||||
(self.tl + self.br) / 2.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Rect {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Rect({}, {})", self.tl, self.br)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Xform(SMatrix<f64, 3, 3>);
|
||||
|
||||
impl Xform {
|
||||
pub fn new(xx: f64, xy: f64, yx: f64, yy: f64, tx: f64, ty: f64) -> Xform {
|
||||
Xform(SMatrix::<f64, 3, 3>::new(|pos| match pos {
|
||||
(0, 0) => xx,
|
||||
(0, 1) => yx,
|
||||
(1, 0) => xy,
|
||||
(1, 1) => yy,
|
||||
(2, 0) => tx,
|
||||
(2, 1) => ty,
|
||||
(2, 2) => 1f64,
|
||||
_ => 0f64,
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn rot(alpha: f64) -> Xform {
|
||||
let sa = alpha.sin();
|
||||
let ca = alpha.cos();
|
||||
Xform(SMatrix::<f64, 3, 3>::new(|position| match position {
|
||||
(0, 0) => ca,
|
||||
(1, 1) => ca,
|
||||
(1, 0) => -sa,
|
||||
(0, 1) => sa,
|
||||
(2, 2) => 1f64,
|
||||
_ => 0f64,
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn scale(x: f64, y: f64) -> Xform {
|
||||
Xform(SMatrix::<f64, 3, 3>::new(|position| match position {
|
||||
(0, 0) => x,
|
||||
(1, 1) => y,
|
||||
(2, 2) => 1f64,
|
||||
_ => 0f64,
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn xlate(x: f64, y: f64) -> Xform {
|
||||
Xform(SMatrix::<f64, 3, 3>::new(|position| match position {
|
||||
(0, 0) => 1f64,
|
||||
(1, 1) => 1f64,
|
||||
(2, 2) => 1f64,
|
||||
(2, 0) => x,
|
||||
(2, 1) => y,
|
||||
_ => 0f64,
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn xx(&self) -> f64 {
|
||||
self.0[(0, 0)]
|
||||
}
|
||||
|
||||
pub fn xy(&self) -> f64 {
|
||||
self.0[(1, 0)]
|
||||
}
|
||||
|
||||
pub fn tx(&self) -> f64 {
|
||||
self.0[(2, 0)]
|
||||
}
|
||||
|
||||
pub fn yx(&self) -> f64 {
|
||||
self.0[(1, 0)]
|
||||
}
|
||||
|
||||
pub fn yy(&self) -> f64 {
|
||||
self.0[(1, 1)]
|
||||
}
|
||||
|
||||
pub fn ty(&self) -> f64 {
|
||||
self.0[(2, 1)]
|
||||
}
|
||||
|
||||
pub fn invert(&self) -> Xform {
|
||||
Xform(self.0.clone().invert())
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Xform {
|
||||
fn default() -> Self {
|
||||
Xform(SMatrix::<f64, 3, 3>::identity(3))
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<&Xform> for Point {
|
||||
fn mul(self, rhs: &Xform) -> Self::Output {
|
||||
Point::new(
|
||||
self.x * rhs.0[(0, 0)] + self.y * rhs.0[(1, 0)] + rhs.0[(2, 0)],
|
||||
self.x * rhs.0[(0, 1)] + self.y * rhs.0[(1, 1)] + rhs.0[(2, 1)],
|
||||
)
|
||||
}
|
||||
|
||||
type Output = Point;
|
||||
}
|
||||
|
||||
impl Mul<Xform> for Xform {
|
||||
type Output = Xform;
|
||||
|
||||
fn mul(self, rhs: Xform) -> Self::Output {
|
||||
Xform(self.0 * rhs.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<&Xform> for &Xform {
|
||||
type Output = Xform;
|
||||
|
||||
fn mul(self, rhs: &Xform) -> Self::Output {
|
||||
Xform(&self.0 * &rhs.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Xform {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
5
rdraught-ui-common/src/lib.rs
Normal file
5
rdraught-ui-common/src/lib.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
mod geo2d;
|
||||
mod types;
|
||||
|
||||
pub use geo2d::{Point, Rect, Xform};
|
||||
pub use types::{SharedMutable, SharedMutableRef, new_shared_mut, new_shared_mut_ref};
|
12
rdraught-ui-common/src/types.rs
Normal file
12
rdraught-ui-common/src/types.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::rc::Rc;
|
||||
|
||||
pub type SharedMutable<T> = Rc<Cell<T>>;
|
||||
pub type SharedMutableRef<T> = Rc<RefCell<T>>;
|
||||
pub fn new_shared_mut_ref<T>(obj: T) -> SharedMutableRef<T> {
|
||||
Rc::new(RefCell::new(obj))
|
||||
}
|
||||
|
||||
pub fn new_shared_mut<T>(obj: T) -> SharedMutable<T> {
|
||||
Rc::new(Cell::new(obj))
|
||||
}
|
Reference in New Issue
Block a user