From 57039541e9f3c3064bb9d8fca268b169e96c24fb Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 25 Sep 2024 16:05:30 +0200 Subject: [PATCH] Add `DebugOptions::show_unaligned` (#5165) This tool highlights coordinates that are non-integer. * Closes https://github.com/emilk/egui/issues/4927 * Will be used for https://github.com/emilk/egui/issues/5163 This is disabled by default (even in debug builds), because so many widgets cause un-alignment currently. --- crates/ecolor/src/color32.rs | 1 + crates/egui/src/style.rs | 14 ++++++++++++++ crates/egui/src/ui.rs | 25 +++++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/crates/ecolor/src/color32.rs b/crates/ecolor/src/color32.rs index d697be28273..80c876d192d 100644 --- a/crates/ecolor/src/color32.rs +++ b/crates/ecolor/src/color32.rs @@ -54,6 +54,7 @@ impl Color32 { pub const LIGHT_RED: Self = Self::from_rgb(255, 128, 128); pub const YELLOW: Self = Self::from_rgb(255, 255, 0); + pub const ORANGE: Self = Self::from_rgb(255, 165, 0); pub const LIGHT_YELLOW: Self = Self::from_rgb(255, 255, 0xE0); pub const KHAKI: Self = Self::from_rgb(240, 230, 140); diff --git a/crates/egui/src/style.rs b/crates/egui/src/style.rs index c460312b678..a0ee81fa139 100644 --- a/crates/egui/src/style.rs +++ b/crates/egui/src/style.rs @@ -1166,6 +1166,13 @@ pub struct DebugOptions { /// Show interesting widgets under the mouse cursor. pub show_widget_hits: bool, + + /// If true, highlight widgets that are not aligned to integer point coordinates. + /// + /// It's usually a good idea to keep to integer coordinates to avoid rounding issues. + /// + /// See for more. + pub show_unaligned: bool, } #[cfg(debug_assertions)] @@ -1181,6 +1188,7 @@ impl Default for DebugOptions { show_resize: false, show_interactive_widgets: false, show_widget_hits: false, + show_unaligned: false, } } } @@ -2190,6 +2198,7 @@ impl DebugOptions { show_resize, show_interactive_widgets, show_widget_hits, + show_unaligned, } = self; { @@ -2219,6 +2228,11 @@ impl DebugOptions { ui.checkbox(show_widget_hits, "Show widgets under mouse pointer"); + ui.checkbox( + show_unaligned, + "Show rectangles not aligned to integer point coordinates", + ); + ui.vertical_centered(|ui| reset_button(ui, self, "Reset debug options")); } } diff --git a/crates/egui/src/ui.rs b/crates/egui/src/ui.rs index dcd1a5117c3..0b6a16b9ad9 100644 --- a/crates/egui/src/ui.rs +++ b/crates/egui/src/ui.rs @@ -2962,8 +2962,33 @@ impl Drop for Ui { /// Show this rectangle to the user if certain debug options are set. #[cfg(debug_assertions)] fn register_rect(ui: &Ui, rect: Rect) { + use emath::Align2; + let debug = ui.style().debug; + if debug.show_unaligned { + let unaligned_line = |p0: Pos2, p1: Pos2| { + let color = Color32::ORANGE; + let font_id = TextStyle::Monospace.resolve(ui.style()); + ui.painter().line_segment([p0, p1], (1.0, color)); + ui.painter() + .text(p0, Align2::LEFT_TOP, "Unaligned", font_id, color); + }; + + if rect.left().fract() != 0.0 { + unaligned_line(rect.left_top(), rect.left_bottom()); + } + if rect.right().fract() != 0.0 { + unaligned_line(rect.right_top(), rect.right_bottom()); + } + if rect.top().fract() != 0.0 { + unaligned_line(rect.left_top(), rect.right_top()); + } + if rect.bottom().fract() != 0.0 { + unaligned_line(rect.left_bottom(), rect.right_bottom()); + } + } + let show_callstacks = debug.debug_on_hover || debug.debug_on_hover_with_all_modifiers && ui.input(|i| i.modifiers.all());