Skip to content

Commit

Permalink
Fix dependency between re_time_panel & re_viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed May 29, 2023
1 parent 00a7030 commit 018742a
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 110 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/re_format/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Miscellaneous tools to format and parse numbers, durations, etc.
pub mod arrow;
mod time;

pub use time::next_grid_tick_magnitude_ns;

// --- Numbers ---

Expand Down
20 changes: 20 additions & 0 deletions crates/re_format/src/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// When showing grid-lines representing time.
///
/// Given some spacing (e.g. 10s), return the next spacing (60s).
pub fn next_grid_tick_magnitude_ns(spacing_ns: i64) -> i64 {
if spacing_ns <= 1_000_000_000 {
spacing_ns * 10 // up to 10 second ticks
} else if spacing_ns == 10_000_000_000 {
spacing_ns * 6 // to the whole minute
} else if spacing_ns == 60_000_000_000 {
spacing_ns * 10 // to ten minutes
} else if spacing_ns == 600_000_000_000 {
spacing_ns * 6 // to an hour
} else if spacing_ns == 60 * 60 * 1_000_000_000 {
spacing_ns * 12 // to 12 h
} else if spacing_ns == 12 * 60 * 60 * 1_000_000_000 {
spacing_ns * 2 // to a day
} else {
spacing_ns.checked_mul(10).unwrap_or(spacing_ns) // multiple of ten days
}
}
47 changes: 47 additions & 0 deletions crates/re_log_types/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,53 @@ impl Time {
}
}

/// Useful when showing dates/times on a timeline
/// and you want it compact.
///
/// Shows dates when zoomed out, shows times when zoomed in,
/// shows relative millisecond when really zoomed in.
pub fn format_time_compact(&self) -> String {
let ns = self.nanos_since_epoch();
let relative_ns = ns % 1_000_000_000;
let is_whole_second = relative_ns == 0;
if is_whole_second {
if let Some(datetime) = self.to_datetime() {
let is_whole_minute = ns % 60_000_000_000 == 0;
let time_format = if self.is_exactly_midnight() {
"[year]-[month]-[day]Z"
} else if is_whole_minute {
"[hour]:[minute]Z"
} else {
"[hour]:[minute]:[second]Z"
};
let parsed_format = time::format_description::parse(time_format).unwrap();
return datetime.format(&parsed_format).unwrap();
}

crate::Duration::from_nanos(ns).to_string()
} else {
// We are in the sub-second resolution.
// Showing the full time (HH:MM:SS.XXX or 3h 2m 6s …) becomes too long,
// so instead we switch to showing the time as milliseconds since the last whole second:
let ms = relative_ns as f64 * 1e-6;
if relative_ns % 1_000_000 == 0 {
format!("{ms:+.0} ms")
} else if relative_ns % 100_000 == 0 {
format!("{ms:+.1} ms")
} else if relative_ns % 10_000 == 0 {
format!("{ms:+.2} ms")
} else if relative_ns % 1_000 == 0 {
format!("{ms:+.3} ms")
} else if relative_ns % 100 == 0 {
format!("{ms:+.4} ms")
} else if relative_ns % 10 == 0 {
format!("{ms:+.5} ms")
} else {
format!("{ms:+.6} ms")
}
}
}

