Merge pull request #462 from storytold/fix/417-viewer-resolution
Viewer: each composition keeps its Resolution in the project, as in After Effects (#417) # Conflicts: # crates/ui-egui/src/control.rs # crates/ui-egui/src/menus.rs # crates/ui-egui/src/panels/viewer_tools.rs
This commit is contained in:
@@ -223,8 +223,16 @@ pub fn handle(app: &mut EffectcraftApp, ctx: &egui::Context, req: &ControlReques
|
||||
if let Some(z) = v.get("zoom") {
|
||||
app.ui.viewer.zoom = z.as_f64().map(|z| z as f32);
|
||||
}
|
||||
// The active comp's Resolution (`view.res.*`), saved with the project.
|
||||
if let Some(r) = v.get("res").and_then(Value::as_str) {
|
||||
app.ui.viewer.res = Resolution::ALL.into_iter().find(|x| x.label().eq_ignore_ascii_case(r)).unwrap_or(app.ui.viewer.res);
|
||||
let Some(res) = Resolution::ALL.into_iter().find(|x| x.label().eq_ignore_ascii_case(r)) else {
|
||||
return err(format!("unknown resolution `{r}` (auto, full, half, third, quarter)"));
|
||||
};
|
||||
if let Some(cid) = app.session.active_comp_id()
|
||||
&& let Err(e) = app.session.set_resolution(cid, res)
|
||||
{
|
||||
return err(e);
|
||||
}
|
||||
}
|
||||
if let Some(Value::Array(a)) = v.get("pan") {
|
||||
app.ui.viewer.pan = [a.first().and_then(Value::as_f64).unwrap_or(0.0) as f32, a.get(1).and_then(Value::as_f64).unwrap_or(0.0) as f32];
|
||||
@@ -522,11 +530,16 @@ pub fn inspect(app: &EffectcraftApp, ctx: &egui::Context) -> Value {
|
||||
let automation_id = app.auto.elements.iter().chain(app.auto.previous.iter()).find(|e| e.response_id == Some(id)).map(|e| e.id.as_str());
|
||||
json!({"id": format!("{:016x}", id.value()), "rect": rect, "automationId": automation_id, "registryCandidates": candidates})
|
||||
});
|
||||
let mut ui = json!(app.ui);
|
||||
// The viewer's Resolution is the active comp's (kept in the project).
|
||||
if let Some(v) = ui.get_mut("viewer").and_then(Value::as_object_mut) {
|
||||
v.insert("res".into(), json!(app.viewer_res()));
|
||||
}
|
||||
json!({
|
||||
"window": [size.x, size.y],
|
||||
"pixelsPerPoint": ctx.pixels_per_point(),
|
||||
"fps": app.fps,
|
||||
"ui": app.ui,
|
||||
"ui": ui,
|
||||
"playing": app.playback.playing,
|
||||
"time": app.session.time().seconds(),
|
||||
"activeComp": app.session.state.active_comp.map(|i| i.0),
|
||||
|
||||
@@ -826,27 +826,26 @@ impl EffectcraftApp {
|
||||
self.viewer_image.clone()
|
||||
}
|
||||
|
||||
/// The viewer's Resolution: the active composition's (saved with the project).
|
||||
pub fn viewer_res(&self) -> state::Resolution {
|
||||
self.session.active_comp().map(|c| c.resolution).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// The render scale used by the viewer right now.
|
||||
pub fn viewer_scale(&self, zoom: f32, ppp: f32) -> f64 {
|
||||
// The preview's Resolution (when not Auto) while it plays.
|
||||
if self.playback.playing
|
||||
&& let Some(f) = self.playback.res
|
||||
{
|
||||
let r = match f {
|
||||
1 => state::Resolution::Full,
|
||||
2 => state::Resolution::Half,
|
||||
3 => state::Resolution::Third,
|
||||
4 => state::Resolution::Quarter,
|
||||
n => state::Resolution::Custom(n.min(40) as u8),
|
||||
};
|
||||
return r.scale(zoom, ppp);
|
||||
return state::Resolution::from_factor(f.into()).scale(zoom, ppp);
|
||||
}
|
||||
// Auto follows the zoom alike while playing and paused, so frames cached while scrubbing
|
||||
// play back, and frames cached by a preview stay on screen when it stops.
|
||||
let full = self.ui.viewer.res.scale(zoom, ppp);
|
||||
let res = self.viewer_res();
|
||||
let full = res.scale(zoom, ppp);
|
||||
// Fast Previews ▸ Adaptive Resolution / Fast Draft: lower resolution while dragging.
|
||||
let (_, k) = self.session.state.viewer.fast_previews.render(self.ui.viewer.interacting || self.ui.viewer.property_interacting);
|
||||
if k < 1.0 && self.ui.viewer.res == state::Resolution::Auto {
|
||||
if k < 1.0 && res.is_auto() {
|
||||
return full.min((full * 0.5).max(self.session.prefs.adaptive_limit()));
|
||||
}
|
||||
full
|
||||
|
||||
+13
-34
@@ -35,7 +35,6 @@ pub const UI_COMMANDS: &[UiCommand] = &[
|
||||
uic!("playback.stop", "Stop", [], None),
|
||||
uic!("view.fit", "Fit", [], Some("Shift+/")),
|
||||
uic!("view.actualSize", "100%", [], Some("/")),
|
||||
uic!("view.res.auto", "Resolution: Auto", [], None),
|
||||
uic!("viewer.nudge.up", "Move Selected Layers Up 1 Pixel at Current Magnification", [], Some("ArrowUp")),
|
||||
uic!("viewer.nudge.down", "Move Selected Layers Down 1 Pixel at Current Magnification", [], Some("ArrowDown")),
|
||||
uic!("viewer.nudge.left", "Move Selected Layers Left 1 Pixel at Current Magnification", [], Some("ArrowLeft")),
|
||||
@@ -529,10 +528,7 @@ pub fn invoke(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, params: V
|
||||
crate::panels::markers_ui::open_dialog(app, crate::panels::markers_ui::MarkerRef { layer, index })?;
|
||||
return Ok(json!({"dialog": id}));
|
||||
}
|
||||
if id == "view.res.auto" {
|
||||
app.ui.viewer.res = Resolution::Auto;
|
||||
return Ok(Value::Null);
|
||||
}
|
||||
|
||||
if id == "view.theme.toggle" {
|
||||
app.cycle_appearance(ctx);
|
||||
return Ok(json!({"appearanceMode": app.session.prefs.appearance.appearance_mode}));
|
||||
@@ -928,33 +924,19 @@ pub fn frontend(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, p: Valu
|
||||
v.zoom = Some(next);
|
||||
json!({"zoom": next})
|
||||
}
|
||||
"view.res.full" | "view.res.half" | "view.res.third" | "view.res.quarter" => {
|
||||
let r = id.trim_start_matches("view.res.");
|
||||
v.res = Resolution::ALL.into_iter().find(|x| x.label().eq_ignore_ascii_case(r)).ok_or("unknown resolution")?;
|
||||
Value::Null
|
||||
}
|
||||
// View ▸ Resolution ▸ Custom without a factor (the engine sets the comp's resolution).
|
||||
"view.res.custom" => {
|
||||
match p.get("factor").and_then(Value::as_u64) {
|
||||
Some(n) => {
|
||||
v.res = match n.clamp(1, 40) {
|
||||
1 => Resolution::Full,
|
||||
n => Resolution::Custom(n as u8),
|
||||
};
|
||||
}
|
||||
None => {
|
||||
let cur = match v.res {
|
||||
Resolution::Custom(n) => n as f64,
|
||||
_ => 2.0,
|
||||
};
|
||||
crate::panels::dialogs::form(
|
||||
app,
|
||||
"Custom Resolution",
|
||||
"view.res.custom",
|
||||
json!({}),
|
||||
vec![crate::panels::dialogs::Field::num("factor", "Render every n-th pixel", cur)],
|
||||
);
|
||||
}
|
||||
}
|
||||
let cur = match app.viewer_res() {
|
||||
Resolution::Custom(n) => f64::from(n),
|
||||
_ => 2.0,
|
||||
};
|
||||
crate::panels::dialogs::form(
|
||||
app,
|
||||
"Custom Resolution",
|
||||
"view.res.custom",
|
||||
json!({}),
|
||||
vec![crate::panels::dialogs::Field::num("factor", "Render every n-th pixel", cur)],
|
||||
);
|
||||
Value::Null
|
||||
}
|
||||
"view.rulers" => toggle(&mut v.rulers, &p),
|
||||
@@ -1450,9 +1432,6 @@ pub(crate) fn entry_checked(app: &EffectcraftApp, e: &MenuEntry) -> Option<bool>
|
||||
"view.layerControls" => Some(v.show_layer_controls),
|
||||
"playback.cacheWhenIdle" => Some(app.ui.cache_when_idle),
|
||||
"playback.audio" => Some(app.session.prefs.preview.active().include_audio),
|
||||
"view.res.full" | "view.res.half" | "view.res.third" | "view.res.quarter" => {
|
||||
Some(v.res.label().eq_ignore_ascii_case(e.command.trim_start_matches("view.res.")))
|
||||
}
|
||||
"window.workspace" => Some(pstr("name").is_some_and(|n| n == app.ui.workspace)),
|
||||
// The Tools panel is the header toolbar: always on screen.
|
||||
"window.panel" => Some(pstr("panel").is_some_and(|n| n.eq_ignore_ascii_case("tools") || PanelKind::from_name(n).is_some_and(|p| panel_shown(app, p)))),
|
||||
|
||||
@@ -314,7 +314,6 @@ pub fn state_key(app: &EffectcraftApp) -> u64 {
|
||||
s.keymaps.active.hash(&mut h);
|
||||
let v = &app.ui.viewer;
|
||||
(v.rulers, v.guides, v.snap_guides, v.lock_guides, v.grid, v.snap_grid, v.show_layer_controls, v.pasteboard).hash(&mut h);
|
||||
v.res.label().hash(&mut h);
|
||||
(app.ui.cache_when_idle, app.session.prefs.preview.active().include_audio, &app.ui.workspace, app.dialog.is_some()).hash(&mut h);
|
||||
// Window menu check marks: the panels on screen.
|
||||
crate::dock::PanelKind::ALL.iter().filter(|p| crate::menus::panel_shown(app, **p)).for_each(|p| p.hash(&mut h));
|
||||
|
||||
@@ -629,13 +629,13 @@ fn bottom_bar_contents(app: &mut EffectcraftApp, ui: &mut egui::Ui, bar: Rect, z
|
||||
// Resolution / down-sample factor.
|
||||
let r = Rect::from_min_size(pos2(x, cy - 10.0), vec2(84.0, 20.0));
|
||||
let scale = app.viewer_scale(zoom, ppp);
|
||||
let response = widgets::dropdown(ui, r, &resolution_label(app.ui.viewer.res, scale), &t, egui::Id::new("vw-res"))
|
||||
let res = app.viewer_res();
|
||||
let response = widgets::dropdown(ui, r, &resolution_label(res, scale), &t, egui::Id::new("vw-res"))
|
||||
.on_hover_text(crate::i18n::tr("Resolution/Down Sample Factor Popup"));
|
||||
if response.clicked() {
|
||||
toggle_popup(ui, "vw-res-pop");
|
||||
}
|
||||
app.auto.add_response("viewer.resolution", &response, "Resolution/Down Sample Factor");
|
||||
let res = app.ui.viewer.res;
|
||||
let items: Vec<(String, bool)> = vec![
|
||||
("Full".into(), res == Resolution::Full),
|
||||
("Half".into(), res == Resolution::Half),
|
||||
|
||||
@@ -224,49 +224,9 @@ impl Tool {
|
||||
}
|
||||
}
|
||||
|
||||
/// Viewer resolution (Auto follows the magnification).
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum Resolution {
|
||||
#[default]
|
||||
Auto,
|
||||
Full,
|
||||
Half,
|
||||
Third,
|
||||
Quarter,
|
||||
/// View ▸ Resolution ▸ Custom: render every n-th pixel (2..=40).
|
||||
Custom(u8),
|
||||
}
|
||||
|
||||
impl Resolution {
|
||||
pub const ALL: [Resolution; 5] = [Resolution::Auto, Resolution::Full, Resolution::Half, Resolution::Third, Resolution::Quarter];
|
||||
pub fn label(self) -> &'static str {
|
||||
match self {
|
||||
Resolution::Auto => "Auto",
|
||||
Resolution::Full => "Full",
|
||||
Resolution::Half => "Half",
|
||||
Resolution::Third => "Third",
|
||||
Resolution::Quarter => "Quarter",
|
||||
Resolution::Custom(_) => "Custom",
|
||||
}
|
||||
}
|
||||
/// Render scale for a viewer magnification (and display pixel density).
|
||||
pub fn scale(self, zoom: f32, ppp: f32) -> f64 {
|
||||
match self {
|
||||
// As in After Effects, Auto renders the pixels the magnification needs: the coarsest
|
||||
// whole factor that still gives every screen pixel a rendered one (Full above 50 %,
|
||||
// Half down to 33.3 %, Third down to 25 %, Quarter below), never fewer (#417).
|
||||
Resolution::Auto => {
|
||||
let n = (1.0 / (zoom * ppp) as f64 + 1e-3).floor();
|
||||
if n >= 1.0 { 1.0 / n.min(4.0) } else { 1.0 }
|
||||
}
|
||||
Resolution::Full => 1.0,
|
||||
Resolution::Half => 0.5,
|
||||
Resolution::Third => 1.0 / 3.0,
|
||||
Resolution::Quarter => 0.25,
|
||||
Resolution::Custom(n) => 1.0 / n.max(1) as f64,
|
||||
}
|
||||
}
|
||||
}
|
||||
/// The viewer's Resolution: each composition keeps its own in the project
|
||||
/// ([`effectcraft_engine::project::Comp::resolution`], `EffectcraftApp::viewer_res`).
|
||||
pub use effectcraft_engine::project::Resolution;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
@@ -275,7 +235,6 @@ pub struct ViewerState {
|
||||
pub zoom: Option<f32>,
|
||||
/// Pan offset in points from the centred position.
|
||||
pub pan: [f32; 2],
|
||||
pub res: Resolution,
|
||||
pub transparency_grid: bool,
|
||||
pub show_masks: bool,
|
||||
pub safe_margins: bool,
|
||||
@@ -314,7 +273,6 @@ impl Default for ViewerState {
|
||||
ViewerState {
|
||||
zoom: None,
|
||||
pan: [0.0, 0.0],
|
||||
res: Resolution::Auto,
|
||||
transparency_grid: false,
|
||||
show_masks: true,
|
||||
safe_margins: false,
|
||||
|
||||
Reference in New Issue
Block a user