From 709e711364cfcdb13705dddf0384920210c583ee Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 16 Dec 2020 21:48:02 +0100 Subject: [PATCH] Deprecated RawInput::screen_size and replaced with screen_rect --- CHANGELOG.md | 4 ++ egui/benches/benchmark.rs | 5 +-- egui/src/containers/area.rs | 4 +- egui/src/input.rs | 73 +++++++++++++++++++++++++++++-------- egui/src/lib.rs | 5 +-- egui_glium/src/backend.rs | 6 ++- egui_web/src/backend.rs | 5 ++- 7 files changed, 74 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bc18295..3986565c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). * `ImageButton` - `ui.add(ImageButton::new(...))`. * `ui.vertical_centered` and `ui.vertical_centered_justified`. * Mouse-over explanation to duplicate ID warning +* You can now easily constrain Egui to a portion of the screen using `RawInput::screen_rect`. ### Changed 🔧 @@ -21,6 +22,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). * `ui.image` now takes `impl Into` as a `size` argument. * Made some more fields of `RawInput` optional. +### Deprecated +* Deprecated `RawInput::screen_size` - use `RawInput::screen_rect` instead. + ## 0.5.0 - 2020-12-13 diff --git a/egui/benches/benchmark.rs b/egui/benches/benchmark.rs index 0a90a614..7f833006 100644 --- a/egui/benches/benchmark.rs +++ b/egui/benches/benchmark.rs @@ -1,10 +1,7 @@ use criterion::{criterion_group, criterion_main, Criterion}; pub fn criterion_benchmark(c: &mut Criterion) { - let raw_input = egui::RawInput { - screen_size: egui::vec2(1280.0, 1024.0), - ..Default::default() - }; + let raw_input = egui::RawInput::default(); { let mut ctx = egui::Context::new(); diff --git a/egui/src/containers/area.rs b/egui/src/containers/area.rs index b4c9f037..b43beecf 100644 --- a/egui/src/containers/area.rs +++ b/egui/src/containers/area.rs @@ -150,7 +150,9 @@ impl Prepared { pub(crate) fn content_ui(&self, ctx: &Arc) -> Ui { let max_rect = Rect::from_min_size(self.state.pos, Vec2::infinity()); - let clip_rect = max_rect.expand(ctx.style().visuals.clip_rect_margin); + let clip_rect = max_rect + .expand(ctx.style().visuals.clip_rect_margin) + .intersect(ctx.input().screen_rect); Ui::new( ctx.clone(), self.layer_id, diff --git a/egui/src/input.rs b/egui/src/input.rs index 573593d6..570b8754 100644 --- a/egui/src/input.rs +++ b/egui/src/input.rs @@ -24,10 +24,19 @@ pub struct RawInput { /// How many points (logical pixels) the user scrolled pub scroll_delta: Vec2, - /// Size of the screen in points. - // TODO: this should be screen_rect for easy sandboxing. + #[deprecated = "Use screen_rect instead: `Some(Rect::from_pos_size(Default::default(), vec2(window_width, window_height)))`"] pub screen_size: Vec2, + /// Position and size of the area that Egui should use. + /// Usually you would set this to + /// + /// `Some(Rect::from_pos_size(Default::default(), vec2(window_width, window_height)))`. + /// + /// but you could also constrain Egui to some smaller portion of your window if you like. + /// + /// `None` will be treated as "same as last frame", with the default being a very big area. + pub screen_rect: Option, + /// Also known as device pixel ratio, > 1 for HDPI screens. /// If text looks blurry on high resolution screens, you probably forgot to set this. pub pixels_per_point: Option, @@ -50,11 +59,13 @@ pub struct RawInput { impl Default for RawInput { fn default() -> Self { + #![allow(deprecated)] // for screen_size Self { mouse_down: false, mouse_pos: None, scroll_delta: Vec2::zero(), - screen_size: Vec2::new(10_000.0, 10_000.0), // Difficult with a good default + screen_size: Default::default(), + screen_rect: None, pixels_per_point: None, time: None, predicted_dt: 1.0 / 60.0, @@ -67,11 +78,13 @@ impl Default for RawInput { impl RawInput { /// Helper: move volatile (deltas and events), clone the rest pub fn take(&mut self) -> RawInput { + #![allow(deprecated)] // for screen_size RawInput { mouse_down: self.mouse_down, mouse_pos: self.mouse_pos, scroll_delta: std::mem::take(&mut self.scroll_delta), screen_size: self.screen_size, + screen_rect: self.screen_rect, pixels_per_point: self.pixels_per_point, time: self.time, predicted_dt: self.predicted_dt, @@ -82,7 +95,7 @@ impl RawInput { } /// What egui maintains -#[derive(Clone, Debug, Default)] +#[derive(Clone, Debug)] pub struct InputState { /// The raw input we got this frame pub raw: RawInput, @@ -92,11 +105,11 @@ pub struct InputState { /// How many pixels the user scrolled pub scroll_delta: Vec2, - /// Size of the screen in points. - pub screen_size: Vec2, + /// Position and size of the Egui area. + pub screen_rect: Rect, /// Also known as device pixel ratio, > 1 for HDPI screens. - pub pixels_per_point: Option, + pub pixels_per_point: f32, /// Time in seconds. Relative to whatever. Used for animation. pub time: f64, @@ -116,6 +129,23 @@ pub struct InputState { pub events: Vec, } +impl Default for InputState { + fn default() -> Self { + Self { + raw: Default::default(), + mouse: Default::default(), + scroll_delta: Default::default(), + screen_rect: Rect::from_min_size(Default::default(), vec2(10_000.0, 10_000.0)), + pixels_per_point: 1.0, + time: 0.0, + unstable_dt: 1.0 / 6.0, + predicted_dt: 1.0 / 6.0, + modifiers: Default::default(), + events: Default::default(), + } + } +} + /// What egui maintains #[derive(Clone, Debug)] pub struct MouseInput { @@ -244,16 +274,25 @@ pub enum Key { impl InputState { #[must_use] pub fn begin_frame(self, new: RawInput) -> InputState { + #![allow(deprecated)] // for screen_size + let time = new .time .unwrap_or_else(|| self.time + new.predicted_dt as f64); - let mouse = self.mouse.begin_frame(time, &new); let unstable_dt = (time - self.time) as f32; + let screen_rect = new.screen_rect.unwrap_or_else(|| { + if new.screen_size != Default::default() { + Rect::from_min_size(Default::default(), new.screen_size) // backwards compatability + } else { + self.screen_rect + } + }); + let mouse = self.mouse.begin_frame(time, &new); InputState { mouse, scroll_delta: new.scroll_delta, - screen_size: new.screen_size, - pixels_per_point: new.pixels_per_point.or(self.pixels_per_point), + screen_rect, + pixels_per_point: new.pixels_per_point.unwrap_or(self.pixels_per_point), time, unstable_dt, predicted_dt: new.predicted_dt, @@ -264,7 +303,7 @@ impl InputState { } pub fn screen_rect(&self) -> Rect { - Rect::from_min_size(pos2(0.0, 0.0), self.screen_size) + self.screen_rect } pub fn wants_repaint(&self) -> bool { @@ -305,7 +344,7 @@ impl InputState { /// Also known as device pixel ratio, > 1 for HDPI screens. pub fn pixels_per_point(&self) -> f32 { - self.pixels_per_point.unwrap_or(1.0) + self.pixels_per_point } /// Size of a physical pixel in logical gui coordinates (points). @@ -393,11 +432,13 @@ impl MouseInput { impl RawInput { pub fn ui(&self, ui: &mut crate::Ui) { + #![allow(deprecated)] // for screen_size let Self { mouse_down, mouse_pos, scroll_delta, - screen_size, + screen_size: _, + screen_rect, pixels_per_point, time, predicted_dt, @@ -410,7 +451,7 @@ impl RawInput { ui.label(format!("mouse_down: {}", mouse_down)); ui.label(format!("mouse_pos: {:.1?}", mouse_pos)); ui.label(format!("scroll_delta: {:?} points", scroll_delta)); - ui.label(format!("screen_size: {:?} points", screen_size)); + ui.label(format!("screen_rect: {:?} points", screen_rect)); ui.label(format!("pixels_per_point: {:?}", pixels_per_point)) .on_hover_text( "Also called HDPI factor.\nNumber of physical pixels per each logical pixel.", @@ -433,7 +474,7 @@ impl InputState { raw, mouse, scroll_delta, - screen_size, + screen_rect, pixels_per_point, time, unstable_dt, @@ -452,7 +493,7 @@ impl InputState { }); ui.label(format!("scroll_delta: {:?} points", scroll_delta)); - ui.label(format!("screen_size: {:?} points", screen_size)); + ui.label(format!("screen_rect: {:?} points", screen_rect)); ui.label(format!( "{:?} physical pixels for each logical point", pixels_per_point diff --git a/egui/src/lib.rs b/egui/src/lib.rs index 8dd65c48..66584b6f 100644 --- a/egui/src/lib.rs +++ b/egui/src/lib.rs @@ -130,10 +130,7 @@ pub(crate) fn has_debug_assertions() -> bool { fn test_egui_e2e() { let mut demo_windows = crate::demos::DemoWindows::default(); let mut ctx = crate::Context::new(); - let raw_input = crate::RawInput { - screen_size: crate::vec2(1280.0, 1024.0), - ..Default::default() - }; + let raw_input = crate::RawInput::default(); const NUM_FRAMES: usize = 5; for _ in 0..NUM_FRAMES { diff --git a/egui_glium/src/backend.rs b/egui_glium/src/backend.rs index 516c828c..eb35cf6b 100644 --- a/egui_glium/src/backend.rs +++ b/egui_glium/src/backend.rs @@ -87,8 +87,10 @@ pub fn run(title: &str, mut storage: Box, mut app: Box egui::RawInput { egui::RawInput { - screen_size: screen_size_in_native_points().unwrap(), + screen_rect: Some(egui::Rect::from_min_size( + Default::default(), + screen_size_in_native_points().unwrap(), + )), pixels_per_point: Some(native_pixels_per_point()), time: Some(now_sec()), ..self.raw.take()