Skip to content

Commit f0075a6

Browse files
author
Tom
committed
Add metafield for features
1 parent 3c19238 commit f0075a6

File tree

6 files changed

+53
-30
lines changed

6 files changed

+53
-30
lines changed

detailer/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ impl<'a> Widget<'a> {
7272
for k in selected {
7373
if let Some(v) = self.drawing.features.get_mut(k) {
7474
match v {
75-
Feature::Point(x, y) => {
75+
Feature::Point(_, x, y) => {
7676
Widget::show_selection_entry_point(ui, &mut commands, &k, x, y)
7777
}
78-
Feature::LineSegment(p1, p2) => {
78+
Feature::LineSegment(_, p1, p2) => {
7979
Widget::show_selection_entry_line(ui, &mut commands, &k)
8080
}
8181
}

drawing/src/feature.rs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,37 +39,53 @@ impl LineSegment {
3939
}
4040
}
4141

42-
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize, PartialEq)]
42+
#[derive(Debug, Clone, Default, serde::Deserialize, serde::Serialize, PartialEq)]
43+
pub struct FeatureMeta {}
44+
45+
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
4346
pub enum Feature {
44-
Point(f32, f32),
45-
LineSegment(K, K),
47+
Point(FeatureMeta, f32, f32),
48+
LineSegment(FeatureMeta, K, K),
4649
}
4750

4851
impl Default for Feature {
4952
fn default() -> Self {
50-
Feature::Point(0., 0.)
53+
Feature::Point(FeatureMeta::default(), 0., 0.)
54+
}
55+
}
56+
57+
impl PartialEq<Feature> for Feature {
58+
fn eq(&self, other: &Feature) -> bool {
59+
use Feature::{LineSegment, Point};
60+
match (self, other) {
61+
(Point(_, x1, y1), Point(_, x2, y2)) => x1 == x2 && y1 == y2,
62+
(LineSegment(_, p00, p01), LineSegment(_, p10, p11)) => {
63+
(p00 == p10 && p01 == p11) || (p00 == p11 && p01 == p10)
64+
}
65+
_ => false,
66+
}
5167
}
5268
}
5369

5470
impl Feature {
5571
pub fn is_point(&self) -> bool {
56-
matches!(self, Feature::Point(_, _))
72+
matches!(self, Feature::Point(_, _, _))
5773
}
5874

5975
pub fn depends_on(&self) -> [Option<K>; 2] {
6076
match self {
61-
Feature::Point(_, _) => [None, None],
62-
Feature::LineSegment(p1, p2) => [Some(*p1), Some(*p2)],
77+
Feature::Point(_, _, _) => [None, None],
78+
Feature::LineSegment(_, p1, p2) => [Some(*p1), Some(*p2)],
6379
}
6480
}
6581

6682
pub fn bb(&self, drawing: &Data) -> egui::Rect {
6783
match self {
68-
Feature::Point(x, y) => egui::Rect {
84+
Feature::Point(_, x, y) => egui::Rect {
6985
min: egui::Pos2 { x: *x, y: *y },
7086
max: egui::Pos2 { x: *x, y: *y },
7187
},
72-
Feature::LineSegment(p1, p2) => {
88+
Feature::LineSegment(_, p1, p2) => {
7389
let (p1, p2) = (
7490
drawing.features.get(*p1).unwrap(),
7591
drawing.features.get(*p2).unwrap(),
@@ -82,17 +98,17 @@ impl Feature {
8298

8399
pub fn screen_dist(&self, drawing: &Data, hp: egui::Pos2, vp: &Viewport) -> f32 {
84100
match self {
85-
Feature::Point(x, y) => vp
101+
Feature::Point(_, x, y) => vp
86102
.translate_point(egui::Pos2 { x: *x, y: *y })
87103
.distance_sq(hp),
88104

89-
Feature::LineSegment(p1, p2) => {
105+
Feature::LineSegment(_, p1, p2) => {
90106
let (f1, f2) = (
91107
drawing.features.get(*p1).unwrap(),
92108
drawing.features.get(*p2).unwrap(),
93109
);
94110
let (p1, p2) = match (f1, f2) {
95-
(Feature::Point(x1, y1), Feature::Point(x2, y2)) => (
111+
(Feature::Point(_, x1, y1), Feature::Point(_, x2, y2)) => (
96112
vp.translate_point(egui::Pos2 { x: *x1, y: *y1 }),
97113
vp.translate_point(egui::Pos2 { x: *x2, y: *y2 }),
98114
),
@@ -112,7 +128,7 @@ impl Feature {
112128
painter: &egui::Painter,
113129
) {
114130
match self {
115-
Feature::Point(_, _) => {
131+
Feature::Point(_, _, _) => {
116132
painter.rect_filled(
117133
params
118134
.vp
@@ -131,13 +147,13 @@ impl Feature {
131147
);
132148
}
133149

134-
Feature::LineSegment(p1, p2) => {
150+
Feature::LineSegment(_, p1, p2) => {
135151
let (f1, f2) = (
136152
drawing.features.get(*p1).unwrap(),
137153
drawing.features.get(*p2).unwrap(),
138154
);
139155
let (p1, p2) = match (f1, f2) {
140-
(Feature::Point(x1, y1), Feature::Point(x2, y2)) => (
156+
(Feature::Point(_, x1, y1), Feature::Point(_, x2, y2)) => (
141157
params.vp.translate_point(egui::Pos2 { x: *x1, y: *y1 }),
142158
params.vp.translate_point(egui::Pos2 { x: *x2, y: *y2 }),
143159
),

drawing/src/handler.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{Data, Feature};
1+
use super::{Data, Feature, FeatureMeta};
22
use crate::tools::ToolResponse;
33

44
#[derive(Debug, Default)]
@@ -10,7 +10,7 @@ impl Handler {
1010
ToolResponse::Handled => {}
1111
ToolResponse::NewPoint(pos) => {
1212
let pos = drawing.vp.screen_to_point(pos);
13-
let p = Feature::Point(pos.x, pos.y);
13+
let p = Feature::Point(FeatureMeta::default(), pos.x, pos.y);
1414

1515
// Make sure it doesnt already exist
1616
for v in drawing.features.values() {
@@ -28,15 +28,16 @@ impl Handler {
2828
drawing.find_point_at(p1).unwrap(),
2929
drawing.find_point_at(p2).unwrap(),
3030
);
31+
let l = Feature::LineSegment(FeatureMeta::default(), f2, f1);
3132

3233
// Make sure it doesnt already exist
3334
for v in drawing.features.values() {
34-
if v == &Feature::LineSegment(f1, f2) || v == &Feature::LineSegment(f2, f1) {
35+
if v == &l {
3536
return;
3637
}
3738
}
3839

39-
drawing.features.insert(Feature::LineSegment(f1, f2));
40+
drawing.features.insert(l);
4041
}
4142

4243
ToolResponse::Delete(k) => {

drawing/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
mod data;
44
pub use data::{Data, Viewport};
55
mod feature;
6-
pub use feature::Feature;
6+
pub use feature::{Feature, FeatureMeta};
77
mod handler;
88
pub use handler::Handler;
99
pub mod tools;

drawing/src/tools.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ impl Tool {
127127
Tool::Line(p1) => {
128128
let c = match (hf, &p1, response.clicked()) {
129129
// No first point, clicked on a point
130-
(Some((_, crate::Feature::Point(x, y))), None, true) => {
130+
(Some((_, crate::Feature::Point(_, x, y))), None, true) => {
131131
*p1 = Some(egui::Pos2 { x: *x, y: *y });
132132
Some(ToolResponse::Handled)
133133
}
134134
// Has first point, clicked on a point
135-
(Some((_, crate::Feature::Point(x2, y2))), Some(starting_point), true) => {
135+
(Some((_, crate::Feature::Point(_, x2, y2))), Some(starting_point), true) => {
136136
let starting_point = starting_point.clone();
137137
*p1 = Some(egui::Pos2 { x: *x2, y: *y2 });
138138
Some(ToolResponse::NewLineSegment(

liquid-cad/src/app.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use detailer;
2-
use drawing;
2+
use drawing::{self, Feature, FeatureMeta};
33

44
#[derive(serde::Deserialize, serde::Serialize)]
55
#[serde(default)] // if we add new fields, give them default values when deserializing old state
@@ -22,15 +22,21 @@ impl Default for App {
2222
// drawing.features.insert(drawing::Feature::Point(188., 188.));
2323
// drawing.features.insert(drawing::Feature::Point(-88., 188.));
2424

25-
let p1 = drawing.features.insert(drawing::Feature::Point(-88., 0.));
26-
let p2 = drawing.features.insert(drawing::Feature::Point(0., 0.));
27-
let p3 = drawing.features.insert(drawing::Feature::Point(88., 0.));
25+
let p1 = drawing
26+
.features
27+
.insert(Feature::Point(FeatureMeta::default(), -88., 0.));
28+
let p2 = drawing
29+
.features
30+
.insert(Feature::Point(FeatureMeta::default(), 0., 0.));
31+
let p3 = drawing
32+
.features
33+
.insert(Feature::Point(FeatureMeta::default(), 88., 0.));
2834
drawing
2935
.features
30-
.insert(drawing::Feature::LineSegment(p1, p2));
36+
.insert(Feature::LineSegment(FeatureMeta::default(), p1, p2));
3137
drawing
3238
.features
33-
.insert(drawing::Feature::LineSegment(p2, p3));
39+
.insert(Feature::LineSegment(FeatureMeta::default(), p2, p3));
3440

3541
let tools = drawing::tools::Toolbar::default();
3642
let handler = drawing::Handler::default();

0 commit comments

Comments
 (0)