From 475cf71d118a590ce830d210f4cdc3f0918eab3c Mon Sep 17 00:00:00 2001 From: Piotr Podusowski Date: Tue, 18 Jul 2023 21:04:43 +0200 Subject: [PATCH 1/5] center at window --- examples/osm.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/examples/osm.rs b/examples/osm.rs index 4fc77d5f..34b1d7d6 100644 --- a/examples/osm.rs +++ b/examples/osm.rs @@ -1,5 +1,5 @@ use egui::{Align2, Context, Painter, RichText, Shape, Ui, Window}; -use walkers::{Map, MapMemory, Position, PositionExt, Tiles}; +use walkers::{Center, Map, MapMemory, Position, PositionExt, Tiles}; fn main() -> Result<(), eframe::Error> { env_logger::init(); @@ -109,7 +109,6 @@ impl eframe::App for Osm { .title_bar(false) .anchor(Align2::LEFT_BOTTOM, [10., -10.]) .show(ui.ctx(), |ui| { - ui.label(format!("zoom: {}", self.map_memory.zoom.round())); ui.horizontal(|ui| { if ui.button(RichText::new("➕").heading()).clicked() { let _ = self.map_memory.zoom.zoom_in(); @@ -120,6 +119,23 @@ impl eframe::App for Osm { } }); }); + + if let Center::Exact(position) = self.map_memory.center_mode { + Window::new("Center") + .collapsible(false) + .resizable(false) + .title_bar(false) + .anchor(Align2::RIGHT_BOTTOM, [-10., -10.]) + .show(ui.ctx(), |ui| { + ui.label(format!("{:.04} {:.04}", position.x(), position.y())); + if ui + .button(RichText::new("go to my (fake) position ").heading()) + .clicked() + { + self.map_memory.center_mode = Center::MyPosition; + } + }); + } }); } } From 4fa77d3fee391dfd5a71270b45c402a419ca80a3 Mon Sep 17 00:00:00 2001 From: Piotr Podusowski Date: Tue, 18 Jul 2023 21:05:40 +0200 Subject: [PATCH 2/5] comment --- examples/osm.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/osm.rs b/examples/osm.rs index 34b1d7d6..a0daf694 100644 --- a/examples/osm.rs +++ b/examples/osm.rs @@ -120,6 +120,7 @@ impl eframe::App for Osm { }); }); + // When map is "detached", show a windows with an option to go back to my position. if let Center::Exact(position) = self.map_memory.center_mode { Window::new("Center") .collapsible(false) From f936932f2545b94d8c7b75437d7a5efb49891d9c Mon Sep 17 00:00:00 2001 From: Piotr Podusowski Date: Tue, 18 Jul 2023 21:10:03 +0200 Subject: [PATCH 3/5] put windows to functions --- examples/osm.rs | 121 +++++++++++++++++++++++++----------------------- 1 file changed, 64 insertions(+), 57 deletions(-) diff --git a/examples/osm.rs b/examples/osm.rs index a0daf694..bd766db1 100644 --- a/examples/osm.rs +++ b/examples/osm.rs @@ -24,6 +24,36 @@ impl Osm { } } +impl eframe::App for Osm { + fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { + egui::CentralPanel::default().show(ctx, |ui| { + ui.horizontal(|ui| { + ui.label("following map uses data from"); + ui.hyperlink("https://www.openstreetmap.org"); + ui.label(", please consider donating at"); + ui.hyperlink("https://donate.openstreetmap.org/"); + }); + + // Typically this would be a GPS acquired position which is tracked by the map. + let my_position = wroclaw_glowny(); + + // Draw the actual map. + let response = ui.add(Map::new( + Some(&mut self.tiles), + &mut self.map_memory, + wroclaw_glowny(), + )); + + // Draw custom shapes. + let painter = ui.painter().with_clip_rect(response.rect); + draw_custom_shapes(ui, painter, &self.map_memory, my_position); + + zoom_window(ui, &mut self.map_memory); + go_to_my_position_window(ui, &mut self.map_memory); + }); + } +} + /// Main train station of the city of Wrocław. /// https://en.wikipedia.org/wiki/Wroc%C5%82aw_G%C5%82%C3%B3wny_railway_station fn wroclaw_glowny() -> Position { @@ -78,65 +108,42 @@ fn draw_custom_shapes(ui: &Ui, painter: Painter, map_memory: &MapMemory, my_posi painter.add(text); } -impl eframe::App for Osm { - fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { - egui::CentralPanel::default().show(ctx, |ui| { +/// Simple GUI to zoom in and out. +fn zoom_window(ui: &Ui, map_memory: &mut MapMemory) { + Window::new("Map") + .collapsible(false) + .resizable(false) + .title_bar(false) + .anchor(Align2::LEFT_BOTTOM, [10., -10.]) + .show(ui.ctx(), |ui| { ui.horizontal(|ui| { - ui.label("following map uses data from"); - ui.hyperlink("https://www.openstreetmap.org"); - ui.label(", please consider donating at"); - ui.hyperlink("https://donate.openstreetmap.org/"); - }); + if ui.button(RichText::new("➕").heading()).clicked() { + let _ = map_memory.zoom.zoom_in(); + } - // Typically this would be a GPS acquired position which is tracked by the map. - let my_position = wroclaw_glowny(); - - // Draw the actual map. - let response = ui.add(Map::new( - Some(&mut self.tiles), - &mut self.map_memory, - wroclaw_glowny(), - )); - - // Draw custom shapes. - let painter = ui.painter().with_clip_rect(response.rect); - draw_custom_shapes(ui, painter, &self.map_memory, my_position); - - // Simple GUI to zoom in and out. - Window::new("Map") - .collapsible(false) - .resizable(false) - .title_bar(false) - .anchor(Align2::LEFT_BOTTOM, [10., -10.]) - .show(ui.ctx(), |ui| { - ui.horizontal(|ui| { - if ui.button(RichText::new("➕").heading()).clicked() { - let _ = self.map_memory.zoom.zoom_in(); - } - - if ui.button(RichText::new("➖").heading()).clicked() { - let _ = self.map_memory.zoom.zoom_out(); - } - }); - }); - - // When map is "detached", show a windows with an option to go back to my position. - if let Center::Exact(position) = self.map_memory.center_mode { - Window::new("Center") - .collapsible(false) - .resizable(false) - .title_bar(false) - .anchor(Align2::RIGHT_BOTTOM, [-10., -10.]) - .show(ui.ctx(), |ui| { - ui.label(format!("{:.04} {:.04}", position.x(), position.y())); - if ui - .button(RichText::new("go to my (fake) position ").heading()) - .clicked() - { - self.map_memory.center_mode = Center::MyPosition; - } - }); - } + if ui.button(RichText::new("➖").heading()).clicked() { + let _ = map_memory.zoom.zoom_out(); + } + }); }); +} + +/// When map is "detached", show a windows with an option to go back to my position. +fn go_to_my_position_window(ui: &Ui, map_memory: &mut MapMemory) { + if let Center::Exact(position) = map_memory.center_mode { + Window::new("Center") + .collapsible(false) + .resizable(false) + .title_bar(false) + .anchor(Align2::RIGHT_BOTTOM, [-10., -10.]) + .show(ui.ctx(), |ui| { + ui.label(format!("{:.04} {:.04}", position.x(), position.y())); + if ui + .button(RichText::new("go to my (fake) position ").heading()) + .clicked() + { + map_memory.center_mode = Center::MyPosition; + } + }); } } From 85ff3d5b86e81d91295aad06d415ced7fd8250a4 Mon Sep 17 00:00:00 2001 From: Piotr Podusowski Date: Tue, 18 Jul 2023 21:11:00 +0200 Subject: [PATCH 4/5] rename --- examples/osm.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/osm.rs b/examples/osm.rs index bd766db1..a4688af2 100644 --- a/examples/osm.rs +++ b/examples/osm.rs @@ -6,16 +6,16 @@ fn main() -> Result<(), eframe::Error> { eframe::run_native( "OpenStreetMap", Default::default(), - Box::new(|cc| Box::new(Osm::new(cc.egui_ctx.clone()))), + Box::new(|cc| Box::new(MyApp::new(cc.egui_ctx.clone()))), ) } -struct Osm { +struct MyApp { tiles: Tiles, map_memory: MapMemory, } -impl Osm { +impl MyApp { fn new(egui_ctx: Context) -> Self { Self { tiles: Tiles::new(walkers::openstreetmap, egui_ctx), @@ -24,7 +24,7 @@ impl Osm { } } -impl eframe::App for Osm { +impl eframe::App for MyApp { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { egui::CentralPanel::default().show(ctx, |ui| { ui.horizontal(|ui| { From 08482538cda1f73e4344ca1576b924897b077dcd Mon Sep 17 00:00:00 2001 From: Piotr Podusowski Date: Tue, 18 Jul 2023 21:13:19 +0200 Subject: [PATCH 5/5] use s binding with clearer name --- examples/osm.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/osm.rs b/examples/osm.rs index a4688af2..ddfd9ac2 100644 --- a/examples/osm.rs +++ b/examples/osm.rs @@ -41,7 +41,7 @@ impl eframe::App for MyApp { let response = ui.add(Map::new( Some(&mut self.tiles), &mut self.map_memory, - wroclaw_glowny(), + my_position, )); // Draw custom shapes.