Add a pivot widget to selected layer(s) to control the origin(s) (#772)

* Add pivot

* Add dragging pivot

* Cleanup

* Remove tabs

* Fix multiplication order

* Restyle pivot

* Add move cursor icon

* Update pivot size

* Code review tweaks

* Fix alt with non-centred pivot

* Comment for add one

* Pivot sets layer panel origin

* Tweek alt centre thing

* Fix division by zero case

* Add pivot dots to properties

* FIx some typos

Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
0HyperCube
2022-08-30 22:36:33 +01:00
committed by Keavon Chambers
co-authored by Keavon Chambers
parent 0f6f3be6e7
commit 1e109dc552
13 changed files with 360 additions and 37 deletions
@@ -1,4 +1,5 @@
use derivative::*;
use glam::DVec2;
use serde::{Deserialize, Serialize};
use crate::messages::layout::utility_types::layout_widget::WidgetCallback;
@@ -14,7 +15,7 @@ pub struct PivotAssist {
pub on_update: WidgetCallback<PivotAssist>,
}
#[derive(Clone, Serialize, Deserialize, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Copy, Serialize, Deserialize, Debug, Default, PartialEq, Eq)]
pub enum PivotPosition {
#[default]
None,
@@ -46,3 +47,52 @@ impl From<&str> for PivotPosition {
}
}
}
impl From<PivotPosition> for Option<DVec2> {
fn from(input: PivotPosition) -> Self {
match input {
PivotPosition::None => None,
PivotPosition::TopLeft => Some(DVec2::new(0., 0.)),
PivotPosition::TopCenter => Some(DVec2::new(0.5, 0.)),
PivotPosition::TopRight => Some(DVec2::new(1., 0.)),
PivotPosition::CenterLeft => Some(DVec2::new(0., 0.5)),
PivotPosition::Center => Some(DVec2::new(0.5, 0.5)),
PivotPosition::CenterRight => Some(DVec2::new(1., 0.5)),
PivotPosition::BottomLeft => Some(DVec2::new(0., 1.)),
PivotPosition::BottomCenter => Some(DVec2::new(0.5, 1.)),
PivotPosition::BottomRight => Some(DVec2::new(1., 1.)),
}
}
}
impl From<DVec2> for PivotPosition {
fn from(input: DVec2) -> Self {
const TOLERANCE: f64 = 1e-5_f64;
if input.y.abs() < TOLERANCE {
if input.x.abs() < TOLERANCE {
return PivotPosition::TopLeft;
} else if (input.x - 0.5).abs() < TOLERANCE {
return PivotPosition::TopCenter;
} else if (input.x - 1.).abs() < TOLERANCE {
return PivotPosition::TopRight;
}
} else if (input.y - 0.5).abs() < TOLERANCE {
if input.x.abs() < TOLERANCE {
return PivotPosition::CenterLeft;
} else if (input.x - 0.5).abs() < TOLERANCE {
return PivotPosition::Center;
} else if (input.x - 1.).abs() < TOLERANCE {
return PivotPosition::CenterRight;
}
} else if (input.y - 1.).abs() < TOLERANCE {
if input.x.abs() < TOLERANCE {
return PivotPosition::BottomLeft;
} else if (input.x - 0.5).abs() < TOLERANCE {
return PivotPosition::BottomCenter;
} else if (input.x - 1.).abs() < TOLERANCE {
return PivotPosition::BottomRight;
}
}
PivotPosition::None
}
}