Skip to content

EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust

3D: close the New 3D Primitive dialog when its project or comp changes; untouched dimensions take the type's defaults (#1734)

tylerjenningswc21210a63a57
diff --git a/crates/ui-egui/src/panels/primitive_dialog.rs b/crates/ui-egui/src/panels/primitive_dialog.rs
index 7626984a3..a3483a976 100644
--- a/crates/ui-egui/src/panels/primitive_dialog.rs
+++ b/crates/ui-egui/src/panels/primitive_dialog.rs
@@ -6,7 +6,7 @@ use std::collections::BTreeMap;
use crate::i18n::tr;
use effectcraft_engine::project::build::{Ids, primitive_geometry_options};
-use effectcraft_engine::project::{ParamUi, PrimitiveKind, Property};
+use effectcraft_engine::project::{ItemId, ParamUi, PrimitiveKind, Property};
use egui::{Color32, vec2};
use serde_json::{Value, json};
@@ -19,9 +19,15 @@ pub struct PrimitiveDraft {
pub name: String,
/// Whether the name was typed; until then it follows the type.
pub named: bool,
- /// Dimensions by Geometry Options match id, kept when the type changes (a sphere's radius
- /// carries over to a cone).
+ /// Dimensions typed, by Geometry Options match id, kept when the type changes (a sphere's
+ /// radius carries over to a cone); the rest are the type's defaults.
pub dims: BTreeMap<String, f64>,
+ /// The project (its epoch) and comp the dialog was opened for.
+ pub target: Option<(u64, ItemId)>,
+}
+
+fn target(app: &EffectcraftApp) -> Option<(u64, ItemId)> {
+ Some((app.session.script_ui.project_epoch, app.session.active_comp_id()?))
}
/// The dimensions a primitive type has: its Geometry Options as the engine builds them.
@@ -32,15 +38,22 @@ fn dimensions(kind: PrimitiveKind) -> Vec<Property> {
/// Open the dialog for a new primitive in the active comp.
pub fn open(app: &mut EffectcraftApp) -> Result<(), String> {
- app.session.active_comp().ok_or("no composition is open")?;
+ let target = Some(target(app).ok_or("no composition is open")?);
let kind = PrimitiveKind::Cube;
- app.dialog_state.primitive = PrimitiveDraft { kind, name: kind.label().to_string(), named: false, dims: BTreeMap::new() };
+ app.dialog_state.primitive = PrimitiveDraft { kind, name: kind.label().to_string(), named: false, dims: BTreeMap::new(), target };
app.dialog = Some(Dialog::NewPrimitive);
Ok(())
}
pub fn dialog(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) {
let mut d = app.dialog_state.primitive.clone();
+ // A project opened or another comp made active since (by a script or the control channel):
+ // the primitive would land somewhere the dialog was not opened for.
+ if target(app) != d.target {
+ app.ui.status = "New 3D Primitive closed: the composition changed".into();
+ app.dialog = None;
+ return;
+ }
let (mut ok, mut close) = (false, false);
super::dialogs::modal(ctx, "New 3D Primitive", vec2(420.0, 300.0), t, |ui| {
egui::Grid::new("primitive-grid").num_columns(2).spacing([14.0, 9.0]).show(ui, |ui| {
@@ -63,12 +76,15 @@ pub fn dialog(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) {
ui.end_row();
for p in dimensions(d.kind) {
let ParamUi::Slider { min, max, decimals, .. } = p.ui else { continue };
- let v = d.dims.entry(p.match_id.clone()).or_insert(p.value.as_f64());
+ let mut v = d.dims.get(&p.match_id).copied().unwrap_or(p.value.as_f64());
ui.label(tr(&p.name));
// Counts (segments, rings) are whole numbers; sizes are pixels.
let suffix = if decimals == 0 { "" } else { " px" };
- let r = ui.add(crate::widgets::drag_value(egui::DragValue::new(v).range(min..=max).max_decimals(decimals as usize).suffix(suffix)));
+ let r = ui.add(crate::widgets::drag_value(egui::DragValue::new(&mut v).range(min..=max).max_decimals(decimals as usize).suffix(suffix)));
app.auto.add_response(&format!("dialog.primitive.{}", p.match_id), &r, &p.name);
+ if r.changed() {
+ d.dims.insert(p.match_id, v);
+ }
ui.end_row();
}
});
diff --git a/crates/ui-egui/tests/ui_new_primitive.rs b/crates/ui-egui/tests/ui_new_primitive.rs
index 2093e69d5..fbb3a34e5 100644
--- a/crates/ui-egui/tests/ui_new_primitive.rs
+++ b/crates/ui-egui/tests/ui_new_primitive.rs
@@ -167,3 +167,61 @@ fn a_created_primitive_survives_save_and_reopen() {
assert_eq!(comp.renderer, Renderer::Advanced3D);
assert_ring(&comp.layers[0]);
}
+
+#[test]
+fn untouched_dimensions_take_each_types_defaults_and_typed_ones_carry_over() {
+ let mut h = harness();
+ open(&mut h);
+ choose(&mut h, PrimitiveKind::Plane);
+ click(&mut h, "dialog.primitive.ok");
+ let plane = layers(&h).remove(0);
+ assert_eq!(geometry(&plane, "width"), 400.0, "a plane's default, not the cube's 300");
+ open(&mut h);
+ type_into(&mut h, "dialog.primitive.width", "120");
+ choose(&mut h, PrimitiveKind::Plane);
+ click(&mut h, "dialog.primitive.ok");
+ let plane = layers(&h).into_iter().find(|l| l.id != plane.id).unwrap();
+ assert_eq!(geometry(&plane, "width"), 120.0, "the typed width carries over");
+ assert_eq!(geometry(&plane, "height"), 400.0);
+}
+
+/// A dialog left open while something else changes the editing context closes without creating
+/// anything rather than adding the primitive where it was not opened.
+fn closes_when(change: impl FnOnce(&mut Session)) {
+ let mut h = harness();
+ let a = h.state().session.active_comp_id().unwrap();
+ open(&mut h);
+ choose(&mut h, PrimitiveKind::Torus);
+ change(&mut h.state_mut().session);
+ let b = h.state().session.active_comp_id().unwrap();
+ let steps = h.state().session.history.undo.len();
+ h.run_steps(2);
+ assert_eq!(h.state().dialog, None, "the dialog closes");
+ assert!(rect(&h, "dialog.primitive.ok").is_none());
+ let s = &h.state().session;
+ for c in [a, b] {
+ if let Some(c) = s.project.comp(c) {
+ assert!(c.layers.is_empty(), "nothing is created");
+ assert_eq!(c.renderer, Renderer::Classic3D);
+ }
+ }
+ assert_eq!(s.history.undo.len(), steps);
+ assert!(h.state().ui.status.contains("changed"), "{}", h.state().ui.status);
+}
+
+#[test]
+fn another_comp_made_active_closes_the_dialog() {
+ closes_when(|s| {
+ s.execute("comp.new", json!({"name": "Other", "width": 640, "height": 360, "duration": 2})).unwrap();
+ });
+}
+
+#[test]
+fn a_replaced_project_closes_the_dialog_even_when_the_comp_id_is_reused() {
+ closes_when(|s| {
+ let before = s.active_comp_id().unwrap();
+ s.execute("file.newProject", json!({})).unwrap();
+ s.execute("comp.new", json!({"name": "Scene", "width": 640, "height": 360, "duration": 2})).unwrap();
+ assert_eq!(s.active_comp_id(), Some(before), "the new project reuses the comp's id");
+ });
+}