Skip to content

Commit

Permalink
Merge pull request #18 from podusowski/example
Browse files Browse the repository at this point in the history
Some minor-ish changes in the example
  • Loading branch information
podusowski authored Jul 18, 2023
2 parents 0d0f5ca + 0848253 commit b2a9b0a
Showing 1 changed file with 68 additions and 44 deletions.
112 changes: 68 additions & 44 deletions examples/osm.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
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();
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),
Expand All @@ -24,6 +24,36 @@ impl 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| {
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,
my_position,
));

// 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 {
Expand Down Expand Up @@ -78,48 +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/");
});

// Typically this would be a GPS acquired position which is tracked by the map.
let my_position = wroclaw_glowny();
if ui.button(RichText::new("➕").heading()).clicked() {
let _ = map_memory.zoom.zoom_in();
}

// 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.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();
}

if ui.button(RichText::new("➖").heading()).clicked() {
let _ = self.map_memory.zoom.zoom_out();
}
});
});
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;
}
});
}
}

0 comments on commit b2a9b0a

Please sign in to comment.