Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scale bar for map view #112

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions rgis-settings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub enum Tool {
pub struct RgisSettings {
pub target_crs: String,
pub current_tool: Tool,
pub show_scale: bool,
}

pub struct Plugin;
Expand All @@ -31,6 +32,7 @@ impl bevy::prelude::Plugin for Plugin {
app.insert_resource(RgisSettings {
target_crs: DEFAULT_TARGET_CRS.into(),
current_tool: Tool::Pan,
show_scale: true,
})
.add_system(systems::handle_crs_changed_events);
}
Expand Down
1 change: 1 addition & 0 deletions rgis-ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod feature_properties_window;
mod manage_layer_window;
mod message_window;
mod operation_window;
mod scale_bar;
mod side_panel;
mod systems;
mod top_panel;
Expand Down
96 changes: 96 additions & 0 deletions rgis-ui/src/scale_bar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use bevy::prelude::*;
use bevy_egui::egui::{self, Widget};

pub fn render_map_scale(
query: Query<
&bevy::transform::components::Transform,
bevy::ecs::query::With<bevy::render::camera::Camera>,
>,
mut bevy_egui_ctx: ResMut<bevy_egui::EguiContext>,
rgis_settings: Res<rgis_settings::RgisSettings>,
) {
if !rgis_settings.show_scale {
return;
}
let transform = query.single();
let scale = transform.scale.x;

let bar_max_width = 100.;
let bar_in_meter = desired_bar_length(scale * bar_max_width);
let bar_width = bar_in_meter / scale;
let bar_text = distance_to_readable_string(bar_in_meter);

egui::Window::new("Scale")
.frame(egui::Frame::none())
.open(&mut true)
.title_bar(false)
.anchor(egui::Align2::LEFT_BOTTOM, [4., -2.])
.fixed_size(egui::Vec2 {
x: bar_max_width,
y: 0.,
})
.show(bevy_egui_ctx.ctx_mut(), |ui| {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing = egui::Vec2 { x: 4., y: 0. };

egui::Button::new("")
.min_size(egui::Vec2 {
x: bar_width,
y: 0.,
})
.small()
.frame(false)
.sense(egui::Sense::hover())
.fill(egui::Color32::GRAY)
.ui(ui);

ui.label(
egui::RichText::new(bar_text)
// .small()
.background_color(ui.visuals().window_fill()), // .strong(),
);
});
});
}

fn desired_bar_length(full_bar_length: f32) -> f32 {
let mut desired_length = 2e38;

for i in full_bar_length.log10().floor() as i32..=f32::MAX_10_EXP {
for j in [1., 2., 5.] {
let length = 10_f32.powi(i) * j;
if full_bar_length < length {
break;
}
desired_length = length;
}
}
desired_length
}

fn distance_to_readable_string(distance: f32) -> String {
let res = get_proper_distance_unit(distance);
format!("{}{}m", (res.1 * distance).round(), res.0)
}

fn get_proper_distance_unit(distance: f32) -> (&'static str, f32) {
match distance.log10().floor() as i32 {
i32::MIN..=-16 => ("a", 1e18),
-15..=-13 => ("f", 1e15),
-12..=-10 => ("p", 1e12),
-9..=-7 => ("n", 1e9),
-6..=-4 => ("µ", 1e6),
-3..=-3 => ("m", 1e3),
-2..=-1 => ("c", 1e2),
0..=2 => ("", 1e0),
3..=8 => ("k", 1e-3), //human readable
// 6..=8 => ("M", 1e-6),
9..=11 => ("G", 1e-9),
12..=14 => ("T", 1e-12),
15..=17 => ("P", 1e-15),
18..=20 => ("E", 1e-18),
21..=23 => ("Z", 1e-21),
24..=26 => ("Y", 1e-24),
27..=i32::MAX => ("Y", 1e-24),
}
}
1 change: 1 addition & 0 deletions rgis-ui/src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ pub fn system_sets() -> [SystemSet; 2] {
.with_system(render_feature_properties_window.after(render_change_crs_window))
.with_system(render_operation_window.after(render_feature_properties_window))
.with_system(render_in_progress.after("top_bottom_panels"))
.with_system(crate::scale_bar::render_map_scale.after(render_side_panel))
.label("rgis_ui"),
]
}
Expand Down
13 changes: 13 additions & 0 deletions rgis-ui/src/top_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ impl<'a> TopPanel<'a> {
ui.add(FullScreenButton {
windows: self.windows,
});

if ui
.button(format!(
"{} scale",
match self.app_settings.show_scale {
true => "Hide",
false => "Show",
}
))
.clicked()
{
self.app_settings.show_scale = !self.app_settings.show_scale;
}
});
ui.menu_button("Help", |ui| {
if ui.button("Debug stats").clicked() {
Expand Down