#[inline]
pub fn lerp(range: RangeInclusive<Time>, t: f32) -> Time {
let (min, max) = (range.start().0, range.end().0);
Expand Down
2 changes: 1 addition & 1 deletion crates/re_time_panel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ all-features = true
re_arrow_store.workspace = true
re_data_store.workspace = true
re_data_ui.workspace = true
re_format.workspace = true
re_log_types.workspace = true
re_ui.workspace = true
re_viewer_context.workspace = true

egui.workspace = true
itertools.workspace = true
serde = "1.0"
time.workspace = true
vec1 = "1.8"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
67 changes: 0 additions & 67 deletions crates/re_time_panel/src/format_time.rs

This file was deleted.

36 changes: 4 additions & 32 deletions crates/re_time_panel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
//! as well as all necessary ui elements that make it up.
mod data_density_graph;
mod format_time; // TODO(andreas): Move to re_format
mod paint_ticks;
mod time_axis;
mod time_control_ui;
mod time_ranges_ui;
mod time_selection_ui;

pub use format_time::{format_time_compact, next_grid_tick_magnitude_ns};

use std::ops::RangeInclusive;

use egui::{pos2, Color32, CursorIcon, NumExt, PointerButton, Rect, Shape, Vec2};
Expand Down Expand Up @@ -328,6 +325,7 @@ impl TimePanel {
time_marker_ui(
&self.time_ranges_ui,
&mut ctx.rec_cfg.time_ctrl,
ctx.re_ui,
ui,
&time_area_painter,
&timeline_rect,
Expand Down Expand Up @@ -627,6 +625,7 @@ fn collapsed_time_marker_and_time(ui: &mut egui::Ui, ctx: &mut ViewerContext<'_>
time_marker_ui(
&time_ranges_ui,
&mut ctx.rec_cfg.time_ctrl,
ctx.re_ui,
ui,
&painter,
&time_range_rect,
Expand Down Expand Up @@ -987,6 +986,7 @@ fn interact_with_streams_rect(
fn time_marker_ui(
time_ranges_ui: &TimeRangesUi,
time_ctrl: &mut TimeControl,
re_ui: &re_ui::ReUi,
ui: &mut egui::Ui,
time_area_painter: &egui::Painter,
timeline_rect: &Rect,
Expand Down Expand Up @@ -1033,7 +1033,7 @@ fn time_marker_ui(
} else {
ui.visuals().widgets.inactive.fg_stroke
};
paint_time_cursor(
re_ui.paint_time_cursor(
time_area_painter,
x,
timeline_rect.top()..=ui.max_rect().bottom(),
Expand Down Expand Up @@ -1077,34 +1077,6 @@ fn time_marker_ui(
}
}

pub fn paint_time_cursor(
painter: &egui::Painter,
x: f32,
y: RangeInclusive<f32>,
stroke: egui::Stroke,
) {
let y_min = *y.start();
let y_max = *y.end();

let stroke = egui::Stroke {
width: 1.5 * stroke.width,
color: stroke.color,
};

let w = 10.0;
let triangle = vec![
pos2(x - 0.5 * w, y_min), // left top
pos2(x + 0.5 * w, y_min), // right top
pos2(x, y_min + w), // bottom
];
painter.add(egui::Shape::convex_polygon(
triangle,
stroke.color,
egui::Stroke::NONE,
));
painter.vline(x, (y_min + w)..=y_max, stroke);
}

// ---------------------------------------------------------------------------

/// Profiling macro for feature "puffin"
Expand Down
5 changes: 2 additions & 3 deletions crates/re_time_panel/src/paint_ticks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use std::ops::RangeInclusive;

use egui::{lerp, pos2, remap_clamp, Align2, Color32, Rect, Rgba, Shape, Stroke};

use re_format::next_grid_tick_magnitude_ns;
use re_log_types::{Time, TimeRangeF, TimeReal, TimeType};

use crate::format_time::{format_time_compact, next_grid_tick_magnitude_ns};

use super::time_ranges_ui::TimeRangesUi;

pub fn paint_time_ranges_and_ticks(
Expand Down Expand Up @@ -69,7 +68,7 @@ fn paint_time_range_ticks(
&ui.clip_rect(),
time_range, // ns
next_grid_tick_magnitude_ns,
|ns| format_time_compact(Time::from_ns_since_epoch(ns)),
|ns| Time::from_ns_since_epoch(ns).format_time_compact(),
)
}
TimeType::Sequence => {
Expand Down
33 changes: 32 additions & 1 deletion crates/re_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub struct TopBarStyle {

// ----------------------------------------------------------------------------

use std::sync::Arc;
use std::{ops::RangeInclusive, sync::Arc};

use parking_lot::Mutex;

Expand Down Expand Up @@ -626,6 +626,37 @@ impl ReUi {
style.background = self.egui_ctx.style().visuals.widgets.noninteractive.bg_fill;
style
}

/// Paints a time cursor for indicating the time on a time axis along x.
#[allow(clippy::unused_self)]
pub fn paint_time_cursor(
&self,
painter: &egui::Painter,
x: f32,
y: RangeInclusive<f32>,
stroke: egui::Stroke,
) {
let y_min = *y.start();
let y_max = *y.end();

let stroke = egui::Stroke {
width: 1.5 * stroke.width,
color: stroke.color,
};

let w = 10.0;
let triangle = vec![
pos2(x - 0.5 * w, y_min), // left top
pos2(x + 0.5 * w, y_min), // right top
pos2(x, y_min + w), // bottom
];
painter.add(egui::Shape::convex_polygon(
triangle,
stroke.color,
egui::Stroke::NONE,
));
painter.vline(x, (y_min + w)..=y_max, stroke);
}
}

// ----------------------------------------------------------------------------
Expand Down
1 change: 0 additions & 1 deletion crates/re_viewport/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ re_renderer = { workspace = true, default-features = false, features = [
"serde",
] }
re_tensor_ops.workspace = true
re_time_panel.workspace = true # TODO(andreas): Unfortunate dependency, caused by `next_grid_tick_magnitude_ns`
re_ui.workspace = true
re_viewer_context.workspace = true

Expand Down
7 changes: 4 additions & 3 deletions crates/re_viewport/src/view_time_series/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use egui::{
};

use re_arrow_store::TimeType;
use re_time_panel::next_grid_tick_magnitude_ns;
use re_format::next_grid_tick_magnitude_ns;
use re_viewer_context::ViewerContext;

use super::SceneTimeSeries;
Expand Down Expand Up @@ -179,7 +179,8 @@ pub(crate) fn view_time_series(
} else {
ui.visuals().widgets.inactive.fg_stroke
};
re_time_panel::paint_time_cursor(ui.painter(), time_x, response.rect.y_range(), stroke);
ctx.re_ui
.paint_time_cursor(ui.painter(), time_x, response.rect.y_range(), stroke);
}

response
Expand All @@ -188,7 +189,7 @@ pub(crate) fn view_time_series(
fn format_time(time_type: TimeType, time_int: i64) -> String {
if time_type == TimeType::Time {
let time = re_log_types::Time::from_ns_since_epoch(time_int);
re_time_panel::format_time_compact(time)
time.format_time_compact()
} else {
time_type.format(re_log_types::TimeInt::from(time_int))
}
Expand Down

0 comments on commit 018742a

Please sign in to comment.