Skip to content

Commit 2f0b243

Browse files
committed
make stats more beautiful
1 parent 2078c75 commit 2f0b243

10 files changed

Lines changed: 175 additions & 115 deletions

File tree

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ egui_wgpu_backend = "0.34"
5151
egui_winit_platform = "0.26"
5252
egui_demo_lib = "0.31"
5353
egui_tiles = "0.12.0"
54+
egui_extras = "0.31.0"
55+
human_bytes = "0.4"
5456

5557
[dev-dependencies]
5658
insta = "1"

src/bin/drawing/ui/mod.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ use osm::css::CSSValue;
2222
use osm::css::Color;
2323
use osm::css::Number;
2424
use osm::css::Rule;
25-
use views::fps::FpsGraph;
25+
use views::fps::view_fps;
2626
use views::location_finder::LocationFinderWindow;
27-
use views::stats::StatsWindow;
27+
use views::panel_right::panel_right;
2828
use wgpu::SurfaceConfiguration;
2929
use widgets::tabs::Pane;
3030
use widgets::tabs::TabsBehavior;
@@ -33,7 +33,7 @@ use crate::app_state::AppState;
3333
use crate::app_state::EditableObject;
3434

3535
pub struct Hud {
36-
pub platform: egui_winit_platform::Platform,
36+
pub(crate) platform: egui_winit_platform::Platform,
3737
rpass: RenderPass,
3838
ui: HudUi,
3939
}
@@ -67,9 +67,7 @@ impl Hud {
6767
let rpass = RenderPass::new(device, surface_config.format, 1);
6868

6969
let ui = HudUi {
70-
stats_window: StatsWindow::new(true),
71-
location_finder_window: LocationFinderWindow::new(true),
72-
fps_graph: FpsGraph { open: true },
70+
location_finder_window: LocationFinderWindow::new(false),
7371
side_panel: SidePanel::new(),
7472
};
7573

@@ -206,9 +204,7 @@ fn add_display_none(ui: &mut Ui, rule: &mut Rule, label: &str) {
206204
}
207205

208206
struct HudUi {
209-
stats_window: StatsWindow,
210207
location_finder_window: LocationFinderWindow,
211-
fps_graph: FpsGraph,
212208
side_panel: SidePanel,
213209
}
214210

@@ -217,6 +213,9 @@ impl HudUi {
217213
{
218214
let pointer_position = ctx.input(|i| i.pointer.hover_pos()).unwrap_or_default();
219215

216+
// This should have precedence for grabbing keystrokes when it's open.
217+
self.location_finder_window.ui(ctx, app_state);
218+
220219
// Draw menubar.
221220
egui::TopBottomPanel::top("Main Menu Bar").show(ctx, |ui| {
222221
ui.horizontal_centered(|ui| {
@@ -240,8 +239,6 @@ impl HudUi {
240239
|ui| {},
241240
);
242241
ui.with_layout(Layout::right_to_left(egui::Align::Center), |ui| {
243-
self.location_finder_window.ui(ui, app_state);
244-
245242
ui.label(format!(
246243
"Frametime {:.2?} at zoom {:.2}",
247244
app_state.stats.get_average(),
@@ -252,13 +249,13 @@ impl HudUi {
252249
"Mouse Position: ({:.1},{:.1})",
253250
pointer_position[0], pointer_position[1]
254251
));
252+
253+
view_fps(ui, app_state);
255254
})
256255
})
257256
});
258257

259-
self.stats_window.ui(ctx, app_state);
260-
261-
self.fps_graph.ui(ctx, app_state);
258+
panel_right(ctx, app_state);
262259

263260
self.side_panel.ui(ctx, app_state);
264261
}

src/bin/drawing/ui/views.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
pub mod fps;
22
pub mod location_finder;
3+
pub mod panel_left;
4+
pub mod panel_right;
35
pub mod stats;

src/bin/drawing/ui/views/fps.rs

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,47 @@ use egui::{
22
containers::Frame,
33
emath,
44
epaint::{self, PathStroke},
5-
pos2, vec2, Color32, Pos2, Rect,
5+
pos2, vec2, Color32, Pos2, Rect, Ui,
66
};
77

88
use crate::{app_state::AppState, stats::FPS_SAMPLES};
99

1010
const WIDTH: f32 = 1.0;
1111

12-
#[derive(Default)]
13-
pub struct FpsGraph {
14-
pub open: bool,
15-
}
16-
17-
impl FpsGraph {
18-
pub fn ui(&mut self, ctx: &egui::Context, app_state: &mut AppState) {
19-
egui::Window::new("FPS")
20-
.open(&mut self.open)
21-
.default_pos([1390.0, 40.0])
22-
.default_size(vec2(500.0, 20.0))
23-
.vscroll(false)
24-
.show(ctx, |ui| {
25-
Frame::canvas(ui.style()).show(ui, |ui| {
26-
ui.ctx().request_repaint();
27-
28-
let desired_size = vec2(ui.available_width(), ui.available_height());
29-
let (_id, rect) = ui.allocate_space(desired_size);
30-
31-
let to_screen = emath::RectTransform::from_to(
32-
Rect::from_x_y_ranges(0.0..=1.0, -1.0..=1.0),
33-
rect,
34-
);
35-
36-
let mut shapes = vec![];
37-
38-
let points: Vec<Pos2> = app_state
39-
.stats
40-
.get_times()
41-
.skip(FPS_SAMPLES - rect.width() as usize)
42-
.enumerate()
43-
.map(|(x, y)| {
44-
to_screen
45-
* pos2(
46-
// Draw all the {ui.available_width()} last frame timings.
47-
x as f32 / rect.width(),
48-
// Draw y in relation to a maximum of 20 ms.
49-
-y.as_secs_f32() * 1000.0 / 20.0,
50-
)
51-
+ vec2(0.0, rect.height() / 2.0)
52-
})
53-
.collect();
54-
55-
shapes.push(epaint::Shape::line(
56-
points,
57-
PathStroke::new(WIDTH, Color32::WHITE),
58-
));
59-
60-
ui.painter().extend(shapes);
61-
});
62-
});
63-
}
12+
pub fn view_fps(ui: &mut Ui, app_state: &mut AppState) {
13+
Frame::canvas(ui.style()).show(ui, |ui| {
14+
ui.ctx().request_repaint();
15+
16+
let desired_size = vec2(ui.available_width(), ui.available_height());
17+
let (_id, rect) = ui.allocate_space(desired_size);
18+
19+
let to_screen =
20+
emath::RectTransform::from_to(Rect::from_x_y_ranges(0.0..=1.0, -1.0..=1.0), rect);
21+
22+
let mut shapes = vec![];
23+
24+
let points: Vec<Pos2> = app_state
25+
.stats
26+
.get_times()
27+
.skip(FPS_SAMPLES - rect.width() as usize)
28+
.enumerate()
29+
.map(|(x, y)| {
30+
to_screen
31+
* pos2(
32+
// Draw all the {ui.available_width()} last frame timings.
33+
x as f32 / rect.width(),
34+
// Draw y in relation to a maximum of 20 ms.
35+
-y.as_secs_f32() * 1000.0 / 20.0,
36+
)
37+
+ vec2(0.0, rect.height() / 2.0)
38+
})
39+
.collect();
40+
41+
shapes.push(epaint::Shape::line(
42+
points,
43+
PathStroke::new(WIDTH, Color32::WHITE),
44+
));
45+
46+
ui.painter().extend(shapes);
47+
});
6448
}

src/bin/drawing/ui/views/location_finder.rs

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use egui::Ui;
1+
use egui::Modifiers;
22

33
use crate::app_state::AppState;
44

@@ -11,22 +11,55 @@ impl LocationFinderWindow {
1111
Self { open }
1212
}
1313

14-
pub fn ui(&mut self, ui: &mut Ui, app_state: &mut AppState) {
15-
ui.label("Center Coordinates");
16-
ui.text_edit_singleline(&mut app_state.ui.loaction_finder.input);
17-
18-
if ui.button("Find").clicked() {
19-
let split: Result<Vec<f32>, _> = app_state
20-
.ui
21-
.loaction_finder
22-
.input
23-
.split(' ')
24-
.map(|s| s.parse::<f32>())
25-
.collect();
26-
if let Ok(split) = split {
27-
if split.len() == 2 {
28-
app_state.set_center((split[0], split[1]));
29-
}
14+
pub fn ui(&mut self, ctx: &egui::Context, app_state: &mut AppState) {
15+
let mut request_focus = false;
16+
if ctx.input_mut(|i| i.consume_key(Modifiers::COMMAND, egui::Key::F)) {
17+
self.open = true;
18+
request_focus = true;
19+
}
20+
21+
if self.open {
22+
let mut close = false;
23+
let mut valid = false;
24+
let width = 600.0;
25+
egui::Window::new("location finder")
26+
.default_pos([(ctx.screen_rect().width() - width) / 2.0, 40.0])
27+
.default_width(width)
28+
.default_height(100.0)
29+
.open(&mut self.open)
30+
.collapsible(false)
31+
.title_bar(false)
32+
.resizable(false)
33+
.show(ctx, |ui| {
34+
ui.label("Center Coordinates");
35+
let input = ui.text_edit_singleline(&mut app_state.ui.loaction_finder.input);
36+
if request_focus {
37+
input.request_focus();
38+
}
39+
40+
let split: Result<Vec<f32>, _> = app_state
41+
.ui
42+
.loaction_finder
43+
.input
44+
.split(' ')
45+
.map(|s| s.parse::<f32>())
46+
.collect();
47+
if let Ok(split) = split {
48+
if split.len() == 2 {
49+
valid = true;
50+
app_state.set_center((split[0], split[1]));
51+
} else {
52+
valid = false
53+
}
54+
}
55+
56+
if valid && ui.input_mut(|i| i.consume_key(Modifiers::NONE, egui::Key::Enter)) {
57+
close = true;
58+
}
59+
});
60+
61+
if close {
62+
self.open = false;
3063
}
3164
}
3265
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use crate::app_state::AppState;
2+
3+
pub fn panel_left(ctx: &egui::Context, app_state: &mut AppState) {
4+
egui::SidePanel::left("panel-left")
5+
.exact_width(300.0)
6+
.show(ctx, |ui| {});
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use crate::app_state::AppState;
2+
3+
use super::stats::view_stats;
4+
5+
pub fn panel_right(ctx: &egui::Context, app_state: &mut AppState) {
6+
egui::SidePanel::right("panel-right")
7+
.exact_width(300.0)
8+
.show(ctx, |ui| {
9+
view_stats(ui, app_state);
10+
});
11+
}

src/bin/drawing/ui/views/stats.rs

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,47 @@
1-
use crate::app_state::AppState;
2-
3-
pub struct StatsWindow {
4-
open: bool,
5-
}
1+
use egui::{Frame, Ui};
2+
use egui_extras::{Column, TableBuilder};
63

7-
impl StatsWindow {
8-
pub fn new(open: bool) -> Self {
9-
Self { open }
10-
}
4+
use crate::app_state::AppState;
115

12-
pub fn ui(&mut self, ctx: &egui::Context, app_state: &mut AppState) {
13-
egui::Window::new("Stats")
14-
.default_pos([320.0, 330.0])
15-
.default_width(400.0)
16-
.default_height(230.0)
17-
.open(&mut self.open)
18-
.show(ctx, |ui| {
19-
// Show cache stats
20-
egui::CollapsingHeader::new("Cache Stats")
21-
.default_open(true)
22-
.show(ui, |ui| {
23-
ui.label(format!("{:#?}", app_state.tile_cache.get_stats()));
6+
pub fn view_stats(ui: &mut Ui, app_state: &mut AppState) {
7+
Frame::default().outer_margin(5.0).show(ui, |ui| {
8+
TableBuilder::new(ui)
9+
.column(Column::auto().resizable(true))
10+
.column(Column::remainder())
11+
.header(20.0, |mut header| {
12+
header.col(|ui| {
13+
ui.label("key");
14+
});
15+
header.col(|ui| {
16+
ui.label("value");
17+
});
18+
})
19+
.body(|body| {
20+
let tile_stats = app_state.tile_cache.get_stats();
21+
let data = [
22+
("cached tiles", format!("{}", tile_stats.cached_tiles)),
23+
("loading tiles", format!("{}", tile_stats.loading_tiles)),
24+
("cached objects", format!("{}", tile_stats.cached_objects)),
25+
("cached features", format!("{}", tile_stats.cached_features)),
26+
("cached vertices", format!("{}", tile_stats.cached_vertices)),
27+
("objects", format!("{}", tile_stats.total_stats.objects)),
28+
("features", format!("{}", tile_stats.total_stats.features)),
29+
("vertices", format!("{}", tile_stats.total_stats.vertices)),
30+
("indices", format!("{}", tile_stats.total_stats.indices)),
31+
(
32+
"size",
33+
human_bytes::human_bytes(tile_stats.total_stats.size as f64),
34+
),
35+
];
36+
body.rows(20.0, data.len(), |mut row| {
37+
let (key, value) = &data[row.index()];
38+
row.col(|ui| {
39+
ui.label(*key);
40+
});
41+
row.col(|ui| {
42+
ui.label(value);
2443
});
44+
});
2545
});
26-
27-
egui::SidePanel::right("hehe")
28-
.exact_width(300.0)
29-
.show(ctx, |ui| {});
30-
}
46+
});
3147
}

0 commit comments

Comments
 (0)