Skip to content

Commit

Permalink
Respect smooth property and allow it to be editable (#395)
Browse files Browse the repository at this point in the history
* Respect smooth property and allow it to be editable

* Update lock file
  • Loading branch information
Subject38 committed Mar 26, 2024
1 parent 49d330e commit 8e73c49
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 15 additions & 13 deletions src/tool_behaviors/move_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub use crate::user_interface::follow::Follow;
use glifparser::PointData;
use glifparser::glif::contour::MFEKContourCommon;
use glifparser::glif::point::MFEKPointCommon;
use MFEKmath::polar::PolarCoordinates;

#[derive(Clone, Debug)]
pub struct MoveHandle {
Expand Down Expand Up @@ -54,12 +55,21 @@ impl MoveHandle {
// Difference in x, difference in y
let (dx, dy) = (cx - x, cy - y);

let follow = if self.creating {
let mut follow = if self.creating {
Follow::Mirror
} else {
Follow::from(self.mouse_info)
};

if point.get_smooth() == Some(true) {
// Follow::Mirror breaks smoothness
follow = Follow::ForceLine;
}

if point.get_handle(self.wh.opposite()).unwrap() == Handle::Colocated && follow == Follow::ForceLine {
// If the opposite handle is colocated, force line crashes due to unwrapping a None value.
follow = Follow::No;
}
match self.wh {
WhichHandle::A | WhichHandle::B => {
point.set_handle(self.wh, Handle::At(x, y));
Expand Down Expand Up @@ -94,21 +104,13 @@ impl MoveHandle {
}
}

fn force_line<PD: PointData>(&mut self, _point: &mut dyn MFEKPointCommon<PD>) {
// TODO: Fix this? I have zero clue why this errors. It's forcing point to `static.
/*let (r, _) = point.polar(self.wh.opposite());
fn force_line<PD: PointData>(&self, mut point: &mut dyn MFEKPointCommon<PD>) {
let (r, _) = point.polar(self.wh.opposite());
let (_, theta) = point.polar(self.wh);
match point.get_handle(self.wh.opposite()).unwrap() {
Handle::At(..) => point.set_polar(self.wh.opposite(), (r, theta.to_degrees())),
Handle::Colocated => {
if !self.warned_force_line {
log::warn!(
"Makes no sense to force line when opposite handle is colocated, ignoring"
);
}
self.warned_force_line = true;
}
}*/
Handle::Colocated => unreachable!("Force line is nonsensical when the opposite handle is colocated")
}
}
}

Expand Down
39 changes: 36 additions & 3 deletions src/user_interface/gui/windows/inspection_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ use crate::{
use egui::Context;
use glifparser::{
glif::{contour::MFEKContourCommon, inner::MFEKContourInnerType, point::MFEKPointCommon},
Handle, PointData, WhichHandle,
Contour, Handle, MFEKPointData, PointData, WhichHandle,
};
use MFEKmath::mfek::ResolveCubic;
use MFEKmath::polar::PolarCoordinates;

use super::egui_parsed_textfield;

Expand Down Expand Up @@ -50,7 +51,7 @@ impl GlifWindow for InspectionWindow {
.show(ctx, |ui| {
if let Some((ci, pi)) = v.selected_point() {
let mut contour = v.get_active_layer_ref().outline[ci].clone();
let point = contour
let mut point = contour
.get_point_mut(pi)
.expect("Editor should have valid selection!");
// do contour stuff
Expand Down Expand Up @@ -114,7 +115,34 @@ impl GlifWindow for InspectionWindow {
egui_parsed_textfield(ui, "px", point.x(), &mut self.edit_buf),
egui_parsed_textfield(ui, "py", point.y(), &mut self.edit_buf),
);

if let Some(smooth) = point.get_smooth() {
let mut checked = smooth;
let smooth_can_be_enabled = point.get_handle(WhichHandle::A).unwrap()
!= Handle::Colocated
&& point.get_handle(WhichHandle::B).unwrap() != Handle::Colocated;
ui.add_enabled_ui(smooth_can_be_enabled, |ui| {
ui.checkbox(&mut checked, "Smooth").
on_disabled_hover_text("Cannot enable smoothness if one of the off curve handles is colocated with the point")
});
if smooth != checked {
point.set_smooth(checked);
// If smooth was enabled just now, we need to ensure the curve is actually smooth.
if checked {
// Force curve points into a line to make the curve smooth
let wh = WhichHandle::A;
let (r, _) = point.polar(wh.opposite());
let (_, theta) = point.polar(wh);
match point.get_handle(wh.opposite()).expect("Both handles should exist") {
Handle::At(..) => point.set_polar(wh.opposite(), (r, theta.to_degrees())),
Handle::Colocated => unreachable!("Handles not being colocated is required to reach this clause")
}
}
}
// If either handle is colocated, smooth *must* be disabled
if !smooth_can_be_enabled {
point.set_smooth(false);
}
}
ui.collapsing("Handles", |ui| {
if point.has_handle(WhichHandle::A) {
ui.label("Handle A:");
Expand Down Expand Up @@ -222,6 +250,10 @@ impl GlifWindow for InspectionWindow {
mutated_point.set_name(name);
}

if let Some(smooth) = point.get_smooth() {
mutated_point.set_smooth(smooth);
}

v.end_modification();
}
});
Expand All @@ -240,6 +272,7 @@ fn point_equivalent<PD: PointData>(
&& a.get_position() == b.get_position()
&& a.get_handle_position(WhichHandle::A) == b.get_handle_position(WhichHandle::A)
&& a.get_handle_position(WhichHandle::B) == b.get_handle_position(WhichHandle::B)
&& a.get_smooth() == b.get_smooth()
}

/*
Expand Down

0 comments on commit 8e73c49

Please sign in to comment.