-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Dimitris Zervas <[email protected]>
- Loading branch information
Showing
5 changed files
with
190 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use std::cell::RefCell; | ||
use std::fmt::Debug; | ||
use std::rc::Rc; | ||
|
||
use isotope::primitives::PrimitiveCell; | ||
use serde::{Deserialize, Serialize}; | ||
use tsify_next::Tsify; | ||
|
||
use crate::message::{Identifiable, MessageHandler}; | ||
use crate::IDType; | ||
|
||
use super::{compound_rectangle, ISketch}; | ||
|
||
pub trait CompoundLike: Debug { | ||
fn references(&self) -> Vec<PrimitiveCell>; | ||
fn created_references(&self) -> Vec<PrimitiveCell>; | ||
fn populate_created_references(&self, sketch: &mut isotope::sketch::Sketch) -> anyhow::Result<()> { | ||
for reference in self.created_references() { | ||
sketch.add_primitive(reference)?; | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Tsify, Debug, Serialize, Deserialize, Clone)] | ||
#[tsify(into_wasm_abi, from_wasm_abi)] | ||
#[non_exhaustive] | ||
pub enum Compound { | ||
Rectangle(compound_rectangle::Rectangle), | ||
} | ||
|
||
impl Identifiable for Rc<RefCell<Compound>> { | ||
type Parent = Rc<RefCell<ISketch>>; | ||
const ID_NAME: &'static str = "compound_id"; | ||
|
||
fn from_parent_id(parent: &Self::Parent, id: IDType) -> anyhow::Result<Self> { | ||
Ok(parent.borrow().compounds.get(&id).ok_or(anyhow::anyhow!("No feature with ID {} was found", id))?.clone()) | ||
} | ||
} | ||
|
||
impl Compound { | ||
pub fn as_compound_like(&self) -> &dyn CompoundLike { | ||
match self { | ||
Compound::Rectangle(rectangle) => rectangle, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Tsify, Debug, Clone, Serialize, Deserialize)] | ||
#[tsify(from_wasm_abi, into_wasm_abi)] | ||
pub struct DeleteCompound { | ||
id: IDType, | ||
} | ||
|
||
impl MessageHandler for DeleteCompound { | ||
type Parent = Rc<RefCell<ISketch>>; | ||
fn handle_message(&self, sketch_ref: Rc<RefCell<ISketch>>) -> anyhow::Result<Option<IDType>> { | ||
let mut isketch = sketch_ref.borrow_mut(); | ||
let mut sketch = isketch.sketch.borrow_mut(); | ||
let compound = isketch.compounds.get(&self.id).ok_or(anyhow::anyhow!("No compound with ID {} was found", self.id))?; | ||
|
||
for reference in compound.borrow().as_compound_like().created_references() { | ||
let id = sketch.get_primitive_id(&reference).ok_or(anyhow::anyhow!("Failed to find primitive with reference {:?}", reference))?; | ||
sketch.delete_primitive(id)?; | ||
} | ||
drop(sketch); | ||
|
||
isketch.compounds.remove(&self.id); | ||
Ok(None) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use std::cell::RefCell; | ||
use std::rc::Rc; | ||
|
||
use isotope::primitives::line::Line; | ||
use isotope::primitives::point2::Point2; | ||
use isotope::primitives::PrimitiveCell; | ||
use serde::{Deserialize, Serialize}; | ||
use tsify_next::Tsify; | ||
|
||
use crate::message::MessageHandler; | ||
use crate::IDType; | ||
|
||
use super::compound::{Compound, CompoundLike}; | ||
use super::ISketch; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone)] | ||
pub struct Rectangle { | ||
start: Rc<RefCell<Point2>>, | ||
end: Rc<RefCell<Point2>>, | ||
new_points: [Rc<RefCell<Point2>>; 2], | ||
new_lines: [Rc<RefCell<Line>>; 4], | ||
} | ||
|
||
impl Rectangle { | ||
pub fn new(start: Rc<RefCell<Point2>>, end: Rc<RefCell<Point2>>) -> Self { | ||
let start_point = start.borrow().clone(); | ||
let end_point = end.borrow().clone(); | ||
|
||
let other_start = Rc::new(RefCell::new(Point2::new(start_point.x(), end_point.y()))); | ||
let other_end = Rc::new(RefCell::new(Point2::new(end_point.x(), start_point.y()))); | ||
|
||
let new_lines = [ | ||
Rc::new(RefCell::new(Line::new(start.clone(), other_start.clone()))), | ||
Rc::new(RefCell::new(Line::new(other_start.clone(), end.clone()))), | ||
Rc::new(RefCell::new(Line::new(end.clone(), other_end.clone()))), | ||
Rc::new(RefCell::new(Line::new(other_end.clone(), start.clone()))), | ||
]; | ||
Self { | ||
start, | ||
end, | ||
new_points: [other_start, other_end], | ||
new_lines, | ||
} | ||
} | ||
|
||
} | ||
|
||
impl CompoundLike for Rectangle { | ||
fn references(&self) -> Vec<PrimitiveCell> { | ||
vec![ | ||
PrimitiveCell::Point2(self.start.clone()), | ||
PrimitiveCell::Point2(self.end.clone()), | ||
] | ||
} | ||
|
||
fn created_references(&self) -> Vec<PrimitiveCell> { | ||
vec![ | ||
PrimitiveCell::Point2(self.new_points[0].clone()), | ||
PrimitiveCell::Point2(self.new_points[1].clone()), | ||
PrimitiveCell::Line(self.new_lines[0].clone()), | ||
PrimitiveCell::Line(self.new_lines[1].clone()), | ||
PrimitiveCell::Line(self.new_lines[2].clone()), | ||
PrimitiveCell::Line(self.new_lines[3].clone()), | ||
] | ||
} | ||
} | ||
|
||
#[derive(Tsify, Debug, Clone, Serialize, Deserialize)] | ||
#[tsify(from_wasm_abi, into_wasm_abi)] | ||
pub struct Add { | ||
pub start: IDType, | ||
pub end: IDType, | ||
} | ||
|
||
impl MessageHandler for Add { | ||
type Parent = Rc<RefCell<ISketch>>; | ||
fn handle_message(&self, sketch_ref: Rc<RefCell<ISketch>>) -> anyhow::Result<Option<IDType>> { | ||
let mut isketch = sketch_ref.borrow_mut(); | ||
let mut sketch = isketch.sketch.borrow_mut(); | ||
|
||
let start_point = if let PrimitiveCell::Point2(point) = sketch.get_primitive_by_id(self.start).unwrap() { | ||
point | ||
} else { | ||
return Err(anyhow::anyhow!("Start point is not a point")); | ||
}; | ||
let end_point = if let PrimitiveCell::Point2(point) = sketch.get_primitive_by_id(self.end).unwrap() { | ||
point | ||
} else { | ||
return Err(anyhow::anyhow!("End point is not a point")); | ||
}; | ||
|
||
let rectangle = Rectangle::new(start_point.clone(), end_point.clone()); | ||
rectangle.populate_created_references(&mut sketch)?; | ||
drop(sketch); | ||
|
||
let point_id = isketch.compounds_next_id; | ||
isketch.compounds.insert(point_id, Rc::new(RefCell::new(Compound::Rectangle(rectangle)))); | ||
isketch.compounds_next_id += 1; | ||
Ok(Some(point_id))} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters