use super::{
dependents::moved_tiers,
drag::{DragStart, DragValue, SnapMode},
handles::HandleKind,
projection::ScreenPoint,
provisional::Snapshot,
target::HandleTarget,
text,
};
use crate::session::EditorSession;
use indicatrix::geometry::meet_solver::{MeetConstraint, SolvedTier};
use indicatrix_cut_core::{ConstraintTier, EditError};
use std::time::Duration;
pub const MOVED_TOLERANCE: f64 = 1e-6;
#[must_use]
pub const fn kind_from_int(kind: i32) -> Option<HandleKind> {
match kind {
0 => Some(HandleKind::Angle),
1 => Some(HandleKind::Depth),
2 => Some(HandleKind::Index),
_ => None,
}
}
#[must_use]
pub const fn kind_to_int(kind: HandleKind) -> i32 {
match kind {
HandleKind::Angle => 0,
HandleKind::Depth => 1,
HandleKind::Index => 2,
}
}
#[must_use]
pub const fn snap_mode(shift: bool, snap_off: bool) -> SnapMode {
if snap_off {
SnapMode::Off
} else if shift {
SnapMode::Fine
} else {
SnapMode::Coarse
}
}
#[must_use]
pub fn tier_label(tier: &ConstraintTier, index: usize) -> String {
if tier.name.is_empty() {
format!("tier {}", index + 1)
} else {
tier.name.clone()
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Step {
Angle(f64),
Mast(f64),
Teeth(i64),
}
#[derive(Debug, Clone, PartialEq)]
pub struct AppliedEdit {
pub generation: u64,
pub replaced_meet: Option<MeetConstraint>,
}
#[derive(Debug, Default)]
pub struct DragProgress {
pub last_value: Option<DragValue>,
pub teeth_applied: i64,
pub applied_any: bool,
pub applied_generation: Option<u64>,
pub replaced_meet: Option<MeetConstraint>,
}
impl DragProgress {
pub fn plan(&mut self, value: DragValue) -> Option<Step> {
if self.last_value == Some(value) {
return None;
}
self.last_value = Some(value);
match value {
DragValue::AngleDeg(deg) => Some(Step::Angle(deg)),
DragValue::Mast(mast) => Some(Step::Mast(mast)),
DragValue::IndexTeeth(total) => {
let more = total - self.teeth_applied;
(more != 0).then_some(Step::Teeth(more))
}
}
}
pub fn record(&mut self, step: Step, applied: AppliedEdit) {
self.applied_any = true;
self.applied_generation = Some(applied.generation);
if let Step::Teeth(more) = step {
self.teeth_applied += more;
}
if self.replaced_meet.is_none() {
self.replaced_meet = applied.replaced_meet;
}
}
}
pub fn drain_value(
progress: &mut DragProgress,
value: DragValue,
apply: impl FnOnce(Step) -> Option<AppliedEdit>,
) -> bool {
let Some(step) = progress.plan(value) else {
return false;
};
let Some(applied) = apply(step) else {
return false;
};
progress.record(step, applied);
true
}
pub fn apply_step(
session: &mut EditorSession,
tier: usize,
step: Step,
now: Duration,
) -> Result<Option<AppliedEdit>, EditError> {
Ok(match step {
Step::Angle(deg) => session
.set_tier_angle(tier, deg, now)?
.map(|change| AppliedEdit {
generation: change.generation,
replaced_meet: None,
}),
Step::Mast(mast) => session
.pin_tier_mast(tier, mast, now)?
.map(|pin| AppliedEdit {
generation: pin.change.generation,
replaced_meet: pin.replaced_meet,
}),
Step::Teeth(more) => {
session
.rotate_tier_indices(tier, more, now)?
.map(|change| AppliedEdit {
generation: change.generation,
replaced_meet: None,
})
}
})
}
#[must_use]
pub const fn start_value(kind: HandleKind, start_angle_deg: f64, start_mast: f64) -> DragValue {
match kind {
HandleKind::Angle => DragValue::AngleDeg(start_angle_deg),
HandleKind::Depth => DragValue::Mast(start_mast),
HandleKind::Index => DragValue::IndexTeeth(0),
}
}
#[derive(Debug, Clone)]
pub struct GestureInputs {
pub start_angle_deg: f64,
pub masts: Option<Vec<SolvedTier>>,
pub restore: Option<Snapshot>,
}
#[derive(Debug)]
pub struct ActiveDrag {
pub start: DragStart,
pub tier: usize,
pub label: String,
pub start_masts: Vec<SolvedTier>,
pub progress: DragProgress,
pub requested: Option<DragValue>,
pub outlined: Vec<usize>,
pub gesture_now: Duration,
pub provisional: bool,
pub restore: Option<Snapshot>,
}
impl ActiveDrag {
#[must_use]
pub fn begin(
kind: HandleKind,
target: &HandleTarget,
inputs: GestureInputs,
pointer: ScreenPoint,
gesture_now: Duration,
) -> Option<Self> {
let GestureInputs {
start_angle_deg,
masts,
restore,
} = inputs;
if kind == HandleKind::Depth && masts.is_none() {
return None;
}
let start_masts = masts.unwrap_or_default();
let start_mast = start_masts.get(target.tier).map_or(0.0, |s| s.mast);
Some(Self {
start: DragStart {
kind,
start_angle_deg,
start_mast,
pointer,
layout: target.layout,
},
tier: target.tier,
label: target.label.clone(),
start_masts,
progress: DragProgress::default(),
requested: None,
outlined: Vec::new(),
gesture_now,
provisional: target.provisional,
restore,
})
}
#[must_use]
pub fn opening_hint(&self, provisional_hint: Option<String>) -> String {
if self.provisional {
return provisional_hint.unwrap_or_default();
}
let value = start_value(
self.start.kind,
self.start.start_angle_deg,
self.start.start_mast,
);
text::drag_live_hint(self.start.kind, &self.label, &value, 0)
}
#[must_use]
pub fn live_value(&self) -> DragValue {
let untouched = start_value(
self.start.kind,
self.start.start_angle_deg,
self.start.start_mast,
);
self.requested
.or(self.progress.last_value)
.unwrap_or(untouched)
}
#[must_use]
pub fn final_value(&self) -> DragValue {
match self.start.kind {
HandleKind::Index => DragValue::IndexTeeth(self.progress.teeth_applied),
_ => self.live_value(),
}
}
#[must_use]
pub fn live_feedback(&self, now_masts: Option<&[SolvedTier]>) -> (String, Vec<usize>) {
let moved = now_masts.map_or_else(Vec::new, |now| {
moved_tiers(&self.start_masts, now, self.tier, MOVED_TOLERANCE)
});
let hint = text::drag_live_hint(
self.start.kind,
&self.label,
&self.live_value(),
moved.len(),
);
(hint, moved)
}
#[must_use]
pub fn done_toast(&self) -> Option<String> {
(self.progress.applied_any && !self.provisional).then(|| {
text::drag_done_toast(
self.start.kind,
&self.label,
&self.final_value(),
self.progress.replaced_meet.as_ref(),
)
})
}
}