Persist gravity points per layer in Midra projects
This commit is contained in:
parent
c64fbd58f4
commit
59a342cbc3
|
|
@ -65,9 +65,7 @@ pub struct Editor {
|
|||
resize_mode: ResizeMode,
|
||||
grid_tool: Option<u32>,
|
||||
pivot_tool: bool,
|
||||
pivot_relative: [f64; 2],
|
||||
pivot_selection: Vec<usize>,
|
||||
pivot_drag: Option<(usize, [f64; 2])>,
|
||||
pivot_drag: Option<(usize, Layout)>,
|
||||
format_draft: Option<FormatDraft>,
|
||||
property_tab: PropertyTab,
|
||||
output_draft: Option<OutputDraft>,
|
||||
|
|
@ -277,8 +275,6 @@ impl Default for Editor {
|
|||
resize_mode: ResizeMode::Individual,
|
||||
grid_tool: None,
|
||||
pivot_tool: false,
|
||||
pivot_relative: [0.5; 2],
|
||||
pivot_selection: Vec::new(),
|
||||
pivot_drag: None,
|
||||
format_draft: None,
|
||||
property_tab: PropertyTab::default(),
|
||||
|
|
@ -502,19 +498,6 @@ impl Editor {
|
|||
}
|
||||
}
|
||||
fn sync(&mut self) {
|
||||
let selection: Vec<_> = self
|
||||
.layout
|
||||
.selected_indices()
|
||||
.iter()
|
||||
.map(|&i| self.layout.layer_info(i).id)
|
||||
.collect();
|
||||
if selection != self.pivot_selection {
|
||||
if !self.layout.dragging() {
|
||||
self.pivot_relative = [0.5; 2];
|
||||
}
|
||||
self.pivot_drag = None;
|
||||
self.pivot_selection = selection;
|
||||
}
|
||||
let ratio_layer = self.layout.selected().map(|i| self.layout.layer_info(i).id);
|
||||
if self.ratio_layer != ratio_layer {
|
||||
self.ratio_layer = ratio_layer;
|
||||
|
|
@ -740,7 +723,7 @@ impl Editor {
|
|||
self.zoom_drag = None;
|
||||
self.panning = false;
|
||||
if let Some((_, before)) = self.pivot_drag.take() {
|
||||
self.pivot_relative = before;
|
||||
self.layout = before;
|
||||
}
|
||||
if let Some(m) = self.marquee.take() {
|
||||
self.layout.set_selection(m.previous);
|
||||
|
|
@ -776,6 +759,7 @@ impl Editor {
|
|||
|| self.marquee.is_some()
|
||||
|| self.panning
|
||||
|| self.zoom_drag.is_some()
|
||||
|| self.pivot_drag.is_some()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
|
@ -1864,19 +1848,7 @@ impl Editor {
|
|||
}
|
||||
|
||||
fn pivot_for(&self, i: usize) -> [f64; 2] {
|
||||
let mut relative = self.pivot_relative;
|
||||
if self.layout.selected_indices().first().copied() != Some(i) {
|
||||
if matches!(
|
||||
self.active_resize_mode(),
|
||||
ResizeMode::Mirror | ResizeMode::Opposite
|
||||
) {
|
||||
relative[0] = 1.0 - relative[0];
|
||||
}
|
||||
if self.active_resize_mode() == ResizeMode::Opposite {
|
||||
relative[1] = 1.0 - relative[1];
|
||||
}
|
||||
}
|
||||
relative
|
||||
self.layout.layer_info(i).pivot
|
||||
}
|
||||
fn pivot_hit(&self, pointer: Pos2, origin: Pos2, scale: f32) -> Option<usize> {
|
||||
self.layout.selected_indices().into_iter().rev().find(|&i| {
|
||||
|
|
@ -1914,20 +1886,36 @@ impl Editor {
|
|||
}
|
||||
fn move_pivot(&mut self, i: usize, point: [f64; 2]) {
|
||||
let g = self.layout.layers()[i];
|
||||
let mut relative = [(point[0] - g.x) / g.width, (point[1] - g.y) / g.height];
|
||||
if self.layout.selected_indices().first().copied() != Some(i) {
|
||||
if matches!(
|
||||
self.active_resize_mode(),
|
||||
ResizeMode::Mirror | ResizeMode::Opposite
|
||||
) {
|
||||
relative[0] = 1.0 - relative[0];
|
||||
}
|
||||
if self.active_resize_mode() == ResizeMode::Opposite {
|
||||
relative[1] = 1.0 - relative[1];
|
||||
}
|
||||
}
|
||||
if relative.iter().all(|v| v.is_finite() && v.abs() <= 1e6) {
|
||||
self.pivot_relative = relative;
|
||||
let relative = [(point[0] - g.x) / g.width, (point[1] - g.y) / g.height];
|
||||
let mode = self.active_resize_mode();
|
||||
let targets = if mode == ResizeMode::Individual {
|
||||
vec![i]
|
||||
} else {
|
||||
self.layout.selected_indices()
|
||||
};
|
||||
let pivots: Vec<_> = targets
|
||||
.into_iter()
|
||||
.map(|j| {
|
||||
let mut pivot = relative;
|
||||
if j != i {
|
||||
if matches!(mode, ResizeMode::Mirror | ResizeMode::Opposite) {
|
||||
pivot[0] = 1.0 - pivot[0];
|
||||
}
|
||||
if mode == ResizeMode::Opposite {
|
||||
pivot[1] = 1.0 - pivot[1];
|
||||
}
|
||||
}
|
||||
(j, pivot)
|
||||
})
|
||||
.collect();
|
||||
let mut next = self
|
||||
.pivot_drag
|
||||
.as_ref()
|
||||
.map_or(&self.layout, |(_, before)| before)
|
||||
.clone();
|
||||
match next.set_layer_pivots(&pivots) {
|
||||
Ok(()) => self.layout = next,
|
||||
Err(error) => self.error = error,
|
||||
}
|
||||
}
|
||||
fn resize_handle(
|
||||
|
|
@ -2514,7 +2502,7 @@ impl Editor {
|
|||
let point = [p.x as f64, p.y as f64];
|
||||
let shift = ui.input(|i| i.modifiers.shift);
|
||||
if let Some(i) = pivot {
|
||||
self.pivot_drag = Some((i, self.pivot_relative));
|
||||
self.pivot_drag = Some((i, self.layout.clone()));
|
||||
} else if self.edit_content {
|
||||
if let Some(corner) = crop_handle {
|
||||
if let Some(i) = self.layout.selected() {
|
||||
|
|
@ -2533,7 +2521,6 @@ impl Editor {
|
|||
) {
|
||||
self.layout.gesture_error = Some(e);
|
||||
}
|
||||
self.layout.set_resize_pivot(self.pivot_for(i));
|
||||
} else if self.layout.hit(point).is_none() {
|
||||
let previous = self.layout.selection_mask();
|
||||
let base = if shift {
|
||||
|
|
@ -2584,7 +2571,7 @@ impl Editor {
|
|||
} else if edit_pointer && let Some(p) = pointer {
|
||||
let p = (p - origin) / scale;
|
||||
let point = [p.x as f64, p.y as f64];
|
||||
if let Some((i, _)) = self.pivot_drag {
|
||||
if let Some(i) = self.pivot_drag.as_ref().map(|(i, _)| *i) {
|
||||
self.move_pivot_snapped(i, point, modifiers, scale);
|
||||
} else if let Some(m) = self.marquee.as_mut() {
|
||||
m.end = point;
|
||||
|
|
@ -2601,7 +2588,7 @@ impl Editor {
|
|||
{
|
||||
let p = (p - origin) / scale;
|
||||
let point = [p.x as f64, p.y as f64];
|
||||
if let Some((i, _)) = self.pivot_drag {
|
||||
if let Some(i) = self.pivot_drag.as_ref().map(|(i, _)| *i) {
|
||||
self.move_pivot_snapped(i, point, modifiers, scale);
|
||||
} else if let Some(m) = self.marquee.as_mut() {
|
||||
m.end = point;
|
||||
|
|
@ -2628,8 +2615,10 @@ impl Editor {
|
|||
{
|
||||
self.edit_content = false;
|
||||
}
|
||||
if response.double_clicked() && pivot_hover.is_some() {
|
||||
self.pivot_relative = [0.5; 2];
|
||||
if response.double_clicked()
|
||||
&& let Some(i) = pivot_hover
|
||||
{
|
||||
self.move_pivot(i, self.layout.layers()[i].center());
|
||||
}
|
||||
if response.clicked()
|
||||
&& edit_pointer
|
||||
|
|
@ -3825,16 +3814,16 @@ mod tests {
|
|||
vec![egui::Event::PointerMoved(start), mouse(start, true)],
|
||||
);
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(end)]);
|
||||
assert_eq!(app.pivot_relative, [0.0; 2]);
|
||||
assert_eq!(app.pivot_for(1), [0.0; 2]);
|
||||
frame(&mut app, &ctx, vec![mouse(end, false)]);
|
||||
assert_eq!(app.pivot_relative, [0.0; 2]);
|
||||
assert_eq!(app.pivot_for(1), [0.0; 2]);
|
||||
let end2 = rect.right_center() - Vec2::new(2.0, 1.0);
|
||||
frame(&mut app, &ctx, vec![mouse(end, true)]);
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(end2)]);
|
||||
assert_eq!(app.pivot_relative, [1.0, 0.5]);
|
||||
assert_eq!(app.pivot_for(1), [1.0, 0.5]);
|
||||
frame(&mut app, &ctx, vec![key(egui::Key::Escape)]);
|
||||
frame(&mut app, &ctx, vec![mouse(end2, false)]);
|
||||
assert_eq!(app.pivot_relative, [0.0; 2]);
|
||||
assert_eq!(app.pivot_for(1), [0.0; 2]);
|
||||
assert_eq!(app.layout.layers(), &before);
|
||||
}
|
||||
|
||||
|
|
@ -4378,7 +4367,13 @@ mod tests {
|
|||
app.layout.set_selection([true; 2]);
|
||||
app.sync();
|
||||
let before = app.layout.layers().clone();
|
||||
app.pivot_relative = [0.2, 0.7];
|
||||
app.move_pivot(
|
||||
0,
|
||||
[
|
||||
before[0].x + before[0].width * 0.2,
|
||||
before[0].y + before[0].height * 0.7,
|
||||
],
|
||||
);
|
||||
let other = app.pivot_for(1);
|
||||
assert_eq!(
|
||||
other,
|
||||
|
|
@ -4395,9 +4390,10 @@ mod tests {
|
|||
app.move_pivot(1, [g.x + g.width * 0.25, g.y + g.height * 0.75]);
|
||||
assert_eq!(app.pivot_for(1), [0.25, 0.75]);
|
||||
assert_eq!(app.layout.layers(), &before);
|
||||
let saved = [app.pivot_for(0), app.pivot_for(1)];
|
||||
app.layout.select(Some(0));
|
||||
app.sync();
|
||||
assert_eq!(app.pivot_relative, [0.5; 2]);
|
||||
assert_eq!([app.pivot_for(0), app.pivot_for(1)], saved);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -4423,9 +4419,9 @@ mod tests {
|
|||
frame_modifiers(&mut app, &ctx, ctrl, vec![egui::Event::PointerMoved(end)]);
|
||||
frame_modifiers(&mut app, &ctx, ctrl, vec![mouse(end, false)]);
|
||||
assert_eq!(app.layout.layers(), &before);
|
||||
assert!((app.pivot_relative[0] - 0.25).abs() < 1e-6);
|
||||
assert!((app.pivot_relative[1] - 0.25).abs() < 1e-6);
|
||||
let relative = app.pivot_relative;
|
||||
assert!((app.pivot_for(1)[0] - 0.25).abs() < 1e-6);
|
||||
assert!((app.pivot_for(1)[1] - 0.25).abs() < 1e-6);
|
||||
let relative = app.pivot_for(1);
|
||||
// Escape restores placement without adding a document history entry.
|
||||
let moved = end + Vec2::new(18.0, 12.0);
|
||||
frame_modifiers(
|
||||
|
|
@ -4435,10 +4431,10 @@ mod tests {
|
|||
vec![egui::Event::PointerMoved(end), mouse(end, true)],
|
||||
);
|
||||
frame_modifiers(&mut app, &ctx, ctrl, vec![egui::Event::PointerMoved(moved)]);
|
||||
assert_ne!(app.pivot_relative, relative);
|
||||
assert_ne!(app.pivot_for(1), relative);
|
||||
frame_modifiers(&mut app, &ctx, ctrl, vec![key(egui::Key::Escape)]);
|
||||
frame_modifiers(&mut app, &ctx, ctrl, vec![mouse(moved, false)]);
|
||||
assert_eq!(app.pivot_relative, relative);
|
||||
assert_eq!(app.pivot_for(1), relative);
|
||||
assert_eq!(app.layout.layers(), &before);
|
||||
|
||||
let fixed = [
|
||||
|
|
@ -4461,8 +4457,12 @@ mod tests {
|
|||
assert!((after.y + relative[1] * after.height - fixed[1]).abs() < 1e-6);
|
||||
app.layout.undo();
|
||||
assert_eq!(app.layout.layers(), &before);
|
||||
// Pivot placement is session UI state, not a document history step.
|
||||
assert_eq!(app.pivot_relative, relative);
|
||||
// Undoing resize retains the previously committed per-layer pivot.
|
||||
assert_eq!(app.pivot_for(1), relative);
|
||||
app.layout.undo();
|
||||
assert_eq!(app.pivot_for(1), [0.5; 2]);
|
||||
app.layout.redo();
|
||||
assert_eq!(app.pivot_for(1), relative);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -447,11 +447,20 @@ impl MidraLink {
|
|||
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct LayerInfo {
|
||||
pub pivot: [f64; 2],
|
||||
pub midra_link: Option<MidraLink>,
|
||||
pub id: usize,
|
||||
pub name: String,
|
||||
pub folder: String,
|
||||
}
|
||||
pub(crate) fn validate_pivot(pivot: [f64; 2]) -> Result<(), String> {
|
||||
if pivot.iter().any(|v| !v.is_finite() || v.abs() > 1e6) {
|
||||
return Err(
|
||||
"Layer pivot coordinates must be finite relative values within ±1,000,000.".into(),
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// Multi-layer document with one undo record per complete gesture/edit.
|
||||
#[derive(Clone)]
|
||||
pub struct Layout {
|
||||
|
|
@ -472,7 +481,7 @@ pub struct Layout {
|
|||
}
|
||||
#[derive(Clone)]
|
||||
struct Resize {
|
||||
pivot: [f64; 2],
|
||||
pivot: Option<[f64; 2]>,
|
||||
use_pivot: bool,
|
||||
mode: ResizeMode,
|
||||
targets: Vec<bool>,
|
||||
|
|
@ -496,8 +505,8 @@ impl Resize {
|
|||
fn anchor(&self, i: usize) -> [f64; 2] {
|
||||
let g = self.before.layers[i];
|
||||
if self.use_pivot {
|
||||
let mut relative = self.pivot;
|
||||
if i != self.index {
|
||||
let mut relative = self.pivot.unwrap_or(self.before.info[i].pivot);
|
||||
if self.pivot.is_some() && i != self.index {
|
||||
if matches!(self.mode, ResizeMode::Mirror | ResizeMode::Opposite) {
|
||||
relative[0] = 1.0 - relative[0];
|
||||
}
|
||||
|
|
@ -529,12 +538,14 @@ impl Default for Layout {
|
|||
Self {
|
||||
info: vec![
|
||||
LayerInfo {
|
||||
pivot: [0.5; 2],
|
||||
midra_link: None,
|
||||
id: 1,
|
||||
name: "Presenter".into(),
|
||||
folder: String::new(),
|
||||
},
|
||||
LayerInfo {
|
||||
pivot: [0.5; 2],
|
||||
midra_link: None,
|
||||
id: 2,
|
||||
name: "Slides".into(),
|
||||
|
|
@ -616,6 +627,22 @@ impl Layout {
|
|||
self.remember(before);
|
||||
Ok(())
|
||||
}
|
||||
/// Commit per-layer relative pivots as one undoable edit, without changing geometry.
|
||||
pub fn set_layer_pivots(&mut self, pivots: &[(usize, [f64; 2])]) -> Result<(), String> {
|
||||
for (i, pivot) in pivots {
|
||||
if !self.selectable(*i) {
|
||||
return Err("Unlock and show this layer first.".into());
|
||||
}
|
||||
validate_pivot(*pivot)?;
|
||||
}
|
||||
self.finish_drag();
|
||||
let before = self.snapshot();
|
||||
for (i, pivot) in pivots {
|
||||
self.info[*i].pivot = *pivot;
|
||||
}
|
||||
self.remember(before);
|
||||
Ok(())
|
||||
}
|
||||
pub fn layer_info(&self, i: usize) -> &LayerInfo {
|
||||
&self.info[i]
|
||||
}
|
||||
|
|
@ -678,6 +705,7 @@ impl Layout {
|
|||
};
|
||||
self.layers.push(g);
|
||||
self.info.push(LayerInfo {
|
||||
pivot: [0.5; 2],
|
||||
midra_link: None,
|
||||
id: self.next_id,
|
||||
name: format!("Layer {}", self.next_id),
|
||||
|
|
@ -807,7 +835,7 @@ impl Layout {
|
|||
return;
|
||||
}
|
||||
self.content_resize = Some(Resize {
|
||||
pivot: [0.5; 2],
|
||||
pivot: None,
|
||||
use_pivot: false,
|
||||
mode: ResizeMode::Individual,
|
||||
targets: self.selected.clone(),
|
||||
|
|
@ -1343,7 +1371,7 @@ impl Layout {
|
|||
}
|
||||
self.select(Some(index));
|
||||
self.resize = Some(Resize {
|
||||
pivot: [0.5; 2],
|
||||
pivot: None,
|
||||
use_pivot: false,
|
||||
mode: ResizeMode::Individual,
|
||||
targets: self.selected.clone(),
|
||||
|
|
@ -1379,7 +1407,7 @@ impl Layout {
|
|||
}
|
||||
self.finish_drag();
|
||||
self.resize = Some(Resize {
|
||||
pivot: [0.5; 2],
|
||||
pivot: None,
|
||||
use_pivot: false,
|
||||
mode,
|
||||
targets: self.selected.clone(),
|
||||
|
|
@ -1396,7 +1424,7 @@ impl Layout {
|
|||
if relative.iter().all(|v| v.is_finite() && v.abs() <= 1e6)
|
||||
&& let Some(resize) = &mut self.resize
|
||||
{
|
||||
resize.pivot = relative;
|
||||
resize.pivot = Some(relative);
|
||||
}
|
||||
}
|
||||
/// Toggle pivot anchoring during a gesture, always recomputing from its initial geometry.
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ pub struct Input {
|
|||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Layer {
|
||||
pub pivot: [f64; 2],
|
||||
#[serde(deserialize_with = "deserialize_link")]
|
||||
pub midra_link: Option<crate::MidraLink>,
|
||||
pub name: String,
|
||||
|
|
@ -122,7 +123,7 @@ impl Project {
|
|||
pub fn from_layout(layout: &Layout) -> Self {
|
||||
Self {
|
||||
format: FORMAT.into(),
|
||||
version: 8,
|
||||
version: 9,
|
||||
next_layer_id: layout.next_id,
|
||||
output_resolution: layout.formats.output.into(),
|
||||
inputs: (0..layout.formats.sources.len())
|
||||
|
|
@ -138,6 +139,7 @@ impl Project {
|
|||
.collect(),
|
||||
layers: (0..layout.layers.len())
|
||||
.map(|i| Layer {
|
||||
pivot: layout.info[i].pivot,
|
||||
midra_link: layout.info[i].midra_link.clone(),
|
||||
name: layout.info[i].name.clone(),
|
||||
folder: layout.info[i].folder.clone(),
|
||||
|
|
@ -170,9 +172,9 @@ impl Project {
|
|||
}
|
||||
/// Build a fresh document only after validating the entire file. No history is restored.
|
||||
pub fn layout(&self) -> Result<Layout, String> {
|
||||
if self.format != FORMAT || self.version != 8 {
|
||||
if self.format != FORMAT || self.version != 9 {
|
||||
return Err(
|
||||
"Unsupported project format/version; expected beffroi.midra-layout version 8."
|
||||
"Unsupported project format/version; expected beffroi.midra-layout version 9."
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
|
|
@ -239,7 +241,9 @@ impl Project {
|
|||
if let Some(link) = &layer.midra_link {
|
||||
link.validate()?;
|
||||
}
|
||||
crate::validate_pivot(layer.pivot)?;
|
||||
layout.info.push(crate::LayerInfo {
|
||||
pivot: layer.pivot,
|
||||
midra_link: layer.midra_link.clone(),
|
||||
id: layer.id,
|
||||
name: layer.name.clone(),
|
||||
|
|
@ -298,8 +302,8 @@ impl Project {
|
|||
}
|
||||
let header: Header =
|
||||
serde_json::from_str(text).map_err(|e| format!("Invalid project JSON: {e}"))?;
|
||||
if !(1..=8).contains(&header.version) {
|
||||
return Err("Unsupported project version; expected versions 1–8.".into());
|
||||
if !(1..=9).contains(&header.version) {
|
||||
return Err("Unsupported project version; expected versions 1–9.".into());
|
||||
}
|
||||
let mut migrated: serde_json::Value = if header.version == 1 || header.version == 2 {
|
||||
let mut value: serde_json::Value =
|
||||
|
|
@ -413,6 +417,16 @@ impl Project {
|
|||
editor.insert("position_layer_point".into(), serde_json::Value::Null);
|
||||
migrated["version"] = 8.into();
|
||||
}
|
||||
if (1..=8).contains(&header.version) {
|
||||
for layer in migrated["layers"].as_array_mut().ok_or("Missing layers")? {
|
||||
let layer = layer.as_object_mut().ok_or("Invalid layer")?;
|
||||
if layer.contains_key("pivot") {
|
||||
return Err("Unexpected layer pivot in old project".into());
|
||||
}
|
||||
layer.insert("pivot".into(), serde_json::json!([0.5, 0.5]));
|
||||
}
|
||||
migrated["version"] = 9.into();
|
||||
}
|
||||
let p: Self =
|
||||
serde_json::from_value(migrated).map_err(|e| format!("Invalid project JSON: {e}"))?;
|
||||
p.layout()?;
|
||||
|
|
|
|||
|
|
@ -352,6 +352,9 @@ mod tests {
|
|||
app.keep_aspect_ratio = false;
|
||||
app.show_pattern = true;
|
||||
app.show_toolbar_icon_names = true;
|
||||
app.layout
|
||||
.set_layer_pivots(&[(0, [-0.25, 0.75]), (1, [0.1, 0.9])])
|
||||
.unwrap();
|
||||
let project = Project::from_json(&app.project().to_json().unwrap()).unwrap();
|
||||
let mut restored = Editor {
|
||||
edit_content: true,
|
||||
|
|
@ -369,6 +372,8 @@ mod tests {
|
|||
"opening a project must not save application defaults"
|
||||
);
|
||||
assert_eq!(restored.project(), project);
|
||||
assert_eq!(restored.pivot_for(0), [-0.25, 0.75]);
|
||||
assert_eq!(restored.pivot_for(1), [0.1, 0.9]);
|
||||
assert!(!restored.edit_content);
|
||||
assert_eq!(restored.reference.as_ref().unwrap().bytes, original_bytes);
|
||||
let mut broken = project.clone();
|
||||
|
|
|
|||
|
|
@ -272,3 +272,69 @@ fn pivot_placement_honors_screen_layer_pixel_and_grid_options() {
|
|||
([1162.0, 270.0], [None; 2])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stored_pivots_follow_layers_and_edits_are_atomic() {
|
||||
let mut layout = Layout::default();
|
||||
let geometry = layout.layers().clone();
|
||||
let custom = [-0.25, 0.75];
|
||||
layout.set_layer_pivots(&[(0, custom)]).unwrap();
|
||||
layout.select(Some(1));
|
||||
assert_eq!(layout.layer_info(0).pivot, custom);
|
||||
assert_eq!(layout.layers(), &geometry);
|
||||
for invalid in [[f64::NAN, 0.0], [f64::INFINITY, 0.0], [1e7, 0.0]] {
|
||||
assert!(
|
||||
layout
|
||||
.set_layer_pivots(&[(1, [0.0; 2]), (0, invalid)])
|
||||
.is_err()
|
||||
);
|
||||
assert_eq!(layout.layer_info(1).pivot, [0.5; 2]);
|
||||
}
|
||||
layout.undo();
|
||||
assert_eq!(layout.layer_info(0).pivot, [0.5; 2]);
|
||||
layout.redo();
|
||||
let id = layout.layer_info(0).id;
|
||||
layout.move_layer(id, 1).unwrap();
|
||||
assert_eq!(layout.layer_info(1).id, id);
|
||||
assert_eq!(layout.layer_info(1).pivot, custom);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn grouped_resize_keeps_each_stored_pivot_fixed() {
|
||||
for mode in [
|
||||
ResizeMode::Individual,
|
||||
ResizeMode::Same,
|
||||
ResizeMode::Mirror,
|
||||
ResizeMode::Opposite,
|
||||
] {
|
||||
let mut layout = Layout::default();
|
||||
let relative = [[0.2, 0.7], [0.6, 0.3]];
|
||||
layout
|
||||
.set_layer_pivots(&[(0, relative[0]), (1, relative[1])])
|
||||
.unwrap();
|
||||
let before = layout.layers().clone();
|
||||
layout.set_selection([true; 2]);
|
||||
let start = [
|
||||
before[0].x + before[0].width,
|
||||
before[0].y + before[0].height,
|
||||
];
|
||||
layout
|
||||
.start_selected_resize(
|
||||
0,
|
||||
ResizeHandle::Corner(midra_layout::Corner::BottomRight),
|
||||
start,
|
||||
false,
|
||||
mode,
|
||||
)
|
||||
.unwrap();
|
||||
layout.use_resize_pivot(true);
|
||||
layout.drag_to([start[0] + 30.0, start[1] + 20.0]);
|
||||
for i in 0..2 {
|
||||
let fixed = pivot(before[i], relative[i]);
|
||||
let after = pivot(layout.layers()[i], relative[i]);
|
||||
near(fixed[0], after[0]);
|
||||
near(fixed[1], after[1]);
|
||||
assert_eq!(layout.layer_info(i).pivot, relative[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -225,7 +225,7 @@ fn persistent_midra_links_roundtrip_migrate_and_undo() {
|
|||
include_str!("../../../docs/interfaces/midra-layout-v5.example.json"),
|
||||
] {
|
||||
let p = Project::from_json(text).unwrap();
|
||||
assert_eq!(p.version, 8);
|
||||
assert_eq!(p.version, 9);
|
||||
assert!(p.layers.iter().all(|l| l.midra_link.is_none()));
|
||||
}
|
||||
let mut value = serde_json::to_value(&project).unwrap();
|
||||
|
|
@ -302,7 +302,7 @@ fn position_point_schema_migrates_corners_and_rejects_mislabeled_files() {
|
|||
.unwrap();
|
||||
old["editor"]["position_corner"] = name.into();
|
||||
let loaded = Project::from_json(&old.to_string()).unwrap();
|
||||
assert_eq!(loaded.version, 8);
|
||||
assert_eq!(loaded.version, 9);
|
||||
assert_eq!(loaded.editor.position_point, expected);
|
||||
old["editor"]["position_corner"] = "center".into();
|
||||
assert!(Project::from_json(&old.to_string()).is_err());
|
||||
|
|
@ -311,7 +311,7 @@ fn position_point_schema_migrates_corners_and_rejects_mislabeled_files() {
|
|||
assert!(Project::from_json(&old.to_string()).is_err());
|
||||
}
|
||||
let value: serde_json::Value = serde_json::from_str(¤t.to_json().unwrap()).unwrap();
|
||||
for version in [0, 9] {
|
||||
for version in [0, 10] {
|
||||
let mut invalid = value.clone();
|
||||
invalid["version"] = version.into();
|
||||
assert!(
|
||||
|
|
@ -352,7 +352,7 @@ fn backdrop_title_roundtrips_and_older_projects_gain_the_default() {
|
|||
}
|
||||
let old = include_str!("../../../docs/interfaces/midra-layout-v6.example.json");
|
||||
let migrated = Project::from_json(old).unwrap();
|
||||
assert_eq!(migrated.version, 8);
|
||||
assert_eq!(migrated.version, 9);
|
||||
assert_eq!(migrated.editor.backdrop_title, DEFAULT_BACKDROP_TITLE);
|
||||
let mut invalid: serde_json::Value = serde_json::from_str(old).unwrap();
|
||||
invalid["editor"]["backdrop_title"] = "Unexpected".into();
|
||||
|
|
@ -409,3 +409,38 @@ fn independent_layer_point_is_explicit_and_old_projects_keep_matched_points() {
|
|||
.remove("position_layer_point");
|
||||
assert!(Project::from_json(&missing.to_string()).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn saved_pivots_roundtrip_and_old_projects_default_to_center() {
|
||||
let mut layout = Layout::default();
|
||||
let custom = [-0.123456789, 1.25];
|
||||
layout.set_layer_pivots(&[(0, custom)]).unwrap();
|
||||
let project = Project::from_layout(&layout);
|
||||
let loaded = Project::from_json(&project.to_json().unwrap())
|
||||
.unwrap()
|
||||
.layout()
|
||||
.unwrap();
|
||||
assert_eq!(loaded.layer_info(0).pivot, custom);
|
||||
assert_eq!(loaded.layer_info(1).pivot, [0.5; 2]);
|
||||
let example = include_str!("../../../docs/interfaces/midra-layout-v9.example.json");
|
||||
assert_eq!(Project::from_json(example).unwrap().version, 9);
|
||||
let old = include_str!("../../../docs/interfaces/midra-layout-v8.example.json");
|
||||
let migrated = Project::from_json(old).unwrap();
|
||||
assert!(migrated.layers.iter().all(|l| l.pivot == [0.5; 2]));
|
||||
let mut old: serde_json::Value = serde_json::from_str(old).unwrap();
|
||||
old["layers"][0]["pivot"] = serde_json::json!([0.2, 0.3]);
|
||||
assert!(Project::from_json(&old.to_string()).is_err());
|
||||
for invalid in [
|
||||
serde_json::json!([1e7, 0.5]),
|
||||
serde_json::json!([0.5]),
|
||||
serde_json::json!([0.5, 0.5, 0.5]),
|
||||
serde_json::Value::Null,
|
||||
] {
|
||||
let mut value = serde_json::to_value(&project).unwrap();
|
||||
value["layers"][0]["pivot"] = invalid;
|
||||
assert!(Project::from_json(&value.to_string()).is_err());
|
||||
}
|
||||
let mut value = serde_json::to_value(&project).unwrap();
|
||||
value["layers"][0].as_object_mut().unwrap().remove("pivot");
|
||||
assert!(Project::from_json(&value.to_string()).is_err());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -111,7 +111,7 @@ The session-only toolbar grid tools select 10 or 100 screen pixels (mutually exc
|
|||
|
||||
**Try autoconnecting at startup** is in Settings → Midra integration. It saves immediately but applies on the next launch; toggling it does not initiate a connection. One background attempt uses the regular connection timeout and error handling, with no automatic retry loop. Failures leave the app offline and are reported in Midra connection. Project loading does not trigger an attempt.
|
||||
|
||||
The session-only Pivot tool can combine with either grid. Ctrl without Shift temporarily inverts Pivot; Ctrl+Shift does not affect it. Pivot positions and tool selections are not persisted in application settings or projects. Tool selections survive project loads and selection changes within a session, and start off on launch; pivot positions reset on selection changes/project loads.
|
||||
The session-only Pivot tool can combine with either grid. Ctrl without Shift temporarily inverts Pivot; Ctrl+Shift does not affect it. Tool selections are not persisted in application settings or projects; they survive project loads and selection changes within a session and start off on launch. Pivot positions belong to each layer and are saved in project v9, not application settings.
|
||||
|
||||
Move/Pan/Zoom navigation tool choice is also session-only and survives project loads.
|
||||
It starts at Move on launch; V/H/Z switch tools only outside text entry. Camera
|
||||
|
|
@ -121,4 +121,4 @@ Pivot placement also has nine built-in local anchors (corners, side midpoints,
|
|||
center), active independently of `snapping.enabled` within the same six-logical-pixel
|
||||
radius. Anchors take priority over active grids and configured edge/pixel snapping.
|
||||
Alt bypasses these anchors and grids while inverting configured snapping as usual.
|
||||
This adds no settings fields; pivot positions remain session-only.
|
||||
This adds no settings fields; pivot positions are saved per layer in projects.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,109 @@
|
|||
{
|
||||
"format": "beffroi.midra-layout",
|
||||
"version": 9,
|
||||
"output_resolution": {
|
||||
"width": 1920,
|
||||
"height": 1080
|
||||
},
|
||||
"inputs": [
|
||||
{
|
||||
"id": 1,
|
||||
"source_format": "sixteen_nine",
|
||||
"native_resolution": {
|
||||
"width": 1920,
|
||||
"height": 1080
|
||||
},
|
||||
"input_crop": {
|
||||
"left": 0.0,
|
||||
"right": 0.0,
|
||||
"top": 0.0,
|
||||
"bottom": 0.0
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"source_format": "sixteen_nine",
|
||||
"native_resolution": {
|
||||
"width": 1920,
|
||||
"height": 1080
|
||||
},
|
||||
"input_crop": {
|
||||
"left": 0.0,
|
||||
"right": 0.0,
|
||||
"top": 0.0,
|
||||
"bottom": 0.0
|
||||
}
|
||||
}
|
||||
],
|
||||
"layers": [
|
||||
{
|
||||
"id": 1,
|
||||
"source_id": 1,
|
||||
"bounds": {
|
||||
"x": 120.0,
|
||||
"y": 180.0,
|
||||
"width": 640.0,
|
||||
"height": 360.0
|
||||
},
|
||||
"advanced_crop": {
|
||||
"left": 0.0,
|
||||
"right": 0.0,
|
||||
"top": 0.0,
|
||||
"bottom": 0.0
|
||||
},
|
||||
"visible": true,
|
||||
"locked": false,
|
||||
"name": "Presenter",
|
||||
"folder": "",
|
||||
"midra_link": null,
|
||||
"pivot": [
|
||||
0.5,
|
||||
0.5
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"source_id": 2,
|
||||
"bounds": {
|
||||
"x": 1160.0,
|
||||
"y": 180.0,
|
||||
"width": 640.0,
|
||||
"height": 360.0
|
||||
},
|
||||
"advanced_crop": {
|
||||
"left": 0.0,
|
||||
"right": 0.0,
|
||||
"top": 0.0,
|
||||
"bottom": 0.0
|
||||
},
|
||||
"visible": true,
|
||||
"locked": false,
|
||||
"name": "Slides",
|
||||
"folder": "",
|
||||
"midra_link": null,
|
||||
"pivot": [
|
||||
0.5,
|
||||
0.5
|
||||
]
|
||||
}
|
||||
],
|
||||
"reference": null,
|
||||
"editor": {
|
||||
"backdrop_title": "Example Conference Name",
|
||||
"selected_layer_ids": [
|
||||
2
|
||||
],
|
||||
"position_point": "top_left",
|
||||
"keep_aspect_ratio": true,
|
||||
"viewport_zoom": 1.0,
|
||||
"viewport_pan": [
|
||||
0.0,
|
||||
0.0
|
||||
],
|
||||
"show_source_pattern": false,
|
||||
"show_toolbar_icon_names": false,
|
||||
"outline_layers": false,
|
||||
"position_layer_point": null
|
||||
},
|
||||
"next_layer_id": 3
|
||||
}
|
||||
|
|
@ -5,7 +5,7 @@ These are offline editor formats, not Analog Way REST payloads. Implementation:
|
|||
[`project.rs`](../../crates/midra-layout/tests/project.rs). Update this contract,
|
||||
examples and tests in the same change as any save/export schema change.
|
||||
|
||||
## Project JSON, version 8
|
||||
## Project JSON, version 9
|
||||
|
||||
Suggested filename: `layout.midra.json`. Encoding: UTF-8, pretty-printed JSON,
|
||||
with a trailing newline. A project preserves applied document values at their
|
||||
|
|
@ -18,16 +18,16 @@ and `next_layer_id: 3`; other values are preserved.
|
|||
The [version 1 example](midra-layout-v1.example.json) remains a migration fixture.
|
||||
Version 4 adds `editor.outline_layers: false`, and reference `native_size: false`
|
||||
and `offset: [0, 0]` when migrating versions 1–3. Old fitting behavior is preserved.
|
||||
Version 5 adds required `midra_link` to every layer. Versions 1–4 migrate with null links; links cannot be supplied under an older version. Version 6 renames `editor.position_corner` to `editor.position_point` and adds `center`. Versions 1–5 retain their original corner; center and the new field are rejected in those older schemas. Version 7 adds `editor.backdrop_title`; versions 1–6 gain `"Example Conference Name"` and cannot supply this field under the old schema. Version 8 adds required `editor.position_layer_point`: null keeps matched points; a point enables independent measurement. Versions 1–7 migrate to null and cannot supply this field. Saving always writes version 8; older applications reject it.
|
||||
Version 5 adds required `midra_link` to every layer. Versions 1–4 migrate with null links; links cannot be supplied under an older version. Version 6 renames `editor.position_corner` to `editor.position_point` and adds `center`. Versions 1–5 retain their original corner; center and the new field are rejected in those older schemas. Version 7 adds `editor.backdrop_title`; versions 1–6 gain `"Example Conference Name"` and cannot supply this field under the old schema. Version 8 adds required `editor.position_layer_point`: null keeps matched points; a point enables independent measurement. Versions 1–7 migrate to null and cannot supply this field. Version 9 adds required `layers[].pivot`; versions 1–8 migrate to `[0.5, 0.5]` and cannot supply this field. Saving always writes version 9; older applications reject it.
|
||||
Incompatible files are never partially applied.
|
||||
|
||||
[Complete example](midra-layout-v8.example.json) is a valid default project,
|
||||
[Complete example](midra-layout-v9.example.json) is a valid default project,
|
||||
without a reference image. It is loaded by the regression tests.
|
||||
|
||||
| Root field | Representation / meaning |
|
||||
| --- | --- |
|
||||
| `format` | Exactly `"beffroi.midra-layout"` |
|
||||
| `version` | Integer `8` |
|
||||
| `version` | Integer `9` |
|
||||
| `next_layer_id` | Positive integer strictly greater than every layer ID; not rewound by undo |
|
||||
| `output_resolution` | `{ "width": integer, "height": integer }`, screen pixels |
|
||||
| `inputs` | 1–1024 input objects, consecutive IDs starting at 1 |
|
||||
|
|
@ -56,6 +56,7 @@ Layer objects:
|
|||
| `id` | Unique positive stable editor layer ID; independent of list position and Midra mapping |
|
||||
| `name` | Nonblank display name, at most 128 characters |
|
||||
| `midra_link` | Required: null (unlinked) or `{ "screen": "2", "layer": "1" }`. Positive decimal device IDs as strings, at most 16 ASCII digits; unknown fields rejected. No host, bank or credentials. |
|
||||
| `pivot` | Required `[x, y]` gravity point relative to the layer top-left, in fractions of width/height. Center is `[0.5, 0.5]`; outside points are allowed. Finite doubles within ±1,000,000, preserved without rounding. Placement/reset is undoable; follows layer movement/reordering. Omitted from SVG and device writes. |
|
||||
| `folder` | Local folder name, at most 128 characters; empty means ungrouped |
|
||||
| `source_id` | Existing input slot ID; multiple layers may share an input |
|
||||
| `bounds` | `{ "x": number, "y": number, "width": number, "height": number }`, final visible box in screen pixels; x/y are its top-left position |
|
||||
|
|
@ -113,7 +114,7 @@ missing fields and non-finite values are rejected before replacing the document.
|
|||
Projects larger than 96 MiB are rejected.
|
||||
|
||||
Save includes **applied values only**. Pending text/format/import drafts, prepared aspect-ratio targets, open
|
||||
dialogs, undo/redo history, crop-edit mode, resize mode, temporary resize pivots, grid/pivot/navigation tool selections, and provisional device
|
||||
dialogs, undo/redo history, crop-edit mode, resize mode, grid/pivot/navigation tool selections, and provisional device
|
||||
export calculations are not persisted. The current resize mode stays selected
|
||||
when opening another project in the same session; a fresh launch starts with
|
||||
Individual mode. Names and folder labels are stored. Removing layers does not renumber surviving
|
||||
|
|
@ -179,7 +180,7 @@ and `viewBox="0 0 W H"` equal to the output screen. The root carries
|
|||
|
||||
## Compatibility changes
|
||||
|
||||
Project version 8 and SVG version 1 are strict contracts. A breaking project field/meaning change must bump
|
||||
Project version 9 and SVG version 1 are strict contracts. A breaking project field/meaning change must bump
|
||||
`version` and either implement a tested migration or clearly reject older/newer
|
||||
files. Changing rectangle/marker semantics requires a corresponding documented
|
||||
SVG interface revision and importer tests. Update this document and its example
|
||||
|
|
|
|||
|
|
@ -365,8 +365,7 @@ movement/resize, not numeric edits or crop-content handles.
|
|||
### Ctrl resize pivot
|
||||
|
||||
With Pivot off, hold **Ctrl** (or select **Pivot**) to show a draggable crosshair on selected layers (center by
|
||||
default). Drag the marker to place it, including outside the layer; this does not
|
||||
change the document. Double-click the visible pivot to reset to center.
|
||||
default). Drag the marker to place it, including outside the layer; this changes only its saved pivot, not its geometry. Double-click the visible pivot to reset to center.
|
||||
**Ctrl-drag a layer resize handle** to resize around the fixed pivot. Ctrl can be
|
||||
pressed/released during the gesture; the result is recalculated from the starting
|
||||
geometry. With Pivot selected, Ctrl temporarily restores the normal opposite-edge anchor. Escape cancels a
|
||||
|
|
@ -375,9 +374,9 @@ resize or restores the previous pivot placement.
|
|||
| Resize mode | Pivot behavior |
|
||||
| --- | --- |
|
||||
| Individual | Only the dragged layer resizes around its pivot |
|
||||
| Same handles | Each layer uses the same relative pivot position and scale factors |
|
||||
| Mirror horizontally | The partner's pivot is reflected left/right |
|
||||
| Opposite handles | The partner's pivot is reflected on both axes |
|
||||
| Same handles | Placing a marker sets the same relative pivot on selected layers |
|
||||
| Mirror horizontally | Placing a marker reflects the partner’s pivot left/right |
|
||||
| Opposite handles | Placing a marker reflects the partner’s pivot on both axes |
|
||||
|
||||
Dragging a pivot snaps within six logical display pixels of the layer's four corners,
|
||||
four side midpoints and center, even with snapping disabled. Nearby local anchors
|
||||
|
|
@ -386,14 +385,12 @@ visible-layer-edge options apply. Guides show reached anchors/edges/grid lines.
|
|||
Alt bypasses the nine built-in anchors and grids while retaining its usual
|
||||
inversion of configured snapping. Release uses the current modifiers.
|
||||
|
||||
Moving any marker updates the related markers. Each layer's pivot stays fixed
|
||||
Moving any marker updates the related markers as above. Changing selection or resize mode preserves stored points. Each layer's own pivot stays fixed
|
||||
while resizing; unequal sizes retain proportional scaling. Aspect-ratio lock,
|
||||
snapping (including edge contact), Alt inversion and Undo still apply. A handle
|
||||
on the pivot's axis cannot resize that axis; move the pivot or use another handle.
|
||||
Layers do not flip when dragged through the pivot; minimum sizes still apply.
|
||||
**Ctrl+Shift remains the 100 px grid** (or temporarily disables a selected 100 px grid); it uses pivot anchoring if Pivot is selected. Pivot positions are temporary,
|
||||
reset on selection changes/project loads, and follow layers when moved. They are
|
||||
not saved and do not affect numeric sizing or Adjust layer crop mode.
|
||||
**Ctrl+Shift remains the 100 px grid** (or temporarily disables a selected 100 px grid); it uses pivot anchoring if Pivot is selected. Pivot positions are saved per layer in the project, survive selection changes and reopening, and follow layers when moved. Placement and reset support Undo/Redo. They do not affect numeric sizing or Adjust layer crop mode.
|
||||
|
||||
### Resizing with corner handles
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
## Unreleased
|
||||
|
||||
- Save gravity points per layer across selection changes and reopening; placement/reset supports Undo. Project v9 migrates older files to centered pivots.
|
||||
|
||||
- Pivot dragging snaps to corners, side midpoints and center even with snapping off; also honors grid/pixel/screen/layer options. Alt bypasses built-in anchors; guides and linked pivots follow the result.
|
||||
|
||||
- Optional “Use a different layer point” exposes independent screen/layer measurement points (e.g. screen top-left → layer center). Numeric sizing preserves the endpoint. Project v8 saves the choice; v1–7 retain matched points.
|
||||
|
|
|
|||
Loading…
Reference in New Issue