diff --git a/src/ui_roads.rs b/src/ui_roads.rs index 5221657..250b819 100644 --- a/src/ui_roads.rs +++ b/src/ui_roads.rs @@ -127,7 +127,7 @@ impl RoadTypes { let Some(roots) = roots else { return }; let left_hand = ed.doc.as_ref().is_some_and(|doc| doc.global.left_hand_traffic); let mut open = self.open; - egui::Window::new(icons::labelled(Icon::Road, tr("Road types", "Типы дорог"))).open(&mut open).default_size(Vec2::new(760.0, 520.0)).show(ctx, |ui| { + egui::Window::new(icons::labelled(Icon::Road, tr("Road types", "Типы дорог"))).open(&mut open).default_size(Vec2::new(760.0, 520.0)).constrain(true).max_height((ctx.content_rect().height() - 48.0).max(0.0)).show(ctx, |ui| { if ui.add_enabled(ed.doc.is_some(), egui::Button::new(tr("Apply bus lane rules to all existing roads", "Применить правила автобусных полос ко всем существующим дорогам"))).clicked() { self.pending_bus_rules = Some(ed.preview_all_bus_rules()); self.bus_rule_result = None; } @@ -151,6 +151,10 @@ impl RoadTypes { if confirm || cancel { self.pending_bus_rules = None; } } if let Some(count) = self.bus_rule_result { ui.label(format!("{}: {count}", tr("Rules added", "Добавлено правил"))); } + if let Some(e) = &self.error { + ui.colored_label(Color32::from_rgb(240, 120, 100), e); + } + let columns_height = ui.available_height(); ui.horizontal_top(|ui| { // the list ui.vertical(|ui| { @@ -160,7 +164,7 @@ impl RoadTypes { self.maker = Some(RoadSpec { name: tr("My road", "Моя дорога").into(), ..Default::default() }); } ui.add(egui::TextEdit::singleline(&mut self.search).hint_text(tr("Search", "Поиск"))); - egui::ScrollArea::vertical().id_salt("road types").max_height(440.0).show(ui, |ui| { + egui::ScrollArea::vertical().id_salt("road types").max_height(ui.available_height()).auto_shrink([false, false]).show(ui, |ui| { for cat in [Category::Roads, Category::Tracks] { for a in lib.filter(Kind::Spline, Some(cat), &self.search) { let on = self.picked.as_deref().map(|p| p.eq_ignore_ascii_case(&a.rel)).unwrap_or(false); @@ -179,112 +183,119 @@ impl RoadTypes { }); ui.separator(); ui.vertical(|ui| { + ui.set_height(columns_height); let mut cancel = false; if let Some(spec) = &mut self.maker { - Self::maker_ui(ui, spec, &mut self.textures, &mut self.tex_search, roots, &mut self.pics, &mut self.canvas.open, left_hand, self.maker_target.as_ref()); - if let Some(warning) = spec.maker_filename_warning(self.maker_target.as_ref()) { - ui.colored_label(Color32::YELLOW, warning); - } - ui.add_space(6.0); - ui.horizontal(|ui| { - if ui.button(icons::labelled(Icon::CheckCircle, tr("Create the road type", "Создать тип дороги"))).clicked() { - match spec.write_in_maker(&roots.content, left_hand, self.maker_target.as_ref()) { - Ok(rel) => { - if spec.pocket > 0.0 { - ed.pocket_type = Some(rel.clone()); + // Reserve the footer before the scrolling body, including wrapped warnings. + egui::Panel::bottom("road maker footer").frame(egui::Frame::NONE).show(ui, |ui| { + if let Some(warning) = spec.maker_filename_warning(self.maker_target.as_ref()) { + ui.colored_label(Color32::YELLOW, warning); + } + ui.add_space(6.0); + ui.horizontal(|ui| { + if ui.button(icons::labelled(Icon::CheckCircle, tr("Create the road type", "Создать тип дороги"))).clicked() { + match spec.write_in_maker(&roots.content, left_hand, self.maker_target.as_ref()) { + Ok(rel) => { + if spec.pocket > 0.0 { + ed.pocket_type = Some(rel.clone()); + } + ed.road_type = Some(rel.clone()); + ed.set_tool(crate::editor::Tool::Roads); + let path = crate::doc::safe::own_path(&roots.content, &rel); + self.maker_target = MakerTarget::open(rel.clone(), path); + self.picked = Some(rel); + self.error = None; + actions.push(Action::ReloadContent(tr("Road type made: the Roads tool builds with it", "Тип дороги создан: инструмент «Дороги» строит им").to_string())); } - ed.road_type = Some(rel.clone()); - ed.set_tool(crate::editor::Tool::Roads); - let path = crate::doc::safe::own_path(&roots.content, &rel); - self.maker_target = MakerTarget::open(rel.clone(), path); - self.picked = Some(rel); - self.error = None; - actions.push(Action::ReloadContent(tr("Road type made: the Roads tool builds with it", "Тип дороги создан: инструмент «Дороги» строит им").to_string())); + Err(e) => self.error = Some(e.to_string()), } - Err(e) => self.error = Some(e.to_string()), } - } - if ui.button(tr("Cancel", "Отмена")).clicked() { - cancel = true; - } + if ui.button(tr("Cancel", "Отмена")).clicked() { + cancel = true; + } + }); }); - } else if let Some(rel) = self.picked.clone() { - let name = lib.find(&rel).map(|a| a.name.clone()).unwrap_or_else(|| rel.clone()); - ui.label(RichText::new(name).strong().color(Color32::WHITE)); - crate::ui::long_label(ui, &rel, egui::TextStyle::Small, Color32::from_gray(140)); - ui.add_space(6.0); - if ui.button(icons::labelled(Icon::Road, tr("Build with it", "Строить им"))).clicked() { - ed.road_type = Some(rel.clone()); - ed.set_tool(crate::editor::Tool::Roads); - } - let path = roots.resolve(&rel); - if let (Some(spec), Some(target)) = (RoadSpec::of_with_traffic(&path, left_hand), MakerTarget::open(rel.clone(), path)) { - if ui.button(icons::labelled(Icon::Settings, tr("Change it in the road maker", "Изменить в конструкторе"))).clicked() { - self.maker = Some(spec); - self.maker_target = Some(target); - } - } - ui.add_space(8.0); - ui.label(RichText::new(tr("Junction pieces for it", "Перекрёстки для неё")).strong()); - let (pieces, fits) = lib.junction_pieces(&rel); - let preferred = ed.road_styles.piece(&rel).map(str::to_string); - let mut pick = preferred.clone(); - ui.horizontal(|ui| { - let shown = preferred.as_deref().and_then(|p| lib.find(p)).map(|a| a.name.clone()).unwrap_or_else(|| tr("made by openMapper", "делает openMapper").to_string()); - egui::ComboBox::from_id_salt("preferred piece").width(260.0).height(300.0).selected_text(shown).show_ui(ui, |ui| { - if ui.selectable_label(preferred.is_none(), tr("made by openMapper", "делает openMapper")).clicked() { - pick = None; + egui::ScrollArea::vertical().id_salt("road maker").auto_shrink([false, false]).max_height(ui.available_height()).show(ui, |ui| { + Self::maker_ui(ui, spec, &mut self.textures, &mut self.tex_search, roots, &mut self.pics, &mut self.canvas.open, left_hand, self.maker_target.as_ref()); + }); + } else { + egui::ScrollArea::vertical().id_salt("road maker").auto_shrink([false, false]).max_height(ui.available_height()).show(ui, |ui| { + if let Some(rel) = self.picked.clone() { + let name = lib.find(&rel).map(|a| a.name.clone()).unwrap_or_else(|| rel.clone()); + ui.label(RichText::new(name).strong().color(Color32::WHITE)); + crate::ui::long_label(ui, &rel, egui::TextStyle::Small, Color32::from_gray(140)); + ui.add_space(6.0); + if ui.button(icons::labelled(Icon::Road, tr("Build with it", "Строить им"))).clicked() { + ed.road_type = Some(rel.clone()); + ed.set_tool(crate::editor::Tool::Roads); } - for a in pieces.iter().take(300) { - if ui.selectable_label(preferred.as_deref().map(|p| p.eq_ignore_ascii_case(&a.rel)).unwrap_or(false), format!("{} · {}", a.name, a.dir)).clicked() { - pick = Some(a.rel.clone()); + let path = roots.resolve(&rel); + if let (Some(spec), Some(target)) = (RoadSpec::of_with_traffic(&path, left_hand), MakerTarget::open(rel.clone(), path)) { + if ui.button(icons::labelled(Icon::Settings, tr("Change it in the road maker", "Изменить в конструкторе"))).clicked() { + self.maker = Some(spec); + self.maker_target = Some(target); } } - }); - if let Some(p) = preferred.as_deref() { - if ui.button(icons::labelled(Icon::Junction, tr("Place it", "Поставить"))).on_hover_text(tr("Choose it in the catalogue to place on the map", "Выбрать в каталоге для установки на карту")).clicked() { - actions.push(Action::PlaceAsset(p.to_string())); + ui.add_space(8.0); + ui.label(RichText::new(tr("Junction pieces for it", "Перекрёстки для неё")).strong()); + let (pieces, fits) = lib.junction_pieces(&rel); + let preferred = ed.road_styles.piece(&rel).map(str::to_string); + let mut pick = preferred.clone(); + ui.horizontal(|ui| { + let shown = preferred.as_deref().and_then(|p| lib.find(p)).map(|a| a.name.clone()).unwrap_or_else(|| tr("made by openMapper", "делает openMapper").to_string()); + egui::ComboBox::from_id_salt("preferred piece").width(260.0).height(300.0).selected_text(shown).show_ui(ui, |ui| { + if ui.selectable_label(preferred.is_none(), tr("made by openMapper", "делает openMapper")).clicked() { + pick = None; + } + for a in pieces.iter().take(300) { + if ui.selectable_label(preferred.as_deref().map(|p| p.eq_ignore_ascii_case(&a.rel)).unwrap_or(false), format!("{} · {}", a.name, a.dir)).clicked() { + pick = Some(a.rel.clone()); + } + } + }); + if let Some(p) = preferred.as_deref() { + if ui.button(icons::labelled(Icon::Junction, tr("Place it", "Поставить"))).on_hover_text(tr("Choose it in the catalogue to place on the map", "Выбрать в каталоге для установки на карту")).clicked() { + actions.push(Action::PlaceAsset(p.to_string())); + } + } + }); + ui.label(RichText::new(if pieces.is_empty() { + tr("The installation has no junction pieces with places for roads.", "В установке нет перекрёстков с местами для дорог.") + } else if fits { + tr("Pieces with a place made for this road type. The one picked is offered first when a junction is put on this road's end (select the road: Junction on an end).", "Перекрёстки с местом под этот тип дороги. Выбранный предлагается первым, когда перекрёсток ставят на конец такой дороги (выделите дорогу: «Перекрёсток на конце дороги»).") + } else { + tr("No piece is made for this road type; the list holds every piece with places for roads.", "Под этот тип дороги перекрёстков нет; в списке все перекрёстки с местами для дорог.") + }).small().color(Color32::from_gray(140))); + if pick != preferred { + ed.road_styles.set_piece(&rel, pick); + if let Err(e) = ed.road_styles.save(&roots.content) { + self.error = Some(e.to_string()); + } } + ui.add_space(8.0); + ui.label(RichText::new(tr("Its junctions", "Его перекрёстки")).strong()); + let mut st = ed.road_styles.get(&rel); + let before = st; + crate::ui::check(ui, &mut st.junctions, tr("Make a junction where it meets another road (else they only cross)", "Делать перекрёсток, где она встречает другую дорогу (иначе просто пересекаются)")); + ui.horizontal(|ui| { + ui.label(tr("Corners", "Углы")); + ui.add(egui::Slider::new(&mut st.corner, 0.5..=3.0).text(tr("× rounder", "× круглее"))); + }); + crate::ui::check(ui, &mut st.lights, tr("Traffic lights by themselves", "Светофоры сами")); + crate::ui::check(ui, &mut st.crossings, tr("Zebra crossings on every arm", "Пешеходные переходы на всех въездах")); + crate::ui::check(ui, &mut st.corner_walks, tr("Footway paths round the corners (AI)", "Пешеходные пути вокруг углов (ИИ)")); + ui.label(RichText::new(tr("Off: no AI paths at the corners; the crossings still join the roads' footways. Join the corners yourself (e.g. with an invisible path spline).", "Выкл.: без путей ИИ на углах; переходы по-прежнему соединены с тротуарами дорог. Соедините углы сами (например, невидимым сплайном пути).")).small().color(Color32::from_gray(140))); + ui.label(RichText::new(tr("Used for the junctions made from now on (where several road types meet, the one asking more wins). A junction already made keeps its own settings: select it to change them.", "Применяется к новым перекрёсткам (где встречаются разные дороги, берётся большее). Готовый перекрёсток хранит свои настройки: выделите его, чтобы изменить.")).small().color(Color32::from_gray(140))); + if st != before { + ed.road_styles.set(&rel, st); + if let Err(e) = ed.road_styles.save(&roots.content) { + self.error = Some(e.to_string()); + } + } + } else { + ui.label(tr("Pick a road type on the left, or make a new one.", "Выберите тип дороги слева или создайте новый.")); } }); - ui.label(RichText::new(if pieces.is_empty() { - tr("The installation has no junction pieces with places for roads.", "В установке нет перекрёстков с местами для дорог.") - } else if fits { - tr("Pieces with a place made for this road type. The one picked is offered first when a junction is put on this road's end (select the road: Junction on an end).", "Перекрёстки с местом под этот тип дороги. Выбранный предлагается первым, когда перекрёсток ставят на конец такой дороги (выделите дорогу: «Перекрёсток на конце дороги»).") - } else { - tr("No piece is made for this road type; the list holds every piece with places for roads.", "Под этот тип дороги перекрёстков нет; в списке все перекрёстки с местами для дорог.") - }).small().color(Color32::from_gray(140))); - if pick != preferred { - ed.road_styles.set_piece(&rel, pick); - if let Err(e) = ed.road_styles.save(&roots.content) { - self.error = Some(e.to_string()); - } - } - ui.add_space(8.0); - ui.label(RichText::new(tr("Its junctions", "Его перекрёстки")).strong()); - let mut st = ed.road_styles.get(&rel); - let before = st; - crate::ui::check(ui, &mut st.junctions, tr("Make a junction where it meets another road (else they only cross)", "Делать перекрёсток, где она встречает другую дорогу (иначе просто пересекаются)")); - ui.horizontal(|ui| { - ui.label(tr("Corners", "Углы")); - ui.add(egui::Slider::new(&mut st.corner, 0.5..=3.0).text(tr("× rounder", "× круглее"))); - }); - crate::ui::check(ui, &mut st.lights, tr("Traffic lights by themselves", "Светофоры сами")); - crate::ui::check(ui, &mut st.crossings, tr("Zebra crossings on every arm", "Пешеходные переходы на всех въездах")); - crate::ui::check(ui, &mut st.corner_walks, tr("Footway paths round the corners (AI)", "Пешеходные пути вокруг углов (ИИ)")); - ui.label(RichText::new(tr("Off: no AI paths at the corners; the crossings still join the roads' footways. Join the corners yourself (e.g. with an invisible path spline).", "Выкл.: без путей ИИ на углах; переходы по-прежнему соединены с тротуарами дорог. Соедините углы сами (например, невидимым сплайном пути).")).small().color(Color32::from_gray(140))); - ui.label(RichText::new(tr("Used for the junctions made from now on (where several road types meet, the one asking more wins). A junction already made keeps its own settings: select it to change them.", "Применяется к новым перекрёсткам (где встречаются разные дороги, берётся большее). Готовый перекрёсток хранит свои настройки: выделите его, чтобы изменить.")).small().color(Color32::from_gray(140))); - if st != before { - ed.road_styles.set(&rel, st); - if let Err(e) = ed.road_styles.save(&roots.content) { - self.error = Some(e.to_string()); - } - } - } else { - ui.label(tr("Pick a road type on the left, or make a new one.", "Выберите тип дороги слева или создайте новый.")); - } - if let Some(e) = &self.error { - ui.colored_label(Color32::from_rgb(240, 120, 100), e); } if cancel { self.maker = None; @@ -486,3 +497,6 @@ impl RoadTypes { ui.label(RichText::new(tr("The roadway texture is laid once across all its lanes (as OMSI 2's lane textures are painted: pick one with as many lanes); the footway texture's right end is the kerb.", "Текстура проезжей части ложится один раз поперёк всех полос (как нарисованы полосные текстуры OMSI 2: берите с тем же числом полос); правый край текстуры тротуара: бордюр.")).small().color(Color32::from_gray(140))); } } + +#[cfg(test)] +mod scroll_tests; diff --git a/src/ui_roads/scroll_tests.rs b/src/ui_roads/scroll_tests.rs new file mode 100644 index 0000000..d6e7f63 --- /dev/null +++ b/src/ui_roads/scroll_tests.rs @@ -0,0 +1,161 @@ +use super::*; + +// Exercise real egui layout and input without a GPU or an OMSI installation. +#[test] +fn maker_scroll_keeps_actions_visible() { + for size in [Vec2::new(1366.0, 768.0), Vec2::new(1920.0, 1080.0)] { + for advanced in [false, true] { + for bus_preview in [false, true] { + eprintln!("case {size:?} advanced={advanced} preview={bus_preview}"); + let ctx = egui::Context::default(); + ctx.set_fonts(icons::fonts()); + let screen = egui::Rect::from_min_size(Pos2::ZERO, size); + let mut roads = RoadTypes::default(); + roads.open_maker(false); + let spec = roads.maker.as_mut().unwrap(); + spec.name = "My road?".into(); + spec.advanced = advanced; + spec.marked = true; + spec.markings = vec![Marking::default(); 5]; + if advanced { + spec.strips = vec![ + crate::road_maker::Strip::Lane { + dir: crate::road_maker::Direction::Fwd, + width: 3.0, + bus_only: false, + }; + 8 + ]; + } + if bus_preview { + roads.pending_bus_rules = Some( + (0..20) + .map(|i| crate::road_maker::bus_rules::batch::Group { + file: format!("road-{i}.sli"), + splines: Vec::new(), + paths: Vec::new(), + }) + .collect(), + ); + } + let mut ed = Editor::default(); + let lib = Library::default(); + let temp = std::env::temp_dir().join(format!( + "openmapper-scroll-{}-{}-{advanced}", + std::process::id(), + size.x + )); + let _ = std::fs::remove_dir_all(&temp); + let roots = Roots { + omsi: temp.join("omsi"), + content: temp.join("content"), + }; + std::fs::create_dir_all(&temp).unwrap(); + let texture = temp.join("asphalt.png"); + image::RgbaImage::from_pixel(8, 8, image::Rgba([70, 70, 70, 255])) + .save(&texture) + .unwrap(); + roads.maker.as_mut().unwrap().roadway_tex = texture; + // Avoid scanning the working directory for textures. + roads.textures = Some(Vec::new()); + let mut actions = Vec::new(); + let mut frame = |events| { + ctx.run_ui( + egui::RawInput { + screen_rect: Some(screen), + events, + ..Default::default() + }, + |root| { + roads.window(root.ctx(), &mut ed, &lib, Some(&roots), &mut actions); + }, + ) + }; + let text_rect = |output: &egui::FullOutput, needle: &str| { + output + .shapes + .iter() + .rev() + .find_map(|shape| { + if let egui::Shape::Text(text) = &shape.shape { + if text.galley.job.text.contains(needle) { + return Some(( + egui::Rect::from_min_size(text.pos, text.galley.size()), + shape.clip_rect, + )); + } + } + None + }) + .unwrap_or_else(|| panic!("missing text: {needle}")) + }; + let mut output = frame(Vec::new()); + for _ in 0..4 { + output = frame(Vec::new()); + } + let (create, clip) = text_rect(&output, "Create the road type"); + assert!(screen.contains_rect(create) && clip.contains_rect(create)); + let (warning, clip) = text_rect(&output, "Some characters in the road name"); + assert!(screen.contains_rect(warning) && clip.contains_rect(warning)); + let (title, title_clip) = text_rect(&output, "Road maker"); + assert!(title_clip.contains_rect(title)); + let hover = title.center() + Vec2::new(100.0, 80.0); + for _ in 0..8 { + output = frame(vec![ + egui::Event::PointerMoved(hover), + egui::Event::MouseWheel { + unit: egui::MouseWheelUnit::Point, + phase: egui::TouchPhase::Move, + delta: Vec2::new(0.0, -10000.0), + modifiers: egui::Modifiers::NONE, + }, + ]); + } + let (last, clip) = text_rect(&output, "The roadway texture is laid once"); + assert!( + clip.contains_rect(last), + "cannot reach bottom: {size:?}, advanced={advanced}: {last:?} / {clip:?}" + ); + let (after, clip) = text_rect(&output, "Create the road type"); + assert_eq!(create, after); + assert!(screen.contains_rect(after) && clip.contains_rect(after)); + // A real click must reach Create, not only paint it within the screen. + for pressed in [true, false] { + output = frame(vec![ + egui::Event::PointerMoved(after.center()), + egui::Event::PointerButton { + pos: after.center(), + button: egui::PointerButton::Primary, + pressed, + modifiers: egui::Modifiers::NONE, + }, + ]); + } + let (cancel, clip) = text_rect(&output, "Cancel"); + assert!(clip.contains_rect(cancel)); + let pos = cancel.center(); + for pressed in [true, false] { + frame(vec![ + egui::Event::PointerMoved(pos), + egui::Event::PointerButton { + pos, + button: egui::PointerButton::Primary, + pressed, + modifiers: egui::Modifiers::NONE, + }, + ]); + } + drop(frame); + assert!(roads.maker.is_none(), "Cancel must remain clickable"); + assert!( + actions + .iter() + .any(|a| matches!(a, Action::ReloadContent(_))), + "Create must write the road: {:?}", + roads.error + ); + std::fs::remove_dir_all(temp).unwrap(); + } + } + } +}