diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index 89c12bb0..8673fda0 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -232,10 +232,18 @@ fn default_export_dir() -> String { } } +/// The split layout starts with the 3D and 2D views at equal width +fn default_split_ratio() -> f32 { + 0.5 +} + #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct UiState { pub mode: Mode, pub layout: ViewLayout, + /// The 3D view's share of the viewport width in the split layout + #[serde(default = "default_split_ratio")] + pub split_ratio: f32, /// The docked, tabbed and floating panels around the viewport. #[serde(default)] pub dock: Dock, @@ -310,6 +318,7 @@ impl Default for UiState { UiState { mode: Mode::Painting, layout: ViewLayout::Split, + split_ratio: default_split_ratio(), dock: Dock::default(), inactive_mode_dock: Dock::for_baking(), dock_is_baking: false, diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index da60a42d..78618f1e 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -3,6 +3,7 @@ use std::sync::Mutex; +use egui::emath::GuiRounding as _; use egui::{Color32, PointerButton, Pos2, Rect, RichText, Sense, Stroke, Ui, pos2, vec2}; use serde_json::json; use texturecraft_engine::camera_cmds::DEFAULT_CAMERA; @@ -34,6 +35,7 @@ pub fn set_focus_picking(ctx: &egui::Context, on: bool) { ctx.data_mut(|d| d.insert_temp(focus_pick_id(), on)); } +/// The viewport dock tab: the paint bar, then the 3D and 2D views in the chosen layout with the mode's overlays pub fn central(app: &mut TextureCraftApp, ui: &mut Ui) { let mode = app.ui.mode; let bake_painting = mode == crate::state::Mode::Baking @@ -43,6 +45,7 @@ pub fn central(app: &mut TextureCraftApp, ui: &mut Ui) { !texture_set.bake.high_poly.use_low_as_high && texture_set.bake.cage.kind == texturecraft_engine::core::CageType::DistanceBased }); app.ui.bake_paint_brush.on = bake_painting; + if !app.ui.hide_ui { crate::toolbar::paint_bar(app, ui); } @@ -50,27 +53,39 @@ pub fn central(app: &mut TextureCraftApp, ui: &mut Ui) { let tokens = Tokens::get(ui.ctx()); let aux = match app.session.view { _ if mode == crate::state::Mode::Painting && app.session.quick_mask.editing => Some(crate::gpu::AuxView::QuickMask), - ViewMode::MeshMap(m) => Some(crate::gpu::AuxView::MeshMap(m)), + ViewMode::MeshMap(map) => Some(crate::gpu::AuxView::MeshMap(map)), ViewMode::Mask(id) => Some(crate::gpu::AuxView::Mask(id)), _ => None, }; let mut sync = PendingSync(Some(aux)); let full = ui.available_rect_before_wrap(); path_keys(app, ui); + // Skew painting needs the 2D view alongside the 3D Bake preview let layout = if mode == crate::state::Mode::Painting || bake_painting { app.ui.layout } else { ViewLayout::Only3D }; match layout { ViewLayout::Only3D => view_3d(app, ui, full, &mut sync), ViewLayout::Only2D => view_2d(app, ui, full, &mut sync), ViewLayout::Split => { - let midpoint = full.center().x; - let left = Rect::from_min_max(full.min, pos2(midpoint - 1.0, full.max.y)); - let right = Rect::from_min_max(pos2(midpoint + 1.0, full.min.y), full.max); + // The 2D view's edges sit on whole pixels, so rounding can't nudge a texture its zoom keeps at one size + let pixels_per_point = ui.ctx().pixels_per_point(); + let full = full.with_max_x(full.right().round_to_pixels(pixels_per_point)); + let divider_x = split_divider_x(app, ui, full); + let left = Rect::from_min_max(full.min, pos2(divider_x - SPLIT_LINE_HALF_WIDTH, full.max.y)); + let right = Rect::from_min_max(pos2(divider_x + SPLIT_LINE_HALF_WIDTH, full.min.y), full.max); + // Each view draws only on its side, so overlays such as the texture set name stay out of a narrow neighbour + let clip = ui.clip_rect(); + ui.set_clip_rect(left.intersect(clip)); view_3d(app, ui, left, &mut sync); + ui.set_clip_rect(right.intersect(clip)); view_2d(app, ui, right, &mut sync); - ui.painter().line_segment([pos2(midpoint, full.top()), pos2(midpoint, full.bottom())], Stroke::new(2.0, tokens.chrome)); + ui.set_clip_rect(clip); + split_divider_grab(ui, full, divider_x); + let line = Stroke::new(SPLIT_LINE_HALF_WIDTH * 2.0, tokens.chrome); + ui.painter().line_segment([pos2(divider_x, full.top()), pos2(divider_x, full.bottom())], line); } } + if mode == crate::state::Mode::Rendering { crate::render_mode::show(app, ui, full); } else if app.render.is_active() { @@ -79,17 +94,109 @@ pub fn central(app: &mut TextureCraftApp, ui: &mut Ui) { if mode == crate::state::Mode::Baking { crate::bake_mode::bake_bar(app, ui.ctx(), full); } + // Keep drawing until the 3D view finishes accumulating anti-aliasing samples let accumulating = layout != ViewLayout::Only2D - && app - .render_state - .as_ref() - .is_some_and(|rs| rs.renderer.read().callback_resources.get::().is_some_and(|g| g.renderer.accumulating(VIEW_3D))); + && app.render_state.as_ref().is_some_and(|render_state| { + render_state.renderer.read().callback_resources.get::().is_some_and(|gpu| gpu.renderer.accumulating(VIEW_3D)) + }); if app.session.stroke_active() || accumulating { ui.ctx().request_repaint(); } } +/// Half the split divider's line width: each view starts this far from the line's centre +const SPLIT_LINE_HALF_WIDTH: f32 = 1.0; + +/// Narrowest a split view may get: a sliver that is still easy to grab and drag back out +const MIN_SPLIT_VIEW_WIDTH: f32 = 16.0; + +/// Width of the split divider's grab zone, centred on its line +const SPLIT_DIVIDER_GRAB_WIDTH: f32 = 8.0; + +/// The split divider's widget id, shared by its drag read and its grab zone +fn split_divider_id(ui: &Ui) -> egui::Id { + ui.scope_id().with("split_divider") +} + +/// The 3D view's share of `width`, keeping both views at least [`MIN_SPLIT_VIEW_WIDTH`] wide when they fit +fn clamp_split_ratio(ratio: f32, width: f32) -> f32 { + let min_ratio = (MIN_SPLIT_VIEW_WIDTH / width).min(0.5); + let ratio = if ratio.is_finite() { ratio } else { 0.5 }; + ratio.clamp(min_ratio, 1.0 - min_ratio) +} + +/// The split divider's x position: while it is dragged it follows the pointer and changes the 2D zoom so the texture keeps its size on screen +fn split_divider_x(app: &mut TextureCraftApp, ui: &Ui, full: Rect) -> f32 { + // The 2D view's left edge, beside the line, sits on a whole pixel + let pixels_per_point = ui.ctx().pixels_per_point(); + let divider_x = |ratio: f32| { + let unsnapped = full.left() + full.width() * clamp_split_ratio(ratio, full.width()); + (unsnapped + SPLIT_LINE_HALF_WIDTH).round_to_pixels(pixels_per_point) - SPLIT_LINE_HALF_WIDTH + }; + let old_x = divider_x(app.ui.split_ratio); + + // Read the drag before the views lay out so they follow the pointer in the same frame + let ctx = ui.ctx(); + if ctx.is_being_dragged(split_divider_id(ui)) + && ctx.input(|i| i.pointer.primary_down()) + && let Some(pointer) = ctx.pointer_interact_pos() + { + // `max` and `min` rather than `clamp`, so a NaN bound from a corrupt saved zoom can't panic + let (min_x, max_x) = divider_range_keeping_uv_size(&app.ui, full, old_x, pixels_per_point); + let target_x = pointer.x.max(min_x).min(max_x); + app.ui.split_ratio = clamp_split_ratio((target_x - full.left()) / full.width(), full.width()); + let new_x = divider_x(app.ui.split_ratio); + keep_uv_view_on_screen(&mut app.ui, full, old_x, new_x); + } + + divider_x(app.ui.split_ratio) +} + +/// Lowest 2D zoom, for the wheel and the divider (inside the renderer's own limit of 0.05) +const UV_VIEW_MIN_ZOOM: f32 = 0.1; + +/// Highest 2D zoom, for the wheel and the divider (inside the renderer's own limit of 64) +const UV_VIEW_MAX_ZOOM: f32 = 40.0; + +/// The 2D view's width when the split divider is at `divider_x` +fn uv_view_width(full: Rect, divider_x: f32) -> f32 { + (full.right() - divider_x - SPLIT_LINE_HALF_WIDTH).max(1.0) +} + +/// The share of the 2D view's height that a zoom of 1 fills at `width`: the view fits the texture to its narrower side +fn uv_view_fit(full: Rect, width: f32) -> f32 { + (width / full.height().max(1.0)).min(1.0) +} + +/// The divider positions where the 2D zoom that keeps the texture's screen size stays inside the 2D zoom range +fn divider_range_keeping_uv_size(ui_state: &crate::state::UiState, full: Rect, old_x: f32, pixels_per_point: f32) -> (f32, f32) { + // The zoom times the fit is the texture's screen size, which a divider drag keeps + let screen_zoom = ui_state.uv_zoom * uv_view_fit(full, uv_view_width(full, old_x)); + + // Whole pixels, rounded inward, so snapping the divider to a pixel can't step past the range + let narrowest_view = (full.height() * screen_zoom / UV_VIEW_MAX_ZOOM * pixels_per_point).ceil() / pixels_per_point; + let widest_view = (full.height() * screen_zoom / UV_VIEW_MIN_ZOOM * pixels_per_point).floor() / pixels_per_point; + + let edge = full.right() - SPLIT_LINE_HALF_WIDTH; + (edge - widest_view, edge - narrowest_view) +} + +/// Keeps the 2D view's texture at the same size and offset from the view's centre when the divider moves its left edge from `old_x` to `new_x` +fn keep_uv_view_on_screen(ui_state: &mut crate::state::UiState, full: Rect, old_x: f32, new_x: f32) { + let (old_width, new_width) = (uv_view_width(full, old_x), uv_view_width(full, new_x)); + ui_state.uv_zoom *= uv_view_fit(full, old_width) / uv_view_fit(full, new_width); + + // The horizontal pan is in half view widths + ui_state.uv_pan[0] *= old_width / new_width; +} + +/// The split divider's grab zone with a resize cursor, claimed after the views so it wins the pointer over them +fn split_divider_grab(ui: &Ui, full: Rect, divider_x: f32) { + let zone = Rect::from_x_y_ranges(divider_x - SPLIT_DIVIDER_GRAB_WIDTH * 0.5..=divider_x + SPLIT_DIVIDER_GRAB_WIDTH * 0.5, full.y_range()); + ui.interact(zone, split_divider_id(ui), Sense::drag()).on_hover_and_drag_cursor(egui::CursorIcon::ResizeHorizontal); +} + fn shade(app: &TextureCraftApp) -> Shade { let (view_mode, channel) = match app.session.view { _ if app.ui.mode == crate::state::Mode::Painting && app.session.quick_mask.editing => (2, 0), @@ -167,6 +274,13 @@ const MAX_NAME_CHARS: usize = 32; /// that view handled its input: a stroke shows in the frame it is painted, not the next. pub struct PendingSync(Option>); +/// A view's render size in pixels, rounded as egui rounds the rect it draws the frame into, so the frame is never stretched +fn frame_size_in_pixels(ui: &Ui, rect: Rect) -> [u32; 2] { + let pixels_per_point = ui.ctx().pixels_per_point(); + let pixels = |from: f32, to: f32| ((to * pixels_per_point).round() - (from * pixels_per_point).round()).max(1.0) as u32; + [pixels(rect.left(), rect.right()), pixels(rect.top(), rect.bottom())] +} + fn add_callback(app: &mut TextureCraftApp, ui: &Ui, rect: Rect, frame: Frame, sync: &mut PendingSync) { let tk = Tokens::get(ui.ctx()); let scene = match (sync.0.take(), app.gpu.as_mut()) { @@ -192,20 +306,38 @@ fn add_callback(app: &mut TextureCraftApp, ui: &Ui, rect: Rect, frame: Frame, sy ui.painter().add(eframe::egui_wgpu::Callback::new_paint_callback(rect, cb)); } -/// The view-mode dropdown shown at the top of each view. +/// Width of the camera and view pickers centred at the top of a view +const VIEW_HEADER_WIDTH: f32 = 300.0; + +/// Height of the view pickers' row +const VIEW_HEADER_HEIGHT: f32 = 22.0; + +/// Gap between the view's top edge and its pickers +const VIEW_HEADER_TOP_GAP: f32 = 8.0; + +/// Width of each of the two pickers +const VIEW_PICKER_WIDTH: f32 = 130.0; + +/// The camera and view pickers at the top of a view, left out when the view is too narrow to hold them fn view_header(app: &mut TextureCraftApp, ui: &mut Ui, rect: Rect, id: &str) { - let header = Rect::from_min_size(rect.min + vec2(rect.width() * 0.5 - 150.0, 8.0), vec2(300.0, 22.0)); + if rect.width() < VIEW_HEADER_WIDTH { + return; + } + + let header_min = rect.min + vec2((rect.width() - VIEW_HEADER_WIDTH) * 0.5, VIEW_HEADER_TOP_GAP); + let header = Rect::from_min_size(header_min, vec2(VIEW_HEADER_WIDTH, VIEW_HEADER_HEIGHT)); let mut child = ui.new_child(egui::UiBuilder::new().max_rect(header).layout(egui::Layout::left_to_right(egui::Align::Center))); + let current = texturecraft_engine::camera_cmds::current(&app.session); - let names: Vec = app.session.doc.project.cameras.iter().map(|c| c.name.clone()).collect(); - let shown = current.and_then(|i| names.get(i)).map_or(tr(DEFAULT_CAMERA), String::as_str); - egui::ComboBox::from_id_salt(format!("{id}_cam")).width(130.0).selected_text(shown).show_ui(&mut child, |ui| { + let names: Vec = app.session.doc.project.cameras.iter().map(|camera| camera.name.clone()).collect(); + let shown = current.and_then(|index| names.get(index)).map_or(tr(DEFAULT_CAMERA), String::as_str); + egui::ComboBox::from_id_salt(format!("{id}_cam")).width(VIEW_PICKER_WIDTH).selected_text(shown).show_ui(&mut child, |ui| { if ui.selectable_label(current.is_none(), tr(DEFAULT_CAMERA)).clicked() { app.run("camera.select", json!({})); } - for (i, name) in names.iter().enumerate() { - if ui.selectable_label(current == Some(i), name).clicked() { - app.run("camera.select", json!({"index": i})); + for (index, name) in names.iter().enumerate() { + if ui.selectable_label(current == Some(index), name).clicked() { + app.run("camera.select", json!({"index": index})); } } ui.separator(); @@ -213,26 +345,27 @@ fn view_header(app: &mut TextureCraftApp, ui: &mut Ui, rect: Rect, id: &str) { app.run("camera.add", json!({})); } }); + let label = match app.session.view { ViewMode::MeshMap(map) => crate::i18n::baked_map_label(map).to_owned(), _ => app.session.view.label(), }; - let channels = app.session.doc.project.texture_set(app.session.ts()).map(|t| t.channel_list()).unwrap_or_default(); - egui::ComboBox::from_id_salt(format!("{id}_mode")).width(130.0).selected_text(tr(&label)).show_ui(&mut child, |ui| { + let channels = app.session.doc.project.texture_set(app.session.ts()).map(|texture_set| texture_set.channel_list()).unwrap_or_default(); + egui::ComboBox::from_id_salt(format!("{id}_mode")).width(VIEW_PICKER_WIDTH).selected_text(tr(&label)).show_ui(&mut child, |ui| { if ui.selectable_label(app.session.view == ViewMode::Material, tr("Material")).clicked() { app.run("viewport.view", json!({"mode": "material"})); } ui.separator(); - for c in channels { - if ui.selectable_label(app.session.view == ViewMode::Channel(c), tr(c.label())).clicked() { - app.run("viewport.view", json!({"mode": c.id()})); + for channel in channels { + if ui.selectable_label(app.session.view == ViewMode::Channel(channel), tr(channel.label())).clicked() { + app.run("viewport.view", json!({"mode": channel.id()})); } } ui.separator(); - for m in BakedMap::ALL { - if ui.selectable_label(app.session.view == ViewMode::MeshMap(m), trf("Mesh map: {map}", &[("map", &tr(crate::i18n::baked_map_label(m)))])).clicked() - { - app.run("viewport.view", json!({"mode": format!("map:{}", m.id())})); + for map in BakedMap::ALL { + let map_label = trf("Mesh map: {map}", &[("map", &tr(crate::i18n::baked_map_label(map)))]); + if ui.selectable_label(app.session.view == ViewMode::MeshMap(map), map_label).clicked() { + app.run("viewport.view", json!({"mode": format!("map:{}", map.id())})); } } ui.separator(); @@ -429,7 +562,7 @@ fn view_3d(app: &mut TextureCraftApp, ui: &mut Ui, rect: Rect, sync: &mut Pendin let frame = Frame { view_id: crate::viewport::VIEW_3D, - size: [(size.x * ui.ctx().pixels_per_point()).max(1.0) as u32, (size.y * ui.ctx().pixels_per_point()).max(1.0) as u32], + size: frame_size_in_pixels(ui, rect), kind: ViewKind::Scene, camera: app.session.camera, shade: shade(app), @@ -968,7 +1101,7 @@ fn view_2d(app: &mut TextureCraftApp, ui: &mut Ui, rect: Rect, sync: &mut Pendin let scroll = ui.input(|i| i.smooth_scroll_delta.y); if scroll != 0.0 { let old = app.ui.uv_zoom; - let new = (old * (1.0 + scroll * 0.002)).clamp(0.1, 40.0); + let new = (old * (1.0 + scroll * 0.002)).clamp(UV_VIEW_MIN_ZOOM, UV_VIEW_MAX_ZOOM); // Zoom about the cursor. if let Some(hp) = resp.hover_pos() { let c = [(hp.x - rect.left()) / size.x * 2.0 - 1.0, 1.0 - (hp.y - rect.top()) / size.y * 2.0]; @@ -1042,7 +1175,7 @@ fn view_2d(app: &mut TextureCraftApp, ui: &mut Ui, rect: Rect, sync: &mut Pendin } let frame = Frame { view_id: VIEW_2D, - size: [(size.x * ui.ctx().pixels_per_point()).max(1.0) as u32, (size.y * ui.ctx().pixels_per_point()).max(1.0) as u32], + size: frame_size_in_pixels(ui, rect), kind: ViewKind::Uv { ts, zoom: app.ui.uv_zoom, pan: app.ui.uv_pan }, camera: app.session.camera, shade: shade(app), diff --git a/crates/ui-egui/tests/split_divider.rs b/crates/ui-egui/tests/split_divider.rs new file mode 100644 index 00000000..d5af8c8f --- /dev/null +++ b/crates/ui-egui/tests/split_divider.rs @@ -0,0 +1,209 @@ +//! The divider between the 3D and 2D views in the split layout resizes them and keeps the pointer from the views + +use egui::{Event, Modifiers, PointerButton, Pos2, pos2}; +use egui_kittest::Harness; +use texturecraft_engine::core::Channel; +use texturecraft_ui_egui::TextureCraftApp; +use texturecraft_ui_egui::state::UiState; + +/// Narrowest a split view may get, as in `viewport.rs` +const MIN_VIEW_WIDTH: f32 = 16.0; + +/// A press clear of the divider's 8 point grab zone and egui's 5 point interact radius +const BESIDE_DIVIDER: f32 = 16.0; + +/// App storage held in memory, saved the way the app saves its state between runs +#[derive(Default)] +struct MemoryStorage(std::collections::HashMap); + +impl eframe::Storage for MemoryStorage { + fn get_string(&self, key: &str) -> Option { + self.0.get(key).cloned() + } + + fn set_string(&mut self, key: &str, value: String) { + self.0.insert(key.to_owned(), value); + } + + fn remove_string(&mut self, key: &str) { + self.0.remove(key); + } + + fn flush(&mut self) {} +} + +/// The sample project in the default split layout at 1600 x 960 +fn split_app() -> Harness<'static, TextureCraftApp> { + let session = texturecraft_engine::sample_session(256, 64).unwrap(); + let mut harness = Harness::builder().with_size(egui::vec2(1600.0, 960.0)).build_eframe(|cc| TextureCraftApp::new(cc, session)); + harness.run_steps(4); + harness +} + +/// Drag with `button` from `from` to `to` in a few moves +fn drag(harness: &mut Harness<'_, TextureCraftApp>, button: PointerButton, from: Pos2, to: Pos2) { + harness.hover_at(from); + harness.run_steps(1); + harness.event(Event::PointerButton { pos: from, button, pressed: true, modifiers: Modifiers::NONE }); + harness.run_steps(2); + + for step in 1..=4 { + harness.hover_at(from + (to - from) * (step as f32 / 4.0)); + harness.run_steps(1); + } + + harness.event(Event::PointerButton { pos: to, button, pressed: false, modifiers: Modifiers::NONE }); + harness.run_steps(3); +} + +/// The divider's x position: the 3D view ends 1 point left of it +fn divider_x(harness: &Harness<'_, TextureCraftApp>) -> f32 { + harness.state().viewport_rect.right() + 1.0 +} + +/// Allocated tiles in the selected layer's base color +fn painted_tiles(harness: &Harness<'_, TextureCraftApp>) -> usize { + let session = &harness.state().session; + let layer = session.selected_layer().expect("a selected layer"); + session.doc.pixels.layer(layer, Channel::BaseColor).map_or(0, |image| image.allocated_count()) +} + +/// The 2D view's texture as drawn: its centre's offset from the view's centre and its half width and height, in pixels at 1 point per pixel +fn uv_texture_on_screen(harness: &Harness<'_, TextureCraftApp>, views_right: f32) -> [f32; 4] { + let app = harness.state(); + + // egui draws the view's frame into its rect rounded to whole pixels + let width = views_right.round() - (divider_x(harness) + 1.0).round(); + let height = app.viewport_rect.bottom().round() - app.viewport_rect.top().round(); + let texture_size = app.session.doc.project.texture_sets[app.session.ts()].size(); + let [scale_x, scale_y] = texturecraft_render::uv_view_scale(width / height, app.ui.uv_zoom, texture_size); + let [pan_x, pan_y] = app.ui.uv_pan; + [pan_x * width * 0.5, pan_y * height * 0.5, scale_x * width * 0.5, scale_y * height * 0.5] +} + +/// The 3D camera's yaw and pitch +fn camera_angles(harness: &Harness<'_, TextureCraftApp>) -> (f32, f32) { + let camera = &harness.state().session.camera; + (camera.yaw, camera.pitch) +} + +#[test] +fn dragging_the_divider_resizes_the_views_within_their_minimum_width() { + let mut harness = split_app(); + let views_left = harness.state().viewport_rect.left(); + let start_x = divider_x(&harness); + let views_width = (start_x - views_left) * 2.0; + let views_right = views_left + views_width; + let y = harness.state().viewport_rect.center().y; + assert_eq!(harness.state().ui.split_ratio, 0.5); + + drag(&mut harness, PointerButton::Primary, pos2(start_x, y), pos2(start_x - 200.0, y)); + assert!((divider_x(&harness) - (start_x - 200.0)).abs() < 0.5, "follows the pointer: {}", divider_x(&harness)); + assert!((harness.state().ui.split_ratio - (start_x - 200.0 - views_left) / views_width).abs() < 1e-3); + + let x = divider_x(&harness); + drag(&mut harness, PointerButton::Primary, pos2(x, y), pos2(views_left + 10.0, y)); + assert!((divider_x(&harness) - views_left - MIN_VIEW_WIDTH).abs() < 0.5, "clamped on the left: {}", divider_x(&harness)); + + let x = divider_x(&harness); + drag(&mut harness, PointerButton::Primary, pos2(x, y), pos2(views_right - 10.0, y)); + assert!((views_right - divider_x(&harness) - MIN_VIEW_WIDTH).abs() < 0.5, "clamped on the right: {}", divider_x(&harness)); +} + +#[test] +fn pressing_on_the_divider_neither_paints_nor_moves_the_camera() { + let mut harness = split_app(); + + // Pan the texture's centre under the divider so a press there would paint if the 2D view got it + harness.state_mut().ui.uv_pan[0] = -1.0; + harness.run_steps(2); + let x = divider_x(&harness); + let y = harness.state().viewport_rect.center().y; + let (tiles, angles) = (painted_tiles(&harness), camera_angles(&harness)); + + harness.hover_at(pos2(x, y)); + harness.run_steps(1); + harness.event(Event::PointerButton { pos: pos2(x, y), button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + harness.event(Event::PointerButton { pos: pos2(x, y), button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + harness.run_steps(3); + drag(&mut harness, PointerButton::Primary, pos2(x, y), pos2(x, y + 120.0)); + drag(&mut harness, PointerButton::Secondary, pos2(x, y), pos2(x + 120.0, y)); + assert_eq!(painted_tiles(&harness), tiles, "nothing painted"); + assert_eq!(camera_angles(&harness), angles, "no orbit"); + assert_eq!(divider_x(&harness), x, "a vertical or right-button drag leaves the divider in place"); + + // Beside the divider the views get the same input again + drag(&mut harness, PointerButton::Primary, pos2(x + BESIDE_DIVIDER, y), pos2(x + BESIDE_DIVIDER, y + 40.0)); + assert!(painted_tiles(&harness) > tiles, "the 2D view paints"); + drag(&mut harness, PointerButton::Secondary, pos2(x - BESIDE_DIVIDER, y), pos2(x - BESIDE_DIVIDER - 120.0, y)); + assert_ne!(camera_angles(&harness), angles, "the 3D view orbits"); +} + +#[test] +fn the_divider_position_survives_a_restart_and_old_saved_states_load_centred() { + let mut storage = MemoryStorage::default(); + let mut ui_state = UiState::default(); + ui_state.split_ratio = 0.3; + eframe::set_value(&mut storage, "ui", &ui_state); + let restored = eframe::get_value::(&storage, "ui").expect("saved state loads"); + assert_eq!(restored.split_ratio, 0.3); + + // A state saved before the divider existed has no ratio + let saved = storage.0.get("ui").cloned().unwrap(); + let without_ratio = saved.replace("split_ratio:0.3,", ""); + assert_ne!(without_ratio, saved, "the saved state names the ratio: {saved}"); + storage.0.insert("ui".to_owned(), without_ratio); + let restored = eframe::get_value::(&storage, "ui").expect("an older saved state loads"); + assert_eq!(restored.split_ratio, 0.5); +} + +#[test] +fn dragging_the_divider_keeps_the_uv_texture_size_and_offset_on_screen() { + // Zoomed in, a 2D sliver needs a zoom far past the one the view starts with + for zoom in [1.0, 4.0] { + let mut harness = split_app(); + harness.state_mut().ui.uv_zoom = zoom; + harness.state_mut().ui.uv_pan = [0.2, 0.1]; + harness.run_steps(2); + // The split starts centred, so the views end as far right of the divider as they start left of it + let views_left = harness.state().viewport_rect.left(); + let views_right = views_left + (divider_x(&harness) - views_left) * 2.0; + let y = harness.state().viewport_rect.center().y; + let before = uv_texture_on_screen(&harness, views_right); + + let assert_unchanged = |harness: &Harness<'_, TextureCraftApp>| { + let after = uv_texture_on_screen(harness, views_right); + for (was, is) in before.iter().zip(after) { + assert!((was - is).abs() < 0.5, "zoom {zoom}, divider at {}: {before:?} became {after:?}", divider_x(harness)); + } + }; + + for to in [views_left + 300.0, views_right - 150.0, views_right - 10.0, views_left + 500.0] { + let x = divider_x(&harness); + drag(&mut harness, PointerButton::Primary, pos2(x, y), pos2(to, y)); + assert_unchanged(&harness); + } + + // A slow drag past the right end, checked every frame for a nudge at the pixel rounding or the stop + let from = pos2(divider_x(&harness), y); + harness.hover_at(from); + harness.run_steps(1); + harness.event(Event::PointerButton { pos: from, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + harness.run_steps(1); + let mut pointer_x = from.x; + while pointer_x < views_right + 20.0 { + // Not a whole number of pixels, so the divider lands at every kind of rounding + pointer_x += 3.3; + harness.hover_at(pos2(pointer_x, y)); + harness.run_steps(1); + assert_unchanged(&harness); + } + harness.event(Event::PointerButton { pos: pos2(pointer_x, y), button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + harness.run_steps(2); + assert_unchanged(&harness); + + // Inside the wheel's 0.1 to 40 range, so the next scroll in the narrow view can't snap the zoom + let stopped_zoom = harness.state().ui.uv_zoom; + assert!((0.1..=40.0).contains(&stopped_zoom), "zoom {zoom} stopped at {stopped_zoom}"); + } +}