Skip to content

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

Merge main into #1439; Preview checkbox moved into main's split comp_settings::show (tab_body), gradient editor keeps main's pub(super) to_color32

bflatastic72a2c4a1bc8c
diff --cc crates/ui-egui/src/panels/comp_settings.rs
index cc0ab9615,0d07afd94..02f58c0a5
--- a/crates/ui-egui/src/panels/comp_settings.rs
+++ b/crates/ui-egui/src/panels/comp_settings.rs
@@@ -486,6 -467,54 +467,55 @@@ fn tab_body
});
}
}
+ }
+
+ /// The height `tab` would take at `top`, from laying it out unseen: it follows the font, the
+ /// language and the scale. Nothing it registers or changes is kept.
+ fn tab_height(ui: &mut egui::Ui, tab: Tab, d: &CompDraft, t: &Tokens, custom: &[CompPreset], top: f32) -> f32 {
+ let rect = egui::Rect::from_min_size(egui::pos2(ui.max_rect().left(), top), vec2(ui.max_rect().width(), 0.0));
+ let mut child = ui.new_child(egui::UiBuilder::new().max_rect(rect).id_salt("comp-tab-measure").invisible());
+ tab_body(&mut child, tab, &mut d.clone(), t, &mut Registry::default(), custom, &mut Vec::new());
+ child.cursor().top() - top
+ }
+
+ /// Draw the dialog. Returns `Some(params)` when OK is pressed, `Some(Null)` on Cancel. `custom`
+ /// are the presets the user saved; saving or deleting one pushes its engine command to `cmds`.
+ pub fn show(
+ ui: &mut egui::Ui,
+ d: &mut CompDraft,
+ t: &Tokens,
+ auto: &mut Registry,
+ custom: &[CompPreset],
+ cmds: &mut Vec<(&'static str, serde_json::Value)>,
++ preview: Option<&mut bool>,
+ ) -> Option<serde_json::Value> {
+ let mut out = None;
+ ui.horizontal(|ui| {
+ ui.label(tr("Composition Name:"));
+ let r = ui.add(egui::TextEdit::singleline(&mut d.name).desired_width(300.0));
+ crate::widgets::focus_on_open(ui.ctx(), &r, &d.name);
+ auto.add("dialog.comp.name", r.rect, "Composition Name");
+ });
+ ui.add_space(8.0);
+ ui.horizontal(|ui| {
+ for (tab, label, key) in [(Tab::Basic, "Basic", "basic"), (Tab::Advanced, "Advanced", "advanced"), (Tab::Renderer, "3D Renderer", "renderer")] {
+ let r = ui.selectable_label(d.tab == tab, label);
+ auto.add(&format!("dialog.comp.tab.{key}"), r.rect, label);
+ if r.clicked() {
+ d.tab = tab;
+ }
+ }
+ });
+ ui.separator();
+ // Every tab takes the height of the tallest one, so Cancel and OK stay put between tabs.
+ let body_top = ui.cursor().top();
+ tab_body(ui, d.tab, d, t, auto, custom, cmds);
+ let shown = ui.cursor().top() - body_top;
+ let tallest = [Tab::Basic, Tab::Advanced, Tab::Renderer]
+ .into_iter()
+ .filter(|tab| *tab != d.tab)
+ .fold(shown, |h, tab| h.max(tab_height(ui, tab, d, t, custom, body_top)));
+ ui.add_space((tallest - shown).max(0.0));
ui.add_space(14.0);
ui.horizontal(|ui| {
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
diff --cc crates/ui-egui/src/panels/gradient_editor.rs
index 127d1f00f,e132a42d1..25e3d1d44
--- a/crates/ui-egui/src/panels/gradient_editor.rs
+++ b/crates/ui-egui/src/panels/gradient_editor.rs
@@@ -263,56 -241,7 +263,56 @@@ fn commit(app: &mut EffectcraftApp, ctx
}
}
+/// Close the editor without keeping the preview: the project and undo history go back to how they
+/// were when it opened (nothing happens when the preview never changed the property).
+pub fn finish(app: &mut EffectcraftApp, ok: bool, ctx: &egui::Context) {
+ if ok {
+ // An edit made in the same frame as OK is not in the last preview yet.
+ sync_preview(app, ctx);
+ commit(app, ctx);
+ return;
+ }
+ let d = std::mem::take(&mut app.dialog_state.gradient);
+ if d.applied.is_some()
+ && let Some(cp) = d.checkpoint
+ {
+ preview_merge::rollback(app, cp, d.applied_at.as_ref());
+ }
+ app.session.history.merge_key = None;
+ app.dialog = None;
+}
+
+/// Keep the property in step with the draft while Preview is on: apply a changed draft with one
+/// merged undo step, and roll back to the checkpoint when Preview goes off or the draft is back to
+/// how it opened.
+fn sync_preview(app: &mut EffectcraftApp, ctx: &egui::Context) {
+ let d = &app.dialog_state.gradient;
+ let Some(tg) = d.target.clone() else { return };
+ let g = d.gradient();
+ let want = d.preview && g != d.baseline;
+ if want && d.applied.as_ref() != Some(&g) {
+ let params = json!({
+ "comp": tg.comp, "layer": tg.layer, "prop": tg.prop, "time": tg.time, "timeBase": "comp",
+ "value": Value::Gradient(g.clone()).to_json(), "merge": format!("gradient-editor-{}", tg.prop),
+ });
+ match crate::menus::invoke(app, ctx, "prop.set", params) {
+ Ok(_) => {
+ app.dialog_state.gradient.applied = Some(g);
+ app.dialog_state.gradient.applied_at = Some(preview_merge::applied(app));
+ }
+ Err(e) => app.ui.status = e,
+ }
+ } else if !want
+ && d.applied.is_some()
+ && let Some(cp) = d.checkpoint.clone()
+ {
+ let at = app.dialog_state.gradient.applied_at.take();
+ preview_merge::rollback(app, cp, at.as_ref());
+ app.dialog_state.gradient.applied = None;
+ }
+}
+
- fn to_color32(c: [f32; 4]) -> Color32 {
+ pub(super) fn to_color32(c: [f32; 4]) -> Color32 {
let b = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8;
Color32::from_rgba_unmultiplied(b(c[0]), b(c[1]), b(c[2]), b(c[3]))
}