Skip to content

Commit

Permalink
Merge pull request #28 from podusowski/mods
Browse files Browse the repository at this point in the history
Minor refactoring in the example
  • Loading branch information
podusowski authored Aug 6, 2023
2 parents 03a4c3e + f627687 commit 5dde30f
Showing 1 changed file with 98 additions and 83 deletions.
181 changes: 98 additions & 83 deletions examples/myapp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use egui::{Align2, Context, Painter, RichText, Shape, Ui, Vec2, Window};
use walkers::{Center, Map, MapMemory, Position, PositionExt, Tiles};
use egui::{Align2, Context, Painter, Shape, Ui, Vec2};
use walkers::{Map, MapMemory, Position, PositionExt, Tiles};

fn main() -> Result<(), eframe::Error> {
env_logger::init();
Expand Down Expand Up @@ -30,7 +30,7 @@ impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
// Typically this would be a GPS acquired position which is tracked by the map.
let my_position = wroclaw_glowny();
let my_position = places::wroclaw_glowny();

// Draw the actual map.
let response = ui.add(Map::new(
Expand All @@ -43,32 +43,42 @@ impl eframe::App for MyApp {
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);
// Draw utility windows.
{
use windows::*;

orthophotomap_window(
ui,
&mut self.geoportal_tiles,
&mut self.map_memory,
my_position,
);
zoom(ui, &mut self.map_memory);
go_to_my_position(ui, &mut self.map_memory);

acknowledge_window(ui);
orthophotomap(
ui,
&mut self.geoportal_tiles,
&mut self.map_memory,
my_position,
);

acknowledge(ui);
}
});
}
}

/// 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 {
Position::new(17.03664, 51.09916)
}
mod places {
//! Few common places in the city of Wrocław, used in the example app.
use walkers::Position;

/// Taking a public bus (line 106) is probably the cheapest option to get from
/// the train station to the airport.
/// https://www.wroclaw.pl/en/how-and-where-to-buy-public-transport-tickets-in-wroclaw
fn dworcowa_bus_stop() -> Position {
Position::new(17.03940, 51.10005)
/// Main train station of the city of Wrocław.
/// https://en.wikipedia.org/wiki/Wroc%C5%82aw_G%C5%82%C3%B3wny_railway_station
pub fn wroclaw_glowny() -> Position {
Position::new(17.03664, 51.09916)
}

/// Taking a public bus (line 106) is probably the cheapest option to get from
/// the train station to the airport.
/// https://www.wroclaw.pl/en/how-and-where-to-buy-public-transport-tickets-in-wroclaw
pub fn dworcowa_bus_stop() -> Position {
Position::new(17.03940, 51.10005)
}
}

/// Turn geographical position into location on the screen.
Expand All @@ -94,7 +104,7 @@ fn screen_position(
/// Shows how to draw various things in the map.
fn draw_custom_shapes(ui: &Ui, painter: Painter, map_memory: &MapMemory, my_position: Position) {
// Position of the point we want to put our shapes.
let position = dworcowa_bus_stop();
let position = places::dworcowa_bus_stop();
let screen_position = screen_position(position, &painter, map_memory, my_position);

// Now we can just use Painter to draw stuff.
Expand All @@ -120,74 +130,79 @@ fn draw_custom_shapes(ui: &Ui, painter: Painter, map_memory: &MapMemory, my_posi
painter.add(text);
}

fn acknowledge_window(ui: &Ui) {
Window::new("Acknowledge")
.collapsible(false)
.resizable(false)
.title_bar(false)
.anchor(Align2::LEFT_TOP, [10., 10.])
.fixed_size([150., 150.])
.show(ui.ctx(), |ui| {
ui.horizontal(|ui| {
ui.label("following map uses data from");
ui.hyperlink("https://www.openstreetmap.org");
});
});
}
mod windows {
use egui::{Align2, RichText, Ui, Window};
use walkers::{Center, Map, MapMemory, Position, Tiles};

fn orthophotomap_window(
ui: &Ui,
tiles: &mut Tiles,
map_memory: &mut MapMemory,
my_position: Position,
) {
Window::new("Orthophotomap")
.collapsible(false)
.resizable(false)
.title_bar(false)
.anchor(Align2::RIGHT_TOP, [-10., 10.])
.fixed_size([150., 150.])
.show(ui.ctx(), |ui| {
ui.add(Map::new(Some(tiles), map_memory, my_position));
});
}
pub fn acknowledge(ui: &Ui) {
Window::new("Acknowledge")
.collapsible(false)
.resizable(false)
.title_bar(false)
.anchor(Align2::LEFT_TOP, [10., 10.])
.fixed_size([150., 150.])
.show(ui.ctx(), |ui| {
ui.horizontal(|ui| {
ui.label("following map uses data from");
ui.hyperlink("https://www.openstreetmap.org");
});
});
}

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

if ui.button(RichText::new("➖").heading()).clicked() {
let _ = map_memory.zoom.zoom_out();
}
pub fn orthophotomap(
ui: &Ui,
tiles: &mut Tiles,
map_memory: &mut MapMemory,
my_position: Position,
) {
Window::new("Orthophotomap")
.collapsible(false)
.resizable(false)
.title_bar(false)
.anchor(Align2::RIGHT_TOP, [-10., 10.])
.fixed_size([150., 150.])
.show(ui.ctx(), |ui| {
ui.add(Map::new(Some(tiles), map_memory, my_position));
});
});
}
}

/// 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")
/// Simple GUI to zoom in and out.
pub fn zoom(ui: &Ui, map_memory: &mut MapMemory) {
Window::new("Map")
.collapsible(false)
.resizable(false)
.title_bar(false)
.anchor(Align2::RIGHT_BOTTOM, [-10., -10.])
.anchor(Align2::LEFT_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;
}
ui.horizontal(|ui| {
if ui.button(RichText::new("➕").heading()).clicked() {
let _ = map_memory.zoom.zoom_in();
}

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.
pub fn go_to_my_position(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 5dde30f

Please sign in to comment.